Create State Machine
Create either a passive or an active state machine:
The above sample uses the enums States
and Events
that define the available states and events.
Define Transitions
A simple transition
If the state machine is in state A
and receives event B
then it performs a transition to state B
.
Transition with action
Actions are defined with Execute
. Multiple actions can be defined on a single transition. The actions are performed in the same order as they are defined. An action is defined as a delegate. Therefore, you can use lambda expressions or normal delegates.
The following signatures are supported:
Actions can use the event argument that were passed to the Fire
method of the state machine.
If the type you specify as the generic parameter type does not match the argument passed with the event, an exception is thrown.
Transition with guard
Guards are used to decide which transition to take if there are multiple transitions defined for a single event on a state. Guards can use the event argument that was passed to the Fire
method of the state machine.
The first transition with a guard that returns true
is taken.
The signature of guards is as follows:
Entry and Exit Actions
When a transition is executed, the exit action of the current state is executed first. Then the transition action is executed. Finally, the entry action of the new current state is executed.
The signature of entry and exit actions are as follows:
Internal and Self Transitions
Internal and Self transitions are transitions from a state to itself.
When an internal transition is performed then the state is not exited, i.e. no exit or entry action is performed. When an self transition is performed then the state is exited and re-entered, i.e. exit and entry actions, if any, are performed.
Define Hierarchies
The following sample defines that B1
, B2
and B3
are sub states of state B
.
B1
is defined to be the initial sub state of state B
.
History Types
When defining hierarchies then you can define which history type is used when a state is re-entered:
- None:
The state enters into its initial sub state. The sub state itself enters its initial sub state and so on until the innermost nested state is reached. - Deep:
The state enters into its last active sub state. The sub state itself enters into its last active state and so on until the innermost nested state is reached. - Shallow:
The state enters into its last active sub state. The sub state itself enters its initial sub state and so on until the innermost nested state is reached.
Initialize, Start and Stop State Machine
Once you have defined your state machine then you can start using it.
First you have to initialize the state machine to set the first state (A
in the sample):
Afterward, you start the state machine:
Events are processed only if the state machine is started. However, you can queue up events before starting the state machine. As soon as you start the state machine, it will start performing the events.
To suspend event processing, you can stop the state machine:
If you want, you can then start the state machine again, then stop it, start again and so on.
Fire Events
To get the state machine to do its work, you send events to it:
This fires the event B
on the state machine and it will perform the corresponding transition for this event on its current state.
You can also pass an argument to the state machine that can be used by transition actions and guards:
Another possibility is to send a priority event:
In this case, the event B
is enqueued in front of all queued events. This is especially helpful in error scenarios to go to the error state immediately without performing any other already queued events first.
That's it for the tutorial. See rest of documentation for more details on specific topics.