| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- <?xml version="1.0" ?>
- <diagram name="CBDSimulator" author="Sadaf Mustafiz and Claudio Gomes and Simon Van Mierlo">
- <description>
- SCCD HUTN model of a CBD simulator
- </description>
-
- <inport name="user_input" />
- <inport name="user_output" />
-
- <top>
- from sccd.runtime.libs.ui import *
- from sccd.runtime.libs.utils import *
- from CBD_Controller import CBDController
- import Options
- import sccd.runtime.accurate_time as accurate_time
-
- class Breakpoint:
- def __init__(self, name, function, enabled, disable_on_trigger):
- self.name = name
- self.function = function
- self.enabled = enabled
- self.disable_on_trigger = disable_on_trigger
- </top>
-
- <class name="CBDSimulator" default="True">
- <attribute name="iteration"/>
- <attribute name="cbdController"/>
- <attribute name="delta"/>
- <attribute name="clock"/>
- <attribute name="model"/>
- <attribute name="depGraph"/>
- <attribute name="strongComponentList"/>
- <attribute name="currentCompIdx"/>
- <method name="CBDSimulator">
- <parameter name="options"/>
- <parameter name="model"/>
- <body>
- <![CDATA[
- self.options = options
- self.delta = self.options.getDeltaT() * 1000.0
- self.model = model
- ]]>
- </body>
- </method>
- <method name="initialize">
- <body>
- <![CDATA[
- self.iteration = 0
- self.clock = 0
- self.time_next = self.delta
- self.cbdController = CBDController(self.model, self.delta)
- self.cbdController.initSimulation()
- self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
- self.breakpoints = []
- self.triggered_bp = None
- ]]>
- </body>
- </method>
- <method name="endCondition">
- <body>
- <![CDATA[
- return self.iteration >= self.options.getMaxIterations()
- ]]>
- </body>
- </method>
- <method name="advanceTime">
- <body>
- <![CDATA[
- self.iteration = self.iteration + 1
- self.clock = self.time_next
- self.cbdController.advanceTimeStep()
- self.time_next = self.clock + self.delta
- ]]>
- </body>
- </method>
- <method name="currentComponentIsCycle">
- <body>
- <![CDATA[
- return self.cbdController.componentIsCycle(self.strongComponentList[self.currentCompIdx], self.depGraph)
- ]]>
- </body>
- </method>
- <method name="hasNextStrongComponent">
- <body>
- <![CDATA[
- return (self.currentCompIdx + 1) < len(self.strongComponentList)
- ]]>
- </body>
- </method>
- <method name="finalize">
- <body>
- <![CDATA[
- from bokeh.plotting import figure, output_file, show
- times = []
- values = []
- for timeValuePair in self.model.getSignal("neg"):
- times.append(timeValuePair.time)
- values.append(timeValuePair.value)
- output_file("./plot.html", title="Plot")
- p = figure(title="Something vs Otherthing", x_axis_label="Time", y_axis_label="Values")
- p.line(times, values, legend="Something", line_width=1, line_color="red")
- show(p)
- ]]>
- </body>
- </method>
- <method name="waitTime">
- <body>
- <![CDATA[
- # First, we convert from wall-clock time to simulated time.
- # 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).
- simulated_diff = (accurate_time.time() - self.realtime_start_time) * self.realtime_scale
- # time_next and simulated_diff are both in simulated time: so now scale back to wall-clock time by dividing.
- # This function returns an amount of miliseconds.
- return ((self.time_next - simulated_diff) / self.realtime_scale)
- ]]>
- </body>
- </method>
- <method name="addBreakpoint">
- <parameter name="name" />
- <parameter name="function" />
- <parameter name="enabled" default="true" />
- <parameter name="disable_on_trigger" default="true" />
- <body>
- <![CDATA[
- if len([bp for bp in self.breakpoints if bp.name == name]) > 0:
- return -1
- self.breakpoints.append(Breakpoint(name, function, enabled, disable_on_trigger))
- return 0
- ]]>
- </body>
- </method>
- <method name="delBreakpoint">
- <parameter name="name" />
- <body>
- <![CDATA[
- if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
- return -1
- self.breakpoints = [bp for bp in self.breakpoints if bp.name != name]
- return 0
- ]]>
- </body>
- </method>
- <method name="toggleBreakpoint">
- <parameter name="name" />
- <body>
- <![CDATA[
- if len([bp for bp in self.breakpoints if bp.name == name]) == 0:
- return -1
- for bp in self.breakpoints:
- if bp.name == name:
- bp.enabled = enabled
- break
- return 0
- ]]>
- </body>
- </method>
- <method name="breakpointTriggers">
- <parameter name="is_realtime_simulation" />
- <body>
- <![CDATA[
- self.triggered_bp = None
- for bp in self.breakpoints:
- if not bp.enabled:
- continue
- # include the function in the scope...
- exec(bp.function)
- # ... and execute it, note that the breakpoint thus has to start with "def breakpoint("
- # note that we pass self.time_next instead of self.simulated_time in the case of as-fast-as-possible simulation (or stepping)
- # 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')
- # in case of realtime simulation, we do pass the current simulated time, since we can stop at (more or less) exactly the right time
- if breakpoint({'clock': (self.clock if is_realtime_simulation else self.time_next) / 1000.0, 'state': self.state}):
- # triggered!
- self.triggered_bp = bp.name
- if bp.disable_on_trigger:
- bp.enabled = False
- return True
- else:
- # not triggered, so continue
- continue
- return False
- ]]>
- </body>
- </method>
- <method name="godEvent">
- <parameter name="block_name" />
- <parameter name="new_val" />
- <body>
- <![CDATA[
- if block_name not in self.state:
- return -1
- for b in self.model.getBlocks():
- if b.getBlockName() == block_name:
- b.setSignal(new_val)
- self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
- return 0
- ]]>
- </body>
- </method>
- <scxml initial="Main" internal_event_lifeline="next_combo_step">
- <parallel id="Main">
- <state id="SimulationState" initial="Paused">
- <state id="Paused">
- <onentry><script>print 'entering SimulationState/Paused'</script></onentry>
- <onexit><script>print 'exiting SimulationState/Paused'</script></onexit>
- <transition target="../Running/Continuous" event="continuous" port="user_input" />
- <transition target="../Running/Realtime" event="realtime" port="user_input">
- <parameter name="realtime_scale" default="1.0" />
- <script>
- self.realtime_scale = float(realtime_scale)
- </script>
- </transition>
- <transition target="../Running/BigStep" event="big_step" port="user_input" />
- </state>
- <state id="PrePaused">
- <transition target="../Paused" after="self.sccd_yield()">
- <raise event="paused" />
- </transition>
- </state>
- <state id="PreBreakpointTriggered">
- <transition target="../Paused" after="self.sccd_yield()">
- <raise event="breakpoint_triggered" />
- </transition>
- </state>
- <state id="Running" initial="Continuous">
- <onentry><script>print 'entering SimulationState/Running'</script></onentry>
- <onexit><script>print 'exiting SimulationState/Running'</script></onexit>
- <transition target="../Stopped" cond="self.endCondition()">
- <raise event="termination_condition" />
- </transition>
- <transition target="../PrePaused" event="pause" port="user_input" />
- <transition target="../PreBreakpointTriggered" cond="self.breakpointTriggers(INSTATE('./Realtime'))" />
- <state id="Continuous" />
- <state id="BigStep">
- <!-- 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. -->
- <transition target="../BigStepDone" event="big_step_done" />
- </state>
- <state id="BigStepDone">
- <!-- We go back to the 'paused' state once the big step has finished. -->
- <transition target="../../Paused" after="self.sccd_yield()" />
- </state>
- <state id="Realtime">
- <onentry>
- <script>
- # If the simulation was paused, we need to reset the start time of the simulation.
- # The start time of the simulation is equal to the point in wall-clock time where simulated time is 0.
- # 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.
- # 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)
- self.realtime_start_time = accurate_time.time() - (self.clock / self.realtime_scale)
- </script>
- </onentry>
- </state>
- </state>
- <state id="Stopped">
- <onentry><script>print 'entering SimulationState/Stopped'</script></onentry>
- <onexit><script>print 'exiting SimulationState/Stopped'</script></onexit>
- </state>
- </state>
- <state id="SimulationFlow" initial="Initialize">
- <state id="Initialize">
- <onentry>
- <script>
- <![CDATA[
- self.initialize()
- ]]>
- </script>
- </onentry>
- <transition target="../CheckTerminationCondition" />
- </state>
- <state id="CheckTerminationCondition">
- <transition target="../CreateDependencyGraph" cond="INSTATE('/Main/SimulationState/Running/Continuous') or INSTATE('/Main/SimulationState/Running/BigStep')" />
- <transition target="../Waiting" cond="INSTATE('/Main/SimulationState/Running/Realtime')" />
- <transition target="../Stopped" cond="INSTATE('/Main/SimulationState/Stopped')" after="self.sccd_yield()" />
- </state>
- <state id="Waiting">
- <!-- We schedule to go back to the check_termination state after the smallest possible delay (to accomodate for pauses). -->
- <transition target="../CheckTerminationCondition" after="self.sccd_yield()" />
- <!-- We execute a step when the wait time is smaller than the smallest possible delay. -->
- <transition target="../CreateDependencyGraph" cond="self.waitTime() / 1000.0 <= self.sccd_yield()" />
- <!-- We set the simulation time to the correct value. -->
- <onexit>
- <script>
- diff = accurate_time.time() - self.realtime_start_time
- self.clock = diff * self.realtime_scale
- </script>
- </onexit>
- </state>
- <state id="CreateDependencyGraph">
- <onentry>
- <script>
- <![CDATA[
- self.depGraph = self.cbdController.createDepGraph(self.iteration)
- ]]>
- </script>
- </onentry>
- <transition target="../IsolateStrongComponents" />
- </state>
- <state id="IsolateStrongComponents">
- <onentry>
- <script>
- <![CDATA[
- self.strongComponentList = self.cbdController.createStrongComponents(self.depGraph, self.iteration)
- ]]>
- </script>
- </onentry>
- <transition target="../ExecuteSimulationStep">
- <script>
- <![CDATA[
- self.currentCompIdx = -1
- ]]>
- </script>
- </transition>
- </state>
- <state id="ExecuteSimulationStep" initial="CheckNextComponent">
- <state id="CheckNextComponent">
- <transition target="../CheckCycle" cond="self.hasNextStrongComponent()">
- <script>
- <![CDATA[
- self.currentCompIdx = self.currentCompIdx + 1
- ]]>
- </script>
- </transition>
- <!-- We wait a minimum amount of time to allow pause requests to be processed. -->
- <transition target="../../CheckTerminationCondition" cond="not self.hasNextStrongComponent()" after="self.sccd_yield()">
- <script>
- <![CDATA[
- self.advanceTime()
- self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
- ]]>
- </script>
- <raise event="big_step_done" />
- </transition>
- </state>
- <state id="CheckCycle">
- <transition target="../CheckNextComponent" cond="not self.currentComponentIsCycle()">
- <script>
- <![CDATA[
- self.cbdController.computeNextBlock(self.strongComponentList[self.currentCompIdx], self.iteration)
- ]]>
- </script>
- </transition>
- <transition target="../CheckNextComponent" cond="self.currentComponentIsCycle()">
- <script>
- <![CDATA[
- self.cbdController.computeNextAlgebraicLoop(self.strongComponentList[self.currentCompIdx], self.iteration)
- ]]>
- </script>
- </transition>
- </state>
- </state>
- <state id="Stopped" />
- </state>
- <state id="BreakpointManager" initial="Listening">
- <state id="Listening">
- <transition target="." event="add_breakpoint" port="user_input">
- <parameter name="name"/>
- <parameter name="function"/>
- <parameter name="enabled"/>
- <parameter name="disable_on_trigger"/>
- <script>
- result = self.addBreakpoint(name, function, bool(enabled), bool(disable_on_trigger))
- </script>
- <raise event="add_breakpoint_result" port="user_output">
- <parameter expr="result" />
- </raise>
- </transition>
- <transition target="." event="del_breakpoint" port="user_input">
- <parameter name="name"/>
- <script>
- result = self.delBreakpoint(name)
- </script>
- <raise event="del_breakpoint_result" port="user_output">
- <parameter expr="result" />
- </raise>
- </transition>
- <transition target="." event="toggle_breakpoint" port="user_input">
- <parameter name="name"/>
- <script>
- result = self.toggleBreakpoint(name)
- </script>
- <raise event="toggle_breakpoint_result" port="user_output">
- <parameter expr="result" />
- </raise>
- </transition>
- <transition target="." event="list_breakpoints" port="user_input">
- <raise event="list_breakpoints_result" port="user_output">
- <parameter expr="[bp.name for bp in self.breakpoints]" />
- </raise>
- </transition>
- </state>
- </state>
- <state id="GodEventManager" initial="Listening">
- <state id="Listening">
- <transition target="." event="god_event" port="user_input" cond="INSTATE('/Main/SimulationState/Paused')">
- <parameter name="block_name" />
- <parameter name="new_val" />
- <script>
- result = self.godEvent(block_name, new_val)
- </script>
- <raise event="god_event_result" port="user_output">
- <parameter expr="result" />
- </raise>
- </transition>
- </state>
- </state>
- <state id="UserOutput" initial="Waiting">
- <state id="Waiting">
- <transition target="." event="termination_condition">
- <raise event="terminated" port="user_output"/>
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.state" />
- </raise>
- </transition>
- <transition target="." event="paused">
- <raise event="paused" port="user_output" />
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.state" />
- </raise>
- </transition>
- <transition target="." event="big_step_done" cond="INSTATE('/Main/SimulationState/Running/Realtime') or INSTATE('/Main/SimulationState/Running/BigStep') or INSTATE('/Main/SimulationState/Running/BigStepDone')">
- <raise event="stepped" port="user_output" />
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.state" />
- </raise>
- </transition>
- <transition target="." event="breakpoint_triggered">
- <raise event="breakpoint_triggered" port="user_output">
- <parameter expr="self.triggered_bp" />
- </raise>
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.state" />
- </raise>
- </transition>
- </state>
- </state>
- <transition target="../SimulationComplete" cond="INSTATE('./SimulationState/Stopped') and INSTATE('./SimulationFlow/Stopped')">
- <script>
- self.finalize()
- </script>
- </transition>
- </parallel>
- <state id="SimulationComplete" />
- </scxml>
- </class>
- </diagram>
|