State Diagram Editor Introduction Part III#
In the first part of the tutorial you learned how to create a first state diagram and to generate code from it. Then in the second part you learned the use of regions and what can be done with them.
In this third part of the tutorial you will learn how to simulate a created model to see for yourself if the machine does what it is supposed to do, or to walk through a model with colleagues or customers and confirm that the behavior is as expected.
A model can only be simulated if it is error free. The absence of errors can be checked with the check button in the button bar. If there are no errors, the simulation is available on the Simulation tab next to the state tree.
Simulation without timer#
Open the Simulation tab and press Start. The machine enters its initial states; the active states are drawn with a red frame. The list at the top shows the events the active states (and their parents) react to, one row per transition, with guard and target. A click on an event triggers the state change, and the list then shows the events of the new active states. Start again starts the machine anew.
- The
Simulationtab. - The events the active states react to. A guarded event has one row per guard, here
ev2[else]andev2[i>0]: the click decides which guard is true. - Event Coverage: the share of the machine’s transitions triggered so far; each guard counts on its own.
- Start: starts the machine from its initial states.
- The active state, in red. The transitions it can take are drawn in blue.
- The output: the code “executed” for every step, in the order it runs.
- Clear empties the output.
The output makes it possible to track which actions are executed when the state changes; above, ev1 ran the exit code of S1, the transition action and the entry code of S2.
Note: actions refer to code entered as entry/exit/do code or transition actions.
Simulation with timers#
A model with time triggers, after(...), every(...) and named timers, is simulated with its timers running. See Time Triggered Events for how they are written. The timers run on a model clock: they start when their state is entered and stop when it is left, as in the generated code, and the clock can be paused, slowed down or sped up. The simulation pane then gets a timer bar and a timer list.
The example is a coffee machine. Brewing prints a dot every second with every(1s); the named timer brewTimer ends brewing after brewTime seconds, a C variable; a choice then decides whether milk is added, and Frothing ends with after(3s). The model is available for download, so you can follow the steps below in your own editor.
- A timer’s row in the event list, with a clock icon. A click lets the timer expire now, to test a timeout without waiting.
- Start runs the machine and the clock; Pause / Run stops and continues the clock (events can still be sent); Next timeout moves the clock to the next expiry and takes that timeout.
- Speed of the model clock: from 0.1× to 10× real time.
- Timer values…: the values the simulation cannot know. The simulation does not run C code, so a duration given by a variable, here
brewTime, is set here once; without a value it takes 1 s. Literal durations such asevery(1s)are shown but need nothing. - The model time, and whether the clock runs.
- The timer list: each running timer with its time left and the model time of its next expiry. A named timer that does not run shows the time it keeps.
The log shows each timeout with its model time, for example [2.000 s] Brewing: every(1s) expired.
When a timeout leads into a choice or to a guarded transition, the simulation cannot decide the guard. It stops the clock, and a banner above the event list asks which branch to take:
brewTimer expires into the choice: the banner asks for the branch, and the event list offers [withMilk] and [else]
[withMilk] was chosen: Frothing is active, and its after(3s) runs
after(3s) has expired by itself: the coffee is Ready The same in a film (1.5 minutes, with sound): brewTime is set under Timer values, the clock runs at half speed, and the simulation asks at the choice.
Timers that your own code handles, with timeout events such as evTimeout, are simulated like any other event: a click on the event sends it. The timer commands in comments (@@@ <TimerCreate .../>) of earlier versions are no longer simulated; they are ordinary comments now.
Summary#
With simulation of your design you can easily understand behavior and check whether transitions or other elements were forgotten. Timers run in the simulation as they do in the generated code, so time-dependent behavior can be checked before any code runs on the target.