fsasimulator_debugging.py.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?xml version="1.0" ?>
  2. <diagram name="FSASimulator" author="Sadaf Mustafiz and Bruno Barroca and Claudio Gomes and Simon Van Mierlo">
  3. <description>
  4. A debuggeable FSA simulator. It supports after and events.
  5. </description>
  6. <inport name="user_input" />
  7. <inport name="user_output" />
  8. <top>
  9. from sccd.runtime.libs.ui import *
  10. from sccd.runtime.libs.utils import *
  11. import fsaclasses
  12. import sccd.runtime.accurate_time as accurate_time
  13. class Breakpoint:
  14. def __init__(self, name, function, enabled, disable_on_trigger):
  15. self.name = name
  16. self.function = function
  17. self.enabled = enabled
  18. self.disable_on_trigger = disable_on_trigger
  19. </top>
  20. <class name="FSASimulator" default="True">
  21. <attribute name="elapsed"/>
  22. <attribute name="super_step_completed"/>
  23. <attribute name="model"/>
  24. <attribute name="clock"/>
  25. <attribute name="timestep"/>
  26. <attribute name="currentEvent"/>
  27. <attribute name="selectedTransition"/>
  28. <attribute name="eventList"/>
  29. <attribute name="currentState"/>
  30. <method name="FSASimulator">
  31. <parameter name="amodel"/>
  32. <parameter name="events"/>
  33. <body>
  34. <![CDATA[
  35. self.model = amodel
  36. self.eventList = events
  37. ]]>
  38. </body>
  39. </method>
  40. <method name="processEvent">
  41. <parameter name="event"/>
  42. <body>
  43. <![CDATA[
  44. if (event != None):
  45. self.eventList.popEvent(event)
  46. event.processed = True
  47. ]]>
  48. </body>
  49. </method>
  50. <method name="getInputEventAt">
  51. <parameter name="time"/>
  52. <body>
  53. <![CDATA[
  54. return self.eventList.getInputAt(time)
  55. ]]>
  56. </body>
  57. </method>
  58. <method name="initialize">
  59. <body>
  60. <![CDATA[
  61. self.clock = 0
  62. self.elapsed = 0
  63. self.timestep = 1.0 * 1000.0
  64. self.time_next = self.timestep
  65. self.currentState = self.model.initialState
  66. ]]>
  67. </body>
  68. </method>
  69. <method name="initializeDebugger">
  70. <body>
  71. <![CDATA[
  72. self.breakpoints = []
  73. self.triggered_bp = None
  74. ]]>
  75. </body>
  76. </method>
  77. <method name="endCondition">
  78. <body>
  79. <![CDATA[
  80. return self.currentState and self.currentState.final
  81. ]]>
  82. </body>
  83. </method>
  84. <method name="advanceTime">
  85. <body>
  86. <![CDATA[
  87. self.clock = self.time_next
  88. self.elapsed = self.elapsed + self.timestep
  89. self.time_next = self.clock + self.timestep
  90. ]]>
  91. </body>
  92. </method>
  93. <method name="finalize">
  94. <body>
  95. <![CDATA[
  96. print 'Simulation finalized.'
  97. ]]>
  98. </body>
  99. </method>
  100. <method name="waitTime">
  101. <body>
  102. <![CDATA[
  103. # First, we convert from wall-clock time to simulated time.
  104. # This means the elapsed time in wall-clock time needs to be scaled according to the realtime scale (for example, if the realtime scale is 2, an elapsed time of 1 second in wall-clock time is equal to an elapsed time of 2 seconds in simulated time).
  105. simulated_diff = (accurate_time.time() - self.realtime_start_time) * self.realtime_scale
  106. # time_next and simulated_diff are both in simulated time: so now scale back to wall-clock time by dividing.
  107. # This function returns an amount of miliseconds.
  108. return ((self.time_next - simulated_diff) / self.realtime_scale)
  109. ]]>
  110. </body>
  111. </method>
  112. <method name="addBreakpoint">
  113. <parameter name="name" />
  114. <parameter name="function" />
  115. <parameter name="enabled" default="true" />
  116. <parameter name="disable_on_trigger" default="true" />
  117. <body>
  118. <![CDATA[
  119. if len([bp for bp in self.breakpoints if bp.name == name]) > 0:
  120. return -1
  121. self.breakpoints.append(Breakpoint(name, function, enabled, disable_on_trigger))
  122. return 0
  123. ]]>
  124. </body>
  125. </method>
  126. <method name="delBreakpoint">
  127. <parameter name="name" />
  128. <body>
  129. <![CDATA[
  130. if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
  131. return -1
  132. self.breakpoints = [bp for bp in self.breakpoints if bp.name != name]
  133. return 0
  134. ]]>
  135. </body>
  136. </method>
  137. <method name="toggleBreakpoint">
  138. <parameter name="name" />
  139. <body>
  140. <![CDATA[
  141. if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
  142. return -1
  143. for bp in self.breakpoints:
  144. if bp.name == name:
  145. bp.enabled = enabled
  146. break
  147. return 0
  148. ]]>
  149. </body>
  150. </method>
  151. <method name="breakpointTriggers">
  152. <parameter name="is_realtime_simulation" />
  153. <body>
  154. <![CDATA[
  155. self.triggered_bp = None
  156. for bp in self.breakpoints:
  157. if not bp.enabled:
  158. continue
  159. # include the function in the scope...
  160. exec(bp.function)
  161. # ... and execute it, note that the breakpoint thus has to start with "def breakpoint("
  162. # note that we pass self.time_next instead of self.simulated_time in the case of as-fast-as-possible simulation (or stepping)
  163. # this is to make sure that the simulation is stopped BEFORE the specified time is reached, and not AFTER (because we don't necessarily implement 'step back')
  164. # in case of realtime simulation, we do pass the current simulated time, since we can stop at (more or less) exactly the right time
  165. if breakpoint({'clock': (self.clock if is_realtime_simulation else self.time_next) / 1000.0, 'state': self.currentState, 'event': self.currentEvent}):
  166. # triggered!
  167. self.triggered_bp = bp.name
  168. if bp.disable_on_trigger:
  169. bp.enabled = False
  170. return True
  171. else:
  172. # not triggered, so continue
  173. continue
  174. return False
  175. ]]>
  176. </body>
  177. </method>
  178. <method name="godEvent">
  179. <parameter name="new_state" />
  180. <body>
  181. <![CDATA[
  182. filtered_states = [s for s in self.model.states if s.name == new_state]
  183. if not len(filtered_states) == 1:
  184. return -1
  185. self.currentState = filtered_states[0]
  186. return 0
  187. ]]>
  188. </body>
  189. </method>
  190. <scxml initial="Main" final="SimulationComplete" internal_event_lifeline="next_combo_step">
  191. <parallel id="Main">
  192. <state id="SimulationState" initial="Paused">
  193. <state id="Paused">
  194. <transition target="../Running/Continuous" event="continuous" port="user_input" />
  195. <transition target="../Running/Realtime" event="realtime" port="user_input">
  196. <parameter name="realtime_scale" default="1.0" />
  197. <script>
  198. self.realtime_scale = float(realtime_scale)
  199. </script>
  200. </transition>
  201. <transition target="../Running/BigStep" event="big_step" port="user_input" />
  202. <transition target="../Running/SmallStep" event="small_step" port="user_input" />
  203. <!-- If a god event enables the end condition, the simulation should stop. -->
  204. <transition target="../PreStopped" cond="self.endCondition()" />
  205. </state>
  206. <state id="PrePaused">
  207. <!-- Here, we make sure the current step has finished, before we raise the 'paused' event. Otherwise, UserOutput will output the old state, not the latest one. -->
  208. <transition target="../Paused" after="self.sccd_yield() * 2">
  209. <raise event="paused" />
  210. </transition>
  211. </state>
  212. <state id="PreBreakpointTriggered">
  213. <!-- Here, we make sure the current step has finished, before we raise the 'paused' event. Otherwise, UserOutput will output the old state, not the latest one. -->
  214. <transition target="../Paused" after="self.sccd_yield() * 2">
  215. <raise event="breakpoint_triggered" />
  216. </transition>
  217. </state>
  218. <state id="Running" initial="Continuous">
  219. <transition target="../PreStopped" cond="self.endCondition()" />
  220. <transition target="../PrePaused" event="pause" port="user_input" />
  221. <transition target="../PreBreakpointTriggered" cond="self.breakpointTriggers(INSTATE('./Realtime'))" />
  222. <state id="Continuous" />
  223. <state id="BigStep">
  224. <!-- We go to a special 'BigStepDone' state because in the 'user_output' state, we need to check whether we are currently executing a big step. -->
  225. <transition target="../BigStepDone" event="big_step_done" />
  226. </state>
  227. <state id="BigStepDone">
  228. <!-- We go back to the 'Paused' state once the big step has finished. -->
  229. <transition target="../../Paused" after="self.sccd_yield()" />
  230. </state>
  231. <state id="SmallStep">
  232. <!-- We go to a special 'SmallStepDone' state because in the 'user_output' state, we need to check whether we are currently executing a small step. -->
  233. <transition target="../SmallStepDone" event="small_step_done" />
  234. </state>
  235. <state id="SmallStepDone">
  236. <!-- We go back to the 'Paused' state once the small step has finished. -->
  237. <transition target="../../Paused" after="self.sccd_yield()" />
  238. </state>
  239. <state id="Realtime">
  240. <onentry>
  241. <script>
  242. # If the simulation was paused, we need to reset the start time of the simulation.
  243. # The start time of the simulation is equal to the point in wall-clock time where simulated time is 0.
  244. # If the simulation was paused, we have to recompute this point in time: it is the difference of the wall-clock time and the simulated time.
  245. # If the scale was changed after the pause, this point of course moves backwards (for scales smaller than 1) or forwards (for scales larger than 1)
  246. self.realtime_start_time = accurate_time.time() - (self.clock / self.realtime_scale)
  247. </script>
  248. </onentry>
  249. </state>
  250. </state>
  251. <state id="PreStopped">
  252. <transition target="../Stopped" after="self.sccd_yield() * 2">
  253. <raise event="termination_condition" />
  254. </transition>
  255. </state>
  256. <state id="Stopped" />
  257. </state>
  258. <state id="SimulationFlow" initial="Started">
  259. <state id="Started">
  260. <transition target="../Initialized">
  261. <script>
  262. <![CDATA[
  263. print('Going to Initialized... ')
  264. self.initialize()
  265. ]]>
  266. </script>
  267. </transition>
  268. </state>
  269. <state id="Initialized">
  270. <transition target="../InitializeDebugger">
  271. <script>
  272. <![CDATA[
  273. print('Going to InitializeDebugger... ')
  274. ]]>
  275. </script>
  276. </transition>
  277. </state>
  278. <state id="InitializeDebugger">
  279. <onentry>
  280. <script>
  281. <![CDATA[
  282. self.initializeDebugger()
  283. print('Going to MacroStepProcessed... ')
  284. ]]>
  285. </script>
  286. </onentry>
  287. <transition target="../CheckTermination" />
  288. </state>
  289. <state id="CheckTermination" initial="MacroStepProcessed">
  290. <state id="MacroStepProcessed">
  291. <transition target="../../Stopped" cond="INSTATE('/Main/SimulationState/Stopped')" after="self.sccd_yield()">
  292. <script>
  293. <![CDATA[
  294. print('Going to Stopped... ')
  295. ]]>
  296. </script>
  297. </transition>
  298. <transition target="../../Waiting" cond="INSTATE('/Main/SimulationState/Running/Realtime')" />
  299. <transition target="../../DoSimulation" cond="INSTATE('/Main/SimulationState/Running/Continuous') or INSTATE('/Main/SimulationState/Running/BigStep') or INSTATE('/Main/SimulationState/Running/SmallStep')" />
  300. </state>
  301. </state>
  302. <state id="Waiting">
  303. <!-- We schedule to go back to the check_termination state after the smallest possible delay (to accomodate for pauses). -->
  304. <transition target="../CheckTermination" after="self.sccd_yield()">
  305. <!-- We set the simulation time to the correct value. -->
  306. <script>
  307. diff = accurate_time.time() - self.realtime_start_time
  308. self.clock = diff * self.realtime_scale
  309. </script>
  310. </transition>
  311. <!-- We execute a step when the wait time is smaller than the smallest possible delay. -->
  312. <transition target="../DoSimulation" cond="self.waitTime() / 1000.0 &lt;= self.sccd_yield()" />
  313. </state>
  314. <state id="DoSimulation" initial="MacroStepPrepared">
  315. <state id="MacroStepPrepared">
  316. <onentry>
  317. <script>
  318. <![CDATA[
  319. print('Entered MacroStepPrepared and reading events... ')
  320. self.currentEvent = self.getInputEventAt(self.clock / 1000.0)
  321. self.selectedTransition = self.model.getTransitionFrom(self.currentState, self.currentEvent, self.elapsed)
  322. print self.clock / 1000.0
  323. print(self.currentEvent)
  324. print(self.selectedTransition)
  325. ]]>
  326. </script>
  327. </onentry>
  328. <transition target="../MicroStepProcessed">
  329. <script>
  330. <![CDATA[
  331. print('Going to MicroStepProcessed... ')
  332. ]]>
  333. </script>
  334. </transition>
  335. </state>
  336. <state id="MicroStepProcessed">
  337. <transition target="../PreMicroStepPrepared" cond="self.selectedTransition != None" after="self.sccd_yield() * 2">
  338. <script>
  339. <![CDATA[
  340. print('Going to MicroStepPrepared... ')
  341. ]]>
  342. </script>
  343. </transition>
  344. <transition target="../../CheckTermination" cond="self.selectedTransition == None" after="self.sccd_yield()">
  345. <script>
  346. <![CDATA[
  347. print('Going to CheckTermination and advancing time... ')
  348. self.advanceTime()
  349. print(self.clock / 1000.0)
  350. print(self.elapsed / 1000.0)
  351. ]]>
  352. </script>
  353. <raise event="big_step_done" />
  354. </transition>
  355. </state>
  356. <state id="PreMicroStepPrepared">
  357. <transition target="../MicroStepPrepared" cond="INSTATE('/Main/SimulationState/Running')" />
  358. </state>
  359. <state id="MicroStepPrepared">
  360. <transition target="../MicroStepProcessed">
  361. <script>
  362. <![CDATA[
  363. print('Going to MicroStepProcessed, taking transition and reading events... ')
  364. print('Transition to be taken: ' + str(self.selectedTransition))
  365. self.currentState = self.selectedTransition.target
  366. self.elapsed = 0
  367. self.processEvent(self.currentEvent)
  368. print('New state: ' + str(self.currentState))
  369. self.currentEvent = self.getInputEventAt(self.clock / 1000.0)
  370. self.selectedTransition = self.model.getTransitionFrom(self.currentState, self.currentEvent, self.elapsed)
  371. print(self.currentEvent)
  372. print(self.selectedTransition)
  373. ]]>
  374. </script>
  375. <raise event="small_step_done" />
  376. </transition>
  377. </state>
  378. </state>
  379. <state id="Stopped" />
  380. </state>
  381. <state id="BreakpointManager" initial="Listening">
  382. <state id="Listening">
  383. <transition target="." event="add_breakpoint" port="user_input">
  384. <parameter name="name"/>
  385. <parameter name="function"/>
  386. <parameter name="enabled"/>
  387. <parameter name="disable_on_trigger"/>
  388. <script>
  389. result = self.addBreakpoint(name, function, bool(enabled), bool(disable_on_trigger))
  390. </script>
  391. <raise event="add_breakpoint_result" port="user_output">
  392. <parameter expr="result" />
  393. </raise>
  394. </transition>
  395. <transition target="." event="del_breakpoint" port="user_input">
  396. <parameter name="name"/>
  397. <script>
  398. result = self.delBreakpoint(name)
  399. </script>
  400. <raise event="del_breakpoint_result" port="user_output">
  401. <parameter expr="result" />
  402. </raise>
  403. </transition>
  404. <transition target="." event="toggle_breakpoint" port="user_input">
  405. <parameter name="name"/>
  406. <script>
  407. result = self.toggleBreakpoint(name)
  408. </script>
  409. <raise event="toggle_breakpoint_result" port="user_output">
  410. <parameter expr="result" />
  411. </raise>
  412. </transition>
  413. <transition target="." event="list_breakpoints" port="user_input">
  414. <raise event="list_breakpoints_result" port="user_output">
  415. <parameter expr="[bp.name for bp in self.breakpoints]" />
  416. </raise>
  417. </transition>
  418. </state>
  419. </state>
  420. <state id="GodEventManager" initial="Listening">
  421. <state id="Listening">
  422. <transition target="." event="god_event" port="user_input" cond="INSTATE('/Main/SimulationState/Paused')">
  423. <parameter name="new_state" />
  424. <script>
  425. result = self.godEvent(new_state)
  426. </script>
  427. <raise event="god_event_result" port="user_output">
  428. <parameter expr="result" />
  429. </raise>
  430. <raise event="current_state" port="user_output">
  431. <parameter expr="self.clock / 1000.0" />
  432. <parameter expr="self.currentState" />
  433. </raise>
  434. </transition>
  435. </state>
  436. </state>
  437. <state id="UserOutput" initial="Waiting">
  438. <state id="Waiting">
  439. <transition target="." event="termination_condition">
  440. <raise event="terminated" port="user_output"/>
  441. <raise event="current_state" port="user_output">
  442. <parameter expr="self.clock / 1000.0" />
  443. <parameter expr="self.currentState" />
  444. </raise>
  445. </transition>
  446. <transition target="." event="paused">
  447. <raise event="paused" port="user_output" />
  448. <raise event="current_state" port="user_output">
  449. <parameter expr="self.clock / 1000.0" />
  450. <parameter expr="self.currentState" />
  451. </raise>
  452. </transition>
  453. <transition target="." event="big_step_done" cond="INSTATE('/Main/SimulationState/Running/Realtime') or INSTATE('/Main/SimulationState/Running/BigStep') or INSTATE('/Main/SimulationState/Running/BigStepDone')">
  454. <raise event="big_step_done" port="user_output" />
  455. <raise event="current_state" port="user_output">
  456. <parameter expr="self.clock / 1000.0" />
  457. <parameter expr="self.currentState" />
  458. </raise>
  459. </transition>
  460. <transition target="." event="small_step_done" cond="INSTATE('/Main/SimulationState/Running/SmallStep') or INSTATE('/Main/SimulationState/Running/SmallStepDone')">
  461. <raise event="small_step_done" port="user_output" />
  462. <raise event="current_state" port="user_output">
  463. <parameter expr="self.clock / 1000.0" />
  464. <parameter expr="self.currentState" />
  465. </raise>
  466. </transition>
  467. <transition target="." event="breakpoint_triggered">
  468. <raise event="breakpoint_triggered" port="user_output">
  469. <parameter expr="self.triggered_bp" />
  470. </raise>
  471. <raise event="current_state" port="user_output">
  472. <parameter expr="self.clock / 1000.0" />
  473. <parameter expr="self.currentState" />
  474. </raise>
  475. </transition>
  476. </state>
  477. </state>
  478. <transition target="../SimulationComplete" cond="INSTATE('./SimulationState/Stopped') and INSTATE('./SimulationFlow/Stopped')">
  479. <script>
  480. self.finalize()
  481. </script>
  482. </transition>
  483. </parallel>
  484. <state id="SimulationComplete" />
  485. </scxml>
  486. </class>
  487. </diagram>