State machines in depth
Extension — not covered by the 1.0 stability promise (
machines; the core)
The core gives a component a state machine: states, states inside states,
transitions with a trigger, a guard and an action, on-enter and on-exit.
The machines extension adds what UML statecharts and Capella have on top:
pseudo-states (a choice, an initial child, a final state…), deadlines on a port
or a transition, activities written as text, and regions.
Some of these run in the simulator and some are only written down. This page
says which, with a pump controller as the worked example. archec check --core
lists every use of the extension in a model.
A worked example: a pump controller#
An operator panel sends an order with a volume. The controller decides at once whether there is anything to pump (a choice), primes the pump, then spins it. A stop ends the run (a final state). An order that reaches the controller too late is not run (a port's deadline).
project Pump {
edition "1.0"
}
message START {
litres: uint16
}
message STOP {}
component Panel "Operator panel" {
in button: can receives START {
on-receive {
send out START { litres: litres }
}
}
out out: can sends START
}
component Controller "Pump controller" {
in order: can receives START {
deadline 5ms
on-deadline {
late += 1
}
}
in stop: can receives STOP
var target: uint16 = 0
var primes: uint16 = 0
var late: uint16 = 0
states {
state Idle
state Sizing {
pseudo choice
}
state Running {
do "Watch the motor current"
entry-activity "Open the inlet valve"
exit-activity "Close the inlet valve"
state Spin
state Prime {
pseudo initial
on-enter {
primes += 1
}
}
Prime -> Spin on "after 20ms"
}
state Done {
pseudo final
}
Idle -> Sizing on START / target = litres
Sizing -> Running [target > 0]
Sizing -> Idle
Running -> Done on STOP
}
}
system Plant {
part panel: Panel
part pump: Controller
panel.out -> pump.order {
latency 8ms
}
}The test plan says what the machine does. Each case is a timeline (test cases):
test-plan TP "Pump" {
case Run "A real order primes, then spins" {
at 0ms do send pump.order START { litres: 40 }
at 1ms expect pump.state == Prime
at 1ms expect pump.target == 40
at 30ms expect pump.state == Spin
at 31ms do send pump.stop STOP {}
at 32ms expect pump.state == Done
}
case Empty "An empty order goes back to Idle" {
at 0ms do send pump.order START { litres: 0 }
at 1ms expect pump.state == Idle
at 1ms expect pump.primes == 0
}
case Late "An order that comes late is not run" {
at 0ms do send panel.button START { litres: 40 }
at 20ms expect pump.late == 1
at 20ms expect pump.state == Idle
}
}
archec check pump.arche
archec test pump.arche
archec diagram pump.arche states:Controller
archec test passes the three cases. The port's deadline is run by the browser
simulator only, so the line above the results says that every case runs there
(which simulator runs a test case).
What each part of the example does:
- The choice.
Sizingispseudo choice: the machine does not stay in it. It takes the first transition out of it whose guard holds, in the order written; one with no guard is the fallback. The guards here readtarget, which the transition into the choice set. - The initial child. Entering
Runningstarts inPrime, itspseudo initialchild, thoughSpinis written first. Without one, a composite starts in its first child. - The final state.
Doneispseudo final. The checker warns about a state with no way out (deadlock-state) unless it is final, so it says the machine is meant to stop there. - The deadline. The panel's link takes 8 ms, more than the port's
deadline 5ms. So the order arrives late:on-deadlineruns instead of the transitions, and the pump stays inIdle. - The activities.
do,entry-activityandexit-activitysay in words what the state does. They are not run (see Limits).
Pseudo-states#
A state is a pseudo-state when it says pseudo and its kind:
pseudo |
What it means | What archec does with it |
|---|---|---|
initial |
The child a composite starts in | Both simulators, the checker, the diagrams and the model checkers start there |
choice |
Decide at once, by the guards out of it | Both simulators take the first transition whose guard holds |
final |
The machine stops here | No deadlock-state warning; drawn as the end circle |
terminate |
The machine ends | As final for the checker |
history, deep-history |
Come back to the child the composite was in | Drawn, and exported; not simulated: see Limits |
fork, join |
Split into regions, join them again | Drawn with their bar; not simulated |
entry, exit |
A named way into or out of a composite | Drawn and exported; not simulated |
mode |
A state that is an operating mode (Capella's Mode) | Drawn and exported as a mode; simulated as a state |
state |
An ordinary state | The default |
A guard out of a choice may also read the fields of the message that led into
it. Idle -> Sizing on START then Sizing -> Running [litres > 0] is checked
with litres as START's field, and the browser simulator runs it so.
Deadlines#
A port or a transition may say how late is too late. Past its deadline, its
on-deadline body runs instead:
- On a port, late is the time between the message being sent and the part
starting on it. A late message runs
on-deadlineinstead of the port'son-receive, its handlers and its transitions. - On a transition, late is the time between its trigger arriving (or its
timer running out) and the part starting on it. Once its guard holds,
on-deadlineruns instead of the transition.
Idle -> Sizing on START {
deadline 2ms
on-deadline {
late += 1
}
}
A part starts late when its link has a latency, or when a deployment's thread
is busy (its wcet). on-deadline with no deadline is a warning
(deadline-handler). Deadlines are generated in C, C++ and Rust (Embassy) too.
A watchdog is their companion for "nothing came in time": it is the
deployment extension's (the spec).
Activities#
Three properties say in words what a state does, as UML's activities do:
do "…": what runs while in the state (its doActivity). It may also name functions of the component:do plan.entry-activity "…": what it does on entering.exit-activity "…": what it does on leaving.
They are written to the Arche project (doActivity, entryActivity,
exitActivity in its models), and read back from it. The code that runs on
entering and leaving is on-enter and on-exit, which are core.
Regions#
A child state may name its region: the children of one composite in different
regions are meant to be active together, as in UML.
state On {
state Lamp {
region light
}
state Cool {
region heater
}
state Hot {
region heater
}
Cool -> Hot on HEAT
}
The checker reads regions: entering On starts in the first child of each
region, and a state in a region is not reported as a deadlock. The Arche
project carries them, and its editor draws each region apart. The simulators
do not run them, and archec diagram does not draw them apart.
Limits#
- Regions are not simulated. A part is in one state at a time. Entering a
composite with regions starts in the first child of the first region only,
and the other regions do nothing.
archec gen --target uppaalmodels only the first region too, and says so in its notes. Write each concurrent part as a part of its own instead. - History is not simulated. A transition to a
historyordeep-historystate leaves the part in that state: it does not resume the child it was in. The UPPAAL export goes back to the composite's initial child, and says so. - Activities are text.
do,entry-activityandexit-activityare not run by either simulator, even whendonames a function. Put the code inon-enter,on-exit, a timed transition or a thread. - Fork, join, entry and exit points are drawn and exported, not simulated: the simulators treat them as ordinary states.
- Deadlines on the browser simulator only. Arche's simulator does not run
deadlines, so
archec testruns a case of a part with one on the browser simulator. A guard out of a choice that reads the incoming message's fields also fails in Arche's simulator: set a variable on the way in, as the example does. - The diagrams (
archec diagram) draw choices, forks, joins, final states and the initial child. They do not draw the activities, nor the regions apart.