uml statechart diagrams

38
1 Spring 2005 Specification and Analysis of Information Systems Session 6: Modeling States with UML Statechart Diagrams Spring 2008 Analysis and Specification of Information Systems Eran Toch http:// www.technion.ac.il/~erant

Upload: eran-toch

Post on 06-May-2015

25.494 views

Category:

Education


3 download

DESCRIPTION

Course in behavioral modeling with UML Statechart diagrams

TRANSCRIPT

Page 1: UML Statechart diagrams

1Spring 2005Specification and Analysis of Information Systems

Session 6:

Modeling States with UML Statechart Diagrams

Spring 2008

Analysis and Specification of Information Systems

Eran Tochhttp://www.technion.ac.il/~erant

Page 2: UML Statechart diagrams

2

Outline

• Introduction to Statecharts

• Statechart building blocks– States– Transitions

• Advanced Characteristics – Composite States– Parallel States

Page 3: UML Statechart diagrams

3

Modeling Process in UML

Intro | Building Blocks | Advanced

Phase Actions Outcome

Initiation Raising a business needBusiness documents

RequirementsInterviewing stakeholders, exploring the system environment

Organized documentation

SpecificationAnalyze the engineering aspect of the system, building system concepts

Formal specification

DesignDefine architecture, components, data types, algorithms

Formal Specification

ImplementationProgram, build, unit-testing, integrate, documentation

Testable system

Testing & Integration

Integrate all components, verification, validation, installation, guidance

Testing results, Working sys

Maintenance Bug fixes, modifications, adaptationSystem versions

Page 4: UML Statechart diagrams

4

What’s Missing in Behavior Modeling?

Intro | Building Blocks | Advanced

p : Product : ShooppingCart

addProduct (p)

customer

display)(

getPrice)(

checkout ()

In Sequence diagram, we do not really know how the state of the shopping cart changes.

Page 5: UML Statechart diagrams

5

Modeling States

• How can we model the state of the shopping cart?

• Depends on:– Object state )e.g. created, destroyed(– Variable assignment– Relation status )i.e. number of items(– Operating methods & processes– History

addNewProduct)p : Product, quantity : int(removeProduct)p : Product(

dateCreated : DatedateUpdated : Date

ShoppingCart

productId : Stringname : Stringphoto : Picture

Product

**

quantity

Item

Intro | Building Blocks | Advanced

Page 6: UML Statechart diagrams

6

The Automata

• A machine whose output behavior is not only a direct consequence of the current input, but of some past history of its inputs.

• Characterized by an internal state which represents this past experience.

If the phone is ON, then clicking will turn it off

If the phone is OFF, then clicking will turn it on

Intro | Building Blocks | Advanced

Page 7: UML Statechart diagrams

7

Automata – Mathematical Foundations

• An Automata is a 4-tuple: S,, ,S0– S is a set of states is an alphabet - finite set of symbols is the transition function: : S S – S0 is an initial state

Many uses:– Stochastic processes )x, y & z are probabilities(– Formal and natural languages )x, y & z are characters(– Complex systems )x, y & z are…(

Intro | Building Blocks | Advanced

offoff ononclick

clickGiven a state and an input, the automata jumps to a new stateS0

Page 8: UML Statechart diagrams

8

Automata Modeling with Statecharts

• A Statechart augments deterministic automata, making them suitable for specifying behavior of complex systems, using:– Visual formalism– Rich alphabet model– State Modularity – Parallel behavior

• Developed by David Harel )Weizman Inst.(

Intro | Building Blocks | Advanced

Page 9: UML Statechart diagrams

9

Outline

• Introduction to Statecharts

• Statechart building blocks– States– Transitions

• Advanced Characteristics – Composite States– Parallel States

Page 10: UML Statechart diagrams

10

States & Classes

Order

status: {created, sent, received}hasProblems : boolean

gatherItems)(updateInventory)(sendOrder)(received)customer(sendReciept)(

createdcreated

sentsent

State view

Intro | Building Blocks | Advanced

Class view

in gatheringin gathering

is received

State

Transition

in problem

Page 11: UML Statechart diagrams

11

Activities

• When holding the state, the component can perform activities.– Can be continuous, taking a

specific, or unbounded, time– Activities within a state are

sequential– Activities can be interrupted

• There are special types of activities: enter, exit and event classified activities

in gathering

do: gatherItems()do: updateInventory()

in gathering

do: gatherItems()do: updateInventory()

Intro | Building Blocks | Advanced

created

enter: set hasProblems to false

do: init gathering listexit: clean log file

created

enter: set hasProblems to false

do: init gathering listexit: clean log file

Page 12: UML Statechart diagrams

12

Transitions

xx yyevent [guard] | action

The event that triggers the transition

Conditions that must be met for the transition to take place

The action that takes place when the transition is taken

Intro | Building Blocks | Advanced

Page 13: UML Statechart diagrams

13

Events

• General Events– Method calls: received)customer(

– Event signals: inventoryReady

• Time event– interval expiry;

tm)3 days(, tm)1000 ms(

– calendar/clock time; when)11:50(

• Change Event:– Change in value of some entity;

false)hasProblems(

true)hasProblems(

Intro | Building Blocks | Advanced

createdcreated

sentsent

in gatheringin gathering

is received

inventoryReady

tm)3 days(

false)hasProblems(

received)(

in problem

Page 14: UML Statechart diagrams

14

Guards (Conditions)

• Boolean expressions.• Evaluated when the transition is triggered• Types of guards:

– Simple predicate: [hasProblems], [x > 0]– Combined predicates:

[hasProblems )hasProblems order.sum < 100]– Guards on activities: [active)gatherItems(]– State related )we’ll get back to it later(

Intro | Building Blocks | Advanced

sentsent

in gatheringin gathering

gatheringFinished [all items were gathered]

in problemin problemgatheringFinished [items are not found]

Page 15: UML Statechart diagrams

15

Guards - Example

SellingSelling

UnhappyUnhappy

bid [)value >= 100( AND )value < 200(]

HappyHappybid [value >= 200]

bidbid [value < 100]

Intro | Building Blocks | Advanced

Page 16: UML Statechart diagrams

16

Static Conditional Branching

• A graphical shortcut for convenient rendering of decision trees

[)value >= 100( & )value < 200(]

[value >= 200]

[value < 100]

SellingSellingSellingSelling

UnhappyUnhappyUnhappyUnhappy

HappyHappyHappyHappy

bid

Intro | Building Blocks | Advanced

Page 17: UML Statechart diagrams

17

Empty Transitions

• A transition can have any combination )including none( of the events, guards and actions

• When a transition does not have an event, it is taken after all the activities were ended

Cleaningdo: put waterdo: put soap

do: wash soapdo: drain

doing nothing

Employee

rest[break]

work

Workingdo: shovel

Empty Transition

work

Intro | Building Blocks | Advanced

Page 18: UML Statechart diagrams

18

Guards and Events

• What’s the difference between the two machines?

S1 S2E1

S3true(C)

S1 S2E1

S3 [C]

What happens if C changes to True before E1?

Intro | Building Blocks | Advanced

Page 19: UML Statechart diagrams

19

Actions

• An executable atomic computation

• Types of actions– Variable assignment:

– Throwing a signal:

– Start, or stop activities )and concatenation of actions(:

sentsentsentsent is receivedis receivedreceived)( | status := received

sentsentsentsent is receivedis receivedreceived)( | throw)InventoryUpdate(

sentsentsentsent is receivedis received| start)sendBill(; stop)delivery(; x := x+1

Intro | Building Blocks | Advanced

Page 20: UML Statechart diagrams

20

Transitions - advanced

• Self-transitions: Transitions can be directed to the same state:

• Un-deterministic states – when two transitions are taken in the same time, one of will be taken in an un-deterministic fashion:

S2 E1 / c:=c+1

/ c=0

S1 S2E1

S3

]C1[

Intro | Building Blocks | Advanced

Page 21: UML Statechart diagrams

21

How does a Washing Machine Works?

• On / Off button. Start button )No stop button.(

• Feedback is given on the current stage )soaking, rinsing, draining, drying(

• Three plans:– Regular– Delicate )no soaking(– Super delicate )no soaking, no

drying(

• Off can be clicked only before starting, or after finishing

Intro | Building Blocks | Advanced

Page 22: UML Statechart diagrams

22

Washing Machine

Intro | Building Blocks | Advanced

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

click)power( click)power(

click)start( [plan=regular]

click)start( [plan=delicate or super delicate]

tm)30 mins(

tm)30 mins(

Tm)5 mins( [plan=super delicate] | light)off(

tm)5 mins([plan=not super

delicate]

Tm)10 mins( | light)off(

Page 23: UML Statechart diagrams

23

Outline

• Introduction to Statecharts

• Statechart building blocks– States– Transitions

• Advanced Characteristics – Composite States– Parallel States

Page 24: UML Statechart diagrams

24

State Explosion: An Example

What is the off button can be clicked at any time?

Intro | Building Blocks | Advanced

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

click)power( click)power(

click)start( [plan=regular]

click)start( [plan=delicate or super delicate]

tm)30 mins(

tm)30 mins(

Tm)5 mins( [plan=super delicate] | light)off(

tm)5 mins([plan=not super

delicate]

Tm)10 mins( | light)off(

What if we want to come back to the same state we left?

What if we want to show how many minutes left to the end of the cycle?

Page 25: UML Statechart diagrams

25

Abstraction in Statechart

Finding Common Behavior Separating Independent Regions

Composite States Parallel States

Intro | Building Blocks | Advanced

Page 26: UML Statechart diagrams

26

The State Explosion Problem

• Let– n: Num of Classes– m: Num of variables )assume equality among classes(

• Number of possible states = 2)nm(

• And...– What if the state space of each variable > 2– What about association between objects?

Class 1

v1 : {t, f}v2 : {t, f}v3 : {t, f}...vm : {t, f}

Class 2 Class n

...

Intro | Building Blocks | Advanced

v1 : {t, f}v2 : {t, f}v3 : {t, f}...vm : {t, f}

v1 : {t, f}v2 : {t, f}v3 : {t, f}...vm : {t, f}

Page 27: UML Statechart diagrams

27

Composite States

Intro | Building Blocks | Advanced

On

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

click)start( [plan=regular]

click)start( [plan=delicate or super delicate]

tm)30 mins(

tm)30 mins(

Tm)5 mins( [plan=super delicate] | light)off(

tm)5 mins([plan=not super

delicate]

Tm)10 mins( | light)off(

click)power(click)power(

Page 28: UML Statechart diagrams

28

Composite + History

On

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

click)start( [plan=regular]

click)start( [plan=delicate or super delicate]

tm)30 mins(

tm)30 mins(

Tm)5 mins( [plan=super delicate] | light)off(

tm)5 mins([plan=not super

delicate]

Tm)10 mins( | light)off(

click)power(

click)power(

H

Page 29: UML Statechart diagrams

29

deep and shallow

DiagnosingDiagnosingDiagnosingDiagnosing

Diagnostic1Diagnostic1

setupsetupsetupsetup

Check blood Check blood pressurepressure

Check blood Check blood pressurepressure

Diagnostic2Diagnostic2

Find a veinFind a veinFind a veinFind a vein

Draw BloodDraw BloodDraw BloodDraw Blood

H*H*

Intro | Building Blocks | Advanced

HH

StopStopStopStoppause

return

return-full

Shift ended | clh)(

clh)( – clears the history

Page 30: UML Statechart diagrams

30

Completion Transitions

• Triggered by a completion event– generated automatically when an immediately nested

state machine terminates

CommittingCommittingCommittingCommitting

Phase1Phase1Phase1Phase1

Phase2Phase2Phase2Phase2Commit DoneCommit DoneCommit DoneCommit Done

completion transition )no trigger(

Intro | Building Blocks | Advanced

Page 31: UML Statechart diagrams

31

LampFlashingLampFlashing

off/off/

FlashOffFlashOff

FlashOnFlashOn

Triggering Rules

• Two or more transitions may have the same event trigger– inner transition takes precedence– if no transition is triggered, event is discarded

on/on/

on/on/

Intro | Building Blocks | Advanced

Page 32: UML Statechart diagrams

32

Order of Actions: Complex Case

• Same approach as for the simple case

S1S1exit:exS1exit:exS1

S1S1exit:exS1exit:exS1

S11S11exit:exS11exit:exS11

S11S11exit:exS11exit:exS11

S2S2entry:enS2entry:enS2

S2S2entry:enS2entry:enS2

S21S21entry:enS21entry:enS21

S21S21entry:enS21entry:enS21

| initS2| initS2E/actEE/actE

Actions execution sequence:

exS11 exS1 actEenS2 initS2 enS21

Intro | Building Blocks | Advanced

Page 33: UML Statechart diagrams

33

Parallel States

• Sometimes, some aspect of an entity are independent of other aspects

single

married

divorced

Martial

StaffMember

Manager

employee

Intro | Building Blocks | Advanced

Page 34: UML Statechart diagrams

34

Parallel States Annotation

• Combine multiple simultaneous descriptions

StaffStaffMemberMember

ManagerManager

employeeemployee

StaffStaffMemberMember

ManagerManager

employeeemployee

singlesingle

marriedmarried

divorceddivorced

MartialMartial

singlesingle

marriedmarried

divorceddivorced

Martial Martial

Intro | Building Blocks | Advanced

Page 35: UML Statechart diagrams

35

Interactions Between Parallel States

Intro | Building Blocks | Advanced

On

idle

soak

rinse

drain

idle

Do: pump in water

in

Regular

Delicate

click)start( [Plan in Regular]

Entered)soak(

Tm)30([Plan not in Super Delicate]

Tm)30(

click)plan(

Pump PlanController

Super Delicate

click)plan(

click)plan(

click)start([Plan not in Regular]

Tm)30(

Tm)30([Plan in

Super Delicate]

idle

Rotor

Do: spin)150rpm(

Spin Slow

Do: pump out water

out

Entered)rinse(

Entered)drain(

Do: spin)400rpm(

Spin Fast

Exited)drain(

Exited)rinse(Tm)entered)rinse(,15(

Entered)rinse( [Plan not in Regular]

off

click)power(

click)power(

H

Page 36: UML Statechart diagrams

36

“Flat” and Parallel Machines

Use of Orthogonal

Regions

No Use of Orthogonal

Regions

B

C

E

G

F

A D

)in G(

C,F

C,E C,G

B,GB,E

B,F

• Every parallel machine can be transformed into a sequential machine:

Intro | Building Blocks | Advanced

Page 37: UML Statechart diagrams

37

Transition Forks and Joins

• For transitions into/out of orthogonal regions:

Credit Card verificationCredit Card verification

chargingcharging

ProcessingProcessing SentSent ConfirmedConfirmed

shippingshipping

Receipt SentReceipt Sent

Intro | Building Blocks | Advanced

In problemIn problem

[ok]

[not ok] Joinfork

Page 38: UML Statechart diagrams

38

Summary

Statechart:– State-based modeling– Based on Automatas

Statechart building blocks– States– Transitions

Advanced Characteristics – Composite States– Parallel States