task.xml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <class name="Task">
  2. <relationships>
  3. <association name="parent" class="MvKController" min="1" max="1"/>
  4. </relationships>
  5. <constructor>
  6. <parameter name="taskname"/>
  7. <parameter name="mvs_operations"/>
  8. <parameter name="mvk"/>
  9. <body>
  10. <![CDATA[
  11. self.taskname = taskname
  12. self.mvs_operations = mvs_operations
  13. self.mvk = mvk
  14. self.unlocked = True
  15. self.input_queue = []
  16. self.output_queue = []
  17. self.outputs = []
  18. ]]>
  19. </body>
  20. </constructor>
  21. <method name="execute_modelverse">
  22. <parameter name="taskname"/>
  23. <parameter name="operation"/>
  24. <parameter name="params"/>
  25. <body>
  26. <![CDATA[
  27. reply = None
  28. commands = []
  29. mvk = self.mvk
  30. mvs_operations = self.mvs_operations
  31. try:
  32. while 1:
  33. commands = mvk.execute_yields(taskname, operation, params, reply)
  34. if commands is None:
  35. break
  36. reply = [mvs_operations[command[0]](*(command[1])) for command in commands]
  37. return (0.0, False)
  38. except SleepKernel as e:
  39. return (e.timeout, e.interruptable)
  40. except KeyboardInterrupt:
  41. raise
  42. except:
  43. import traceback
  44. print("Error on requests: " + str(commands))
  45. print("For taskname " + str(taskname))
  46. print("Stack @ MvK:\n" + str("\n".join(['\t%s:%s' % (gen['generator'].__name__, gen['generator'].gi_frame.f_lineno) for gen in mvk.request_handlers[taskname][operation].generator_stack if gen['generator'].__name__ not in ['execute_rule', 'execute_jit', 'execute_jit_internal']])))
  47. print(traceback.format_exc())
  48. return (float('inf'), False)
  49. ]]>
  50. </body>
  51. </method>
  52. <scxml initial="main">
  53. <parallel id="main">
  54. <state id="queue">
  55. <state id="queue">
  56. <transition event="input" target=".">
  57. <parameter name="params"/>
  58. <script>
  59. self.input_queue.extend(params)
  60. </script>
  61. </transition>
  62. <transition event="output" target=".">
  63. <parameter name="params"/>
  64. <script>
  65. self.output_queue.append(params)
  66. </script>
  67. </transition>
  68. <transition cond="self.outputs" target=".">
  69. <script>
  70. source, value = self.outputs.pop(0)
  71. </script>
  72. <raise event="HTTP_input" scope="narrow" target="'parent/to_mvi/%s' % source">
  73. <parameter expr="json.dumps(value)"/>
  74. </raise>
  75. </transition>
  76. </state>
  77. </state>
  78. <state id="process" initial="running">
  79. <state id="running" initial="components">
  80. <transition event="suspend" target="../suspended"/>
  81. <history id="history" type="deep"/>
  82. <parallel id="components">
  83. <state id="input">
  84. <state id="input">
  85. <transition cond="self.input_queue" target=".">
  86. <script>
  87. for args_entry in self.input_queue:
  88. self.execute_modelverse(self.taskname, "set_input", [args_entry])
  89. self.input_queue = []
  90. </script>
  91. <raise event="wake_timer"/>
  92. </transition>
  93. </state>
  94. </state>
  95. <state id="processing" initial="processing">
  96. <state id="processing">
  97. <onentry>
  98. <script>
  99. start_time = time.time()
  100. # Grant each task some milliseconds of execution
  101. while (time.time() - start_time &lt; 0.05):
  102. timeout = self.execute_modelverse(self.taskname, "execute_rule", [])
  103. if timeout[0] > 0.0:
  104. # We should not continue immediately
  105. break
  106. self.timeout = timeout
  107. </script>
  108. </onentry>
  109. <transition cond="self.timeout[0] == 0.0" after="self.sccd_yield()" target="."/>
  110. <transition cond="0.0 &lt; self.timeout[0] &lt; float('inf')" after="self.sccd_yield()" target="../blocked">
  111. <script>
  112. self.unlocked = False
  113. </script>
  114. <raise event="start_timer">
  115. <parameter expr="self.timeout[0]"/>
  116. <parameter expr="self.timeout[1]"/>
  117. </raise>
  118. </transition>
  119. <transition cond="self.timeout[0] == float('inf')" target="../failed"/>
  120. </state>
  121. <state id="blocked">
  122. <transition cond="self.unlocked" target="../processing"/>
  123. </state>
  124. <state id="failed">
  125. <script>
  126. print("TODO: task has failed")
  127. </script>
  128. </state>
  129. </state>
  130. <state id="output">
  131. <state id="output">
  132. <onentry>
  133. <script>
  134. if self.output_queue:
  135. if self.execute_modelverse(self.taskname, "get_output", []):
  136. if self.mvk.success:
  137. self.outputs.append((self.output_queue.pop(0), self.mvk.returnvalue))
  138. </script>
  139. </onentry>
  140. <transition after="self.sccd_yield() + 0.1" target="."/>
  141. </state>
  142. </state>
  143. </parallel>
  144. </state>
  145. <state id="suspended">
  146. <transition event="resume" target="../running/history"/>
  147. </state>
  148. </state>
  149. <state id="timer" initial="ready">
  150. <state id="waiting">
  151. <transition after="self.sccd_yield() + self.timer_duration" target="../ready"/>
  152. <transition event="wake_timer" cond="self.interruptable" target="../ready"/>
  153. </state>
  154. <state id="ready">
  155. <onentry>
  156. <script>
  157. self.unlocked = True
  158. </script>
  159. </onentry>
  160. <transition event="start_timer" target="../waiting">
  161. <parameter name="duration"/>
  162. <parameter name="interruptable"/>
  163. <script>
  164. self.timer_duration = duration
  165. self.interruptable = interruptable
  166. </script>
  167. </transition>
  168. </state>
  169. </state>
  170. </parallel>
  171. </scxml>
  172. </class>