cbdsim_debugging.py.xml 25 KB

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