5. state diagrams

27
Slide 1 Object-Oriented Software Systems Engineering – Chapter 5 State Diagrams Chapter 5

Transcript of 5. state diagrams

Page 1: 5. state diagrams

Slide 1Object-Oriented Software Systems Engineering – Chapter 5

State Diagrams

Chapter 5

Page 2: 5. state diagrams

Slide 2Object-Oriented Software Systems Engineering – Chapter 5

Objectives

In this chapter we will: Introduce the terms used with respect to state

diagrams Discuss the context in which state diagrams are

used Introduce substates Discuss concurrent state diagrams

Page 3: 5. state diagrams

Slide 3Object-Oriented Software Systems Engineering – Chapter 5

Statechart Diagrams

State diagrams describe the life of an object using three main elements:

States of an objectTransitions between statesEvents that trigger the transitions

A state diagram or statechart specifies a state machine

A state machine is described for a classEach object has it’s own state machine

Page 4: 5. state diagrams

Slide 4Object-Oriented Software Systems Engineering – Chapter 5

Why Use Statechart Diagrams?

Statecharts typically are used to describe state-dependent behaviour for an object

An object responds differently to the same event depending on what state it is in

Usually applied to objects but can be applied to any element that has behaviour

Actors, use cases, methods, subsystems, systems

Statecharts are typically used in conjunction with interaction diagrams (usually sequence diagrams)

A statechart describes all events (and states and transitions for a single object)

A sequence diagram describes the events for a single interaction across all objects involved

Page 5: 5. state diagrams

Slide 5Object-Oriented Software Systems Engineering – Chapter 5

States

Show what the dependencies are between the state of an object and its reactions to messages or other events

Stateis a condition or situation during the life of an object

within which it performs some activity, or waits for some events

Has a nameHas actions -- execute the stateHas internal transitions -- transitions cause no change in

a statesubstates -- the nested structure of a state involving

disjoint or concurrent substates

Page 6: 5. state diagrams

Slide 6Object-Oriented Software Systems Engineering – Chapter 5

States

For example:

entry/unlock doordo/prepare materialstelephone rings/answer telephoneinclude/lecture stateexit/lock door

At Work state nameaction performed on entry to stateactivity performed while in stateaction performed on arrival of named eventname of a sub-state machineaction performed on leaving state

Page 7: 5. state diagrams

Slide 7Object-Oriented Software Systems Engineering – Chapter 5

Initial and Final States

The initial state of a state machine is indicated with a solid circle

Known as a pseudo-state

A transition from this state will show the first real state

The final state of a state machine is shown as concentric circles

A closed loop state machine does not have a final state; the object lives until the entire system terminates

An open loop state machine represents an object that may terminate before the system terminates

Page 8: 5. state diagrams

Slide 8Object-Oriented Software Systems Engineering – Chapter 5

Initial and Final States

An example:

At Work At Homego home

go to work

die die

Page 9: 5. state diagrams

Slide 9Object-Oriented Software Systems Engineering – Chapter 5

Actions and Activities

Actionis an executable atomic computationincludes operation calls, the creation or destruction of

another object, or the sending of a signal to an objectassociated with transitions and during which an action is

not interruptible -- e.g., entry, exit

Activity is associated with statesNon-atomic or ongoing computationMay run to completion or continue indefinitelyWill be terminated by an event that causes a transition

from the state in which the activity is defined

Page 10: 5. state diagrams

Slide 10Object-Oriented Software Systems Engineering – Chapter 5

Events

An event signature is described as Event-name (comma-separated-parameter-list)

Events appear in the internal transition compartment of a state or on a transition between states

An event may be one of four typesSignal event

Corresponding to the arrival of an asynchronous message or signal

Call event Corresponding to the arrival of a procedural call to an

operationTime event Change event

Page 11: 5. state diagrams

Slide 11Object-Oriented Software Systems Engineering – Chapter 5

Events

A time event occurs after a specified time has elapsed

Event name is specified as keyword afterParameter list is an expression evaluating to a time

interval after(10 seconds after state “At Work” is entered)

No specified start time implies “since entry to the current state”

after(2 seconds)

Page 12: 5. state diagrams

Slide 12Object-Oriented Software Systems Engineering – Chapter 5

Events

A change event occurs whenever a specified condition is met

Event name is specified as keyword whenParameter list is a boolean expressionThe event occurs when both of the following conditions

are met, irrespective of the order when they happen The expression evaluates to true The object is in the required state

For example when (state = At Work) when (date = January 1 2007)

Page 13: 5. state diagrams

Slide 13Object-Oriented Software Systems Engineering – Chapter 5

Transitions

A transition is drawn as an arrow between states annotated with a transition string

The transition string denotes the event and consequent action

Only one form of arrowhead is used on statecharts The distinction between call events and signal events must be

deducted from elsewhere e.g. an interaction diagram

A transition string is described as Event-signature [guard-condition]/action-expression^object.message

If the guard condition is met the transition occurs immediately

Page 14: 5. state diagrams

Slide 14Object-Oriented Software Systems Engineering – Chapter 5

Transitions

A transition whose string contains neither an event signature nor a guard condition is said to be unlabeled

Occurs immediatelyMay still carry an action expression

Page 15: 5. state diagrams

Slide 15Object-Oriented Software Systems Engineering – Chapter 5

Transitions

A transition is triggered when its event occursIf the guard condition is met, the transition is firedIf the condition is not met the event is discarded

The guard condition is checked only once

If there is no guard condition, triggering will always cause firing

Note the distinction between a guard condition and a change event

A guard condition is evaluated once, when the associated event occurs

A change event occurs whenever its associated condition is met

Behaviour is as if the condition were being continually evaluated

Page 16: 5. state diagrams

Slide 16Object-Oriented Software Systems Engineering – Chapter 5

State Diagrams notation

PaidUnpaidpaying

Invoice created

Invoice destroyed

name

state

final stateInitial state

transition

event

Page 17: 5. state diagrams

Slide 17Object-Oriented Software Systems Engineering – Chapter 5

State Diagram Example

on loan

entry / myBkCpy.borrow()

on the shelf

entry / myBkCpy.return() borrow()

return()

on loan

exit / myBkCpy.returned()

on the shelf

exit / myBkCpy.borrowed() borrow()

return()

Entry action : any action that is marked as linked to the entry action is executed whenever the given state is entered via a transition

Exit action : any action that is marked as linked to the exit action is executed whenever the state is left via a transition

This shows the state of an object myBkCpy from a BookCopy class

Page 18: 5. state diagrams

Slide 18Object-Oriented Software Systems Engineering – Chapter 5

A Class of BookCopy

BookCopy

onShelf : Boolean value := ‘Y’ on the shelfvalue := ‘N’ on loan

return()

borrow()

action / onShelf=‘Y’

action / onShelf=‘N’

Page 19: 5. state diagrams

Slide 19Object-Oriented Software Systems Engineering – Chapter 5

State Diagram Example

Checkingdo / check Item

Dispatching

do / initiate delivery

Ordering Delivering

*[all items checked]get next item

[all items checked && some items not in stock]Order Item

[all items checked && all items available]Dispatch items

[all items a

vailable]

Item re

ceive

d

delivery

get first item

Canceling

cancelledExit/ Item receiveddo / order Item

entry / deliver Items

do / Remove Item

some itemsnot in stock

all items in stock

Page 20: 5. state diagrams

Slide 20Object-Oriented Software Systems Engineering – Chapter 5

State Diagram - Nested States

Super-state

A Bevent-1

C

event-2

A B

C

event-1

event-2event-2

Page 21: 5. state diagrams

Slide 21Object-Oriented Software Systems Engineering – Chapter 5

Checking

do / check Item

Dispatchingdo / initiate delivery

Delivering

[all items checked && some items not in stock]Order item

[all items checked && all items available]Dispatch items

[all items a

vailable]

Item re

ceive

d

delivery

get first item

Cancelingcancelled

Ordering

Exit/ Item receiveddo / order Item

*[all items checked]get next item

entry / deliver Items

do / Remove Item

State Diagram Example including substates

Page 22: 5. state diagrams

Slide 22Object-Oriented Software Systems Engineering – Chapter 5

Concurrent State models

Orthogonal Components and Concurrency shown separated by dashed line

supports concurrency Objects must be in only one state from each of the

orthogonal components

Starting

A

FinishingB

C

E

F

D G

Explicit control

branching(fork)

s

T

Page 23: 5. state diagrams

Slide 23Object-Oriented Software Systems Engineering – Chapter 5

Concurrent State Models

Three different ways for orthogonal components to Three different ways for orthogonal components to communicate:communicate:

Broadcast EventsBroadcast Events

Propogated EventsPropogated Events

IN operatorsIN operators

Page 24: 5. state diagrams

Slide 24Object-Oriented Software Systems Engineering – Chapter 5

Concurrent State ModelsConcurrent State Models

Broadcast eventsBroadcast events

events that more than one orthogonal component events that more than one orthogonal component

acceptsaccepts

For example an event T1 is sent to all active orthogonal For example an event T1 is sent to all active orthogonal

components it need not be acted on by all componentscomponents it need not be acted on by all components

what happens if component S1 is in state A, S2 is in state E what happens if component S1 is in state A, S2 is in state E

and S3 is in state G when a T1 event occurs?and S3 is in state G when a T1 event occurs?

what happens if S1 is in state A and S2 is in state D when what happens if S1 is in state A and S2 is in state D when

the event T1 occurs?the event T1 occurs?

Page 25: 5. state diagrams

Slide 25Object-Oriented Software Systems Engineering – Chapter 5

Concurrent State Models

Propagated events are indicated with the caret Propagated events are indicated with the caret

following the event name (and optional parameters following the event name (and optional parameters

and guard)and guard)

IN operators are used as a guard on transition T4. IN operators are used as a guard on transition T4.

This allows the S3 component to take the This allows the S3 component to take the

transition T4 only if S2 is currently in state Dtransition T4 only if S2 is currently in state D

Page 26: 5. state diagrams

Slide 26Object-Oriented Software Systems Engineering – Chapter 5

Concurrent State Models

A

B

C

E

F

D

G

S1

T3(a,b,c)

H

H

T1

T2^T3(x,y,z)

H

T1

S2

T4[IN(D)]

T3(a,b,c)

S3

T5

T6

S

Page 27: 5. state diagrams

Slide 27Object-Oriented Software Systems Engineering – Chapter 5

Summary

In this chapter we have: Introduced the terms used with respect to state

diagrams Discussed the context in which state diagrams are

used Introduced substates Discussed concurrent state diagrams