38
1 Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch http://www.technion.ac.il/ ~erant Spring 2007

1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

1Spring 2005Specification and Analysis of Information Systems

Specifying State-based BehaviorWith UML Statechart Diagrams

Eran Toch

http://www.technion.ac.il/~erant

Spring 2007

Page 2: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

2

Outline

• Introduction to Statecharts

• Statechart building blocks– States– Transitions

• Advanced Characteristics – Composite States– Parallel States

Page 3: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

3

Modeling Process in UML

Intro | Building Blocks | Advanced

PhaseActionsOutcome

InitiationRaising 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

MaintenanceBug fixes, modifications, adaptationSystem versions

Page 4: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

7

Automata – Mathematical Foundations

• An Automata is a 5-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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

9

Outline

• Introduction to Statecharts

• Statechart building blocks– States– Transitions

• Advanced Characteristics – Composite States– Parallel States

Page 10: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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 falsedo: init gathering listexit: clean log file

created

enter: set hasProblems to falsedo: init gathering listexit: clean log file

Page 12: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

15

Guards - Example

SellingSelling

UnhappyUnhappy

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

HappyHappybid [value >= 200]

bidbid [value < 100]

Intro | Building Blocks | Advanced

Page 16: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

23

Outline

• Introduction to Statecharts

• Statechart building blocks– States– Transitions

• Advanced Characteristics – Composite States– Parallel States

Page 24: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

24

State Explosion: An Example

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

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

Intro | Building Blocks | Advanced

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

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 25: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

25

Abstraction in Statechart

Finding Common Behavior Separating Independent Regions

Composite States Parallel States

Intro | Building Blocks | Advanced

Page 26: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

26

The State Explosion Problem

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

• Number of possible states = 2)nm(

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

Class

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

Class

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

Class

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

...

Intro | Building Blocks | Advanced

Page 27: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

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: 1Spring 2005 Specification and Analysis of Information Systems Specifying State-based Behavior With UML Statechart Diagrams Eran Toch erant

38

Summary

Statechart:– State-based modeling– Based on Automatas

Statechart building blocks– States– Transitions

Advanced Characteristics – Composite States– Parallel States