state machines/statecharts collaboration diagrams sequence ... · state machines/statecharts...

59
PART 3: UML Dynamic Modelling Notations State machines/statecharts Collaboration diagrams Sequence diagrams Activity diagrams. Chapter 19 of the textbook is relevant to this part. 1

Upload: vanbao

Post on 26-Aug-2018

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

PART 3: UML Dynamic Modelling Notations

• State machines/statecharts

• Collaboration diagrams

• Sequence diagrams

• Activity diagrams.

Chapter 19 of the textbook is relevant to this part.

1

Page 2: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

State machines

• State machines describe dynamic behaviour of objects, show

life history of objects over time + object communications.

• Used for real-time system design (eg., robotics); GUI design

(states represent UI screens/modes).

Example shows simple state machine with two states, On and Off

and transitions between them.

2

Page 3: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Off

On

swon

swoff

Switch

Simple state machine

3

Page 4: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

State machines

Elements of a state machine are:

States: Rounded-corner boxes, containing state name.

Transitions: Arrows from one state, source of transition, to

another, target, labelled with event that causes transition.

Default initial state: State of object at start of its life history.

Shown as target of transition from initial pseudostate (black

filled circle).

Termination of state machine can be shown by ‘bullseye’ symbol.

4

Page 5: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

State machines

UML divides state machines into two kinds:

1. Protocol state machines – describe allowed life histories of

objects of a class. Events on transitions are operations of that

class, transitions may have pre and post conditions.

Transitions cannot have generated actions (although we will

allow this).

2. Behaviour state machines – describe operation

execution/implementation of object behaviour. Transitions do

not have postconditions, but can have actions.

State machines describe behaviour of objects of a particular class,

or execution processing of an operation.

5

Page 6: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

State machines

• Class diagrams describe system data, independently of time.

• State machines show how system/objects can change over time.

• Switch state machine is protocol state machine for objects of

Switch class.

• Life history of object obj of Switch is sequence of alternations

between On and Off states, beginning in Off :

Off −→swon On −→swoff Off −→swon ...

6

Page 7: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Switch

state: State

swon()swoff()

State <<enumeration>>OnOff

Switch class

7

Page 8: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

State machine semantics

For protocol state machine attached to a class:

1. Each object of class begins life history in default state of state

machine.

2. If object obj is in state s and event α occurs on obj , then if

there is transition labelled with α with source s, this transition

occurs, object moves into target state of transition.

If no transition for α from state s, object remains in state s.

8

Page 9: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

����

RedAmber

Green RedAmber

goAmber

goRed

goRedAmber

goGreen

Traffic light state machine

9

Page 10: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Transition Guards and Actions

• Transitions may have guards and actions.

• Guard is evaluated when transition event occurs on object, if

object is in source state of transition.

• If guard is true, transition takes place, otherwise it does not.

• Action on transition describes event(s) generated when

transition occurs. Usually operation invocations on objects of

supplier classes of state machine class.

Example: setaatt(x ) also sets b.batt to x for each b : br , when

x = 1:

10

Page 11: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

setaatt(x) [x /= 1]

setaatt(x) [x = 1]/ br.setbatt(x)

setbatt(x)A

B

State machines of classes A and B

11

Page 12: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

A B

br*

? aatt: Integer ! batt: Integer

setaatt(x: Integer) setbatt(x: Integer)

post: batt = xpost: aatt = x

Classes A and B

12

Page 13: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Actions

• Actions written following forward slash, after event and guard

(if present). Multiple actions can be specified, separated by /

or ; .

• Actions can include updates feature := val of local

attributes/roles, or invocations of operations on supplier

objects.

• Postconditions of operation op express its state-independent

behaviour, actions of op-triggered transitions express its

state-dependent behaviours.

13

Page 14: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Rules for state machines

1. For each state s, and event α of state machine, at most one

unguarded transition for α from s.

2. If several transitions for α, all must be guarded and have

disjoint guards, so impossible for more than one to be true at

once.

3. For simple state machines an object is in exactly one state at

all times after its creation and before its destruction.

14

Page 15: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Composite states

• States can be nested within others to express relationships of

generalisation/specialisation, and to simplify diagram.

• Following shows example of composite state Off , containing

substates Standby and NoPower .

• When swoff occurs in state On, system goes to Standby ,

default state of Off . When swon occurs, if system is in either

Standby or NoPower , then next state is On.

15

Page 16: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

On swoff

OffStandby

NoPower

swon

Switch2

powerOff

State machine with nesting

16

Page 17: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Student life history example

State machine for BSc students (3 year), with:

• States Year1, Year2, Year3 as substates of Studying

• States Interrupted and terminal (graduated) at top level

• Transitions for progression, repeating year, interruption and

graduation.

17

Page 18: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

����

��������

Interrupted

Studying

Year1

Year2

Year3 graduate

interrupt

H

resume

progress12

progress23

retake1

retake2

retake3

State machine of students

18

Page 19: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Student life history example

• Transition from superstate Studying to Interrupted equivalent

to 3 separate transitions from Year1, Year2, Year3 to

Interrupted .

• graduate transition only occurs for Year3 students.

• Final state shown as bullseye symbol, history state denoted by

H symbol.

• A transition to history state has actual target the most recently

occupied substate of composite state containing history state.

• When student resumes their studies, they return to year they

were in when they interrupted.

History states should only be used if essential.

19

Page 20: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Nested states

• If object is in basic state s, also in every state that contains s.

• A Year1 student is also in Studying , etc.

• States can have attached constraints, evaluate true for any

object while it is in state, including any substate of state.

• Constraint written in square brackets after state name.

20

Page 21: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Concurrent states

• States can be concurrent, consist of 2 or more composite states,

simultaneously occupied.

• Eg., monitoring system with two sensors, sensor 1 and sensor 2

can change state independently of each other.

Possible sets of states are

{sen1undetecting , sen2undetecting}{sen1undetecting , sen2detecting}{sen1detecting , sen2undetecting}{sen1detecting , sen2detecting}

21

Page 22: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Sensor 1 Sensor 2

sen1detecting

sen2detecting

sen1triggered triggered

sen2

sen1 sen2 untriggered untriggered

sen2undetecting

sen1undetecting

State machine with concurrent states

22

Page 23: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Concurrent States

• When concurrent composite state entered, each subcomponent

is entered, usually at default states.

• Also possible to specify direct entry to specific component

states.

• Transitions may exit from multiple sources: these sources must

be from different concurrent components of concurrent state.

• Transitions can enter multiple targets, in different concurrent

components of concurrent state.

• Such multi-transitions are written with vertical bar joining

exit/entry arrows.

23

Page 24: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

A B

S1

S2

S3

S4

S5

E1

E2

Entering a concurrent state

24

Page 25: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

A B

S1

S2

S3

S4

S5

C

S6

S7

t

t"

State machine with strange transitions

25

Page 26: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Transition Priorities

• If event has transitions both from substate of a state and from

state itself, substate transition has higher priority and will fire

if event occurs when substate is occupied.

• In following diagram, transition from A2 has priority for event

e when object is in state A2.

26

Page 27: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

A

A1

A2

A3

S1

S2

e

e

Transition priority example

27

Page 28: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

History States

• History states ‘remember’ what substate of a composite state

was last occupied.

• History state written as an H within a circle.

• Transition to circle actually goes to the substate of closest

enclosing (non-concurrent) composite state which was last

occupied when composite state last exited.

• If composite state not previously occupied, default substate of

composite state is entered instead.

Sequence of events α; β; γ puts system into state S2:

28

Page 29: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

HS1

S2

S3

S4

α

β

γ δ

History state example

29

Page 30: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Example of refactoring of state machine: Marriage

Refactoring means re-writing model to be simpler, retaining same

meaning.

• Consider problem of specifying history of a person’s marital

state over time.

• Identify states single, married , divorced , separated , widowed ,

and events marry , divorce, separate, partnerDies, etc.

• Simplify diagram by grouping similar states together. States

s1, ..., sn can be grouped if have identical outgoing transitions

(same events, guards, target states) to states not in group.

30

Page 31: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Single1[age < 16]

Single2

Married

Separated

Divorced

Widowedbirth

birthday[ age = 15]

birthday[age < 15]

death

death death

death

death

death

marry partner Dies

marry

marry

separate

reunite

partner Dies

divorcedivorce

[age >= 16]

Marital status state machine

31

Page 32: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Single1[age < 16]

birthday[age < 15]

Married

Widowed

Single2

Separated

Divorced

age = 15]birthday[

marry

separate divorce

partner dies

partner dies

divorce

death

AliveMarriageable

[age >= 16]

birth

reunite

Marital status state machine: marriageable states grouped

32

Page 33: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Improved marriage state machine

• birthday event has transitions on every state, only transitions

on states where event may change the state are shown.

• Class description for persons shows rest of birthday behaviour.

33

Page 34: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Person

age: int = 0

birthday()marry()separate()divorce()reunite()partnerDies()

pre: truepost: age = age@pre + 1

Person class

34

Page 35: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Behaviour state machines

• Define processing of operation, or behaviour of an active class.

• Transitions do not have explicit triggers – take place as soon as

source state is completed. May have timeout triggers after(t).

• Guards can overlap – designer must ensure behaviour is

sensible if non-deterministic.

Eg., bubblesort to sort values in variables a, b, c in increasing order:

35

Page 36: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

[a > b]/swapab()

[b > c]/swapbc()

[a <= b & b <= c]Sorting

Sorted

State machine for bubblesort operation

36

Page 37: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

RedAmber

Green RedAmber

after(60s)

after(20s)

after(20s)

after(60s)

State machine for timed traffic light

37

Page 38: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Interaction diagrams

• Interaction diagrams show examples (scenarios) of particular

use cases.

• Two forms of interaction diagram in UML: Sequence diagrams

emphasise timing and history of objects. Sequence diagrams

relate interaction to state machine models.

• Collaboration diagrams emphasise connection to class diagram.

Show an interaction as set of messages sent between objects.

38

Page 39: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

1:withdraw(d)

[ok = true] 3: deduct(d)

2: ok := checkBalance(d)

p1: Person a1: Account

b1: Bank

Example collaboration diagram

39

Page 40: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Collaboration Diagrams

• Objects represented by rectangles containing name/identifier of

object, name of its class, all underlined: object: Class.

• Objects created during operation execution noted as {new}.

• Links (instances of associations) can have arrows indicating

navigability: if message is sent from object a of class A to

object b of class B then a must have access to b, eg, by

association from A to B .

• Messages numbered consecutively starting at 1, nesting

indicated by suffixes: eg: 3.1.4 follows 3.1.3 within

subprocedure 3.1.

• Conditions given in square brackets: message only sent if

condition is true.

40

Page 41: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Sequence Diagrams

Consist of:

• Object lifelines – vertical dashed lines.

• Vertical rectangles indicate activity of object (usually an

execution of an operation on object), duration of activity.

• Arrows from one lifeline to another represent messages, eg.,

operation invocations or signals.

Show scenarios of system behaviour + object interactions.

41

Page 42: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

p1: Person a1: Account b1: Bank

withdraw(d)

deduct(d)[ok = true]

Balance(d)ok := check

Example sequence diagram

42

Page 43: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

message

call)

Message return(return fromoperation call)

message (operationSynchronous

Asynchronous

Message types

43

Page 44: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Sequence diagrams

• Interactions describe intended scenarios of behaviour of system,

different cases of execution of use cases.

• In requirements analysis, show variations of behaviours of a use

case.

Figure shows typical interaction for use case which queries some

data and produces result to GUI.

44

Page 45: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

fc: SystemFunctionsgui: SystemGUI

op()

oplookupData

op_return()

db: DataRepository

lookupData_return()

lookupData()

Use case interaction model

45

Page 46: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Activities

• Graphical description of program control flow + processing

blocks

• Can show sequencing, choice, iteration, parallel execution

(multiple concurrent threads)

• Any well-structured activity can be expressed instead as a state

machine, but activity notation is simpler.

46

Page 47: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Activity Diagrams

Visually describe behaviour composed of collections of actions (eg.,

algorithms of operations, or workflows of business processes).

Activity: specification of behaviour as coordinated sequencing of

subordinate units whose individual elements are actions.

Action: represents single step within an activity, that is, one that

is not further decomposed. An action may be complex in its

effect and not atomic.

Activities are generalisation of programming constructs such as

sequencing, conditionals and loops.

47

Page 48: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Activity diagrams

Consist of:

• Actions (state boxes)

• Arrowed lines denote control flows (sequencing of actions)

• choice points branching and joining (diamonds)

• parallel flows (starting and ending at vertical bars).

Parallel flows mean separate threads of control.

48

Page 49: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

s1

s2

s3

[E]

[not(E)]

s1

s2

s3

s3

s3

s2

s2

s1

s1

Decision node Merge node

Fork node Join node

Activity diagram notations

49

Page 50: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

RegisterProblem

EvaluateImpact

RevisePlan

FixProblem

TestFix

ReleaseFix

[priority = 1]

[else]

Handle problem workflow

50

Page 51: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

RegisterProblem

FixProblem

TestFix

ReleaseFix

CheckPriority

EvaluateImpact

RevisePlan

[priority=1] [priority /= 1]

Handle problem workflow, as behaviour state machine

51

Page 52: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Activities

Structured activities can also be expressed as pseudocode:

• if E then S1 else S2

• while E do S

• S1 ; S2

• for x : s do S

• S1 parallel S2

• execute E

52

Page 53: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Handle problem workflow as statement

registerProblem() ;

( ( if priority = 1

then evaluateImpact() ; revisePlan()

else skip ) parallel

( fixProblem() ; testFix() ) ) ;

releaseFix()

More direct to derive code from activity diagrams, compared to

from state machines.

53

Page 54: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Use case activity example: addCustomerAccount

addCustomerAccount(cId: String, aId: String)

activity:

if cId /: Customer->collect(customerId)

then

"No customer with cId"->display()

else if aId /: Account->collect(accountId)

then

"No account with aId"->display()

else

( c : Customer := Customer[cId] ;

a : Account := Account[aId] ;

if a : c.accounts

then "Account already in customer’s accounts"->display()

else

execute a : c.accounts )

54

Page 55: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Operation activity example: gcd

For gcd, there is well-known recursive computation:

static query gcd(x: int, y: int): int

pre: x >= 0 & y >= 0

post:

(x = 0 => result = y) &

(y = 0 => result = x) &

(x = y => result = x) &

(x < y => result = gcd(x, y mod x)) &

(y < x => result = gcd(x mod y, y))

But inefficient for large x, y. Instead, define explicit algorithm:

static query gcd(x: int, y: int): int

pre: x >= 0 & y >= 0

post: true

activity:

l : int ; k : int ; l := x ; k := y ;

55

Page 56: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

while l /= 0 & k /= 0 & l /= k

do

if l < k then k := k mod l

else l := l mod k ;

if l = 0 then result := k

else result := l ;

return result

Language-independent, can be directly mapped to C, Java, etc.

56

Page 57: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

General specification guidelines

• Define system by its declarative specification (eg., class

invariants, operation + use case pre/postconditions).

• Make this as complete and thorough as possible.

• Make specification as clear + efficient as possible.

• Operation activities can be derived from declarative

constraints.

Primary difference between specification models and

design models is that specifications specify what system

should do, and designs define how system carries out its

functions.

57

Page 58: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Summary of Parts 2 and 3

UML notations provide comprehensive notations to define

specifications:

• Use cases describe functional requirements

• Class diagrams define data of system

• OCL allows expression of detailed functionality and data

properties

• State machines + activities can precisely describe behaviour

• Sequence diagrams can give examples of behaviour.

58

Page 59: State machines/statecharts Collaboration diagrams Sequence ... · State machines/statecharts Collaboration diagrams Sequence diagrams Activity ... Transition from superstate Studying

Coursework: Design

• Write pseudocode activities for use cases + operations, defining

steps of processing.

• Eg.:

query payout() : double

post:

result = payments->collect(amount)->sum()

59