| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <?xml version="1.0" ?>
- <diagram name="FSASimulator" author="Sadaf Mustafiz and Bruno Barroca and Claudio Gomes and Simon Van Mierlo">
- <description>
- A debuggeable FSA simulator. It supports after and events.
- </description>
-
- <inport name="user_input" />
- <inport name="user_output" />
-
- <top>
- from sccd.runtime.libs.ui import *
- from sccd.runtime.libs.utils import *
- import fsaclasses
- 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="FSASimulator" default="True">
- <attribute name="elapsed"/>
- <attribute name="super_step_completed"/>
- <attribute name="model"/>
- <attribute name="clock"/>
- <attribute name="timestep"/>
- <attribute name="currentEvent"/>
- <attribute name="selectedTransition"/>
- <attribute name="eventList"/>
- <attribute name="currentState"/>
- <method name="FSASimulator">
- <parameter name="amodel"/>
- <parameter name="events"/>
- <body>
- <![CDATA[
- self.model = amodel
- self.eventList = events
- ]]>
- </body>
- </method>
- <method name="processEvent">
- <parameter name="event"/>
- <body>
- <![CDATA[
- if (event != None):
- self.eventList.popEvent(event)
- event.processed = True
- ]]>
- </body>
- </method>
- <method name="getInputEventAt">
- <parameter name="time"/>
- <body>
- <![CDATA[
- return self.eventList.getInputAt(time)
- ]]>
- </body>
- </method>
- <method name="initialize">
- <body>
- <![CDATA[
- self.clock = 0
- self.elapsed = 0
- self.timestep = 1.0 * 1000.0
- self.time_next = self.timestep
- self.currentState = self.model.initialState
- ]]>
- </body>
- </method>
- <method name="initializeDebugger">
- <body>
- <![CDATA[
- self.breakpoints = []
- self.triggered_bp = None
- ]]>
- </body>
- </method>
- <method name="endCondition">
- <body>
- <![CDATA[
- return self.currentState and self.currentState.final
- ]]>
- </body>
- </method>
- <method name="advanceTime">
- <body>
- <![CDATA[
- self.clock = self.time_next
- self.elapsed = self.elapsed + self.timestep
- self.time_next = self.clock + self.timestep
- ]]>
- </body>
- </method>
- <method name="finalize">
- <body>
- <![CDATA[
- print 'Simulation finalized.'
- ]]>
- </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.currentState, 'event': self.currentEvent}):
- # 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="new_state" />
- <body>
- <![CDATA[
- filtered_states = [s for s in self.model.states if s.name == new_state]
- if not len(filtered_states) == 1:
- return -1
- self.currentState = filtered_states[0]
- return 0
- ]]>
- </body>
- </method>
- <scxml initial="Main" final="SimulationComplete" internal_event_lifeline="next_combo_step">
- <parallel id="Main">
- <state id="SimulationState" initial="Paused">
- <state id="Paused">
- <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" />
- <transition target="../Running/SmallStep" event="small_step" port="user_input" />
- <!-- If a god event enables the end condition, the simulation should stop. -->
- <transition target="../PreStopped" cond="self.endCondition()" />
- </state>
- <state id="PrePaused">
- <!-- 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. -->
- <transition target="../Paused" after="self.sccd_yield() * 2">
- <raise event="paused" />
- </transition>
- </state>
- <state id="PreBreakpointTriggered">
- <!-- 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. -->
- <transition target="../Paused" after="self.sccd_yield() * 2">
- <raise event="breakpoint_triggered" />
- </transition>
- </state>
- <state id="Running" initial="Continuous">
- <transition target="../PreStopped" cond="self.endCondition()" />
- <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="SmallStep">
- <!-- 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. -->
- <transition target="../SmallStepDone" event="small_step_done" />
- </state>
- <state id="SmallStepDone">
- <!-- We go back to the 'Paused' state once the small 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="PreStopped">
- <transition target="../Stopped" after="self.sccd_yield() * 2">
- <raise event="termination_condition" />
- </transition>
- </state>
- <state id="Stopped" />
- </state>
- <state id="SimulationFlow" initial="Started">
- <state id="Started">
- <transition target="../Initialized">
- <script>
- <![CDATA[
- print('Going to Initialized... ')
- self.initialize()
- ]]>
- </script>
- </transition>
- </state>
- <state id="Initialized">
- <transition target="../InitializeDebugger">
- <script>
- <![CDATA[
- print('Going to InitializeDebugger... ')
- ]]>
- </script>
- </transition>
- </state>
- <state id="InitializeDebugger">
- <onentry>
- <script>
- <![CDATA[
- self.initializeDebugger()
- print('Going to MacroStepProcessed... ')
- ]]>
- </script>
- </onentry>
- <transition target="../CheckTermination" />
- </state>
- <state id="CheckTermination" initial="MacroStepProcessed">
- <state id="MacroStepProcessed">
- <transition target="../../Stopped" cond="INSTATE('/Main/SimulationState/Stopped')" after="self.sccd_yield()">
- <script>
- <![CDATA[
- print('Going to Stopped... ')
- ]]>
- </script>
- </transition>
- <transition target="../../Waiting" cond="INSTATE('/Main/SimulationState/Running/Realtime')" />
- <transition target="../../DoSimulation" cond="INSTATE('/Main/SimulationState/Running/Continuous') or INSTATE('/Main/SimulationState/Running/BigStep') or INSTATE('/Main/SimulationState/Running/SmallStep')" />
- </state>
- </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="../CheckTermination" after="self.sccd_yield()">
- <!-- We set the simulation time to the correct value. -->
- <script>
- diff = accurate_time.time() - self.realtime_start_time
- self.clock = diff * self.realtime_scale
- </script>
- </transition>
- <!-- We execute a step when the wait time is smaller than the smallest possible delay. -->
- <transition target="../DoSimulation" cond="self.waitTime() / 1000.0 <= self.sccd_yield()" />
- </state>
- <state id="DoSimulation" initial="MacroStepPrepared">
- <state id="MacroStepPrepared">
- <onentry>
- <script>
- <![CDATA[
- print('Entered MacroStepPrepared and reading events... ')
- self.currentEvent = self.getInputEventAt(self.clock / 1000.0)
- self.selectedTransition = self.model.getTransitionFrom(self.currentState, self.currentEvent, self.elapsed)
- print self.clock / 1000.0
- print(self.currentEvent)
- print(self.selectedTransition)
- ]]>
- </script>
- </onentry>
- <transition target="../MicroStepProcessed">
- <script>
- <![CDATA[
- print('Going to MicroStepProcessed... ')
- ]]>
- </script>
- </transition>
- </state>
- <state id="MicroStepProcessed">
- <transition target="../PreMicroStepPrepared" cond="self.selectedTransition != None" after="self.sccd_yield() * 2">
- <script>
- <![CDATA[
- print('Going to MicroStepPrepared... ')
- ]]>
- </script>
- </transition>
- <transition target="../../CheckTermination" cond="self.selectedTransition == None" after="self.sccd_yield()">
- <script>
- <![CDATA[
- print('Going to CheckTermination and advancing time... ')
- self.advanceTime()
- print(self.clock / 1000.0)
- print(self.elapsed / 1000.0)
- ]]>
- </script>
- <raise event="big_step_done" />
- </transition>
- </state>
- <state id="PreMicroStepPrepared">
- <transition target="../MicroStepPrepared" cond="INSTATE('/Main/SimulationState/Running')" />
- </state>
- <state id="MicroStepPrepared">
- <transition target="../MicroStepProcessed">
- <script>
- <![CDATA[
- print('Going to MicroStepProcessed, taking transition and reading events... ')
- print('Transition to be taken: ' + str(self.selectedTransition))
- self.currentState = self.selectedTransition.target
- self.elapsed = 0
- self.processEvent(self.currentEvent)
- print('New state: ' + str(self.currentState))
- self.currentEvent = self.getInputEventAt(self.clock / 1000.0)
- self.selectedTransition = self.model.getTransitionFrom(self.currentState, self.currentEvent, self.elapsed)
- print(self.currentEvent)
- print(self.selectedTransition)
- ]]>
- </script>
- <raise event="small_step_done" />
- </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="new_state" />
- <script>
- result = self.godEvent(new_state)
- </script>
- <raise event="god_event_result" port="user_output">
- <parameter expr="result" />
- </raise>
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.currentState" />
- </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.currentState" />
- </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.currentState" />
- </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="big_step_done" port="user_output" />
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.currentState" />
- </raise>
- </transition>
- <transition target="." event="small_step_done" cond="INSTATE('/Main/SimulationState/Running/SmallStep') or INSTATE('/Main/SimulationState/Running/SmallStepDone')">
- <raise event="small_step_done" port="user_output" />
- <raise event="current_state" port="user_output">
- <parameter expr="self.clock / 1000.0" />
- <parameter expr="self.currentState" />
- </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.currentState" />
- </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>
|