(Last modified Fri Jun 06 19:11 2008)
Statecharts are a formalism invented by David Harel in order to address some of the limitations of classical notations for finite state machines (FSMs) in describing complex systems [Harel1987-svfc].
An FSM models behavior by presenting states and transitions between them; each transition is associated with an input event to the FSM (or possibly the empty event, in the case of nondeterministic FSMs). The transitions available from each state correspond to the possible events expected in that situation, and each state represents all the possible histories of events that can lead up to that state.
Actions can be associated with states and/or transitions. The FSM can produce a particular action every time a particular state is entered, or exited, or while the FSM is in that state; and/or the FSM can produce a particular action every time a particular transition is taken. Actions on states and actions on transitions work equally well, and an FSM with actions on states only can be automatically transformed into an equivalent one with actions on transitions only, and vice versa.
FSMs have the advantage of conceptual simplicity, computability, and an intuitive diagram form, but the number of states and transitions required to model behaviors increases rapidly as the behaviors become more complex. This rapid increase is termed state explosion and is a serious disadvantage of FSMs for modelling anything beyond simple behavior.
Statecharts are a notation that addresses the state explosion problem by adding these features to FSMs:
The examples below illustrate these features of the notation. Statecharts are now part of the OMG UML standard (OMG Unified Modeling Language Specification (2003)), from which most of the figures are drawn.
|
|
|
| "In all airborne states, when yellow handle is pulled seat will be ejected" (clustering) | "gearbox change of state is independent of braking system" (orthogonality) | "display-mode consists of time-display, date-display, and stopwatch-display" (refinement) |
As with any FSM, there are states, transitions between states and the events that cause them, and actions.
A state is an equivalence class of paths through the state machine: it represents all the paths by which you can reach that state. They have to be equivalent because once you reach that state, there is no record of how you got there, so whatever you do next depends only on that you are there in the state.
A transition is something that can happen next, once you are in a state. A transition occurs when its event happens.
An action is something the state machine makes happen in the world. Whereas states and transitions are of the state machine, events and actions are of the world. The events and actions are the ways (the only ways) the state machine interacts with the world it is in. An action can be attached to a transition, so that it is performed when that transition is taken; or to a state, so that it is performed when the state is entered, during the state, or when the state is exited (Statecharts use entry/action, do/action, and exit/action to distinguish these).
An event is something that makes a transition happen. In Statecharts, an event is written inputEvent[guard]/action, with all three parts optional. The inputEvent is what happens in the outside world that triggers the transition. An empty inputEvent means that the transition is taken immediately (if the guard is true at that time). The guard is a logical formula that filters out inputEvents; an inputEvent that occurs when its guard is false is ignored, permanently. An empty guard (the empty string instead of [condition]) represents "true". The action can be empty (in which case the / is omitted too).
As with any state machine, you can consider whether you need to add a new state, or combine two states into one.
Active contains many substates.
Active state is entered through the
"lift receiver" event, and
begins with the DialTone substate
(indicated by the initial pseudostate popsicle).
Active has no final state,
all its substates may exit through the superstate transition event
"caller hangs up".
A final pseudostate
is shown as a popsicle with a concentric circle
(see next diagram for an example).
connected")
whose meaning is defined elsewhere;
dial digit (n)") to stand for any of several related events
("dial 1", "dial 2", etc. in this case);
after (15 sec.)");
dial digit(n) [incomplete]")
so that only those occurrences of events when the guard is true
are indicated;
the meaning of the condition is defined elsewhere;
when(BooleanExpression);
and/or
lift receiver / get dial tone",
with "lift receiver" the event that triggers the transition
and "get dial tone" the action that occurs as a result)
and the meaning of the action is defined elsewhere.
If an event, guard, and action are present,
the syntax is event [guard] / action .
do / play dial tone").
The action label do indicates the action is performed continuously
while the FSM is in the state.
Action labels can be
This state contains that little symbol (representing two blank states connected by a transition) indicating it is a composite state composed of substates that are not shown.
|
|
A synchronization state indicates that concurrent substates may have to pause to synchronize before moving to their following states. (The "*" in it has some significance but I am not sure what.)
[Harel1987-svfc] David Harel. "Statecharts: A visual formalism for complex systems". Science of Computer Programming 8[3] pp. 231-274, June 1987.