task.xml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. stack = [gen for gen in mvk.request_handlers[taskname][operation].generator_stack if gen is not None and gen.__name__ not in ['execute_rule', 'execute_jit']]
  47. printed_stack = []
  48. for gen in stack:
  49. try:
  50. line = gen.gi_frame.f_lineno
  51. except:
  52. line = "?"
  53. try:
  54. variables = gen.gi_frame.f_locals
  55. except:
  56. variables = "?"
  57. printed_stack.append("%s:%s {%s}" % (gen.__name__, line, variables))
  58. print("Stack @ MvK:\n" + str("\n".join(printed_stack)))
  59. print(traceback.format_exc())
  60. return (float('inf'), False)
  61. ]]>
  62. </body>
  63. </method>
  64. <scxml initial="main">
  65. <parallel id="main">
  66. <state id="queue">
  67. <state id="queue">
  68. <transition event="input" target=".">
  69. <parameter name="params"/>
  70. <script>
  71. self.input_queue.extend(params)
  72. </script>
  73. </transition>
  74. <transition event="output" target=".">
  75. <parameter name="params"/>
  76. <script>
  77. self.output_queue.append(params)
  78. </script>
  79. </transition>
  80. <transition cond="self.outputs" target=".">
  81. <script>
  82. source, value = self.outputs.pop(0)
  83. </script>
  84. <raise event="HTTP_input" scope="narrow" target="'parent/to_mvi/%s' % source">
  85. <parameter expr="json.dumps(value)"/>
  86. </raise>
  87. </transition>
  88. </state>
  89. </state>
  90. <state id="process" initial="running">
  91. <state id="running" initial="components">
  92. <transition event="suspend" target="../suspended"/>
  93. <history id="history" type="deep"/>
  94. <parallel id="components">
  95. <state id="input">
  96. <state id="input">
  97. <transition cond="self.input_queue" target=".">
  98. <script>
  99. for args_entry in self.input_queue:
  100. self.execute_modelverse(self.taskname, "set_input", [args_entry])
  101. self.input_queue = []
  102. </script>
  103. <raise event="wake_timer"/>
  104. </transition>
  105. </state>
  106. </state>
  107. <state id="processing" initial="processing">
  108. <state id="processing">
  109. <onentry>
  110. <script>
  111. start_time = time.time()
  112. # Grant each task some milliseconds of execution
  113. while (time.time() - start_time &lt; 0.05):
  114. timeout = self.execute_modelverse(self.taskname, "execute_rule", [])
  115. if timeout[0] > 0.0:
  116. # We should not continue immediately
  117. break
  118. self.timeout = timeout
  119. </script>
  120. </onentry>
  121. <transition cond="self.timeout[0] == 0.0" after="self.sccd_yield()" target="."/>
  122. <transition cond="0.0 &lt; self.timeout[0] &lt; float('inf')" after="self.sccd_yield()" target="../blocked">
  123. <script>
  124. self.unlocked = False
  125. </script>
  126. <raise event="start_timer">
  127. <parameter expr="self.timeout[0]"/>
  128. <parameter expr="self.timeout[1]"/>
  129. </raise>
  130. </transition>
  131. <transition cond="self.timeout[0] == float('inf')" target="../failed"/>
  132. </state>
  133. <state id="blocked">
  134. <transition cond="self.unlocked" target="../processing"/>
  135. </state>
  136. <state id="failed">
  137. <script>
  138. print("TODO: task has failed")
  139. </script>
  140. </state>
  141. </state>
  142. <state id="output">
  143. <state id="output">
  144. <onentry>
  145. <script>
  146. if self.output_queue:
  147. if self.execute_modelverse(self.taskname, "get_output", []):
  148. if self.mvk.success:
  149. self.outputs.append((self.output_queue.pop(0), self.mvk.returnvalue))
  150. </script>
  151. </onentry>
  152. <transition after="self.sccd_yield() + 0.1" target="."/>
  153. </state>
  154. </state>
  155. </parallel>
  156. </state>
  157. <state id="suspended">
  158. <transition event="resume" target="../running/history"/>
  159. </state>
  160. </state>
  161. <state id="timer" initial="ready">
  162. <state id="waiting">
  163. <transition after="self.sccd_yield() + self.timer_duration" target="../ready"/>
  164. <transition event="wake_timer" cond="self.interruptable" target="../ready"/>
  165. </state>
  166. <state id="ready">
  167. <onentry>
  168. <script>
  169. self.unlocked = True
  170. </script>
  171. </onentry>
  172. <transition event="start_timer" target="../waiting">
  173. <parameter name="duration"/>
  174. <parameter name="interruptable"/>
  175. <script>
  176. self.timer_duration = duration
  177. self.interruptable = interruptable
  178. </script>
  179. </transition>
  180. </state>
  181. </state>
  182. </parallel>
  183. </scxml>
  184. </class>