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="delta"/>
  24. <attribute name="clock"/>
  25. <attribute name="state"/>
  26. <attribute name="model"/>
  27. <attribute name="depGraph"/>
  28. <attribute name="strongComponentList"/>
  29. <attribute name="currentCompIdx"/>
  30. <attribute name="cbdController"/>
  31. <attribute name="options"/>
  32. <method name="CBDSimulator">
  33. <parameter name="options"/>
  34. <parameter name="model"/>
  35. <body>
  36. <![CDATA[
  37. self.options = options
  38. self.delta = self.options.getDeltaT() * 1000.0 # in miliseconds for real-time simulation
  39. self.model = model
  40. self.initialized = False
  41. ]]>
  42. </body>
  43. </method>
  44. <method name="initialize">
  45. <body>
  46. <![CDATA[
  47. self.iteration = 0
  48. self.clock = 0
  49. self.time_next = self.delta
  50. self.cbdController = CBDController(self.model, self.delta / 1000.0)
  51. self.cbdController.initSimulation()
  52. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  53. self.initialized = True
  54. ]]>
  55. </body>
  56. </method>
  57. <method name="endCondition">
  58. <body>
  59. <![CDATA[
  60. return self.initialized and self.iteration >= self.options.getMaxIterations()
  61. ]]>
  62. </body>
  63. </method>
  64. <method name="advanceTime">
  65. <body>
  66. <![CDATA[
  67. self.iteration = self.iteration + 1
  68. self.clock = self.time_next
  69. self.cbdController.advanceTimeStep()
  70. self.time_next = self.clock + self.delta
  71. ]]>
  72. </body>
  73. </method>
  74. <method name="finalize">
  75. <body>
  76. <![CDATA[
  77. from bokeh.plotting import figure, output_file, show
  78. times = []
  79. values = []
  80. for timeValuePair in self.model.getSignal("neg"):
  81. times.append(timeValuePair.time)
  82. values.append(timeValuePair.value)
  83. output_file("./plot.html", title="Plot")
  84. p = figure(title="Something vs Otherthing", x_axis_label="Time", y_axis_label="Values")
  85. p.line(times, values, legend="Something", line_width=1, line_color="red")
  86. show(p)
  87. print 'Simulation finalized.'
  88. ]]>
  89. </body>
  90. </method>
  91. <method name="currentComponentIsCycle">
  92. <body>
  93. <![CDATA[
  94. return self.cbdController.componentIsCycle(self.strongComponentList[self.currentCompIdx], self.depGraph)
  95. ]]>
  96. </body>
  97. </method>
  98. <method name="hasNextStrongComponent">
  99. <body>
  100. <![CDATA[
  101. return self.currentCompIdx < len(self.strongComponentList)
  102. ]]>
  103. </body>
  104. </method>
  105. <method name="computeBlock">
  106. <body>
  107. <![CDATA[
  108. if self.currentComponentIsCycle():
  109. self.cbdController.computeNextAlgebraicLoop(self.strongComponentList[self.currentCompIdx], self.iteration)
  110. else:
  111. self.cbdController.computeNextBlock(self.strongComponentList[self.currentCompIdx], self.iteration)
  112. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  113. ]]>
  114. </body>
  115. </method>
  116. <method name="initializeDebugger">
  117. <body>
  118. <![CDATA[
  119. self.breakpoints = []
  120. self.triggered_bp = None
  121. ]]>
  122. </body>
  123. </method>
  124. <method name="waitTime">
  125. <body>
  126. <![CDATA[
  127. # First, we convert from wall-clock time to simulated time.
  128. # 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).
  129. simulated_diff = (accurate_time.time() - self.realtime_start_time) * self.realtime_scale
  130. # time_next and simulated_diff are both in simulated time: so now scale back to wall-clock time by dividing.
  131. # This function returns an amount of miliseconds.
  132. return ((self.time_next - simulated_diff) / self.realtime_scale)
  133. ]]>
  134. </body>
  135. </method>
  136. <method name="syncSimulatedTime">
  137. <body>
  138. <![CDATA[
  139. diff = accurate_time.time() - self.realtime_start_time
  140. self.clock = diff * self.realtime_scale
  141. ]]>
  142. </body>
  143. </method>
  144. <method name="addBreakpoint">
  145. <parameter name="name" />
  146. <parameter name="function" />
  147. <parameter name="enabled" default="true" />
  148. <parameter name="disable_on_trigger" default="true" />
  149. <body>
  150. <![CDATA[
  151. if len([bp for bp in self.breakpoints if bp.name == name]) > 0:
  152. return -1
  153. self.breakpoints.append(Breakpoint(name, function, enabled, disable_on_trigger))
  154. return 0
  155. ]]>
  156. </body>
  157. </method>
  158. <method name="delBreakpoint">
  159. <parameter name="name" />
  160. <body>
  161. <![CDATA[
  162. if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
  163. return -1
  164. self.breakpoints = [bp for bp in self.breakpoints if bp.name != name]
  165. return 0
  166. ]]>
  167. </body>
  168. </method>
  169. <method name="toggleBreakpoint">
  170. <parameter name="name" />
  171. <body>
  172. <![CDATA[
  173. if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
  174. return -1
  175. for bp in self.breakpoints:
  176. if bp.name == name:
  177. bp.enabled = enabled
  178. break
  179. return 0
  180. ]]>
  181. </body>
  182. </method>
  183. <method name="breakpointTriggers">
  184. <parameter name="is_realtime_simulation" />
  185. <body>
  186. <![CDATA[
  187. self.triggered_bp = None
  188. for bp in self.breakpoints:
  189. if not bp.enabled:
  190. continue
  191. # include the function in the scope...
  192. exec(bp.function)
  193. # ... and execute it, note that the breakpoint thus has to start with "def breakpoint("
  194. # note that we pass self.time_next instead of self.simulated_time in the case of as-fast-as-possible simulation (or stepping)
  195. # 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')
  196. # in case of realtime simulation, we do pass the current simulated time, since we can stop at (more or less) exactly the right time
  197. if breakpoint({'clock': (self.clock if is_realtime_simulation else self.time_next) / 1000.0, 'state': self.state}):
  198. # triggered!
  199. self.triggered_bp = bp.name
  200. if bp.disable_on_trigger:
  201. bp.enabled = False
  202. return True
  203. else:
  204. # not triggered, so continue
  205. continue
  206. return False
  207. ]]>
  208. </body>
  209. </method>
  210. <method name="godEvent">
  211. <parameter name="block_name" />
  212. <parameter name="new_val" />
  213. <body>
  214. <![CDATA[
  215. if block_name not in self.state:
  216. return -1
  217. for b in self.model.getBlocks():
  218. if b.getBlockName() == block_name:
  219. b.setSignal(new_val)
  220. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  221. return 0
  222. ]]>
  223. </body>
  224. </method>
  225. <scxml initial="Main" final="SimulationComplete" internal_event_lifeline="next_combo_step">
  226. <parallel id="Main">
  227. <state id="SimulationState" initial="Paused">
  228. <state id="Paused">
  229. <transition target="../Running/Continuous" event="continuous" port="user_input" />
  230. <transition target="../Running/Realtime" event="realtime" port="user_input">
  231. <parameter name="realtime_scale" default="1.0" />
  232. <script>
  233. self.realtime_scale = float(realtime_scale)
  234. </script>
  235. </transition>
  236. <transition target="../Running/BigStep" event="big_step" port="user_input" />
  237. <transition target="../Running/SmallStep" event="small_step" port="user_input" />
  238. <!-- If a god event enables the end condition, the simulation should stop. -->
  239. <transition target="../PreStopped" cond="self.endCondition()" />
  240. </state>
  241. <state id="PrePaused">
  242. <!-- 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. -->
  243. <transition target="../Paused" after="self.sccd_yield() * 2">
  244. <raise event="paused" />
  245. </transition>
  246. </state>
  247. <state id="PreBreakpointTriggered">
  248. <!-- 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. -->
  249. <transition target="../Paused" after="self.sccd_yield() * 2">
  250. <raise event="breakpoint_triggered" />
  251. </transition>
  252. </state>
  253. <state id="Running" initial="Continuous">
  254. <transition target="../PreStopped" cond="self.endCondition()" />
  255. <transition target="../PrePaused" event="pause" port="user_input" />
  256. <transition target="../PreBreakpointTriggered" cond="self.breakpointTriggers(INSTATE('./Realtime'))" />
  257. <state id="Continuous" />
  258. <state id="BigStep">
  259. <!-- 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. -->
  260. <transition target="../BigStepDone" event="big_step_done" />
  261. </state>
  262. <state id="BigStepDone">
  263. <!-- We go back to the 'Paused' state once the big step has finished. -->
  264. <transition target="../../Paused" after="self.sccd_yield()" />
  265. </state>
  266. <state id="SmallStep">
  267. <!-- 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. -->
  268. <transition target="../SmallStepDone" event="small_step_done" />
  269. </state>
  270. <state id="SmallStepDone">
  271. <!-- We go back to the 'Paused' state once the small step has finished. -->
  272. <transition target="../../Paused" after="self.sccd_yield()" />
  273. </state>
  274. <state id="Realtime">
  275. <onentry>
  276. <script>
  277. # If the simulation was paused, we need to reset the start time of the simulation.
  278. # The start time of the simulation is equal to the point in wall-clock time where simulated time is 0.
  279. # 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.
  280. # 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)
  281. self.realtime_start_time = accurate_time.time() - (self.clock / self.realtime_scale)
  282. </script>
  283. </onentry>
  284. </state>
  285. </state>
  286. <state id="PreStopped">
  287. <transition target="../TerminationConditionRaised" after="self.sccd_yield() * 2">
  288. <raise event="termination_condition" />
  289. </transition>
  290. </state>
  291. <state id="TerminationConditionRaised">
  292. <transition target="../Stopped" after="self.sccd_yield()" />
  293. </state>
  294. <state id="Stopped" />
  295. </state>
  296. <state id="SimulationFlow" initial="Started">
  297. <state id="Started">
  298. <transition target="../Initialized">
  299. <script>
  300. <![CDATA[
  301. self.initialize()
  302. ]]>
  303. </script>
  304. </transition>
  305. </state>
  306. <state id="Initialized">
  307. <transition target="../InitializeDebugger" />
  308. </state>
  309. <state id="InitializeDebugger">
  310. <onentry>
  311. <script>
  312. <![CDATA[
  313. self.initializeDebugger()
  314. ]]>
  315. </script>
  316. </onentry>
  317. <transition target="../CheckTermination" />
  318. </state>
  319. <state id="CheckTermination">
  320. <state id="MacroStepProcessed">
  321. <transition target="../../DoSimulation" cond="INSTATE('/Main/SimulationState/Running/Continuous') or INSTATE('/Main/SimulationState/Running/BigStep') or INSTATE('/Main/SimulationState/Running/SmallStep')" />
  322. <transition target="../../Waiting" cond="INSTATE('/Main/SimulationState/Running/Realtime')" />
  323. <transition target="../../Stopped" cond="INSTATE('/Main/SimulationState/Stopped')" />
  324. </state>
  325. </state>
  326. <state id="Waiting">
  327. <!-- We schedule to go back to the check_termination state after the smallest possible delay (to accomodate for pauses). -->
  328. <transition target="../CheckTermination" after="self.sccd_yield()">
  329. <!-- We set the simulation time to the correct value. -->
  330. <script>
  331. self.syncSimulatedTime()
  332. </script>
  333. </transition>
  334. <!-- We execute a step when the wait time is smaller than the smallest possible delay. -->
  335. <transition target="../DoSimulation" cond="self.waitTime() / 1000.0 &lt;= self.sccd_yield()" />
  336. </state>
  337. <state id="DoSimulation" initial="MacroStepPrepared">
  338. <state id="MacroStepPrepared">
  339. <onentry>
  340. <script>
  341. self.currentCompIdx = 0
  342. self.depGraph = self.cbdController.createDepGraph(self.iteration)
  343. self.strongComponentList = self.cbdController.createStrongComponents(self.depGraph, self.iteration)
  344. </script>
  345. </onentry>
  346. <transition target="../MicroStepProcessed" />
  347. </state>
  348. <state id="MicroStepProcessed">
  349. <!-- Here, we wait long enough for the simulation state to settle to 'Paused' if we are small stepping. -->
  350. <transition target="../PreMicroStepPrepared" cond="self.hasNextStrongComponent()" after="self.sccd_yield() * 3" />
  351. <!-- We wait a minimum amount of time to allow pause requests to be processed. -->
  352. <transition target="../../CheckTermination" cond="not self.hasNextStrongComponent()" after="self.sccd_yield()">
  353. <script>
  354. <![CDATA[
  355. self.advanceTime()
  356. ]]>
  357. </script>
  358. <raise event="big_step_done" />
  359. </transition>
  360. </state>
  361. <!-- TODO: Do we want to allow a pause after a small step? -->
  362. <state id="PreMicroStepPrepared">
  363. <transition target="../MicroStepPrepared" cond="INSTATE('/Main/SimulationState/Running')" />
  364. </state>
  365. <state id="MicroStepPrepared">
  366. <transition target="../MicroStepProcessed">
  367. <script>
  368. <![CDATA[
  369. self.computeBlock()
  370. self.currentCompIdx = self.currentCompIdx + 1
  371. ]]>
  372. </script>
  373. <raise event="small_step_done" />
  374. </transition>
  375. </state>
  376. </state>
  377. <state id="Stopped" />
  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>