cbdsim_parallel_debugging.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?xml version="1.0" ?>
  2. <diagram name="CBDSimulator" author="Sadaf Mustafiz and Claudio Gomes and Simon Van Mierlo">
  3. <description>
  4. A 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. 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="CBDSimulator" default="True">
  21. <attribute name="iteration"/>
  22. <attribute name="delta"/>
  23. <attribute name="clock"/>
  24. <attribute name="state"/>
  25. <attribute name="model"/>
  26. <attribute name="depGraph"/>
  27. <attribute name="strongComponentList"/>
  28. <attribute name="currentCompIdx"/>
  29. <attribute name="cbdController"/>
  30. <attribute name="options"/>
  31. <method name="CBDSimulator">
  32. <parameter name="options"/>
  33. <parameter name="model"/>
  34. <body>
  35. <![CDATA[
  36. self.options = options
  37. self.delta = self.options.getDeltaT()
  38. self.model = model
  39. ]]>
  40. </body>
  41. </method>
  42. <method name="initializeSimulation">
  43. <body>
  44. <![CDATA[
  45. self.iteration = 0
  46. self.clock = 0.0
  47. self.cbdController = CBDController(self.model, self.delta)
  48. self.cbdController.initSimulation()
  49. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  50. ]]>
  51. </body>
  52. </method>
  53. <method name="initializeBigStep">
  54. <body>
  55. <![CDATA[
  56. self.currentCompIdx = 0
  57. self.depGraph = self.cbdController.createDepGraph(self.iteration)
  58. self.strongComponentList = self.cbdController.createStrongComponents(self.depGraph, self.iteration)
  59. ]]>
  60. </body>
  61. </method>
  62. <method name="initializeSmallStep">
  63. <body>
  64. <![CDATA[
  65. self.small_step_executed = False
  66. ]]>
  67. </body>
  68. </method>
  69. <method name="finalizeSimulation">
  70. <body>
  71. <![CDATA[
  72. from bokeh.plotting import figure, output_file, show
  73. times = []
  74. values = []
  75. for timeValuePair in self.model.getSignal("neg"):
  76. times.append(timeValuePair.time)
  77. values.append(timeValuePair.value)
  78. output_file("./plot.html", title="Plot")
  79. p = figure(title="Something vs Otherthing", x_axis_label="Time", y_axis_label="Values")
  80. p.line(times, values, legend="Something", line_width=1, line_color="red")
  81. show(p)
  82. print 'Simulation finalized.'
  83. ]]>
  84. </body>
  85. </method>
  86. <method name="finalizeBigStep">
  87. <body>
  88. <![CDATA[
  89. self.advanceTime()
  90. ]]>
  91. </body>
  92. </method>
  93. <method name="finalizeSmallStep">
  94. <body>
  95. <![CDATA[
  96. self.currentCompIdx = self.currentCompIdx + 1
  97. ]]>
  98. </body>
  99. </method>
  100. <method name="endConditionSimulation">
  101. <body>
  102. <![CDATA[
  103. return self.iteration >= self.options.getMaxIterations()
  104. ]]>
  105. </body>
  106. </method>
  107. <method name="endConditionBigStep">
  108. <body>
  109. <![CDATA[
  110. return self.currentCompIdx >= len(self.strongComponentList)
  111. ]]>
  112. </body>
  113. </method>
  114. <method name="endConditionSmallStep">
  115. <body>
  116. <![CDATA[
  117. return self.small_step_executed
  118. ]]>
  119. </body>
  120. </method>
  121. <method name="advanceTime">
  122. <body>
  123. <![CDATA[
  124. self.iteration = self.iteration + 1
  125. self.cbdController.advanceTimeStep()
  126. self.clock = self.clock + self.delta
  127. ]]>
  128. </body>
  129. </method>
  130. <method name="currentComponentIsCycle">
  131. <body>
  132. <![CDATA[
  133. return self.cbdController.componentIsCycle(self.strongComponentList[self.currentCompIdx], self.depGraph)
  134. ]]>
  135. </body>
  136. </method>
  137. <method name="computeBlock">
  138. <body>
  139. <![CDATA[
  140. if self.currentComponentIsCycle():
  141. self.cbdController.computeNextAlgebraicLoop(self.strongComponentList[self.currentCompIdx], self.iteration)
  142. else:
  143. self.cbdController.computeNextBlock(self.strongComponentList[self.currentCompIdx], self.iteration)
  144. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  145. ]]>
  146. </body>
  147. </method>
  148. <!-- Debugging methods /-->
  149. <method name="initializeSimulationDebugger">
  150. <body>
  151. <![CDATA[
  152. self.breakpoints = []
  153. self.triggered_bp = None
  154. ]]>
  155. </body>
  156. </method>
  157. <method name="addBreakpoint">
  158. <parameter name="name" />
  159. <parameter name="function" />
  160. <parameter name="enabled" default="True" />
  161. <parameter name="disable_on_trigger" default="True" />
  162. <body>
  163. <![CDATA[
  164. if len([bp for bp in self.breakpoints if bp.name == name]) > 0:
  165. return -1
  166. self.breakpoints.append(Breakpoint(name, function, enabled, disable_on_trigger))
  167. return 0
  168. ]]>
  169. </body>
  170. </method>
  171. <method name="delBreakpoint">
  172. <parameter name="name" />
  173. <body>
  174. <![CDATA[
  175. if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
  176. return -1
  177. self.breakpoints = [bp for bp in self.breakpoints if bp.name != name]
  178. return 0
  179. ]]>
  180. </body>
  181. </method>
  182. <method name="toggleBreakpoint">
  183. <parameter name="name" />
  184. <body>
  185. <![CDATA[
  186. if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
  187. return -1
  188. for bp in self.breakpoints:
  189. if bp.name == name:
  190. bp.enabled = not bp.enabled
  191. break
  192. return 0
  193. ]]>
  194. </body>
  195. </method>
  196. <method name="breakpointTriggers">
  197. <body>
  198. <![CDATA[
  199. self.triggered_bp = None
  200. for bp in self.breakpoints:
  201. if not bp.enabled:
  202. continue
  203. # include the function in the scope...
  204. functionCallable = eval(bp.function)
  205. if functionCallable(self.clock, self.state):
  206. # triggered!
  207. self.triggered_bp = bp.name
  208. if bp.disable_on_trigger:
  209. bp.enabled = False
  210. return True
  211. else:
  212. # not triggered, so continue
  213. continue
  214. return False
  215. ]]>
  216. </body>
  217. </method>
  218. <scxml initial="Main" final="SimulationComplete">
  219. <parallel id="Main">
  220. <state id="SimulationState" initial="Paused">
  221. <state id="Paused">
  222. <transition target="../Continuous" event="continuous" port="user_input"/>
  223. <transition target="../BigStep" event="big_step" port="user_input" />
  224. <state id="BreakpointTriggered" />
  225. </state>
  226. <state id="Continuous">
  227. <transition target="../Paused" event="pause" port="user_input"/>
  228. <transition target="../Paused/BreakpointTriggered" cond="self.breakpointTriggers()">
  229. <raise event="Breakpoint.Triggered" />
  230. </transition>
  231. </state>
  232. <state id="BigStep">
  233. <transition target="../Paused" event="BigStep.Finished">
  234. <raise event="BigStep.Finished" port="user_output" />
  235. <raise event="State" port="user_output">
  236. <parameter expr="self.clock" />
  237. </raise>
  238. </transition>
  239. </state>
  240. </state>
  241. <state id="BreakpointManager" initial="Listening">
  242. <state id="Listening">
  243. <transition target="." event="add_breakpoint" port="user_input">
  244. <parameter name="name"/>
  245. <parameter name="function"/>
  246. <parameter name="enabled"/>
  247. <parameter name="disable_on_trigger"/>
  248. <script>
  249. result = self.addBreakpoint(name, function, bool(enabled), bool(disable_on_trigger))
  250. </script>
  251. <raise event="add_breakpoint_result" port="user_output">
  252. <parameter expr="result" />
  253. </raise>
  254. </transition>
  255. <transition target="." event="del_breakpoint" port="user_input">
  256. <parameter name="name"/>
  257. <script>
  258. result = self.delBreakpoint(name)
  259. </script>
  260. <raise event="del_breakpoint_result" port="user_output">
  261. <parameter expr="result" />
  262. </raise>
  263. </transition>
  264. <transition target="." event="toggle_breakpoint" port="user_input">
  265. <parameter name="name"/>
  266. <script>
  267. result = self.toggleBreakpoint(name)
  268. </script>
  269. <raise event="toggle_breakpoint_result" port="user_output">
  270. <parameter expr="result" />
  271. </raise>
  272. </transition>
  273. <transition target="." event="list_breakpoints" port="user_input">
  274. <raise event="list_breakpoints_result" port="user_output">
  275. <parameter expr="[bp.name for bp in self.breakpoints]" />
  276. </raise>
  277. </transition>
  278. </state>
  279. </state>
  280. <state id="DebugSimulation" initial="Started">
  281. <state id="Stopped"/>
  282. <state id="Started">
  283. <transition target="../Initialized">
  284. <script>
  285. <![CDATA[
  286. self.initializeSimulation()
  287. self.initializeSimulationDebugger()
  288. ]]>
  289. </script>
  290. </transition>
  291. </state>
  292. <state id="Initialized">
  293. <transition target="../CheckTermination" />
  294. </state>
  295. <state id="CheckTermination">
  296. <transition target="../InitializingChild" cond="(INSTATE('../../SimulationState/Continuous') or INSTATE('../../SimulationState/BigStep')) and not self.endConditionSimulation()">
  297. <raise event="BigStep.Reset" />
  298. </transition>
  299. <transition target="../Stopped" cond="(INSTATE('../../SimulationState/Continuous') or INSTATE('../../SimulationState/BigStep')) and self.endConditionSimulation()">
  300. <script>
  301. print 'Simulation finished'
  302. self.finalizeSimulation()
  303. </script>
  304. </transition>
  305. </state>
  306. <state id="InitializingChild">
  307. <transition target="../Executing" event="BigStep.Initialized">
  308. <raise event="BigStep.Execute" />
  309. </transition>
  310. </state>
  311. <state id="Executing">
  312. <transition target="../CheckTermination" event="BigStep.Finished" />
  313. </state>
  314. </state>
  315. <state id="ExecuteBigStep" initial="Stopped">
  316. <state id="Stopped">
  317. <transition target="../Started" event="BigStep.Reset" />
  318. </state>
  319. <state id="Started">
  320. <transition target="../Initialized">
  321. <raise event="BigStep.Initialized" />
  322. <script>
  323. <![CDATA[
  324. self.initializeBigStep()
  325. ]]>
  326. </script>
  327. </transition>
  328. </state>
  329. <state id="Initialized">
  330. <transition target="../CheckTermination" event="BigStep.Execute" />
  331. </state>
  332. <state id="CheckTermination">
  333. <transition target="../InitializingChild" cond="not self.endConditionBigStep()">
  334. <raise event="SmallStep.Reset" />
  335. </transition>
  336. <transition target="../Stopped" cond="self.endConditionBigStep()">
  337. <script>
  338. print 'big step executed'
  339. self.finalizeBigStep()
  340. print 'Iteration: ' + str(self.iteration)
  341. print 'Clock: ' + str(self.clock)
  342. </script>
  343. <raise event="BigStep.Finished" />
  344. </transition>
  345. </state>
  346. <state id="InitializingChild">
  347. <transition target="../Executing" event="SmallStep.Initialized">
  348. <raise event="SmallStep.Execute" />
  349. </transition>
  350. </state>
  351. <state id="Executing">
  352. <transition target="../CheckTermination" event="SmallStep.Finished" />
  353. </state>
  354. </state>
  355. <state id="ExecuteSmallStep" initial="Stopped">
  356. <state id="Stopped">
  357. <transition target="../Started" event="SmallStep.Reset" />
  358. </state>
  359. <state id="Started">
  360. <transition target="../Initialized">
  361. <raise event="SmallStep.Initialized" />
  362. <script>
  363. <![CDATA[
  364. self.initializeSmallStep()
  365. ]]>
  366. </script>
  367. </transition>
  368. </state>
  369. <state id="Initialized">
  370. <transition target="../CheckTermination" event="SmallStep.Execute"/>
  371. </state>
  372. <state id="CheckTermination">
  373. <transition target="../InitializingChild" cond="not self.endConditionSmallStep()">
  374. <raise event="Block.Reset" />
  375. </transition>
  376. <transition target="../Stopped" cond="self.endConditionSmallStep()">
  377. <script>
  378. print 'small step executed'
  379. self.finalizeSmallStep()
  380. </script>
  381. <raise event="SmallStep.Finished" />
  382. </transition>
  383. </state>
  384. <state id="InitializingChild">
  385. <transition target="../Executing" event="Block.Initialized">
  386. <raise event="Block.Execute" />
  387. </transition>
  388. </state>
  389. <state id="Executing">
  390. <transition target="../CheckTermination" event="Block.Finished">
  391. <script>
  392. self.small_step_executed = True
  393. </script>
  394. </transition>
  395. </state>
  396. </state>
  397. <state id="ExecuteBlock" initial="Stopped">
  398. <state id="Stopped">
  399. <transition target="../Started" event="Block.Reset" />
  400. </state>
  401. <state id="Started">
  402. <transition target="../Initialized">
  403. <raise event="Block.Initialized" />
  404. </transition>
  405. </state>
  406. <state id="Initialized">
  407. <transition target="../Executing" event="Block.Execute" />
  408. </state>
  409. <state id="Executing">
  410. <transition target="../Stopped">
  411. <script>
  412. self.computeBlock()
  413. </script>
  414. <raise event="Block.Finished" />
  415. </transition>
  416. </state>
  417. </state>
  418. </parallel>
  419. </scxml>
  420. </class>
  421. </diagram>