29
State Diagrams Fowler, Chapter 10

State Diagrams

Embed Size (px)

Citation preview

State DiagramsFowler, Chapter 10

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 1

Announcements

Homework 2 due tomorrow at NoonHomework 3 on implementation and testing will be handed out next week

Homework 3 due date extended by one week to November 18th

Tutorial 3 starts next week.

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 2

We Will Cover

State Machines An alternate way of capturing scenarios Large classes of scenarios

Syntax and SemanticsWhen to use state machines

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 3

Events, Conditions, and StatesEvent

Something that happens at a point in time Operator presses self-test button The alarm goes off

Condition Something that has a duration The fuel level is high The alarm is on

State An abstraction of the attributes and links of an object (or entire

system) The controller is in the state self-test after the self-test button

has been pressed and the rest-button has not yet been pressed

The tank is in the state too-low when the fuel level has been below level-low for alarm-threshold seconds

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 4

Making a Phone Call Scenario

To make a call, the caller lifts receiver. The caller gets a dial dial tone and the caller dials digit (x). The dial tone ends. the caller completes dialing the number. The callee phone begins ringing at the same time a ringing begins in caller phone. When the callee answers the called phone stops ringing and ringing ends in caller phone. The phones are now connected. The caller hangs up and the phones are disconnected. The callee hangs up.

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 5

Partial Class Diagram

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 6

L i n e P h o n e1 . . 1

1 . . 1

C a l l e r

1 . . 1

1 . . 1

C a l l e e

Event Trace

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 7

Caller CalleeLine

caller lifts receiver

dial tone begins

dials digit (4)

dial tone ends

dials digit (2)

dials digit (3)

dials digit (4)

dials digit (5)

phone ringsringing tone

callee answers

ringing stopstone stops

phones connected phones connected

callee hangs up

phones disconnected phones disconnected

caller hangs up

State Diagram for Scenario

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 8

off-hook

digit(x)

valid-number

called-phone-answers

called-phone-hangs-up

digit(x)

on-hook Idle

Dial tone

Ringing

Connected

Disconnected

Dialing

Scenario 2

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 9

Caller CalleeLine

caller lifts receiver

dial tone begins

dials digit (4)

dial tone ends

dials digit (2)

dials digit (3)

dials digit (4)

dials digit (5)

busy tone

caller hangs up

Modified State Machine

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 10

Idle

off-hook

Connecting

Busy tone

digit(x)

valid-number

called-phone-answers

called-phone-hangs-up

digit(x)

on-hook

routed

number-busy

Dial tone

Ringing

Connected

Disconnected

Dialing

ConditionsSometimes the state transitions are conditional

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 11

Idle

off-hook

Busy tone

digit(x)

valid-number

digit(x) [x = 8]

on-hook

routed

number-busy

digit(x) [x != 8]

Dialing

digit(x) digit(x)

valid-number

Dial tone

Dialing

Connecting

Ringing

Dial tone(external)

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 12

Idle

off-hook

Connecting

Busy tone

digit(x)

valid-number

called-phone-answers / connect line

called-phone-hangs-up / disconnect line

digit(x)

on-hook

routed

number-busy

do/ find connection

do/ busy tone

on-hook / disconnect line

on-hook

on-hook

on-hook

Actions are performed when a transition is taken or performed while in a state

Actions are terminated when leaving the state

on-hook

on-hook

Operations (AKA Actions)

Dial tone

Dialing

Ringing

Connected

Disconnected

do/ sound dial tone

do/ ring bell

Transition label: trigger-event [guard]/activity

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 13

Group states with similar characteristics

Enables information hiding

Simplifies the diagrams

Dial tone

on-hook

dial(x) [x is a digit]

do/ sound dial tone

Voice Mail

dial(x) [x = *]

Connecting

valid-number

do/ find connectionBusy tone

Ringing

Connected

Disconnected

do/ ring bell

dial(x)

routed

called-phone-answers / connect line

called-phone-hangs-up / disconnect line

on-hook

on-hook / disconnect line

off-hook

on-hook

Hierarchical State Machines

Idle

Make Call

Dialing

do/ busy tone

Establish call

number-busy

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 14

Dial tone

on-hook

dial(x) [x is a digit]

do/ sound dial tone

Voice Mail

Make Call

dial(x) [x = *]

Connected

Disconnected

Establish call

called-phone-answers / connect line

called-phone-hangs-up / disconnect line

on-hook

on-hook /disconnect line

on-hook

off-hook

Connecting

valid-number

do/ find connectionBusy tone

Ringingdo/ ring bell

do/ busy tone

Establish calldial(x)

routed

number-busy

Information Hiding

Dialing

Idle

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 15

Alarms EnabledVisual Alarm

Aural Alarm

On

On

Off

Off

reset

reset

aural-on

visual-on

Alarms Disabled

out-of-bounds-event

Concurrency

Some states represent several concurrent concepts

Concurrency is supported by the state machines

Concurrent state machines are separated by dashed lines

State Machines - Summary

Events instances in time

Conditions conditions over time

States abstraction of the attributes

and associationsTransitions

Takes the state machine from one state to the next

Triggered by events Guarded by conditions Cause actions to happen

Internal actions something performed in a

stateHierarchies

allows abstraction and information hiding

Parallelism models concurrent concepts

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 16

When to use State Machines

When you want to describe the behavior of one object for all (or at least many) scenarios that effect that object

Not good at showing the interaction between objects Use interaction diagrams or activity diagrams

Do not use them for all classes Some methods prescribe this (I would) Very time consuming and questionable benefit

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 17

Coming up with the State Diagrams

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 18

Modeling Approach

Prepare scenarios Work with the customer Start with normal scenarios Add abnormal scenarios

Identify events (often messages) Group into event classes

Draw some sequence diagrams Find objects with complex functionality you want

to understand betterBuild a state diagram for the complex classes

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 19

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 20

RoomFuelValveController

open-valve

BurnerWaterPump

start-burner

pump-on

request-temp

respond-tempEvery 5s

Temp Low

request-temp

respond-temp

Temp Normal

Every 5s

close-valve

stop-burner

pump-off

open-water-valve

close-water-valve

Scenario-1

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 21

RoomFuelValveController

open-valve

BurnerWaterPump

start-burner

pump-on

request-temp

respond-tempEvery 5s

Desired temp change

Temp Normal

Every 5s

close-valve

stop-burner

pump-off

ControlPanel

request-temp

respond-temp

request-temp

respond-tempEvery 5s

Temp Low

desired-temp-change

open-water-valve

close-water-valve

Scenario-2

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 22

Water Pump pump-on

OffOn

pump-off

Burner start-burner

OffOn

stop-burner

Fuel Valve open-valve

ClosedOpen

close-valve

Dynamic Model

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 23

Room

open-water-valve/wv-open

Idle

Valve

close-water-valve/wv-close Processing

Request

Temp-Sensor

request-temp temp-report(x)/respond-temp(x)

Water-Valve

More Dynamic Model

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 24

Room

open-water-valve/wv-open

Idle

Valve

close-water-valve/wv-close

ProcessingRequest

Temp-Sensor

request-temp temp-report(x)/respond-temp(x)

Water-Valve

More Dynamic Model

Even More Dynamic Model

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 25

Controller

timeout(5s)\request-temp Temp-Low

timeout(1s)/start-burnerHome Heating System

Temperature

Temp-Normal

All-Running

Burner-On Fuel-Open

Water-Off Fuel-Off

All-Off

respond-temp(x)[x<desired-temp-2]/start-heating

respond-temp(x)[x>desired-temp+2]/stop-heating

timeout(5s)\request-temp

timeout(1s)/pump-on,open-water-valve

start-heating/open-valve

timeout(1s)/stop-burner

timeout(1s)/close-valvestop-heating/pump-off,close-water-valve

Identify Key Operations

Operations from the object model Accessing and setting attributes and associations

(often not shown)Operations from events All events represent some operation

● Operations from actions and activitiesActions and activities represent some processing activity within some objectOperations from functions Each function typically represent one or more

operationsShopping list operations Inherent operations (what should be there)

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 26

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 27

Complete OO Model

open-valve()close-valve()room-temp()

Room

Home HeatingSystem

setting

On-Off Switch

desired-temp

Thermostat

Operator

Water Valve

temperature

Temp Sensor

Furnace

Control ler

Pushes Adjusts

Notifies

Monitor

Heats

Ignites Opens/Closes

Runs

1..*

1..*

1..*

operating()target-temp()

Control Panel

on()off()

Water Pump

open()close()

Fuel Valve

on()off()

Burner

Iterate the Model

Keep on doing this until you, your customer, and your engineers are happy with the model

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 28

Iterate

We Have Learned

State machines are used to capture classes of scenarios The consist of states, transitions, events, and

guarding conditions Activities can be performed in a state

How to find operations (methods) from the various models

Fall 2013 CSci 5801 - Dr. Mats Heimdahl 29