##// END OF EJS Templates
ControllerThread was eliminated from controller.py and it was added to controller_api.py
Miguel Valdez -
r636:a6c4caf9dc4c
parent child
Show More
@@ -0,0 +1,137
1 import threading
2
3 from PyQt4 import QtCore
4 from PyQt4.QtCore import SIGNAL
5
6 from schainpy.controller import Project
7
8 class ControllerThread(threading.Thread, Project):
9
10 def __init__(self, filename):
11
12 threading.Thread.__init__(self)
13 Project.__init__(self)
14
15 self.setDaemon(True)
16
17 self.filename = filename
18 self.control = {'stop':False, 'pause':False}
19
20 def __del__(self):
21
22 self.control['stop'] = True
23 # self.pause(1)
24 # self.wait()
25
26 def stop(self):
27 self.control['stop'] = True
28
29 def pause(self):
30 self.control['pause'] = not(self.control['pause'])
31
32 def __run(self):
33
34 print
35 print "*"*40
36 print " Starting SIGNAL CHAIN PROCESSING "
37 print "*"*40
38 print
39
40 keyList = self.procUnitConfObjDict.keys()
41 keyList.sort()
42
43 while(True):
44
45 finalSts = False
46 #executed proc units
47 procUnitExecutedList = []
48
49 for procKey in keyList:
50 # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
51
52 procUnitConfObj = self.procUnitConfObjDict[procKey]
53
54 inputId = procUnitConfObj.getInputId()
55
56 sts = procUnitConfObj.run()
57 finalSts = finalSts or sts
58
59 procUnitExecutedList.append(procUnitConfObj.id)
60
61 #If every process unit finished so end process
62 if not(finalSts):
63 print "Every process unit have finished"
64 break
65
66 if self.control['pause']:
67 print "Process suspended"
68
69 while True:
70 sleep(0.1)
71
72 if not self.control['pause']:
73 break
74
75 if self.control['stop']:
76 break
77 print "Process reinitialized"
78
79 if self.control['stop']:
80 # print "Process stopped"
81 break
82
83 #Closing every process
84 for procKey in keyList:
85 procUnitConfObj = self.procUnitConfObjDict[procKey]
86 procUnitConfObj.close()
87
88 print "Process finished"
89
90 def run(self):
91 self.control['stop'] = False
92 self.control['pause'] = False
93
94 self.readXml(self.filename)
95 self.createObjects()
96 self.connectObjects()
97 Project.run(self)
98
99 def isRunning(self):
100
101 return self.is_alive()
102
103 def isFinished(self):
104
105 return not self.is_alive()
106
107 class ControllerQThread(QtCore.QThread, Project):
108
109 def __init__(self, filename):
110
111 QtCore.QThread.__init__(self)
112 Project.__init__(self)
113
114 self.filename = filename
115 self.control = {'stop':False, 'pause':False}
116
117 def __del__(self):
118
119 self.control['stop'] = True
120 self.wait()
121
122 def stop(self):
123 self.control['stop'] = True
124
125 def pause(self):
126 self.control['pause'] = not(self.control['pause'])
127
128 def run(self):
129 self.control['stop'] = False
130 self.control['pause'] = False
131
132 self.readXml(self.filename)
133 self.createObjects()
134 self.connectObjects()
135 self.emit( SIGNAL( "jobStarted( PyQt_PyObject )" ), 1)
136 Project.run(self)
137 self.emit( SIGNAL( "jobFinished( PyQt_PyObject )" ), 1) No newline at end of file
@@ -1,1209 +1,1089
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 5 from xml.etree.ElementTree import Element, SubElement
6 6 from xml.etree import ElementTree as ET
7 7 from xml.dom import minidom
8 8
9 #import datetime
10 9 from model import *
11 10
12 11 try:
13 12 from gevent import sleep
14 13 except:
15 14 from time import sleep
16 15
17 16 import ast
18 17
19 18 def prettify(elem):
20 19 """Return a pretty-printed XML string for the Element.
21 20 """
22 21 rough_string = ET.tostring(elem, 'utf-8')
23 22 reparsed = minidom.parseString(rough_string)
24 23 return reparsed.toprettyxml(indent=" ")
25 24
26 25 class ParameterConf():
27 26
28 27 id = None
29 28 name = None
30 29 value = None
31 30 format = None
32 31
33 32 __formated_value = None
34 33
35 34 ELEMENTNAME = 'Parameter'
36 35
37 36 def __init__(self):
38 37
39 38 self.format = 'str'
40 39
41 40 def getElementName(self):
42 41
43 42 return self.ELEMENTNAME
44 43
45 44 def getValue(self):
46 45
47 46 value = self.value
48 47 format = self.format
49 48
50 49 if self.__formated_value != None:
51 50
52 51 return self.__formated_value
53 52
54 53 if format == 'str':
55 54 self.__formated_value = str(value)
56 55 return self.__formated_value
57 56
58 57 if value == '':
59 58 raise ValueError, "%s: This parameter value is empty" %self.name
60 59
61 60 if format == 'bool':
62 61 value = int(value)
63 62
64 63 if format == 'list':
65 64 strList = value.split(',')
66 65
67 66 self.__formated_value = strList
68 67
69 68 return self.__formated_value
70 69
71 70 if format == 'intlist':
72 71 """
73 72 Example:
74 73 value = (0,1,2)
75 74 """
76 75 value = value.replace('(', '')
77 76 value = value.replace(')', '')
78 77
79 78 value = value.replace('[', '')
80 79 value = value.replace(']', '')
81 80
82 81 strList = value.split(',')
83 82 intList = [int(x) for x in strList]
84 83
85 84 self.__formated_value = intList
86 85
87 86 return self.__formated_value
88 87
89 88 if format == 'floatlist':
90 89 """
91 90 Example:
92 91 value = (0.5, 1.4, 2.7)
93 92 """
94 93
95 94 value = value.replace('(', '')
96 95 value = value.replace(')', '')
97 96
98 97 value = value.replace('[', '')
99 98 value = value.replace(']', '')
100 99
101 100 strList = value.split(',')
102 101 floatList = [float(x) for x in strList]
103 102
104 103 self.__formated_value = floatList
105 104
106 105 return self.__formated_value
107 106
108 107 if format == 'date':
109 108 strList = value.split('/')
110 109 intList = [int(x) for x in strList]
111 110 date = datetime.date(intList[0], intList[1], intList[2])
112 111
113 112 self.__formated_value = date
114 113
115 114 return self.__formated_value
116 115
117 116 if format == 'time':
118 117 strList = value.split(':')
119 118 intList = [int(x) for x in strList]
120 119 time = datetime.time(intList[0], intList[1], intList[2])
121 120
122 121 self.__formated_value = time
123 122
124 123 return self.__formated_value
125 124
126 125 if format == 'pairslist':
127 126 """
128 127 Example:
129 128 value = (0,1),(1,2)
130 129 """
131 130
132 131 value = value.replace('(', '')
133 132 value = value.replace(')', '')
134 133
135 134 value = value.replace('[', '')
136 135 value = value.replace(']', '')
137 136
138 137 strList = value.split(',')
139 138 intList = [int(item) for item in strList]
140 139 pairList = []
141 140 for i in range(len(intList)/2):
142 141 pairList.append((intList[i*2], intList[i*2 + 1]))
143 142
144 143 self.__formated_value = pairList
145 144
146 145 return self.__formated_value
147 146
148 147 if format == 'multilist':
149 148 """
150 149 Example:
151 150 value = (0,1,2),(3,4,5)
152 151 """
153 152 multiList = ast.literal_eval(value)
154 153
155 154 if type(multiList[0]) == int:
156 155 multiList = ast.literal_eval("(" + value + ")")
157 156
158 157 self.__formated_value = multiList
159 158
160 159 return self.__formated_value
161 160
162 161 format_func = eval(format)
163 162
164 163 self.__formated_value = format_func(value)
165 164
166 165 return self.__formated_value
167 166
168 167 def updateId(self, new_id):
169 168
170 169 self.id = str(new_id)
171 170
172 171 def setup(self, id, name, value, format='str'):
173 172
174 173 self.id = str(id)
175 174 self.name = name
176 175 self.value = str(value)
177 176 self.format = str.lower(format)
178 177
179 178 def update(self, name, value, format='str'):
180 179
181 180 self.name = name
182 181 self.value = str(value)
183 182 self.format = format
184 183
185 184 def makeXml(self, opElement):
186 185
187 186 parmElement = SubElement(opElement, self.ELEMENTNAME)
188 187 parmElement.set('id', str(self.id))
189 188 parmElement.set('name', self.name)
190 189 parmElement.set('value', self.value)
191 190 parmElement.set('format', self.format)
192 191
193 192 def readXml(self, parmElement):
194 193
195 194 self.id = parmElement.get('id')
196 195 self.name = parmElement.get('name')
197 196 self.value = parmElement.get('value')
198 197 self.format = str.lower(parmElement.get('format'))
199 198
200 199 #Compatible with old signal chain version
201 200 if self.format == 'int' and self.name == 'idfigure':
202 201 self.name = 'id'
203 202
204 203 def printattr(self):
205 204
206 205 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
207 206
208 207 class OperationConf():
209 208
210 209 id = None
211 210 name = None
212 211 priority = None
213 212 type = None
214 213
215 214 parmConfObjList = []
216 215
217 216 ELEMENTNAME = 'Operation'
218 217
219 218 def __init__(self):
220 219
221 220 self.id = '0'
222 221 self.name = None
223 222 self.priority = None
224 223 self.type = 'self'
225 224
226 225
227 226 def __getNewId(self):
228 227
229 228 return int(self.id)*10 + len(self.parmConfObjList) + 1
230 229
231 230 def updateId(self, new_id):
232 231
233 232 self.id = str(new_id)
234 233
235 234 n = 1
236 235 for parmObj in self.parmConfObjList:
237 236
238 237 idParm = str(int(new_id)*10 + n)
239 238 parmObj.updateId(idParm)
240 239
241 240 n += 1
242 241
243 242 def getElementName(self):
244 243
245 244 return self.ELEMENTNAME
246 245
247 246 def getParameterObjList(self):
248 247
249 248 return self.parmConfObjList
250 249
251 250 def getParameterObj(self, parameterName):
252 251
253 252 for parmConfObj in self.parmConfObjList:
254 253
255 254 if parmConfObj.name != parameterName:
256 255 continue
257 256
258 257 return parmConfObj
259 258
260 259 return None
261 260
262 261 def getParameterObjfromValue(self, parameterValue):
263 262
264 263 for parmConfObj in self.parmConfObjList:
265 264
266 265 if parmConfObj.getValue() != parameterValue:
267 266 continue
268 267
269 268 return parmConfObj.getValue()
270 269
271 270 return None
272 271
273 272 def getParameterValue(self, parameterName):
274 273
275 274 parameterObj = self.getParameterObj(parameterName)
276 275
277 276 # if not parameterObj:
278 277 # return None
279 278
280 279 value = parameterObj.getValue()
281 280
282 281 return value
283 282
284 283 def setup(self, id, name, priority, type):
285 284
286 285 self.id = str(id)
287 286 self.name = name
288 287 self.type = type
289 288 self.priority = priority
290 289
291 290 self.parmConfObjList = []
292 291
293 292 def removeParameters(self):
294 293
295 294 for obj in self.parmConfObjList:
296 295 del obj
297 296
298 297 self.parmConfObjList = []
299 298
300 299 def addParameter(self, name, value, format='str'):
301 300
302 301 id = self.__getNewId()
303 302
304 303 parmConfObj = ParameterConf()
305 304 parmConfObj.setup(id, name, value, format)
306 305
307 306 self.parmConfObjList.append(parmConfObj)
308 307
309 308 return parmConfObj
310 309
311 310 def changeParameter(self, name, value, format='str'):
312 311
313 312 parmConfObj = self.getParameterObj(name)
314 313 parmConfObj.update(name, value, format)
315 314
316 315 return parmConfObj
317 316
318 317 def makeXml(self, upElement):
319 318
320 319 opElement = SubElement(upElement, self.ELEMENTNAME)
321 320 opElement.set('id', str(self.id))
322 321 opElement.set('name', self.name)
323 322 opElement.set('type', self.type)
324 323 opElement.set('priority', str(self.priority))
325 324
326 325 for parmConfObj in self.parmConfObjList:
327 326 parmConfObj.makeXml(opElement)
328 327
329 328 def readXml(self, opElement):
330 329
331 330 self.id = opElement.get('id')
332 331 self.name = opElement.get('name')
333 332 self.type = opElement.get('type')
334 333 self.priority = opElement.get('priority')
335 334
336 335 #Compatible with old signal chain version
337 336 #Use of 'run' method instead 'init'
338 337 if self.type == 'self' and self.name == 'init':
339 338 self.name = 'run'
340 339
341 340 self.parmConfObjList = []
342 341
343 342 parmElementList = opElement.getiterator(ParameterConf().getElementName())
344 343
345 344 for parmElement in parmElementList:
346 345 parmConfObj = ParameterConf()
347 346 parmConfObj.readXml(parmElement)
348 347
349 348 #Compatible with old signal chain version
350 349 #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
351 350 if self.type != 'self' and self.name == 'Plot':
352 351 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
353 352 self.name = parmConfObj.value
354 353 continue
355 354
356 355 self.parmConfObjList.append(parmConfObj)
357 356
358 357 def printattr(self):
359 358
360 359 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
361 360 self.id,
362 361 self.name,
363 362 self.type,
364 363 self.priority)
365 364
366 365 for parmConfObj in self.parmConfObjList:
367 366 parmConfObj.printattr()
368 367
369 368 def createObject(self):
370 369
371 370 if self.type == 'self':
372 371 raise ValueError, "This operation type cannot be created"
373 372
374 373 if self.type == 'external' or self.type == 'other':
375 374 className = eval(self.name)
376 375 opObj = className()
377 376
378 377 return opObj
379 378
380 379 class ProcUnitConf():
381 380
382 381 id = None
383 382 name = None
384 383 datatype = None
385 384 inputId = None
386 385 parentId = None
387 386
388 387 opConfObjList = []
389 388
390 389 procUnitObj = None
391 390 opObjList = []
392 391
393 392 ELEMENTNAME = 'ProcUnit'
394 393
395 394 def __init__(self):
396 395
397 396 self.id = None
398 397 self.datatype = None
399 398 self.name = None
400 399 self.inputId = None
401 400
402 401 self.opConfObjList = []
403 402
404 403 self.procUnitObj = None
405 404 self.opObjDict = {}
406 405
407 406 def __getPriority(self):
408 407
409 408 return len(self.opConfObjList)+1
410 409
411 410 def __getNewId(self):
412 411
413 412 return int(self.id)*10 + len(self.opConfObjList) + 1
414 413
415 414 def getElementName(self):
416 415
417 416 return self.ELEMENTNAME
418 417
419 418 def getId(self):
420 419
421 420 return self.id
422 421
423 422 def updateId(self, new_id, parentId=parentId):
424 423
425 424
426 425 new_id = int(parentId)*10 + (int(self.id) % 10)
427 426 new_inputId = int(parentId)*10 + (int(self.inputId) % 10)
428 427
429 428 #If this proc unit has not inputs
430 429 if self.inputId == '0':
431 430 new_inputId = 0
432 431
433 432 n = 1
434 433 for opConfObj in self.opConfObjList:
435 434
436 435 idOp = str(int(new_id)*10 + n)
437 436 opConfObj.updateId(idOp)
438 437
439 438 n += 1
440 439
441 440 self.parentId = str(parentId)
442 441 self.id = str(new_id)
443 442 self.inputId = str(new_inputId)
444 443
445 444
446 445 def getInputId(self):
447 446
448 447 return self.inputId
449 448
450 449 def getOperationObjList(self):
451 450
452 451 return self.opConfObjList
453 452
454 453 def getOperationObj(self, name=None):
455 454
456 455 for opConfObj in self.opConfObjList:
457 456
458 457 if opConfObj.name != name:
459 458 continue
460 459
461 460 return opConfObj
462 461
463 462 return None
464 463
465 464 def getOpObjfromParamValue(self, value=None):
466 465
467 466 for opConfObj in self.opConfObjList:
468 467 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
469 468 continue
470 469 return opConfObj
471 470 return None
472 471
473 472 def getProcUnitObj(self):
474 473
475 474 return self.procUnitObj
476 475
477 476 def setup(self, id, name, datatype, inputId, parentId=None):
478 477
479 478 #Compatible with old signal chain version
480 479 if datatype==None and name==None:
481 480 raise ValueError, "datatype or name should be defined"
482 481
483 482 if name==None:
484 483 if 'Proc' in datatype:
485 484 name = datatype
486 485 else:
487 486 name = '%sProc' %(datatype)
488 487
489 488 if datatype==None:
490 489 datatype = name.replace('Proc','')
491 490
492 491 self.id = str(id)
493 492 self.name = name
494 493 self.datatype = datatype
495 494 self.inputId = inputId
496 495 self.parentId = parentId
497 496
498 497 self.opConfObjList = []
499 498
500 499 self.addOperation(name='run', optype='self')
501 500
502 501 def removeOperations(self):
503 502
504 503 for obj in self.opConfObjList:
505 504 del obj
506 505
507 506 self.opConfObjList = []
508 507 self.addOperation(name='run')
509 508
510 509 def addParameter(self, **kwargs):
511 510 '''
512 511 Add parameters to "run" operation
513 512 '''
514 513 opObj = self.opConfObjList[0]
515 514
516 515 opObj.addParameter(**kwargs)
517 516
518 517 return opObj
519 518
520 519 def addOperation(self, name, optype='self'):
521 520
522 521 id = self.__getNewId()
523 522 priority = self.__getPriority()
524 523
525 524 opConfObj = OperationConf()
526 525 opConfObj.setup(id, name=name, priority=priority, type=optype)
527 526
528 527 self.opConfObjList.append(opConfObj)
529 528
530 529 return opConfObj
531 530
532 531 def makeXml(self, procUnitElement):
533 532
534 533 upElement = SubElement(procUnitElement, self.ELEMENTNAME)
535 534 upElement.set('id', str(self.id))
536 535 upElement.set('name', self.name)
537 536 upElement.set('datatype', self.datatype)
538 537 upElement.set('inputId', str(self.inputId))
539 538
540 539 for opConfObj in self.opConfObjList:
541 540 opConfObj.makeXml(upElement)
542 541
543 542 def readXml(self, upElement):
544 543
545 544 self.id = upElement.get('id')
546 545 self.name = upElement.get('name')
547 546 self.datatype = upElement.get('datatype')
548 547 self.inputId = upElement.get('inputId')
549 548
550 549 if self.ELEMENTNAME == "ReadUnit":
551 550 self.datatype = self.datatype.replace("Reader", "")
552 551
553 552 if self.ELEMENTNAME == "ProcUnit":
554 553 self.datatype = self.datatype.replace("Proc", "")
555 554
556 555 if self.inputId == 'None':
557 556 self.inputId = '0'
558 557
559 558 self.opConfObjList = []
560 559
561 560 opElementList = upElement.getiterator(OperationConf().getElementName())
562 561
563 562 for opElement in opElementList:
564 563 opConfObj = OperationConf()
565 564 opConfObj.readXml(opElement)
566 565 self.opConfObjList.append(opConfObj)
567 566
568 567 def printattr(self):
569 568
570 569 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
571 570 self.id,
572 571 self.name,
573 572 self.datatype,
574 573 self.inputId)
575 574
576 575 for opConfObj in self.opConfObjList:
577 576 opConfObj.printattr()
578 577
579 578 def createObjects(self):
580 579
581 580 className = eval(self.name)
582 581 procUnitObj = className()
583 582
584 583 for opConfObj in self.opConfObjList:
585 584
586 585 if opConfObj.type == 'self':
587 586 continue
588 587
589 588 opObj = opConfObj.createObject()
590 589
591 590 self.opObjDict[opConfObj.id] = opObj
592 591 procUnitObj.addOperation(opObj, opConfObj.id)
593 592
594 593 self.procUnitObj = procUnitObj
595 594
596 595 return procUnitObj
597 596
598 597 def run(self):
599 598
600 599 finalSts = False
601 600
602 601 for opConfObj in self.opConfObjList:
603 602
604 603 kwargs = {}
605 604 for parmConfObj in opConfObj.getParameterObjList():
606 605 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
607 606 continue
608 607
609 608 kwargs[parmConfObj.name] = parmConfObj.getValue()
610 609
611 610 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
612 611 sts = self.procUnitObj.call(opType = opConfObj.type,
613 612 opName = opConfObj.name,
614 613 opId = opConfObj.id,
615 614 **kwargs)
616 615 finalSts = finalSts or sts
617 616
618 617 return finalSts
619 618
620 619 def close(self):
621 620
622 621 for opConfObj in self.opConfObjList:
623 622 if opConfObj.type == 'self':
624 623 continue
625 624
626 625 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
627 626 opObj.close()
628 627
629 628 self.procUnitObj.close()
630 629
631 630 return
632 631
633 632 class ReadUnitConf(ProcUnitConf):
634 633
635 634 path = None
636 635 startDate = None
637 636 endDate = None
638 637 startTime = None
639 638 endTime = None
640 639
641 640 ELEMENTNAME = 'ReadUnit'
642 641
643 642 def __init__(self):
644 643
645 644 self.id = None
646 645 self.datatype = None
647 646 self.name = None
648 647 self.inputId = None
649 648
650 649 self.parentId = None
651 650
652 651 self.opConfObjList = []
653 652 self.opObjList = []
654 653
655 654 def getElementName(self):
656 655
657 656 return self.ELEMENTNAME
658 657
659 658 def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs):
660 659
661 660 #Compatible with old signal chain version
662 661 if datatype==None and name==None:
663 662 raise ValueError, "datatype or name should be defined"
664 663
665 664 if name==None:
666 665 if 'Reader' in datatype:
667 666 name = datatype
668 667 else:
669 668 name = '%sReader' %(datatype)
670 669
671 670 if datatype==None:
672 671 datatype = name.replace('Reader','')
673 672
674 673 self.id = id
675 674 self.name = name
676 675 self.datatype = datatype
677 676
678 677 self.path = path
679 678 self.startDate = startDate
680 679 self.endDate = endDate
681 680 self.startTime = startTime
682 681 self.endTime = endTime
683 682
684 683 self.inputId = '0'
685 684 self.parentId = parentId
686 685
687 686 self.addRunOperation(**kwargs)
688 687
689 688 def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs):
690 689
691 690 #Compatible with old signal chain version
692 691 if datatype==None and name==None:
693 692 raise ValueError, "datatype or name should be defined"
694 693
695 694 if name==None:
696 695 if 'Reader' in datatype:
697 696 name = datatype
698 697 else:
699 698 name = '%sReader' %(datatype)
700 699
701 700 if datatype==None:
702 701 datatype = name.replace('Reader','')
703 702
704 703 self.datatype = datatype
705 704 self.name = name
706 705 self.path = path
707 706 self.startDate = startDate
708 707 self.endDate = endDate
709 708 self.startTime = startTime
710 709 self.endTime = endTime
711 710
712 711 self.inputId = '0'
713 712 self.parentId = parentId
714 713
715 714 self.updateRunOperation(**kwargs)
716 715
717 716 def addRunOperation(self, **kwargs):
718 717
719 718 opObj = self.addOperation(name = 'run', optype = 'self')
720 719
721 720 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
722 721 opObj.addParameter(name='path' , value=self.path, format='str')
723 722 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
724 723 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
725 724 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
726 725 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
727 726
728 727 for key, value in kwargs.items():
729 728 opObj.addParameter(name=key, value=value, format=type(value).__name__)
730 729
731 730 return opObj
732 731
733 732 def updateRunOperation(self, **kwargs):
734 733
735 734 opObj = self.getOperationObj(name = 'run')
736 735 opObj.removeParameters()
737 736
738 737 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
739 738 opObj.addParameter(name='path' , value=self.path, format='str')
740 739 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
741 740 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
742 741 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
743 742 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
744 743
745 744 for key, value in kwargs.items():
746 745 opObj.addParameter(name=key, value=value, format=type(value).__name__)
747 746
748 747 return opObj
749 748
750 749 class Project():
751 750
752 751 id = None
753 752 name = None
754 753 description = None
755 754 # readUnitConfObjList = None
756 755 procUnitConfObjDict = None
757 756
758 757 ELEMENTNAME = 'Project'
759 758
760 759 def __init__(self, control=None, dataq=None):
761 760
762 761 self.id = None
763 762 self.name = None
764 763 self.description = None
765 764
766 765 self.procUnitConfObjDict = {}
767 766
768 767 #global data_q
769 768 #data_q = dataq
770 769
771 770 if control==None:
772 771 control = {'stop':False,'pause':False}
773 772
774 773 self.control = control
775 774
776 775 def __getNewId(self):
777 776
778 777 id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1
779 778
780 779 return str(id)
781 780
782 781 def getElementName(self):
783 782
784 783 return self.ELEMENTNAME
785 784
786 785 def getId(self):
787 786
788 787 return self.id
789 788
790 789 def updateId(self, new_id):
791 790
792 791 self.id = str(new_id)
793 792
794 793 keyList = self.procUnitConfObjDict.keys()
795 794 keyList.sort()
796 795
797 796 n = 1
798 797 newProcUnitConfObjDict = {}
799 798
800 799 for procKey in keyList:
801 800
802 801 procUnitConfObj = self.procUnitConfObjDict[procKey]
803 802 idProcUnit = str(int(self.id)*10 + n)
804 803 procUnitConfObj.updateId(idProcUnit, parentId = self.id)
805 804
806 805 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
807 806 n += 1
808 807
809 808 self.procUnitConfObjDict = newProcUnitConfObjDict
810 809
811 810 def setup(self, id, name, description):
812 811
813 812 self.id = str(id)
814 813 self.name = name
815 814 self.description = description
816 815
817 816 def update(self, name, description):
818 817
819 818 self.name = name
820 819 self.description = description
821 820
822 821 def addReadUnit(self, datatype=None, name=None, **kwargs):
823 822
824 823 idReadUnit = self.__getNewId()
825 824
826 825 readUnitConfObj = ReadUnitConf()
827 826 readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs)
828 827
829 828 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
830 829
831 830 return readUnitConfObj
832 831
833 832 def addProcUnit(self, inputId='0', datatype=None, name=None):
834 833
835 834 idProcUnit = self.__getNewId()
836 835
837 836 procUnitConfObj = ProcUnitConf()
838 837 procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id)
839 838
840 839 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
841 840
842 841 return procUnitConfObj
843 842
844 843 def removeProcUnit(self, id):
845 844
846 845 if id in self.procUnitConfObjDict.keys():
847 846 self.procUnitConfObjDict.pop(id)
848 847
849 848 def getReadUnitId(self):
850 849
851 850 readUnitConfObj = self.getReadUnitObj()
852 851
853 852 return readUnitConfObj.id
854 853
855 854 def getReadUnitObj(self):
856 855
857 856 for obj in self.procUnitConfObjDict.values():
858 857 if obj.getElementName() == "ReadUnit":
859 858 return obj
860 859
861 860 return None
862 861
863 862 def getProcUnitObj(self, id=None, name=None):
864 863
865 864 if id != None:
866 865 return self.procUnitConfObjDict[id]
867 866
868 867 if name != None:
869 868 return self.getProcUnitObjByName(name)
870 869
871 870 return None
872 871
873 872 def getProcUnitObjByName(self, name):
874 873
875 874 for obj in self.procUnitConfObjDict.values():
876 875 if obj.name == name:
877 876 return obj
878 877
879 878 return None
880 879
881 880 def procUnitItems(self):
882 881
883 882 return self.procUnitConfObjDict.items()
884 883
885 884 def makeXml(self):
886 885
887 886 projectElement = Element('Project')
888 887 projectElement.set('id', str(self.id))
889 888 projectElement.set('name', self.name)
890 889 projectElement.set('description', self.description)
891
892 # for readUnitConfObj in self.readUnitConfObjList:
893 # readUnitConfObj.makeXml(projectElement)
894 890
895 891 for procUnitConfObj in self.procUnitConfObjDict.values():
896 892 procUnitConfObj.makeXml(projectElement)
897 893
898 894 self.projectElement = projectElement
899 895
900 896 def writeXml(self, filename):
901 897
902 898 self.makeXml()
903 899
904 900 #print prettify(self.projectElement)
905 901
906 902 ElementTree(self.projectElement).write(filename, method='xml')
907 903
908 904 def readXml(self, filename):
909 905
910 #tree = ET.parse(filename)
911 906 self.projectElement = None
912 # self.readUnitConfObjList = []
913 907 self.procUnitConfObjDict = {}
914 908
915 909 self.projectElement = ElementTree().parse(filename)
916 910
917 911 self.project = self.projectElement.tag
918 912
919 913 self.id = self.projectElement.get('id')
920 914 self.name = self.projectElement.get('name')
921 915 self.description = self.projectElement.get('description')
922 916
923 917 readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName())
924 918
925 919 for readUnitElement in readUnitElementList:
926 920 readUnitConfObj = ReadUnitConf()
927 921 readUnitConfObj.readXml(readUnitElement)
928 922
929 923 if readUnitConfObj.parentId == None:
930 924 readUnitConfObj.parentId = self.id
931 925
932 926 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
933 927
934 928 procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName())
935 929
936 930 for procUnitElement in procUnitElementList:
937 931 procUnitConfObj = ProcUnitConf()
938 932 procUnitConfObj.readXml(procUnitElement)
939 933
940 934 if procUnitConfObj.parentId == None:
941 935 procUnitConfObj.parentId = self.id
942 936
943 937 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
944 938
945 939 def printattr(self):
946 940
947 941 print "Project[%s]: name = %s, description = %s" %(self.id,
948 942 self.name,
949 943 self.description)
950 944
951 # for readUnitConfObj in self.readUnitConfObjList:
952 # readUnitConfObj.printattr()
953
954 945 for procUnitConfObj in self.procUnitConfObjDict.values():
955 946 procUnitConfObj.printattr()
956 947
957 948 def createObjects(self):
958 949
959 # for readUnitConfObj in self.readUnitConfObjList:
960 # readUnitConfObj.createObjects()
961
962 950 for procUnitConfObj in self.procUnitConfObjDict.values():
963 951 procUnitConfObj.createObjects()
964 952
965 953 def __connect(self, objIN, thisObj):
966 954
967 955 thisObj.setInput(objIN.getOutputObj())
968 956
969 957 def connectObjects(self):
970 958
971 959 for thisPUConfObj in self.procUnitConfObjDict.values():
972 960
973 961 inputId = thisPUConfObj.getInputId()
974 962
975 963 if int(inputId) == 0:
976 964 continue
977 965
978 966 #Get input object
979 967 puConfINObj = self.procUnitConfObjDict[inputId]
980 968 puObjIN = puConfINObj.getProcUnitObj()
981 969
982 970 #Get current object
983 971 thisPUObj = thisPUConfObj.getProcUnitObj()
984 972
985 973 self.__connect(puObjIN, thisPUObj)
986 974
987 975 def run(self):
988 976
989 # for readUnitConfObj in self.readUnitConfObjList:
990 # readUnitConfObj.run()
991 977 print
992 978 print "*"*40
993 979 print " Starting SIGNAL CHAIN PROCESSING "
994 980 print "*"*40
995 981 print
996 982
997 983 keyList = self.procUnitConfObjDict.keys()
998 984 keyList.sort()
999 985
1000 986 while(True):
1001 987
1002 988 finalSts = False
1003 989
1004 990 for procKey in keyList:
1005 991 # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
1006 992
1007 993 procUnitConfObj = self.procUnitConfObjDict[procKey]
1008 994 sts = procUnitConfObj.run()
1009 995 finalSts = finalSts or sts
1010 996
1011 997 #If every process unit finished so end process
1012 998 if not(finalSts):
1013 999 print "Every process unit have finished"
1014 1000 break
1015 1001
1016 1002 if self.control['pause']:
1017 1003 print "Process suspended"
1018 1004
1019 1005 while True:
1020 1006 sleep(0.1)
1021 1007
1022 1008 if not self.control['pause']:
1023 1009 break
1024 1010
1025 1011 if self.control['stop']:
1026 1012 break
1027 1013 print "Process reinitialized"
1028 1014
1029 1015 if self.control['stop']:
1030 print "Process stopped"
1016 # print "Process stopped"
1031 1017 break
1032 1018
1033 1019 #Closing every process
1034 1020 for procKey in keyList:
1035 1021 procUnitConfObj = self.procUnitConfObjDict[procKey]
1036 1022 procUnitConfObj.close()
1037 1023
1038 1024 print "Process finished"
1039 1025
1040 1026 def start(self, filename):
1041 1027
1042 1028 self.writeXml(filename)
1043 1029 self.readXml(filename)
1044 1030
1045 1031 self.createObjects()
1046 1032 self.connectObjects()
1047 1033 self.run()
1048
1049 class ControllerThread(threading.Thread, Project):
1050
1051 def __init__(self, filename):
1052
1053 threading.Thread.__init__(self)
1054 Project.__init__(self)
1055
1056 self.setDaemon(True)
1057
1058 self.filename = filename
1059 self.control = {'stop':False, 'pause':False}
1060
1061 def stop(self):
1062 self.control['stop'] = True
1063
1064 def pause(self):
1065 self.control['pause'] = not(self.control['pause'])
1066
1067 def run(self):
1068 self.control['stop'] = False
1069 self.control['pause'] = False
1070
1071 self.readXml(self.filename)
1072 self.createObjects()
1073 self.connectObjects()
1074 Project.run(self)
1075 1034
1076 1035 if __name__ == '__main__':
1077 1036
1078 1037 desc = "Segundo Test"
1079 1038 filename = "schain.xml"
1080 1039
1081 1040 controllerObj = Project()
1082 1041
1083 1042 controllerObj.setup(id = '191', name='test01', description=desc)
1084 1043
1085 1044 readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage',
1086 1045 path='data/rawdata/',
1087 1046 startDate='2011/01/01',
1088 1047 endDate='2012/12/31',
1089 1048 startTime='00:00:00',
1090 1049 endTime='23:59:59',
1091 1050 online=1,
1092 1051 walk=1)
1093 1052
1094 # opObj00 = readUnitConfObj.addOperation(name='printInfo')
1095
1096 1053 procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId())
1097 1054
1098 1055 opObj10 = procUnitConfObj0.addOperation(name='selectChannels')
1099 1056 opObj10.addParameter(name='channelList', value='3,4,5', format='intlist')
1100 1057
1101 1058 opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
1102 1059 opObj10.addParameter(name='minHei', value='90', format='float')
1103 1060 opObj10.addParameter(name='maxHei', value='180', format='float')
1104 1061
1105 1062 opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external')
1106 1063 opObj12.addParameter(name='n', value='10', format='int')
1107 1064
1108 1065 procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId())
1109 1066 procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int')
1110 1067 # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='')
1111 1068
1112 1069
1113 1070 opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
1114 1071 opObj11.addParameter(name='idfigure', value='1', format='int')
1115 1072 opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str')
1116 1073 opObj11.addParameter(name='zmin', value='40', format='int')
1117 1074 opObj11.addParameter(name='zmax', value='90', format='int')
1118 1075 opObj11.addParameter(name='showprofile', value='1', format='int')
1119
1120 # opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='external')
1121 # opObj11.addParameter(name='idfigure', value='2', format='int')
1122 # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot', format='str')
1123 # opObj11.addParameter(name='zmin', value='40', format='int')
1124 # opObj11.addParameter(name='zmax', value='90', format='int')
1125
1126
1127 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId())
1128 #
1129 # opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype='external')
1130 # opObj12.addParameter(name='n', value='2', format='int')
1131 # opObj12.addParameter(name='overlapping', value='1', format='int')
1132 #
1133 # procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId())
1134 # procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int')
1135 #
1136 # opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype='external')
1137 # opObj11.addParameter(name='idfigure', value='2', format='int')
1138 # opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str')
1139 # opObj11.addParameter(name='zmin', value='40', format='int')
1140 # opObj11.addParameter(name='zmax', value='90', format='int')
1141 # opObj11.addParameter(name='showprofile', value='1', format='int')
1142
1143 # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='external')
1144 # opObj11.addParameter(name='idfigure', value='10', format='int')
1145 # opObj11.addParameter(name='wintitle', value='RTI', format='str')
1146 ## opObj11.addParameter(name='xmin', value='21', format='float')
1147 ## opObj11.addParameter(name='xmax', value='22', format='float')
1148 # opObj11.addParameter(name='zmin', value='40', format='int')
1149 # opObj11.addParameter(name='zmax', value='90', format='int')
1150 # opObj11.addParameter(name='showprofile', value='1', format='int')
1151 # opObj11.addParameter(name='timerange', value=str(60), format='int')
1152
1153 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
1154 # opObj10.addParameter(name='channelList', value='0,2,4,6', format='intlist')
1155 #
1156 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='external')
1157 # opObj12.addParameter(name='n', value='2', format='int')
1158 #
1159 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
1160 # opObj11.addParameter(name='idfigure', value='2', format='int')
1161 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
1162 # opObj11.addParameter(name='zmin', value='70', format='int')
1163 # opObj11.addParameter(name='zmax', value='90', format='int')
1164 #
1165 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
1166 # opObj10.addParameter(name='channelList', value='2,6', format='intlist')
1167 #
1168 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='external')
1169 # opObj12.addParameter(name='n', value='2', format='int')
1170 #
1171 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
1172 # opObj11.addParameter(name='idfigure', value='3', format='int')
1173 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
1174 # opObj11.addParameter(name='zmin', value='70', format='int')
1175 # opObj11.addParameter(name='zmax', value='90', format='int')
1176
1177
1178 # opObj12 = procUnitConfObj1.addOperation(name='decoder')
1179 # opObj12.addParameter(name='ncode', value='2', format='int')
1180 # opObj12.addParameter(name='nbauds', value='8', format='int')
1181 # opObj12.addParameter(name='code0', value='001110011', format='int')
1182 # opObj12.addParameter(name='code1', value='001110011', format='int')
1183
1184
1185
1186 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj1.getId())
1187 #
1188 # opObj21 = procUnitConfObj2.addOperation(name='IncohInt', optype='external')
1189 # opObj21.addParameter(name='n', value='2', format='int')
1190 #
1191 # opObj11 = procUnitConfObj2.addOperation(name='SpectraPlot', optype='external')
1192 # opObj11.addParameter(name='idfigure', value='4', format='int')
1193 # opObj11.addParameter(name='wintitle', value='SpectraPlot OBJ 2', format='str')
1194 # opObj11.addParameter(name='zmin', value='70', format='int')
1195 # opObj11.addParameter(name='zmax', value='90', format='int')
1196
1076
1197 1077 print "Escribiendo el archivo XML"
1198 1078
1199 1079 controllerObj.writeXml(filename)
1200 1080
1201 1081 print "Leyendo el archivo XML"
1202 1082 controllerObj.readXml(filename)
1203 1083 #controllerObj.printattr()
1204 1084
1205 1085 controllerObj.createObjects()
1206 1086 controllerObj.connectObjects()
1207 1087 controllerObj.run()
1208 1088
1209 1089 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now