45
UML Discussion on State Machines

UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Embed Size (px)

Citation preview

Page 1: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

UML

Discussion on State Machines

Page 2: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Perfectly static system is intensely uninteresting

Because nothing ever happens

Page 3: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Real Systems

Dynamic in nature and always something is happening internally or externally.

ATM Machine.

Robot.

Chemical Reaction.

Network Router.

Page 4: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Objects

Objects work together to realize higher-order behaviors defined by use cases.

Object Behavior with respect to time Simple State Continuous

Page 5: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

What is State Machine

Its abstract

It has finite state of conditions of existence called states

It has set of actions performed in each state

It has set of events which causes state change to well defined rule set.

Page 6: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State Machine Example

Possible States

RED

YELLOW

GREEN

Flashing YellowFlashing Red …

Page 7: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

What things exhibit state ?

Classes

Use cases

State Machine can be applied to object oriented systems as well as functionally decomposed ones.

Page 8: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Use Cases State MachinesCollaborations

Common Behavior

Activity Graphs

Actions

Behavioral Elements

Page 9: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Pseudostate

kind : PseudostateKind

SimpleState

Sy nchState

bound : UnlimitedInteger

StubState

ref erenceState : Name

FinalStateCompositeState

SubmachineState

ModelElement

(from Core)

Guard

expression : BooleanExpression

Event

StateVertex

0..*

0..1

+subv ertex

0..*

+container

0..1

StateMachine

*

1

*

+submachine

1

*

0..1

+behav ior *

+context 0..1

State

0..*

0..*

0..* +def errableEv ent

0..*

1

0..1

+top1

0..1

Procedure

(f rom Common Behav ior)0..1

0..1

0..1 +doActiv ity

0..10..1

0..1

0..1 +exit

0..10..1 0..10..1

+entry

0..1

Transition

1

0..1

1

+guard0..1

0..1

*

+trigger0..1

*1 *

+source

1

+outgoing

*

1 *

+target

1

+incoming

*

*

0..1

+transitions*

0..1

*

0..1

+internalTransition*

0..1

0..1

0..1

+ef f ect0..1

0..1

State

Transition

State Machine Guard

Event

Page 10: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens
Page 11: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State Machine

Using state machine you can Model behavior of individual object.

State Machine illustrates sequential behavior of object. Statechart Diagrams.

State to State. Activity Diagrams.

Activity to Activity.

Page 12: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Example

Payment Authorization

Authorizing

Authorized

Delivered

Rejected

Page 13: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Web Application Not yet logged in.

Action: Display the login page

Logged in and viewing the account balance

Display the account balance

Display logout button, and view transaction button.

Logged in and viewing recent transactions

Display recent transactions

Display logout button, and view account balance button.

Logged out

Display “Goodbye page”

Page 14: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens
Page 15: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Validate Login/PIN

Page 16: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Word Processing

Page 17: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Brew Controller of coffee machine

Page 18: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Coffee Machine Tank

Page 19: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Sequential Sub states

Page 20: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Concurrent Substates

Page 21: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Food For Thought….

Identify States

Page 22: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Hypothetical State Machine

Event Queue When event is received it is placed in

queue of target

Event Dispatcher When delivered to state machine

Event Processor Consumed and event is complete

Page 23: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State Machine Concepts

Context An association to the model element

that whose behavior is specified by this state machine

States

Transitions

Event

Page 24: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State Machine Concepts

Context

States models a situation during which

some (usually implicit) invariant condition holds.

Transitions

Event

Page 25: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State

Simple State No SubstatesInitial State Object comes into beingFinal State The entire state machine has completed A final state cannot have any outgoing

transitions

Page 26: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State Machine Concepts

Context

States

Transitions A transition is a directed relationship

between a source state and target state

Event

Page 27: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Transition

Guard

TriggerSource

Target

Effects

Page 28: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Guard

A guard is a Boolean expression that is attached to a transition as a fine-grained control over its firing. If the guard is true at that time, the

transition is enabled, otherwise, it is disabled.

Page 29: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

State Machine Concepts

Context

States

Transitions

Event An event is a specification of a type

of observable occurrence

Page 30: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

TimeEvent

when : TimeExpression

ChangeEvent

changeExpression : BooleanExpression

Operation(from Core)

CallEvent

1

*

+operation 1

+occurrence *

SignalEvent

Signal(from Common Behavior)

*

1

+occurrence *

+signal 1

Parameter(from Core)

Event

* 0..1

+parameter

*

{order...

0..1

ModelElement(from Core)

Page 31: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Events

Call Event Reception of request to synchronously

invoke operation. Creation of object

Deletion of object

Time Event Models expiration of specific deadline.

Page 32: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Events…

Signal Event Reception of asynchronous signal

Page 33: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens
Page 34: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Events…

Change Event Implicit Results as a change in some value of

object attribute

Page 35: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Practical Implementations

Mapping of Statechart to efficient code

Common Approaches Double Nested Switch Statements Action state tables Generic state machine interpreters State Patterns in OO Paradigm

Page 36: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens
Page 37: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

/* States. */ #define IDLE 0 #define DIAL 1 #define ALERTING 2 #define CONNECTED 3 #define BUSY 4 #define RINGING 5

/* Possible inputs */ #define ON_HOOK_IND 0 #define OFF_HOOK_IND 1 #define DIGITS_IND 2 #define CONNECTED_IND 3 #define INCOMING_CALL_IND 4 #define TIMEOUT_IND 5 #define PICKUP_IND 6

Page 38: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

int transtab[][] = /* transtab[Input][CurrentState] => NextState */ { /* IDLE DIAL ALERTING CONNECTED BUSY RINGING */

{ IDLE, IDLE, BUSY, IDLE, IDLE, IDLE }, /* ON_HOOK_IND */ { DIAL, DIAL, BUSY, IDLE, IDLE, IDLE }, /* OFF_HOOK_IND */ { IDLE, ALERTING, IDLE, IDLE, IDLE, IDLE }, /* DIGITS_IND */ { IDLE, DIAL, ALERTING, IDLE, IDLE, IDLE }, /* CONNECTED_IND */ { RINGING, DIAL, IDLE, CONNECTED, BUSY, RINGING }, /* INCOMING_CALL_IND */ { IDLE, DIAL, BUSY, IDLE, IDLE, IDLE }, /* TIMEOUT_IND */ { IDLE, DIAL, BUSY, IDLE, IDLE, CONNECTED }, /* PICKUP_IND */ };

curstate = transtab[event][curstate];

int curstate = IDLE;

Page 39: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Who defines state transitions ?

May Define StateMachine

Page 40: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Who defines statemachine..

Context

States Themselves

State Tables

Page 41: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Guidelines

Create A State Chart When Behavior Differs Based on State

Place The Initial State In The Top-Left Corner

Place The Final State In The Bottom-Right Corner

State Names Should be Simple but Descriptive

Question “Black Hole” States

Question “Miracle” States

Create a Hierarchy of State Charts for Very Complex Entities

Name Transition Events in Past Tense

Page 42: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

What Next

Identify the implementation schemes

Use XMI

Aim for changes in statemachine which will need no or minimal changes to existing state implementations

Page 43: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens

Referencehttp://www.uml.org

Page 44: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens
Page 45: UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens