Title: State Machine Diagrams Chapter 10
1State Machine DiagramsChapter 10
- Ragnhild Van Der Straeten
2State Diagrams
- State diagrams describe all the possible states
an object can get into and how the objects state
changes as a result of events that reach the
object - State diagrams are drawn for a single class to
show the lifetime behavior of a single object - Use state diagrams for those classes that exhibit
interesting behavior only - UI and control objects are popular candidates
3States
- State is a condition or situation during the life
of an object during which it - satisfies some condition,
- performs some activity or
- waits for some event.
- Object remains in a state for a finite amount of
time.
4States
- Different parts of a state
- Name textual string a state may be anonymous
meaning that it has no name. - Internal activities states react to events
without transition. - Entry/exit activities activities executed on
entering and exiting the state resp. - Composite states nested structure of a state,
involving disjoint (sequentially active) or
orthogonal (concurrently active) substates
5Initial and Final States
- Initial state indicates the default starting
place for the state machine or composite
state.(filled black circle) - Final state indicates that the execution of the
state machine or the enclosing state has been
completed(filled black circle surrounded by an
unfilled circle) - Initial and final states are pseudo-states.
Neither may have the usual parts of a normal
state, except for a name.
6Transitions
- Transition is a relationship between two states
indicating that - an object in the first state will perform certain
activities and - enter the second state when a specified event
occurs and specified conditions are satisfied. - On such a change of state, the transition is said
to fire.
7Transitions
- Transition has 5 parts
- Source state the state affected by the
transition if an object is in the source state,
an outgoing transition may fire when the object
receives the trigger event of the transtion and
if the guard condition is satisfied - Target state the state that is active after the
completion of the transition
8Transitions
- Trigger-signature Guard / Activity
- Trigger-signature an event whose reception by
the object in the source state makes the
transition legible to fire, providing the guard
condition is satisfied. - Guard a boolean expression, evaluated when the
transition is triggered byt the reception of the
event trigger if the expression evaluates True,
the transition is legible to fire, if the
expression evaluates to False, the transition
does not fire and if there is no other transition
that could be triggered by the same event, the
event is lost. - Activity some behaviour executed during the
transition
9Trigger-signature
- Event-name ( comma-separated-parameter-list )
- parameter-name type-expression
- Elapsed-time eventafter(5 seconds)after(10
seconds since exit from state A) - Condition becoming true is shown with the keyword
when followed by a boolean expression.
(continuous test for the condition until it is
true) - Transitions without a Trigger-signature within
their label occur as soon as the activity
associated with the state is finished
10Guard Condition
- Boolean expression enclosed in square brackets
and placed after the trigger event. - Evaluated only after the trigger event for its
transition occurs. - It is possible to have multiple transitions from
the same source state and with the same event
trigger, as long as those conditions do not
overlap. - Within the boolean expression, it is possible to
include conditions about the state of an object.
11Activity
- Activity is executed if and when the transition
fires - Activities may include
- operation calls to the object that owns the state
diagram or to other visible objects), - attributes and links of the context object,
- parameters of the triggering event
- activity sequence
12Ok / New, PutGreen, Open
Enter
idle Do/display welcome mess
checking Do/Checks Do/display wait mess
not Ok
refusing-access Do/display refusal mess
after 30 sec
13(No Transcript)
14AccountOk / New, PutGreen, Open
Enter / CardOk?
idle
checking-card
CardOk / AccountOk?
after 30 sec
checking-account
not CardOk
refusing-access
not AccountOk
15Advanced States and Transitions
- UMLs state diagrams have some advanced features
helping to - Manage complex behavioral models
- Reduce the number of states and transitions
- Codify a number of common and somewhat complex
idioms. - Internal Activities
16Entry and Exit Activities
- Dispatch the same activity whenever you enter a
state - In the symbol for the state, include an entry
activity marked by the keyword event entry - Dispatch the same activities whenever you leave a
state - In the symbol for the state, include an exit
activity marked by the keyword event exit - Entry and exit activities may not have arguments
or guard conditions.
17Internal Activities
- Events occuring inside a state but handled
without leaving the state. - Difference with self-transitions!!
- Self-transition event triggers the transition,
state is left, an activity (if any) is
dispatched, same state is reentered. A
self-transition dispatches the states exit
activity and dispatches the states entry
activity. - Internal activity if event is triggered, the
corresponding activity is dispatched WITHOUT
leaving and then reentering the state!! - Internal activities may have events with
parameters and guard conditions, internal
activities are essentially interrupts.
18Activities
- Ongoing activity while the object is in a state,
waiting for an event to occur, i.e., while in a
state, the object does some work that will
continue until it is interrupted by an event. - Do transition specifying the work that is to be
done inside a state after the entry activity is
dispatched. - The activity of a do activity might
- name another state machine,
- specify a sequence of activities.
- Regular activities cannot be interrupted, while
do-activities can take finite time and can be
interrupted.
19Sensorwarning / Blockmotor
Stop-bell
ringing
silent
opening Do/activate-motor-till-closed Entry/Ring-b
ell Exit/Stop-bell
Ring-bell
Open
closed
open
closing Do/Activate-motor-till-open SensorWarning/
Blockmotor Entry/Ring-bell Exit/Stop-bell
Close
20Composite States
- Composite states can be introduced to group a
number of states. All substates inherit any
transition on the composite state. The use of
composite states makes diagrams often more
readable - Orthogonal state diagrams combine different
independent behaviours of a given object. An
object can be in different states, each from an
orthogonal region in the diagram
21Sequential Composite States
- Sequential composite states composite states can
be introduced to group a number of states. - If the object is in a composite state, it is in
only one of its substates at a time. (substates
are disjoint) - Transition leading out of a composite state may
have its source the composite state or a
substate. In either case, control first leaves
the nested state, then it leaves the composite
state. - From a source outside a composite state, a
transition may target the composite state or a
substate.
22Sequential Composite States (continued)
- If the target is the composite state, this must
include an initial state to which control passes
after entering the composite state and after
dispatching its entry activity (if any). - It the target is a substate, control passes to
the substate after dispatching the entry activity
(if any) of the composite state and then the
entry activity (if any) of the substate.
23AccountOk / New, PutGreen, Open
Cancel
Enter / CardOk?
idle
Cancel
checking-card
CardOk / AccountOk?
Cancel
after 30 sec
checking-account
not CardOk
refusing-access
not AccountOk
24AccountOk / New, PutGreen, Open
Cancel
idle
Enter / CardOk?
active
checking-card
CardOk / AccountOk?
after 30 sec
checking-account
not CardOk
refusing-access
not AccountOk
25Stop-bell
ringing
silent
opening Do/activate-motor-till-closed Entry/Ring-b
ell Exit/Stop-bell
Ring-bell
Open
closed
open
closing Do/Activate-motor-till-open Entry/Ring-bel
l Exit/Stop-bell
Close
26Orthogonal Composite States
- An orthogonal composite state lets you specify
two or more state machines that execute in
parallel in the context of the enclosing object. - If one orthogonal region reaches its final state
before the other, control in that region waits at
its final state. When both nested regions reach
their final state, control from the two
orthogonal regions joins back in one flow.
27/ New, PutGreen, Open
idle
Cancel
Enter
checking
CardOk?
checking-card
CardOk
AccountOk?
checking-account
AccountOk
28Orthogonal Composite States (continued)
- Transition to a composite state decomposed into
orthogonal regions control forks into as many
orthogonal flows as there are orthogonal regions. - Transition from a composite state decomposed into
orthogonal regions control joins back into one
flow. - If all orthogonal regions reach their final
state, or if there is an explicit transition out
of the enclosing orthogonal composite state,
control joins back into one flow.
29Well-structured state machine (1/2)
- One state machine represents the dynamic aspects
of an individual object, representing, e.g., an
instance of a class or the system as a whole
across several use cases. - Is simple and therefore should not contain any
superfluous states or transitions. - Has a clear context and therefore may have access
to all the objects visible to its enclosing
object. - Is efficient and therefore should carry out its
behaviour with an optimal balance of time and
resources required by the activities it
dispatches.
30Well-structured state machine (2/2)
- Is understandable and therefore should name its
states and transitions from the vocabulary of the
system. - Is not nested too deeply.
- Uses orthogonal composite states sparingly.
31Exercises!!!