This example demonstrates the usage of multi state machines. For more details, please also refer to our documentation.
Using multiple state machines allows to split up large, complex models into smaller ones, which can be reused. Thus, complex systems can be modeled step by step in separated components. To achieve this, statecharts can import other statecharts and instantiate them.
As an example we will use a simple light controller that controls two LEDs. Each LED's behavior is defined by the same state machine. The controller uses this state machine twice: once for the red and once for the green LED. Both LED state machines are controlled separately to toggle the LEDs in an alternating fashion.
State machines can be imported using the
import: "" statement. Except for cyclic dependencies, there are no restrictions, whereby models can be imported over more than one hierarchy.
Imagine three different state machines: A, B, C:
The following dependency chain is allowed: A -> B -> C or A -> C
The following dependency chain is not allowed: A -> A or A -> B -> A
Importing allows the user to use the state machine as a type, which can be used for variables, operation parameters or even events. Following examples show possible usages of the led state machine type:
var machine : led
operation myOp(param : led)
in/out event : led
Defining a variable with a state machine type allows the user to access the state machine's API, which is usually generated.
machine.enter() // starts the state machine
machine.runCycle() //only for @CycleBased state machines, used to invoke a run-cycle step
machine.isActive() // true, if at least one state is active
machine.isStateActive(state : ledStates) // true, if given state is active
machine.exit() // stops the state machine
In order to communicate with a sub machine, its in events can be raised with the
raise command, like in this example with
It is also possible to access a sub machine's interface members, like variables or operations.
In order to simulate a multi state machine scenario, just simulate the root machine, in this example the controller machine. Simulating the controller machine automatically sets the simulation up to also simulate all sub machines. These can be selected in the simulation view.