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). Statecharts are now part of the OMG UML standard (OMG2003-umls).
An FSM models behavior by presenting states and transitions between the states; each transition is associated with an input event to the FSM (or possibly the empty event, in the case of nondeterministic FSMs). There are a finite number of states. 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 only on states can be automatically transformed into an equivalent one with actions only on transitions, 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.
Feature |
A situation in which it arises
(Harel1987-svfc p.233) |
A Statechart illustrating the feature |
---|---|---|
Clustering |
In all airborne states, when yellow handle is pulled seat will be ejected |
|
Orthogonality |
Gearbox change of state is independent of braking system |
|
Refinement |
Display-mode consists of time-display, date-display, and stopwatch-display |
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.
OMG2003-umls fig. 3-71
Active
contains many substates
(DialTone
,
Timeout
,
Dialing
,
Pinned
,
Talking
,
…).
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 ring
(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
OMG2003-umls fig. 3-73
Dialingcontains three substates (
Start
,
Partial Dial
,
and the anonymous final state).
pseudostateisn't a state; it just indicates the first state.
entry
indicates the action is performed
when the state is entered (entry / start dial tone).
exit
indicates the action is performed
when the state is left (exit / stop dial tone).
[number.isValid()]
prevents that transition from occurring unless the guard condition
is true (here, the guard name seems to indicate that
the guard prevents the transition
unless the number dialed is valid).
OMG2003-umls fig. 3-74
This state contains a little symbol (representing two blank states connected by a transition) indicating it is a composite state composed of substates that are not shown.
OMG2003-umls fig. 3-75
Incomplete
contains
three concurrent substates, separated by dashed lines.
Incomplete
composite state
is distributed across all three substates,
as though there were separate arrows from
Incomplete
's input pseudostate
to Lab1
,
Term Project
, and
Final Test
.
Consequently,
each one's initial state is entered simultaneously.
Passed
is distributed across all three substates.
This is the same as if there were a transition from
Lab2
to
Passed
at the event lab done, a transition from
Term Project
to
Passed
at the event
project done
,
a transition from
Final Test
to
Passed
at the event
pass
.
Because the three concurrent processes
are substates of the same composite state,
the Passed
state begins
when all three substates have terminated.
Failed
.
OMG2003-umls fig. 3-77
A1
and B1
)
occur simultaneously.
Cleanup
from the predecessor states
(A2
and B2
)
occur simultaneously.
Passed
was entered
after all the transitions from Lab2
,
Term Project
, and
Final Exam
had occurred,
but those three transitions (and any actions associated with them)
did not have to be simultaneous.
The synchronization bar
forces the transitions themselves to occur simultaneously.
Process
)
can be given in a tab
rather than in the state.
OMG2003-umls fig. 3-78
W
suppressed.
State0
or
State1
to either
State2
,
State3
, or
State4
.
OMG2003-umls fig. 3-81
OMG2003-umls fig. 3-80
OMG2003-umls fig. 3-82
FailureSubmachine).
OMG2003-umls fig. 3-83
A synchronization state () indicates that concurrent substates may have to pause to synchronize before moving to their following states.