institute of computer science · 1 state modeling -- marlon dumas . statechart for the washing...
TRANSCRIPT
Advanced state modeling
Marlon Dumas Institute of Computer Science
How does a washing machine work?
} On/off (power) button. } Start button (no stop button!) } Light indicates current stage
} soaking, rinsing, draining, drying } Three washing plans that can be
changes using a “mode” button: } Regular } Delicate (no soaking) } Super delicate (no soaking, no drying)
} Off can be pushed only: } before starting } or after finishing
State modeling -- Marlon Dumas 1
Statechart for the washing machine
State modeling -- Marlon Dumas 2
off
idle
do: light(soak)
do: pump(in)
soak
do: light(rinse)
do: stir()
rinse
do: light(drain)
do: pump(out)
drain
do: light(dry)
do: stir()
dry
power power
start[plan=regular]
after (30)
after (30 min)
after (5 min)
[plan<>super delicate]
after (5 min)[plan=super delicate]/light(off)
after (20 min)/
light(off)
start[plan=delicate
or plan=super delicate]
/plan = regular
mode[plan=super delicate]/
plan = regularmode[plan=regular]/plan = delicate
mode[plan=delicate]/plan = super delicate
State explosion and history
State modeling -- Marlon Dumas 3
off
do: selectMode()
idle
do: light(soak)
do: pump(in)
soak
do: light(rinse)
do: stir()
rinse
do: light(drain)
do: pump(out)
drain
do: light(dry)
do: stir()
dry
power power
start[plan=regular]
after (30)
after (30 min)
after (5 min)
[plan<>super delicate]
after (5 min)[plan=super delicate]/light(off)after (20 min)/
light(off)
start[plan=delicate
or plan=super delicate]
What if the “power” button can be clicked at anytime?
What if we want to come back to the same state we left?
State explosion
State modeling -- Marlon Dumas 4
} If we have “n” classes with “m” (boolean) attributes each
(let’s assume that all classes have the same number of attributes)
} Possible states of the whole system = 2nm
Abstraction in Statecharts
Factor out common behavior
Segregate independent behavior
Composite States
Orthogonal/ Parallel States
© Eran Torch, Technion 5
Remember history
History pseudo-states
Composite states
State modeling -- Marlon Dumas 6
off
power power
on
do: selectMode()
idle
do: light(soak)
do: pump(in)
soak
do: light(rinse)
do: stir()
rinse
do: light(drain)
do: pump(out)
drain
do: light(dry)
do: stir()
dry
start[plan=regular]
after (30)
after (30 min)
after (5 min)
[plan<>super delicate]
after (5 min)[plan=super delicate]/light(off)after (20 min)/
light(off)
start[plan=delicate
or plan=super delicate]
The transition can be fired from any internal state
Exercise 1
State modeling -- Marlon Dumas 7
} Group “FlashOn” and “FlashOff” states into a composite state “Flashing”
entry: turnOff()
LampOff
entry: turnOn()
LampOn
entry: turnOn()
FlashOn
entry: turnOff()
FlashOff
after (2 s) after (2 s)
on
off onon
flash
History pseudo-state
State modeling -- Marlon Dumas 8
} Return to a previously visited hierarchical state } Shallow history: just the current level
} Deep history: includes all nested states
} Sometimes it is useful to clear history: } clear-history(state) clh(state) } clear-history(state*) clh(state*)
H
H*
Back to the washing machine…
State modeling -- Marlon Dumas 9
off
do: selectMode()
idle
do: light(soak)
do: pump(in)
soak
do: light(rinse)
do: stir()
rinse
do: light(drain)
do: pump(out)
drain
do: light(dry)
do: stir()
dry
power power
start[plan=regular]
after (30)
after (30 min)
after (5 min)
[plan<>super delicate]
after (5 min)[plan=super delicate]/light(off)after (20 min)/
light(off)
start[plan=delicate
or plan=super delicate]
Washing machine with “history”
State modeling -- Marlon Dumas 10
off
power
on
do: selectMode()
idle
do: light(soak)
do: pump(in)
soak
do: light(rinse)
do: stir()
rinse
do: light(drain)
do: pump(out)
drain
do: light(dry)
do: stir()
dry
start[plan=regular]
after (30)
after (30 min)
after (5 min)
[plan<>super delicate]
after (5 min)[plan=super delicate]/light(off)after (20 min)/
light(off)
start[plan=delicate
or plan=super delicate]
H
power
Shallow vs. Deep history
State modeling -- Marlon Dumas 11
A
H
B
D E
C
F G
M
resume
pause
A
H*
B
D E
C
F G
M
resume
pause
Note on transition precedence
State modeling -- Marlon Dumas 12
} Two or more transitions may have the same event trigger } inner transition takes precedence } if no transition is triggered, event is discarded
outer
inner1
inner2
e f f
Order of activities in nested models
State modeling -- Marlon Dumas 13
} Same approach as for the simple case
exit: exS1
S1
exit: exS11
S11
ev/actE
entry: enS2
S2
entry: enS21
S21
/initS2
Execution sequence:
exS11ð exS1 ð actEðenS2 ð initS2 ð enS21
Exercise 2
State modeling -- Marlon Dumas 14
} Fix and simplify this state machine
Independent behavior
State modeling -- Marlon Dumas 15
} Multiple simultaneous perspectives on the same entity
Age FinancialStatus
child
adult
retiree
poor
rich
Parallelism: States with orthogonal regions
State modeling -- Marlon Dumas 16
} Combine multiple simultaneous descriptions
Age FinancialStatus
child
adult
retiree
poor
rich
child
adult
retiree
poor
rich
Age FinancialStatus
Parallelism: States with orthogonal regions
State modeling -- Marlon Dumas 17
} All mutually orthogonal regions detect the same events and respond to them “simultaneously” } usually reduces to interleaving of some kind
poor
rich
robBank
LegalStatus FinancialStatus
lawAbiding
outlaw
robBank
“Flat” vs. Parallel State Machines
With Orthogonal
Regions
Without Orthogonal
Regions
B
C
E
G
F
A D
α β γ δ
µ
(in G) α
C,F
C,E C,G
B,G B,E
B,F
α β
γ
γ
δ
δ
µ
µ
α α
α
} Every parallel machine can be transformed into a sequential machine:
Intro | Building Blocks | Advanced
Exercise 3: Rewrite this without parallel regions
State modeling -- Marlon Dumas 19
poor
rich
robBank
LegalStatus FinancialStatus
lawAbiding
outlaw
robBank
Synchronization
State modeling -- Marlon Dumas 20
} Orthogonal regions/states can be synchronized via transition guards
D
E
e[A.C in M]
A B
C
M
N
f f
This transition can only be fired when A.C is in M state
Class aggregation and their state diagrams
State modeling -- Marlon Dumas 21
} A state diagram is a collection of state diagrams } Class aggregation will
usually require to combine the state diagrams of all parts
} The whole can be thought as a set of orthogonal regions!
Readings & Resources
State modeling -- Luciano García-Bañuelos 22
} Last week: Blaha & Rumbaugh, Chapter 5 } This week: Blaha & Rumbaugh, Chapter 6