smith’s aerospace © p. bailey & k. vander linden, 2006 state behavior patrick bailey keith...

17
Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

Upload: frederick-leonard

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006

State Behavior

Patrick BaileyKeith Vander LindenCalvin College

Page 2: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

2

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

State Behavior● Objects can display:

– Reactive, state-independent behavior

– Discrete, state-dependent behavior

– Continuous behavior

● UML models this intra-object behavior using State Machine diagrams.

Page 3: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

3

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

State Machine Diagrams● State diagrams can be specified for

classes, use cases, or other systems.● They specify object behavior over a

number of use cases.● Key elements:

– States– Transitions

Page 4: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

4

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Example: State Machine Diagram

Off

Open

Running

Washing

op_tick/dec_wash_time;

Rinsing

[isWashed]

op_tick/dec_rinse_time; Drying

[isRinsed]

op_tick/dec_dry_time;

op_close

op_open

op_start/setup;

op_tick/dec_wash_time;

[isWashed]

op_tick/dec_rinse_time;

[isRinsed]

[isDried]

op_tick/dec_dry_time;

Done

Done

Example from www.ilogix.com, August, 2005

Page 5: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

5

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Outline● History● State Diagrams● Implementing State Transitions● Using State Diagrams

Page 6: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

6

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

● Developed statecharts, a higraph-based amalgam of graphs and Venn diagrams.

● Statecharts are the basis of UML state machine diagrams.

David Harel State Charts

Image from www.wisdom.weizmann.ac.il, August, 2005

Harel’s original digital watch state chart

Page 7: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

7

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

States● Use states to represent conditions

of objects in time.

● Internal Activities:– On Entry– On Exit– Internal Transition– Do Activities

op_close

[isDried]

op_tick/dec_dry_time;

Washing

ada.text_io.put_line("Enter washing");ada.text_io.put_line("Exit washing");

op_tick/dec_wash_time;

[isDried]

op_tick/dec_dry_time;

op_tick/dec_wash_time;

Page 8: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

8

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Transitions● Transitions indicate

movement from one state to another.

● Syntax:

event-name ( parameter-list ) [ guard-expression ] / action-list

op_close

Rinsing

op_tick/dec_rinse_time;

Washing

op_tick/dec_wash_time;

[isWashed]

[isDried]

op_tick/dec_dry_time;

[isRinsed]

op_tick/dec_rinse_time;

op_tick/dec_wash_time;

[isWashed]

op_tick/dec_dry_time;

[isRinsed]

Page 9: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

9

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Transition Events● Events that can trigger transitions

include:– Signal events

– Call events

– Time events

– Change events

● Transitions without events and guards are followed immediately.

Page 10: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

10

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Super-States Use super-

states to represent sets of states that share common transitions and activities.

Running

Washing

op_tick/dec_wash_time;

Rinsing

[isWashed]

op_tick/dec_rinse_time; Drying

[isRinsed]

op_tick/dec_dry_time;

op_close

op_open

op_start/setup;

op_tick/dec_wash_time;

[isWashed]

op_tick/dec_rinse_time;

[isRinsed]

[isDried]

op_tick/dec_dry_time;

Done

Page 11: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

11

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Action Execution Order● Actions are executed in this basic

order:– Exit actions– Transition actions– Entry actions

● In the context of super/sub-states, outer actions are executed before inner actions.

Page 12: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

12

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

And States & Or States● We’ve already seen Or states.● And states represent orthogonal

state spaces.

Example from Fowler, M., UML Distilled, 2004.

Page 13: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

13

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

ExampleActiveDishwasher

Open

Done

Done

Off

Running

op_close

Rinsing

op_tick/dec_rinse_time;

Washing

op_tick/dec_wash_time;

[isWashed]

Drying

[isDried]/GEN(ev_stop)

op_tick/dec_dry_time;

[isRinsed]

op_open

op_start/setup;

op_tick/dec_rinse_time;

op_tick/dec_wash_time;

[isWashed]

op_tick/dec_dry_time;

[isRinsed]

op_close

[isDried]/GEN(ev_stop)

op_open

op_start/setup;

op_tick/dec_rinse_time;

op_tick/dec_wash_time;

[isWashed]

op_tick/dec_dry_time;

[isRinsed]

DisplayingWashTIme

DisplayingCurrentTIme

ev_stop op_startev_stop op_start

op_close

[isDried]/GEN(ev_stop)

op_open

op_start/setup;

op_tick/dec_rinse_time;

op_tick/dec_wash_time;

[isWashed]

op_tick/dec_dry_time;

[isRinsed]

ev_stop op_start

Page 14: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

14

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Pseudo-States

Pseudo-states are state-like constructions that represent a variety of things, including:– Conditions– History– Termination– Branching varieties

Page 15: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

15

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Example

Page 16: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

16

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Implementing State Diagrams Code generators can implement

state behavior in several ways:– Nested switch statements

– State pattern

– State table

Page 17: Smith’s Aerospace © P. Bailey & K. Vander Linden, 2006 State Behavior Patrick Bailey Keith Vander Linden Calvin College

17

Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005

Using State Diagrams● State diagrams represent intra-object

behavior.● Combine them with other diagrams

to represent inter-object behavior.● Use them only for objects with

“interesting” state-based behavior.● Take care to avoid:

– Ambiguous specifications– Race conditions