What is a statechart?
The primary feature of statecharts is that states can be organized in a hierarchy: A statechart is a state machine where each state in the state machine may define its own subordinate state machines, called substates. Those states can again define substates.
Here’s an example of a state in a state machine, with some extra features:
- The state is called “D”
- E and F are substates
- E even has a substate G
- “entry” and “exit” actions are mentioned
When the state machine enters this state D it also starts the state machine within it. The initial state of this machine inside D is in fact E so it enters E too. And since E has a single substate G, it is also entered.
A state with no substates is called an atomic state. A state with substates is called a compound state.
Entering a state enters one of its substates
- When a state is entered, its sub state machine starts and therefore, a substate is entered
- When a state is exited, its sub state machine is exited too, i.e. any substates also exit
In the example above, when the machine is told to enter D, it actually ends up entering D, E, and G. Conversely, when the state machine exits D it also exits any substates.
Like state machines, statecharts also react to events; events are dealt with by the states and the main side effects are specified by what happens upon entering and exiting states.
A state can have many “regions”
A compound state can be split up into completely separate (“orthogonal”) regions. Each region specifies its own state machine. When a state machine enters such a state, it also enters all of the regions of the state at the same time.
If this state is entered, then it enters both the top and bottom regions, and the following statements will hold true so long as A is active:
- Exactly one of B and C is active
- Exactly one of D and E is active
Such a state is called a parallel state, and the regions are often called “orthogonal regions”.
Transitions can be guarded
When an event happens, and the state machine would normally transition from one state to another, statecharts introduce the concepts of guarding the transition. A guard is a condition placed upon the transition, and the transition is essentially ignored if the guard condition is false.
States can have multiple transitions for the same event
The addition of guards allows a state to have more than one transition that reacts to the same event: Two transitions fire on the same event, but only one is picked at run-time depending on which one’s guards evaluate to TRUE.
Transitions can happen automatically
When entering a state, a transition can be defined which is automatically taken. This is useful in conjunction with guards, to move out of a state immediately upon entering it when certain conditions hold, or as soon as those conditions hold.
Transitions can be delayed
Simply being in a state for a duration of time can be enough to transition to a different state. This is accomplished by way of a delayed transition, which defines that the transition should be taken a specific period of time after entering a state.
History
When exiting a compound state and its substate, it is sometimes useful to be able to return to exactly the state that you left. Statecharts introduce the concepts of history states. When a transition goes to a history state, it re-enters the state that “was last active”.
See also
To get a full explanation of the statechart shown above, see the description of the on-off statechart.