sccd_parallel.xml 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?xml version="1.0" ?>
  2. <diagram author="Yentl Van Tendeloo" name="DEVS simulator">
  3. <description>
  4. A restricted PythonPDEVS simulator modelled in SCCD for classic, dynamic structure DEVS models.
  5. </description>
  6. <top>
  7. import cPickle as pickle
  8. import time
  9. # We basically just interface with the basesimulator
  10. from scheduler import Scheduler
  11. from DEVS import directConnect, CoupledDEVS, AtomicDEVS, RootDEVS
  12. from tracer import trace
  13. class Breakpoint(object):
  14. def __init__(self, breakpoint_id, function, enabled, disable_on_trigger):
  15. self.id = breakpoint_id
  16. self.function = function
  17. self.enabled = enabled
  18. self.disable_on_trigger = disable_on_trigger
  19. </top>
  20. <inport name="request"/>
  21. <outport name="reply"/>
  22. <class name="SCCDSimulator" default="true">
  23. <!-- Define the constructor, taking the model to simulate as a parameter -->
  24. <constructor>
  25. <parameter name="model"/>
  26. <body>
  27. <![CDATA[
  28. # Simulation variables
  29. self.termination_time = None
  30. self.termination_condition = None
  31. self.simulation_time = (0.0, 0)
  32. self.model = model
  33. self.listeners = {}
  34. self.root_model = model
  35. self.realtime_scale = 1.0
  36. # Values to be set during simulation
  37. self.realtime_starttime = None
  38. self.inject_queue = []
  39. # Model initialization
  40. self.model_ids = []
  41. self.model.finalize(name="", model_counter=0, model_ids=self.model_ids, locations={None: []}, select_hierarchy=[])
  42. # Direct connection
  43. if isinstance(self.model, CoupledDEVS):
  44. self.model.component_set = directConnect(self.model.component_set, self.listeners)
  45. self.model = RootDEVS(self.model.component_set, self.model.component_set, None)
  46. for m in self.model.component_set:
  47. m.timeLast = (-m.elapsed, 1)
  48. ta = m.timeAdvance()
  49. m.time_next = (m.timeLast[0] + ta, 1)
  50. m.old_states = [(m.timeLast, pickle.dumps(m.state))]
  51. elif isinstance(self.model, AtomicDEVS):
  52. for p in self.model.IPorts:
  53. p.routing_inline = []
  54. p.routing_outline = []
  55. for p in self.model.OPorts:
  56. p.routing_inline = []
  57. p.routing_outline = []
  58. self.model = RootDEVS([self.model], [self.model], None)
  59. self.model.timeLast = (-self.model.elapsed, 1)
  60. ta = self.model.timeAdvance()
  61. self.model.time_next = (self.model.timeLast[0] + ta, 1)
  62. # Fixed configuration options
  63. self.model.scheduler = Scheduler(self.model.component_set, 1e-6, len(self.model.component_set))
  64. self.time_next = self.model.scheduler.readFirst()
  65. # Cached values
  66. self.imminents = None
  67. self.outbags = None
  68. self.inbags = None
  69. self.transitioning = None
  70. self.new_states = None
  71. self.new_tn = None
  72. # Verbose trace file
  73. self.trace_file = None
  74. # Breakpoint management
  75. self.breakpoints = []
  76. # For a reset
  77. self.save_model = {model: (model.elapsed, pickle.dumps(model.state, pickle.HIGHEST_PROTOCOL)) for model in self.model.component_set}
  78. self.transition_times = []
  79. ]]>
  80. </body>
  81. </constructor>
  82. <!-- Serialize the output values in something human-readable instead of internal objects.
  83. Note that this doesn't alter the internal structure, as that is still used for simulation. -->
  84. <method name="serialize">
  85. <parameter name="type"/>
  86. <parameter name="object"/>
  87. <body>
  88. <![CDATA[
  89. if type == "imminents":
  90. return [m.getModelFullName() for m in object]
  91. elif type == "inbags" or type == "outbags":
  92. return {m.getPortFullName(): object[m] for m in object}
  93. elif type == "new_tn" or type == "new_states":
  94. return {m.getModelFullName(): object[m] for m in object}
  95. elif type == "transitioning":
  96. return {m.getModelFullName(): {1: "INTERNAL", 2: "EXTERNAL", 3: "CONFLUENT"}[object[m]] for m in object}
  97. ]]>
  98. </body>
  99. </method>
  100. <!-- Find a port based on a fully qualified name. -->
  101. <method name="find_port_with_name">
  102. <parameter name="name"/>
  103. <body>
  104. <![CDATA[
  105. for model in self.model.component_set:
  106. if name.startswith(model.getModelFullName()):
  107. # Found a potential model
  108. # We can't simply check for equality, as portnames might contain dots too
  109. for port in model.IPorts:
  110. if port.getPortFullName() == name:
  111. # Now everything matches
  112. return port
  113. # Nothing found
  114. return None
  115. ]]>
  116. </body>
  117. </method>
  118. <!-- Find a model based on a fully qualified name. -->
  119. <method name="find_model_with_name">
  120. <parameter name="name"/>
  121. <body>
  122. <![CDATA[
  123. for model in self.model.component_set:
  124. if name == model.getModelFullName():
  125. # Found exact match
  126. return model
  127. return None
  128. ]]>
  129. </body>
  130. </method>
  131. <!-- Calculate the time to wait before triggering the next transition.
  132. This method is also called in non-realtime simulation, so make sure that it returns infinity in that case. -->
  133. <method name="calculate_after">
  134. <body>
  135. <![CDATA[
  136. try:
  137. # Process in parts of 100 milliseconds to repeatedly check the termination condition
  138. nexttime = (self.time_next[0] - (time.time() - self.realtime_starttime) / self.realtime_scale) * self.realtime_scale
  139. x = min(0.1, nexttime)
  140. return x
  141. except TypeError, AttributeError:
  142. # We are probably not simulating in realtime...
  143. return float('inf')
  144. ]]>
  145. </body>
  146. </method>
  147. <!-- Parse a list of options that can be passed together with the request -->
  148. <method name="parse_options">
  149. <parameter name="configuration"/>
  150. <body>
  151. <![CDATA[
  152. self.termination_condition = None if "termination_condition" not in configuration else configuration["termination_condition"]
  153. self.termination_time = None if "termination_time" not in configuration else configuration["termination_time"]
  154. self.realtime_scale = 1.0 if "realtime_scale" not in configuration else 1.0/configuration["realtime_scale"]
  155. # Subtract the current simulation time to allow for pausing
  156. self.realtime_starttime = (time.time() - self.simulation_time[0]*self.realtime_scale)
  157. # Reset the time used in the waiting, as it might not get recomputed
  158. self.the_time = 0.00001
  159. ]]>
  160. </body>
  161. </method>
  162. <!-- Utility function to determine whether or not simulation is finished. -->
  163. <method name="should_terminate">
  164. <parameter name="realtime"/>
  165. <body>
  166. <![CDATA[
  167. # Now that it includes breakpoints, results are to be interpretted as follows:
  168. # -2 --> continue simulation
  169. # -1 --> termination condition
  170. # else --> breakpoint
  171. if realtime:
  172. check_time = self.simulation_time
  173. else:
  174. self.compute_timeNext()
  175. check_time = self.time_next
  176. # Just access the 'transitioned' dictionary
  177. # Kind of dirty though...
  178. if self.transitioning is None:
  179. transitioned = set()
  180. else:
  181. transitioned = set(self.transitioning.keys())
  182. if check_time[0] == float('inf'):
  183. # Always terminate if we have reached infinity
  184. terminate = True
  185. elif self.termination_condition is not None:
  186. terminate = self.termination_condition(check_time, self.root_model, transitioned)
  187. else:
  188. terminate = self.termination_time < check_time[0]
  189. if terminate:
  190. # Always terminate, so don't check breakpoints
  191. return -1
  192. else:
  193. # Have to check breakpoints for termination
  194. for bp in self.breakpoints:
  195. if not bp.enabled:
  196. continue
  197. # Include the function in the scope
  198. exec(bp.function)
  199. # And execute it, note that the breakpoint thus has to start with "def breakpoint("
  200. if breakpoint(check_time, self.root_model, transitioned):
  201. # Triggered!
  202. return bp.id
  203. else:
  204. # Not triggered, so continue
  205. continue
  206. return -2
  207. ]]>
  208. </body>
  209. </method>
  210. <!-- Compute all models of which the internal transition function has to be triggered. -->
  211. <method name="find_internal_imminents">
  212. <body>
  213. <![CDATA[
  214. self.imminents = self.model.scheduler.getImminent(self.simulation_time)
  215. self.transition_times.append(self.simulation_time)
  216. ]]>
  217. </body>
  218. </method>
  219. <!-- Trigger all output functions of imminent models. -->
  220. <method name="compute_outputfunction">
  221. <body>
  222. <![CDATA[
  223. self.outbags = {}
  224. for model in self.imminents:
  225. self.outbags.update(model.outputFnc())
  226. ]]>
  227. </body>
  228. </method>
  229. <!-- Route all messages and apply the required transfer functions. -->
  230. <method name="route_messages">
  231. <body>
  232. <![CDATA[
  233. self.inbags = {}
  234. for outport in self.outbags:
  235. payload = self.outbags[outport]
  236. for inport, z in outport.routing_outline:
  237. if z is not None:
  238. payload = [z(pickle.loads(pickle.dumps(m))) for m in payload]
  239. self.inbags.setdefault(inport, []).extend(payload)
  240. ]]>
  241. </body>
  242. </method>
  243. <!-- Process the injects on the inject_queue. -->
  244. <method name="process_injects">
  245. <body>
  246. <![CDATA[
  247. while self.inject_queue:
  248. if self.inject_queue[0]["time"] > self.simulation_time:
  249. break
  250. config = self.inject_queue.pop(0)
  251. portname = config["port"]
  252. event = config["event"]
  253. port = self.find_port_with_name(portname)
  254. self.inbags.setdefault(port, []).append(event)
  255. ]]>
  256. </body>
  257. </method>
  258. <!-- Combine the information from the routed messages to determine the external, internal and confluent transition functions to trigger. -->
  259. <method name="find_all_imminents">
  260. <body>
  261. <![CDATA[
  262. # Internal codes:
  263. # 1 --> internal transition
  264. # 2 --> external transition
  265. # 3 --> confluent transition
  266. # These codes are a legacy of efficient PyPDEVS, but is kept here for consistency
  267. self.transitioning = {model: 1 for model in self.imminents}
  268. for inport in self.inbags:
  269. aDEVS = inport.host_DEVS
  270. aDEVS.my_input[inport] = self.inbags[inport]
  271. if aDEVS in self.transitioning:
  272. self.transitioning[aDEVS] = 3
  273. else:
  274. self.transitioning[aDEVS] = 2
  275. ]]>
  276. </body>
  277. </method>
  278. <!-- Trigger all transition functions. -->
  279. <method name="compute_transitions">
  280. <body>
  281. <![CDATA[
  282. self.new_states = {}
  283. for aDEVS in self.transitioning:
  284. aDEVS.my_input = {key: pickle.loads(pickle.dumps(aDEVS.my_input[key], pickle.HIGHEST_PROTOCOL)) for key in aDEVS.my_input}
  285. if self.transitioning[aDEVS] == 1:
  286. aDEVS.state = aDEVS.intTransition()
  287. elif self.transitioning[aDEVS] == 2:
  288. aDEVS.elapsed = self.simulation_time[0] - aDEVS.timeLast[0]
  289. aDEVS.state = aDEVS.extTransition(aDEVS.my_input)
  290. elif self.transitioning[aDEVS] == 3:
  291. aDEVS.elapsed = 0.
  292. aDEVS.state = aDEVS.confTransition(aDEVS.my_input)
  293. aDEVS.old_states.append((self.simulation_time, pickle.dumps(aDEVS.state)))
  294. aDEVS.my_input = {}
  295. self.new_states[aDEVS] = aDEVS.state
  296. ]]>
  297. </body>
  298. </method>
  299. <!-- Trigger all timeAdvance functions. -->
  300. <method name="compute_ta">
  301. <body>
  302. <![CDATA[
  303. self.new_tn = {}
  304. t, age = self.simulation_time
  305. for aDEVS in self.transitioning:
  306. ta = aDEVS.timeAdvance()
  307. aDEVS.timeLast = self.simulation_time
  308. aDEVS.time_next = (t + ta, 1 if ta else (age + 1))
  309. self.new_tn[aDEVS] = aDEVS.time_next
  310. trace(self.trace_file, self.transitioning[aDEVS], aDEVS)
  311. self.model.scheduler.massReschedule(self.transitioning)
  312. self.time_next = self.model.scheduler.readFirst()
  313. ]]>
  314. </body>
  315. </method>
  316. <method name="flush_file">
  317. <body>
  318. <![CDATA[
  319. if self.trace_file is not None:
  320. self.trace_file.flush()
  321. ]]>
  322. </body>
  323. </method>
  324. <method name="process_breakpoints">
  325. <parameter name="realtime"/>
  326. <body>
  327. <![CDATA[
  328. breakpoint_id = self.should_terminate(realtime)
  329. for breakpoint in self.breakpoints:
  330. if breakpoint.id == breakpoint_id:
  331. if breakpoint.disable_on_trigger:
  332. breakpoint.enabled = False
  333. return breakpoint_id
  334. ]]>
  335. </body>
  336. </method>
  337. <method name="compute_timeNext">
  338. <body>
  339. <![CDATA[
  340. model_timeNext = self.model.scheduler.readFirst()
  341. if len(self.inject_queue) > 0:
  342. self.time_next = min(model_timeNext, self.inject_queue[0]["time"])
  343. else:
  344. self.time_next = model_timeNext
  345. ]]>
  346. </body>
  347. </method>
  348. <method name="rollback_step">
  349. <body>
  350. <![CDATA[
  351. if len(self.transition_times) == 0:
  352. return
  353. new_time = self.transition_times.pop()
  354. for model in self.model.component_set:
  355. if model.old_states[-1][0] == new_time:
  356. # Remove the current state
  357. del model.old_states[-1]
  358. # Set the new (old...) state
  359. new_state = model.old_states[-1]
  360. model.state = pickle.loads(new_state[1])
  361. model.timeLast = new_state[0]
  362. ta = model.timeAdvance()
  363. model.time_next = (model.timeLast[0] + ta, model.timeLast[1] + 1 if ta == 0 else 0)
  364. self.model.scheduler.massReschedule([model])
  365. self.simulation_time = self.transition_times[-1] if len(self.transition_times) > 0 else (0.0, 0)
  366. ]]>
  367. </body>
  368. </method>
  369. <scxml initial="main">
  370. <!-- Parallel states: one of them controls the simulation flow, the other the type of simulation being performed. -->
  371. <parallel id="main">
  372. <!-- When an injection is received, just append it to the list of pending injections.
  373. These will be processed as soon as the current simulation step is finished.
  374. Afterwards, return to the previous state, as there was no change of state. -->
  375. <state id="injection_monitor">
  376. <state id="inject">
  377. <transition port="request" event="inject" target=".">
  378. <parameter name="configuration"/>
  379. <script>
  380. configuration["time"] = (configuration["time"], 1)
  381. self.inject_queue.append(configuration)
  382. self.inject_queue.sort(key=lambda i: i["time"])
  383. </script>
  384. <raise scope="output" port="reply" event="inject_ok"/>
  385. </transition>
  386. </state>
  387. </state>
  388. <state id="tracer_monitor">
  389. <state id="trace">
  390. <transition port="request" event="trace" target=".">
  391. <parameter name="filename"/>
  392. <script>
  393. if filename is not None:
  394. self.trace_file = open(filename, 'w')
  395. else:
  396. self.trace_file = None
  397. </script>
  398. <raise scope="output" port="reply" event="trace_config_ok"/>
  399. </transition>
  400. </state>
  401. </state>
  402. <!-- The main parallel component: the simulation flow. -->
  403. <state id="simulation_flow" initial="initialize">
  404. <state id="initialize">
  405. <transition target="../check_termination">
  406. <raise scope="output" port="reply" event="all_states">
  407. <parameter expr="self.simulation_time"/>
  408. <parameter expr="{m.getModelFullName(): (m.time_next, m.state) for m in self.model.component_set}"/>
  409. </raise>
  410. </transition>
  411. </state>
  412. <state id="check_termination" initial="workaround">
  413. <onexit>
  414. <script>
  415. self.simulation_time = self.time_next
  416. </script>
  417. </onexit>
  418. <state id="workaround">
  419. <transition after="0" target="../check_termination"/>
  420. </state>
  421. <state id="wait">
  422. <onexit>
  423. <script>
  424. diff = time.time() - self.realtime_starttime
  425. self.simulation_time = (diff / self.realtime_scale, 1)
  426. </script>
  427. </onexit>
  428. <transition after="self.calculate_after()" target="../check_termination"/>
  429. <transition cond="INSTATE('../../../simulation_state/paused')" target="../check_termination"/>
  430. </state>
  431. <state id="small_step_check">
  432. <transition cond="self.should_terminate(False) == -2" target="../../do_simulation"/>
  433. <transition cond="self.should_terminate(False) == -1" target="../check_termination">
  434. <raise event="termination_condition"/>
  435. </transition>
  436. <transition cond="self.should_terminate(False) &gt; -1" target="../check_termination">
  437. <script>
  438. breakpoint_id = self.process_breakpoints(False)
  439. </script>
  440. <raise scope="output" port="reply" event="breakpoint_triggered">
  441. <parameter expr="breakpoint_id"/>
  442. </raise>
  443. <raise event="termination_condition"/>
  444. </transition>
  445. </state>
  446. <state id="check_termination">
  447. <onentry>
  448. <script>
  449. self.compute_timeNext()
  450. self.the_time = self.calculate_after()
  451. </script>
  452. </onentry>
  453. <!-- Continue simulation -->
  454. <transition cond="INSTATE('../../../simulation_state/continuous') and (self.should_terminate(False) == -2)" target="../../do_simulation"/>
  455. <transition cond="INSTATE('../../../simulation_state/big_step') and (self.should_terminate(False) == -2)" target="../../do_simulation"/>
  456. <!-- Realtime and didn't reach the required time_next yet -->
  457. <transition cond="INSTATE('../../../simulation_state/realtime') and (self.should_terminate(True) == -2) and (self.the_time &gt; 0.0)" target="../wait"/>
  458. <transition cond="INSTATE('../../../simulation_state/realtime') and (self.should_terminate(True) == -2) and (self.the_time &lt;= 0.0)" target="../../do_simulation"/>
  459. <transition port="request" event="small_step" target="../small_step_check">
  460. <parameter name="configuration" type="dict" default="{}"/>
  461. <script>
  462. self.parse_options(configuration)
  463. </script>
  464. </transition>
  465. <!-- Pause simulation -->
  466. <transition cond="(not INSTATE('../../../simulation_state/paused') and INSTATE('../../../simulation_state/realtime') and (self.should_terminate(True) == -1))" target="../workaround">
  467. <raise event="termination_condition"/>
  468. </transition>
  469. <transition cond="(not INSTATE('../../../simulation_state/paused') and not INSTATE('../../../simulation_state/realtime') and (self.should_terminate(False) == -1))" target="../workaround">
  470. <raise event="termination_condition"/>
  471. </transition>
  472. <transition cond="(not INSTATE('../../../simulation_state/paused')) and INSTATE('../../../simulation_state/realtime') and (self.should_terminate(True) &gt; -1)" target="../workaround">
  473. <script>
  474. breakpoint_id = self.process_breakpoints(True)
  475. </script>
  476. <raise scope="output" port="reply" event="breakpoint_triggered">
  477. <parameter expr="breakpoint_id"/>
  478. </raise>
  479. <raise event="termination_condition"/>
  480. </transition>
  481. <transition cond="(not INSTATE('../../../simulation_state/paused')) and not INSTATE('../../../simulation_state/realtime') and (self.should_terminate(False) &gt; -1)" target="../workaround">
  482. <script>
  483. breakpoint_id = self.process_breakpoints(False)
  484. </script>
  485. <raise scope="output" port="reply" event="breakpoint_triggered">
  486. <parameter expr="breakpoint_id"/>
  487. </raise>
  488. <raise event="termination_condition"/>
  489. </transition>
  490. <!-- Process god event -->
  491. <transition cond="INSTATE('../../../simulation_state/paused')" port="request" event="god_event" target="../workaround">
  492. <parameter name="configuration"/>
  493. <script>
  494. modelname = configuration["model"]
  495. state_attribute = configuration["attribute"]
  496. new_value = configuration["value"]
  497. model = self.find_model_with_name(modelname)
  498. setattr(model.state, state_attribute, new_value)
  499. # Call the timeadvance method again and compute new ta
  500. ta = model.timeAdvance()
  501. model.time_next = (model.timeLast[0] + ta, 1 if ta else (model.timeLast[1] + 1))
  502. self.model.scheduler.massReschedule([model])
  503. # self.simulation_time = self.model.scheduler.readFirst()
  504. </script>
  505. <raise scope="output" port="reply" event="god_event_ok">
  506. <parameter expr="{model.getModelFullName(): str(model.state)}"/>
  507. </raise>
  508. <raise scope="output" port="reply" event="new_tn">
  509. <parameter expr="self.simulation_time"/>
  510. <parameter expr="{model.getModelFullName(): model.time_next}"/>
  511. </raise>
  512. </transition>
  513. <!-- Omniscient debugging -->
  514. <transition cond="INSTATE('../../../simulation_state/paused')" port="request" event="backwards_step" target="../workaround">
  515. <script>
  516. self.rollback_step()
  517. </script>
  518. <raise scope="output" port="reply" event="all_states">
  519. <parameter expr="self.simulation_time"/>
  520. <parameter expr="{m.getModelFullName(): (m.time_next, m.state) for m in self.model.component_set}"/>
  521. </raise>
  522. </transition>
  523. </state>
  524. </state>
  525. <state id="do_simulation" initial="init">
  526. <state id="init">
  527. <onexit>
  528. <script>
  529. self.find_internal_imminents()
  530. </script>
  531. </onexit>
  532. <transition cond="not INSTATE('../../../simulation_state/paused')" target="../found_internal_imminents"/>
  533. <!-- Always output this if paused, as we only got here because a small step was fired previously -->
  534. <transition cond="INSTATE('../../../simulation_state/paused')" target="../found_internal_imminents">
  535. <raise scope="output" port="reply" event="imminents">
  536. <parameter expr="self.simulation_time"/>
  537. <parameter expr="self.serialize('imminents', self.imminents)"/>
  538. </raise>
  539. </transition>
  540. </state>
  541. <state id="found_internal_imminents">
  542. <onexit>
  543. <script>
  544. self.compute_outputfunction()
  545. </script>
  546. </onexit>
  547. <transition cond="not INSTATE('../../../simulation_state/paused')" target="../computed_outputfunction"/>
  548. <transition port="request" event="small_step" target="../computed_outputfunction">
  549. <raise scope="output" port="reply" event="outbags">
  550. <parameter expr="self.simulation_time"/>
  551. <parameter expr="self.serialize('outbags', self.outbags)"/>
  552. </raise>
  553. </transition>
  554. </state>
  555. <state id="computed_outputfunction">
  556. <onexit>
  557. <script>
  558. self.route_messages()
  559. self.process_injects()
  560. </script>
  561. </onexit>
  562. <transition cond="not INSTATE('../../../simulation_state/paused')" target="../routed_messages">
  563. </transition>
  564. <transition port="request" event="small_step" target="../routed_messages">
  565. <raise scope="output" port="reply" event="inbags">
  566. <parameter expr="self.simulation_time"/>
  567. <parameter expr="self.serialize('inbags', self.inbags)"/>
  568. </raise>
  569. </transition>
  570. </state>
  571. <state id="routed_messages">
  572. <onexit>
  573. <script>
  574. self.find_all_imminents()
  575. </script>
  576. </onexit>
  577. <transition cond="not INSTATE('../../../simulation_state/paused')" target="../found_all_imminents">
  578. </transition>
  579. <transition port="request" event="small_step" target="../found_all_imminents">
  580. <raise scope="output" port="reply" event="transitioning">
  581. <parameter expr="self.simulation_time"/>
  582. <parameter expr="self.serialize('transitioning', self.transitioning)"/>
  583. </raise>
  584. </transition>
  585. </state>
  586. <state id="found_all_imminents">
  587. <onexit>
  588. <script>
  589. self.compute_transitions()
  590. </script>
  591. </onexit>
  592. <transition cond="not INSTATE('../../../simulation_state/paused')" target="../computed_transitions"/>
  593. <transition port="request" event="small_step" target="../computed_transitions">
  594. <raise scope="output" port="reply" event="new_states">
  595. <parameter expr="self.simulation_time"/>
  596. <parameter expr="self.serialize('new_states', self.new_states)"/>
  597. </raise>
  598. </transition>
  599. </state>
  600. <state id="computed_transitions">
  601. <onexit>
  602. <script>
  603. self.compute_ta()
  604. </script>
  605. </onexit>
  606. <transition cond="INSTATE('../../../simulation_state/continuous')" target="../../check_termination"/>
  607. <transition port="request" event="small_step" target="../../check_termination">
  608. <raise scope="output" port="reply" event="new_tn">
  609. <parameter expr="self.simulation_time"/>
  610. <parameter expr="self.serialize('new_tn', self.new_tn)"/>
  611. </raise>
  612. </transition>
  613. <transition cond="INSTATE('../../../simulation_state/realtime') or INSTATE('../../../simulation_state/big_step')" target="../../check_termination">
  614. <raise event="big_step_done"/>
  615. <raise scope="output" port="reply" event="new_states">
  616. <parameter expr="self.simulation_time"/>
  617. <parameter expr="self.serialize('new_states', self.new_states)"/>
  618. </raise>
  619. <raise scope="output" port="reply" event="new_tn">
  620. <parameter expr="self.simulation_time"/>
  621. <parameter expr="self.serialize('new_tn', self.new_tn)"/>
  622. </raise>
  623. </transition>
  624. </state>
  625. </state>
  626. </state>
  627. <state id="simulation_state" initial="paused">
  628. <state id="paused">
  629. <transition port="request" event="simulate" target="../continuous">
  630. <parameter name="configuration" type="dict" default="{}"/>
  631. <script>
  632. self.parse_options(configuration)
  633. </script>
  634. </transition>
  635. <transition port="request" event="realtime" target="../realtime">
  636. <parameter name="configuration" type="dict" default="{}"/>
  637. <script>
  638. self.parse_options(configuration)
  639. </script>
  640. </transition>
  641. <transition port="request" event="big_step" target="../big_step">
  642. <parameter name="configuration" type="dict" default="{}"/>
  643. <script>
  644. self.parse_options(configuration)
  645. </script>
  646. </transition>
  647. </state>
  648. <state id="continuous">
  649. <transition port="request" event="pause" target=".">
  650. <script>
  651. # Just override termination condition
  652. self.termination_condition = lambda i, j, k : True
  653. self.termination_time = None
  654. </script>
  655. </transition>
  656. <transition event="termination_condition" target="../paused">
  657. <raise port="reply" event="terminate">
  658. <parameter expr="self.simulation_time"/>
  659. </raise>
  660. <script>
  661. self.flush_file()
  662. </script>
  663. <raise scope="output" port="reply" event="all_states">
  664. <parameter expr="self.simulation_time"/>
  665. <parameter expr="{m.getModelFullName(): (m.time_next, m.state) for m in self.model.component_set}"/>
  666. </raise>
  667. </transition>
  668. </state>
  669. <state id="realtime">
  670. <transition port="request" event="pause" target=".">
  671. <script>
  672. # Just override termination condition
  673. self.termination_condition = lambda i, j, k : True
  674. self.termination_time = None
  675. # Don't forget to correctly set the simulation time
  676. diff = time.time() - self.realtime_starttime
  677. self.simulation_time = (diff / self.realtime_scale, 1)
  678. </script>
  679. </transition>
  680. <transition event="termination_condition" target="../paused">
  681. <raise port="reply" event="terminate">
  682. <parameter expr="self.simulation_time"/>
  683. </raise>
  684. <script>
  685. self.flush_file()
  686. </script>
  687. </transition>
  688. </state>
  689. <state id="big_step">
  690. <transition event="big_step_done" target="../paused"/>
  691. <transition event="termination_condition" target="../paused">
  692. <raise port="reply" event="terminate">
  693. <parameter expr="self.simulation_time"/>
  694. </raise>
  695. <script>
  696. self.flush_file()
  697. </script>
  698. </transition>
  699. </state>
  700. </state>
  701. <state id="breakpoint_manager">
  702. <state id="breakpoint_manage">
  703. <transition port="request" event="add_breakpoint" target=".">
  704. <parameter name="breakpoint_id"/>
  705. <parameter name="function"/>
  706. <parameter name="enabled"/>
  707. <parameter name="disable_on_trigger"/>
  708. <script>
  709. self.breakpoints.append(Breakpoint(breakpoint_id, function, enabled, disable_on_trigger))
  710. </script>
  711. </transition>
  712. <transition port="request" event="del_breakpoint" target=".">
  713. <parameter name="del_breakpoint_id"/>
  714. <script>
  715. self.breakpoints = [breakpoint for breakpoint in self.breakpoints if breakpoint.id != del_breakpoint_id]
  716. </script>
  717. </transition>
  718. <transition port="request" event="toggle_breakpoint" target=".">
  719. <parameter name="breakpoint_id"/>
  720. <parameter name="enabled"/>
  721. <script>
  722. for breakpoint in self.breakpoints:
  723. if breakpoint.id == breakpoint_id:
  724. breakpoint.enabled = enabled
  725. break
  726. </script>
  727. </transition>
  728. </state>
  729. </state>
  730. <state id="reset">
  731. <state id="reset">
  732. <transition port="request" event="reset" target=".">
  733. <script>
  734. for model in self.model.component_set:
  735. model.state = pickle.loads(self.save_model[model][1])
  736. model.elapsed = self.save_model[model][0]
  737. model.timeLast = (-model.elapsed, 1)
  738. ta = model.timeAdvance()
  739. model.time_next = (model.timeLast[0] + ta, 1)
  740. self.simulation_time = (0.0, 0)
  741. self.model.scheduler.massReschedule(self.model.component_set)
  742. # Reset trace file
  743. if self.trace_file is not None:
  744. self.trace_file = open(self.trace_file.name, 'w')
  745. </script>
  746. <raise scope="output" port="reply" event="all_states">
  747. <parameter expr="(0.0, 0)"/>
  748. <parameter expr="{m.getModelFullName(): (m.time_next, m.state) for m in self.model.component_set}"/>
  749. </raise>
  750. </transition>
  751. </state>
  752. </state>
  753. </parallel>
  754. </scxml>
  755. </class>
  756. </diagram>