26
Behavioral diagrams Behavioral diagrams Lecture Lecture p4 p4 T120B029 T120B029 20 20 12 12 pavasario sem. pavasario sem.

Behavioral diagrams Lecture p4 T120B029 2012 pavasario sem

Embed Size (px)

Citation preview

Behavioral diagramsBehavioral diagramsLectureLecture

p4p4T120B029T120B029

20201212 pavasario sem. pavasario sem.

2

State diagrams

• Positioning in development process• Basic definitions and symbols• Notations• Examples• Remarks• Concurrency• Events, signals, exceptions

T120B029

3

Main processes of the team assignment

requirementsanalysis

designprocess

coding testing

Used technique:UML use case diagramsUML activity diagrams

Used technique:UML component diagrams,UML class diagrams, UML state diagramsUML sequence diagrams,UML activity diagrams

Used languages:JavaC++

requirementsdocument

problem description

design document

T120B029

4

Refined design processes of the team assignment

requirementsanalysis

designprocess

coding testing

create systemarchitecture

specify componentinterfaces

develop component design

Used technique:UML class diagrams, UML state diagramsUML sequence diagrams,UML activity diagrams

Used technique:UML component diagrams

T120B029

5

State diagrams• Shows the behavior of one object of a

single class– shows all possible states of this object– shows how the object’s state changes as a

result of messages it receives– narrowly focused, fine-grained

• Other names– State transition diagram– Harel diagram (statecharts)

T120B029

6

Object states• State

= set of values that describe an object (its condition/situation) at a specific moment in time

• State is determined based on the attribute values

overdrafted

ok

Account

balance : Float states

T120B029

7

State changes (1)• States may be

changed when an event occurs

• State transition: relationship indicating a state change – atomic (i.e. non-

interruptible)

overdrafted

ok

deposit(sum) withdraw(sum)

T120B029

8

State changes (2)• Events: Messages or signals

received• Events may or may

not change the state– Self-transition

overdrafted

ok

deposit

withdraw

deposit

withdraw

T120B029

9

State diagram notation:for one state

• Activity: Can take longer and can be interrupted

• Action: Occurs quickly– “quickly” = non-interruptible– entry: an action that is performed on entry

to the state– exit: an action performed on exiting the

state

• do: an ongoing activity performed while in the state (example: display window)

• on: an action performed as a result of a specific event– Different from self-transition

State name

entry / entry-action

exit / exit-action

do / activity-Aevent1(a:T)[exp]: action1

state variable(s)

eventn(a:T)[exp]: actionn

T120B029

10

State-A State-BEvent(arguments)[condition]/action

State diagram notation:from one state to another

• Event: significant occurrence that has a location in time and space– triggers the transition– signals, calls, passing of time, change in state

• Guard condition: • Transition only occurs when guard evaluates to true• Guards of transition exiting one state are mutually

exclusive• Action: Processes considered to occur quickly and are

not interruptibleNote: Each part can be omitted!

T120B029

11

State diagram notation:special states

• Start state– No event triggers allowed – branch conditions allowed– may not remain in start state

• End state– Top level end state terminates a state machine

Initial state State-BEvent(attribute)

Start State End State

T120B029

12

Let us look at anexample of Statechart

Diagrams :Order Management

T120B029

13

State transitions for an order

Checking

do: check item

Dispatching

do: initiate delivery

Waiting

Delivered

/ get first item

Item received [ some items not in stock ]

Item received[ all items available ]

Delivered

[ All items checked && some items not in stock ]

[ All items checked && all items available ]

[ not all items checked ] / get next item

Event

Guard

Action

T120B029

14

Problem: Cancel the order

• Want to be able to cancel an order at any time

• Solutions– Transitions from every state to state

“cancelled”– Superstate and single transition

T120B029

15

Transitions to “cancelled”

Dispatching

do: initiate delivery

Waiting

Delivered

Item received[ some items not in stock ]

Item received[ all items available ]

Delivered

Cancelled

Checkingdo: check

item

[ not all items checked ] / get next item

/ get first item

[ All items checked &&some items not in stock ]

[ All items checked && all items available ]

cancelled

cancelled

cancelled

T120B029

16

State diagram notation:structuring diagrams (1)

Superstate

State-A State-BState-A State-BEvent A

Event B Event C

Composite state:Sequential substates

T120B029

17

Superstate / Substates

Dispatching

do: initiate delivery

Waiting

Delivered

Item received[ some items not in stock ]

Item received[ all items available ]

Delivered

Cancelled

Checkingdo: check

item

[ not all items checked ]/ get next item

/ get first item

[ All items checked && some items not in stock ]

[ All items checked && all items available ]

Active

cancelled

T120B029

18

State diagram notation:structuring diagrams (2)

Superstate

State-A State-BState-A State-BEvent A

Event B Event C

H

History stateStart with the last substatethat was active prior to leaving the composite state

T120B029

19

Another example:Icehockey with superstate

Normal

playing

break

Fighting

end of game

shootout

penalty

face off

time is uo [win ]

time is up [tie]

T120B029

20

Remarks (1)

• Statechart diagrams do not have to refer to classes or objects, they can also refer to subsystems etc…

• However, often describing classes is the most widely use of statechart diagrams...

T120B029

21

Remarks (2)• Only one initial state may occur

(directly) within a composite state• End state represents completion of

a composite• End state triggers transition with

composite as source

T120B029

22

Orthogonal components and concurrency

• Unrelated components of objectscombinatorial number of states

• Example: Car states– engine (started, stopped)– doors (open, closed)

• What happens when we add one component?– seat belt (fastened, open)

4 car states:started_openstarted_closedstopped_openstopped_closed

8 car states:started_open_open started_open_fastenedstarted_closed_open started_closed_fastenedstopped_open_open stopped_open_fastenedstopped_closed_open stopped_closed_fastened

T120B029

23

Example: Payment authorization in class

Order

Authorizing

do: check payment

Authorized

Delivered

Rejected

[ payment ok ]

[ payment not ok ]2 parallel processes:- authorization- order handling

T120B029

24

Concurrent state diagram for the class

Order

Checking

Waiting

Dispatching

Authorizing Authorized

Checking

Waiting

Dispatching

Authorizing Authorized

Cancelled

Delivered

Rejected

T120B029

25

Rules of thumb

• Not every class needs a state diagram• Often: State diagram not very complex• State diagrams are often used for UI

and control objects• Not too many concurrent sets of

behavior occurring in a single object (in that case: split into separate objects)

T120B029

26

Proposed Exercises• Define a statechart diagram for

– a hockey game– an elevator– a soccer game– …

T120B029