Administration

Object-Oriented
Analysis and Design
Session 3b: Behavioral Modeling – State machine diagrams
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
1
Outline
1.
2.
3.
4.
5.
6.
7.
8.
Introduction to State Machine.……………..……... 3
State Machine Diagrams Syntax and Semantics….11
State Hierarchy………………………………...….31
State History Mechanism ………………...……....41
State Concurrency ……………………….……….46
Concluding Example ……………………………..57
State Machine Diagrams and UML ……..……….62
Summary ……………………………………...….65
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
2
Introduction to State
Machine
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
3
Characterization of reactive systems
•
Continuous interaction with the environment
–
•
•
•
•
inputs and outputs are asynchronous in time.
Respond to interrupts.
Stringent time requirements.
Multiple possible scenarios of operation,
depending on the history of previous
behavior.
Based on interacting processes that operate
in parallel.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
4
Automata
• A machine whose output behavior is not
only a direct consequence of the current
input, but also of some past history of its
inputs.
• Characterized by an internal state which
represents this past experience.
ON
OFF
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
5
What are Statecharts?
• Statecharts are visual formalism for
specifying behavior of complex systems.
• Developed by David Harel.
Employee
Working
Mira Balaban & Arnon Sturm
rest[break]
Resting
Object-Oriented Analysis and Design
6
State Machine (Automaton) Diagram
• Graphical rendering of automata behavior
on
Lamp On
on
off
off
Lamp Off
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
7
Outputs and Actions
• As the automaton changes state it can
generate outputs:
on
Lamp On
print(”on”)
Lamp
On
on
on/print(”on”)
off
off
Lamp
Off
off
Mealy automaton
Mira Balaban & Arnon Sturm
on
Lamp
Off
off
Moore automaton
Object-Oriented Analysis and Design
8
Extended State Machines (1)
• Addition of variables (“extended state”)
on
ctr : Integer
Lamp On
on/ctr := ctr + 1
off
off
Lamp Off
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
9
Extended State Machines (2)
• An extended (Mealy) state machine is defined by:
–
–
–
–
a set of input signals (input alphabet)
a set of output signals (output alphabet)
a set of states
a set of transitions
• triggering signal
• action
– a set of extended state variables
– an initial state designation
– a set of final states (if terminating automaton)
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
10
State Machine Diagrams
Syntax and Semantics
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
11
State Machine Diagram
“top” state
Initial
pseudostate
State
top
Trigger
Ready
Transition
stop /ctr := 0
Final
state
Mira Balaban & Arnon Sturm
Done
Action
stop
Object-Oriented Analysis and Design
12
Statechart Diagram Elements
• States
– Any component or object within the system can
be at a specific state in a given time.
– When holding the state, an object (or a
component) can perform activities (which can
be interrupted).
• Transitions
– Specification of rules for moving between
states.
– A transition may consist of a trigger, condition,
and an action.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
13
Initial and Final States
start
White’s
turn
Black wins
stalemate
white
moves
black
moves
Draw
stalemate
Black’s
turn
Mira Balaban & Arnon Sturm
checkmate
checkmate
Object-Oriented Analysis and Design
White wins
14
Activity and Action
• An activity:
–
–
–
–
can be performed within a state.
can be continuous.
can be sequential.
takes time.
• An action:
– can be performed within a state or during a
transition.
– can not be interrupted.
– is atomic.
– can take time.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
15
Event-Driven Behavior
• Event = a type of observable occurrence
– interactions:
• object operation invocation (call event).
• asynchronous signal reception (signal event).
– occurrence of time instants (time event)
• interval expiry.
• calendar/clock time.
– change in value of some entity (change event).
• Event Instance = an instance of an event (type)
– occurs at a particular time instant and has no
duration.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
16
Transitions
• A transition is enabled if the object is at a state
preceding the transition.
• An enabled transition is activated upon its trigger
activation.
E1
S1
E2
S2
S3
• An initial transition indicates that the default is
entering S2.
• A transition can connect the same state.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
17
Object Behavior - General Model
• Simple server model:
Initialize
Object
Handling depends on
specific request type
Wait for
Request
void:offHook ();
{busy = true;
obj.reqDialtone();
…
};
Handle
Request
Terminate
Object
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
18
Object Behavior and State Machines
• Direct mapping:
on
Initialize
Object
Lamp
On
Wait for
Event
Handle
Event
on/print(”on”)
off
Lamp
Off
Terminate
Object
Mira Balaban & Arnon Sturm
off
stop
Object-Oriented Analysis and Design
19
State Entry and Exit Actions
LampOn
entry/lamp.on();
e2
exit/lamp.off();
e1
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
20
Order of Actions: Simple Case
• Entry actions are performed after (entering) transition actions.
• Exit actions are performed before (exiting) transition actions.
LampOn
entry/lamp.on();
off/printf(“to off”);
entry/lamp.off();
exit/printf(“exiting”);
exit/printf(“exiting”);
Resulting action sequence:
printf(“exiting”);
printf(“to off”);
lamp.off();
Mira Balaban & Arnon Sturm
LampOff
off/printf(“needless”);
printf(“exiting”);
printf(“needless”);
lamp.off();
Object-Oriented Analysis and Design
21
Internal Transitions
• Self-transitions that bypass entry and exit.
Internal
transition
actions
triggered by
an “off” event
LampOff
entry/lamp.off();
exit/printf(“exiting”);
off/null;
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
22
State (“Do”) Activities
• Forks a concurrent thread that executes
until:
– the action completes or
– the state is exited.
“do” activity
Error
entry/printf(“error!”)
do/while (true) alarm.ring();
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
23
Guards
• Conditional execution of transitions
– side-effect free
bid [value < 100] /reject
bid [value >= 200] /sell
Selling
Happy
bid [(value >= 100) & (value < 200)] /sell
Unhappy
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
24
Guards and Events (1)
S1
S1
Mira Balaban & Arnon Sturm
E1
E1
S2
S2
Entered C
[C]
Object-Oriented Analysis and Design
S3
S3
25
Guards and Events (2)
• Special operators
–
–
–
–
in(a) – means in state a.
en(b) – means the occurrence of entering state b.
ex(c) – means the occurrence of exiting state c.
tm(min) – means trigger if min time units have
passed.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
26
Phone Line Example
on hook
on hook
Idle
off hook
on hook
Dial tone
do: sound dial tone
digit (n)
digit (n)
do: find connection
routed
do: play message
message done
Ringing
on hook / disconnect line
on hook
Recorded
message
Connecting
on hook
on hook
Mira Balaban & Arnon Sturm
valid number
busy
do: sound busy tone
Time out
invalid number
Dialing
Busy tone
time out
time out
called phone answers
/ connect line
Connected
called phone hangs up
/ disconnect line
Disconnected
Object-Oriented Analysis and Design
28
Static Conditional Branching
• Merely a graphical shortcut for convenient
rendering of decision trees.
Selling
Happy
bid
[value >= 200] /sell
[value < 100] /reject
[(value >= 100) & (value < 200)] /sell
Unhappy
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
29
Dynamic Conditional Branching
• Choice pseudostate: guards are evaluated
only when the decision point is reached.
Selling
Happy
bid /gain := calculatePotentialGain(value)
[gain >= 200] /sell
[gain < 100] /reject
[(gain >= 100) & (gain < 200)] /sell
Dynamic
choicepoint
Mira Balaban & Arnon Sturm
Unhappy
Object-Oriented Analysis and Design
30
State Hierarchy
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
31
States Hierarchy (1)
• Intended to cluster together alternative states of
a single aspect / object.
• Results: OR states. An OR state is a super-state
of its sub-states.
• A state that has no sub-states is a basic state.
Indentation-status
The states in an
IndentationStatus statechart:
Mira Balaban & Arnon Sturm
On
Left
Right
Center
Justify
Off
Object-Oriented Analysis and Design
32
States Hierarchy (2)
States hierarchy is used for describing different
levels of abstraction.
High Level
Detailed Level
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
33
States Hierarchy (3)
States hierarchy is used for clustering.
Un clustered
clustered
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
34
States Hierarchy (4)
Initial States– one per state.
To be in a state is to be in ONE of its sub states.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
35
States Hierarchy (5)
• Graduated attack on complexity
– states decomposed into state machines
LampOff
entry/lamp.off()
flash/
LampFlashing
FlashOn
entry/lamp.on()
off/
1sec/
on/
LampOn
1sec/
on/
on/
FlashOff
entry/lamp.off()
entry/lamp.on()
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
36
States Hierarchy (6)
• Higher-level transitions
LampOff
entry/lamp.off()
flash/
Default transition to
the initial pseudostate
LampFlashing
FlashOn
entry/lamp.on()
off/
1sec/
1sec/
on/
LampOn
on/
FlashOff
entry/lamp.off()
entry/lamp.on()
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
37
States Hierarchy (7)
• Triggered by a completion event
– generated automatically when an immediately
nested state machine terminates.
Committing
completion
transition (no trigger)
Phase1
CommitDone
Phase2
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
38
States Hierarchy (8)
• Two or more transitions may have the same
triggered event
– inner transition takes precedence.
– if no transition is triggered, event is discarded.
LampFlashing
FlashOn
on/
on/
off/
FlashOff
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
39
States Hierarchy (9)
• Same approach as for the simple case
S1
exit/exS1
S2
entry/enS2
initS2
S11
exit/exS11
E/actE
S21
entry/enS21
Actions execution sequence:
exS11 exS1  actEenS2  initS2  enS21
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
40
State History
Mechanism
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
41
History Mechanism (1)
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
42
History Mechanism (2)
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
43
History Mechanism (3)
G
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
44
History Mechanism (4)
• Return to a previously visited hierarchical state
– deep and shallow history options
Diagnosing
suspend/
resume/
H*
Mira Balaban & Arnon Sturm
Diagnostic1
Diagnostic2
Step11
Step21
Step12
Step22
Object-Oriented Analysis and Design
45
State Concurrency
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
46
Orthogonality and Concurrency (1)
• Multiple simultaneous perspectives on the same
entity
age
employee
Child
Staff
Member
Adult
Manager
Retiree
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
47
Orthogonality and Concurrency (2)
• Describe independent components of a behavior.
• Behavior of parts of an aggregate object.
• Enforce synchronization of concurrent activities.
Concurrency is an AND-decomposition of a state.
To be in a state S is to be in ALL of its
components. S is an AND state.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
48
Orthogonality and Concurrency (3)
• Combine multiple simultaneous descriptions
age
employee
Child
Staff
Member
Adult
age
Retiree
Manager
Child
Adult
Retiree
Mira Balaban & Arnon Sturm
employee
Staff
Member
Manager
Object-Oriented Analysis and Design
49
Orthogonality and Concurrency (4)
A
D
B
a
d
g
b
G
m
g
B,G
a
a
g
m
F
d
B,F
a
C,E
Use of
Orthogonal Regions
a
(in G)
C
B,E
m
E
a
C,F
No Use of
Orthogonal Regions
b
C,G
d
Object-Oriented Analysis and Design
50
Orthogonality and Concurrency (5)
• All mutually orthogonal regions detect the same
events and respond to them “simultaneously”.
legalStatus
LawAbiding
robBank/
Outlaw
Mira Balaban & Arnon Sturm
financialStatus
Poor
robBank/
Rich
Object-Oriented Analysis and Design
51
Orthogonality and Concurrency (6)
• Typically through shared variables or
awareness of other regions’ state changes.
sane : Boolean
flying : Boolean
Catch22
sanityStatus
flightStatus
Crazy
Flying
entry/sane := false;
entry/flying := true;
(flying)/
request
Grounding/
(sane)/
(~sane)/
Sane
Grounded
entry/sane := true;
entry/flying := false;
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
52
Transition Sequence
A transition might be a trigger
 An event can trigger additional transitions
 Several transition can be triggered within a single step
 Solution
 The number of triggered transition should be
limited
A
b/d
C
a/c
B
d/a
c/b
D
Object-Oriented Analysis and Design
53
Transition Forks and Joins
• For transitions into/out of orthogonal
regions:
age
Child
Staff
Member
Adult
Retiree
Manager
employee
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
54
Reducing Multiple Transitions (1)
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
55
Reducing Multiple Transitions (2)
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
56
Concluding Example
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
57
Mini Organ Example (1)
ON\OFF
a
Display
STYLE
SONG
b
c
1
2
3
4
5
6
7
8
9
Name
Mode
Number
START
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
58
Mini Organ Example (2)
1. The button a is used for switch the organ on and off
2. The organ enables a song selection and its style. When the
organ is turned on the display shows the mode: STYLE or
SONG and its number. The default is song #1/sytle #1 and
the mode is STYLE.
3. To select a style one need to press the b button and the
required style number (1-9). Each style has a function that
return its name – StyleName(x).
4. To select a song one need to press the c button and the
required song number(1-9). Each song has a function that
return its name – SongName(x).
5. Pressing the start button plays the current song following the
current style. Another press stop the music. It is possible to
change the song or style during music playing.
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
59
Mini Organ Example (3)
Events
a - pressing the a button
b - pressing the b button
c - pressing the c button
s - pressing the START button
i - pressing the a digit button (2-9)
1 - pressing the 1 button
songend – end of song playing
Variables
i,j – can get values between 2-9
k,m – can get values between 1-9
w – can get a song or style name
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
60
Mini Organ Example (4)
on
play
mode
song#
style#
display
a
disp-mode
disable
off
a
s or
songend
s
style
b [c2]
1
1
1[c1]
enable
Do: if song# in
k and style# in
m then play
song k in style
m
Mira Balaban & Arnon Sturm
style
c
b
1[c2]
song
j
name
song
number
w
i
x [c4]
j[c1]
c [c1]
k
y [c3]
i[c2]
Object-Oriented Analysis and Design
61
State Machine and UML
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
62
Dishwasher – Class Diagram
Dishwasher
cycles : int
rinseTime : int
washTime : int
dryTime : int
Tank
1
itsTank
evTankDrain()
evTankFill()
Dishwasher()
setup()
evStart()
evOpen()
evClose()
evQuick()
evNormal()
evIntensive()
evService()
Jet
1
itsJet
evJetOff()
evJetSpray()
evJetPulse()
itsHeater 1
Heater
evHeaterOff()
evHeaterOn()
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
63
Dishwasher – State Machine Diagram
AcmeHeater
Dishwasher
AcmeJet
off
evHeaterOn
evHeaterOff
idle
on
evJetSpray
AcmeTank
evTankFill
evJetOff
running
tm(4000)/
itsDishwasher->GEN(evFull);
filling
spraying
empty>
tm(4000)/
itsDishwasher->GEN(evEmpty);
Mira Balaban & Arnon Sturm
evJetPulse
pulsing
full>
draining
evTankDrain
Object-Oriented Analysis and Design
64
Summary
• UML uses a variant of Harel’s statecharts
– adjusted to software modeling needs
• Used to model event-driven (reactive) behavior
– well-suited to the server model inherent in the object paradigm
• Includes a number of sophisticated features that realize common
state-machine usage patterns:
– entry/exit actions
– state activities
– dynamic and static conditional branching
• Also, provides hierarchical modeling for dealing with very
complex systems
– hierarchical states
– hierarchical transitions
– orthogonality
Mira Balaban & Arnon Sturm
Object-Oriented Analysis and Design
65