Title: ObjectOriented Analysis and Design
1Object-Oriented Analysis and Design
Session 3b Behavioral Modeling State machine
diagrams
2Outline
- 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
3Introduction to State Machine
4 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.
5Automata
- 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.
6What are Statecharts?
- Statecharts are visual formalism for specifying
behavior of complex systems. - Developed by David Harel.
restbreak
Employee
Resting
Working
7State Machine (Automaton) Diagram
- Graphical rendering of automata behavior
Lamp On
Lamp Off
8Outputs and Actions
- As the automaton changes state it can generate
outputs
9Extended State Machines (1)
- Addition of variables (extended state)
ctr Integer
10Extended 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)
11State Machine Diagrams Syntax and Semantics
12State Machine Diagram
top state
State
Initial pseudostate
top
Trigger
Ready
Transition
/ctr 0
stop
Done
Final state
Action
13Statechart 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.
14Initial and Final States
start
Whites turn
checkmate
Black wins
stalemate
white moves
black moves
Draw
stalemate
Blacks turn
White wins
checkmate
15Activity 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.
16Event-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.
17Transitions
- A transition is enabled if the object is at a
state preceding the transition. - An enabled transition is activated upon its
trigger activation. - An initial transition indicates that the default
is entering S2. - A transition can connect the same state.
18Object Behavior - General Model
InitializeObject
Handling depends on specific request type
Wait forRequest
HandleRequest
TerminateObject
19Object Behavior and State Machines
20State Entry and Exit Actions
entry/lamp.on()
exit/lamp.off()
21Order of Actions Simple Case
- Entry actions are performed after (entering)
transition actions. - Exit actions are performed before (exiting)
transition actions.
Resulting action sequence printf(exiting) pr
intf(to off) lamp.off()
printf(exiting) printf(needless) lamp.off()
22Internal Transitions
- Self-transitions that bypass entry and exit.
actions
Internal transition triggered by an off event
off/null
23State (Do) Activities
- Forks a concurrent thread that executes until
- the action completes or
- the state is exited.
do activity
do/while (true) alarm.ring()
24Guards
- Conditional execution of transitions
- side-effect free
Selling
25Guards and Events (1)
26Guards 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.
27Traffic Light Example
N/S may go straight
N/S may turn left
tm(1) cars in N/S left lanes
tm(1)no cars in N/S left lanes
tm(1)
tm(1)
tm(1)no cars in E/W left lanes
E/W may turn left
E/W may go straight
tm(1)cars in E/W left lanes
28Phone Line Example
29Static Conditional Branching
- Merely a graphical shortcut for convenient
rendering of decision trees.
value gt 200 /sell
value lt 100 /reject
(value gt 100) (value lt 200) /sell
30Dynamic Conditional Branching
- Choice pseudostate guards are evaluated only
when the decision point is reached.
bid /gain calculatePotentialGain(value)
gain gt 200 /sell
gain lt 100 /reject
(gain gt 100) (gain lt 200) /sell
Dynamic choicepoint
31State Hierarchy
32States 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.
The states in an IndentationStatus statechart
33States Hierarchy (2)
- States hierarchy is used for describing different
levels of abstraction.
High Level
Detailed Level
34States Hierarchy (3)
- States hierarchy is used for clustering.
Un clustered
clustered
35States Hierarchy (4)
- Initial States one per state.
- To be in a state is to be in ONE of its sub
states.
36States Hierarchy (5)
- Graduated attack on complexity
- states decomposed into state machines
LampFlashing
37States Hierarchy (6)
Default transition to the initial pseudostate
LampFlashing
38States Hierarchy (7)
- Triggered by a completion event
- generated automatically when an immediately
nested state machine terminates.
completion transition (no trigger)
39States 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
off/
FlashOff
40States Hierarchy (9)
- Same approach as for the simple case
initS2
E/actE
Actions execution sequence exS11?? exS1 ??
actE??enS2 ?? initS2 ?? enS21
41State History Mechanism
42History Mechanism (1)
43History Mechanism (2)
44History Mechanism (3)
45History Mechanism (4)
- Return to a previously visited hierarchical state
- deep and shallow history options
Diagnosing
Diagnostic1
Diagnostic2
46State Concurrency
47Orthogonality and Concurrency (1)
- Multiple simultaneous perspectives on the same
entity
48Orthogonality 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.
49Orthogonality and Concurrency (3)
- Combine multiple simultaneous descriptions
50Orthogonality and Concurrency (4)
Use of Orthogonal Regions
No Use of Orthogonal Regions
51Orthogonality and Concurrency (5)
- All mutually orthogonal regions detect the same
events and respond to them simultaneously.
52Orthogonality and Concurrency (6)
- Typically through shared variables or awareness
of other regions state changes.
sane Boolean
flying Boolean
53Transition 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
C
b/d
a/c
d/a
c/b
B
D
54Transition Forks and Joins
- For transitions into/out of orthogonal regions
55Reducing Multiple Transitions (1)
56Reducing Multiple Transitions (2)
57Concluding Example
58Mini Organ Example (1)
59Mini Organ Example (2)
- The button a is used for switch the organ on and
off - 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. - 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). - 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). - 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.
60Mini 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
61Mini Organ Example (4)
c
b
x
y
62State Machine and UML
63Dishwasher Class Diagram
64Dishwasher State Machine Diagram
AcmeHeater
AcmeJet
Dishwasher
AcmeTank
65Summary
- UML uses a variant of Harels 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