institute of computer science · 1 state modeling -- marlon dumas . statechart for the washing...

23
Advanced state modeling Marlon Dumas Institute of Computer Science

Upload: others

Post on 06-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

Advanced state modeling

Marlon Dumas Institute of Computer Science

Page 2: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 3: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 4: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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?

Page 5: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... 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

Page 6: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

Abstraction in Statecharts

Factor out common behavior

Segregate independent behavior

Composite States

Orthogonal/ Parallel States

© Eran Torch, Technion 5

Remember history

History pseudo-states

Page 7: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 8: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 9: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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*

Page 10: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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]

Page 11: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 12: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 13: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 14: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 15: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

Exercise 2

State modeling -- Marlon Dumas 14

}  Fix and simplify this state machine

Page 16: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

Independent behavior

State modeling -- Marlon Dumas 15

}  Multiple simultaneous perspectives on the same entity

Age FinancialStatus

child

adult

retiree

poor

rich

Page 17: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 18: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 19: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

“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

Page 20: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

Exercise 3: Rewrite this without parallel regions

State modeling -- Marlon Dumas 19

poor

rich

robBank

LegalStatus FinancialStatus

lawAbiding

outlaw

robBank

Page 21: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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

Page 22: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

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!

Page 23: Institute of Computer Science · 1 State modeling -- Marlon Dumas . Statechart for the washing machine 2 State modeling -- Marlon Dumas off idle ... back to the same state we left?

Readings & Resources

State modeling -- Luciano García-Bañuelos 22

}  Last week: Blaha & Rumbaugh, Chapter 5 }  This week: Blaha & Rumbaugh, Chapter 6