Statecharts PDF
Statecharts PDF
About Statecharts
The statechart notation was developed by David Harel
inadequacies of state transition diagrams (STDs).
States
Stable state represents a condition in which an object
may exist for some identifiable period of time.
When an event occurs, the object may move from state to
state (a transition).
Events may also trigger self- and internal transitions, in
which the source and the target of the transition are the same
state.
In reaction to an event or a state change, the object may
respond by dispatching an action.
On the shelf
borrow()
name
state
Describing a Copy of a Book object
initial state
8
idle
Active
maintenance
[continue]
selecting
processing
[not continue]
entry / readCard
exit / ejectCard
transition from
substate
sequential
substate
validating
cancel
maintain
composite state
printing
Simple Transition
1(C)
10
Inside States
To understand what happens inside state A we can draw the
lower level of detail for that state.
A
C
Using Depth
Depth allows a state diagram to be viewed at different levels of
abstraction. It provides an effective way of managing the
design of a complex system. The high-level states can be
identified first and the lower-level details deferred.
Systems designed as a hierarchy are generally easier to
understand because the structure of the system is not obscured
by irrelevant details.
Depth in statecharts is not restricted to refining states into
lower level states. States can also be used to cluster groups of
states in order to reduce event arrows in a statechart.
13
2
C
1
2
B
More on clustering
A
1
2
C
B
Although in this simple example the number of arrows has
been reduced by just one, if a large group of states is
clustered by a state, then a very significant reduction in the
number of arrows can be achieved.
15
Advantages of depth
Low-level details of high-level states can be produced
simultaneously by different people.
The system is easy to understand
because it is viewed at different levels of abstraction and the
details of one part can be understood without having to
understand the entire system in detail.
18
B
End state
21
22
6
A
H
B
3
D
C
4
5
2
E
23
6
A
H
D
3
G
4
H
25
F
1
6
A
H *
D
3
G
4
C
26
Concurrency
A major problem with STDs is the rapid growth of
states to describe the simplest systems.
The statechart approach overcomes this with the
use of concurrency.
As an example imagine the buttons you can use on
the toolbar of Word - here lets model the bold,
italics and underline in a statechart.
27
Concurrency Example
Bold off
Italics off
Underline off
Bold on
Italics off
Underline off
Bold off
Italics on
Underline off
Bold on
Italics on
Underline off
Bold off
Italics off
Underline on
Bold on
Italics off
Underline on
Bold off
Italics on
Underline on
Bold on
Italics on
Underline on
29
Concurrency in use
When a user clicks the B, I and U, the system will
change states and cause the format of the
highlighted text to change. A STD would be too
chaotic to draw (each state has 3 possible exits
because a user could click on any one of the
buttons, and with 8 states, there are 24
transitions).
Adding more functionality would make a STD
unmanageable.
30
Simplicity of Concurrency
1
Bold on
Bold
clicked
Bold
clicked
2
Bold off
3
Underline on
Underline Underline
clicked
clicked
4
Underline off
5
Italics on
Italics
clicked
Italics
clicked
6
Italics off
32
A different example of
Concurrency
The left, right, centre and justify buttons do
not operate independently of each other.
They are in fact a group of radio buttons
because at any one moment only one of
them can be pushed down. If the user
pushes another button, the previous button
pops up.
This is modelled on the next slide.
33
Example modelled
R
R
Left
Right
L
Centre
L
J
C
R
Justify
C
Modelling a radio button example how to make a messy statechart
34
Concurrency (9)
Left clicked
Right clicked
7
Left
8
Right
9
Justify
10
Centre
Justify clicked
Centre clicked
36
Delays
A
< 10 sec
OK clicked
Time-outs
A
< 20 min
time-out
39
40
41
42
A
(No data
returned)
E
Transient
(status = X)
(status = Y)
Conditions,
not events
43
44
Eject
3
Closing CD
Drawer
(CD in drawer)
2
CD Drawer
Open
Eject
Eject
CD Loaded
Event
Actions
Next state
start
Application started
cd_player.close_drawer;
cd_player.open_drawer;
cd_player.close_drawer;
(cd_player.cd_loaded = false)
none
(cd_player.cd_loaded = true)
none
CD loaded
Note that there are no actions for the last 2 entries. This is
because state 3 is a transient state. It moves to the next
state dependent upon the conditions stated.
46