State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An...

17
State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeS CRIPT R State Transition

Transcript of State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An...

Page 1: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

State Transition Diagram& UML’s enhancements

• Another “tool” for depicting the Design

• An integral part of the DeSCRIPTR

– State– Transition

Page 2: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

State Transition Diagram

– A state transition diagram is used to depict:

1. The states of an entity2. The transitions of states of the entity3. The trigger or the event that caused the transition of state of

the entity

• The entity may be a physical device such as a light switch or a vending machine; it may be a software system or component such as a word processor or an operating system; it may be a biological system such as a cell or a human; or - - - -

This modeling technique came from a more formal area - automata theory and switching theory. State transition diagram depicts a Finite State Machine.

Page 3: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Software Program View

• The end product of a software development is a program which executes. In depicting the program (or an object) we can consider:

– Variables which take on different values– Control structure and assignment statements

(events) in the program that change the values of the variables; but “little” is said about how the control structure or the statements work

1. Combination of values of the data (variables & constants) at any point of the program represent the program state at that point.

2. The change made to the values of the variables through assignment statements represent a transition of state

Page 4: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

A very simple example light switch (page 368 of your text)

Alt

switch light

[l_st=on]

[l_st=off]

TurnOff ( )

TurnOn ( )

From State(light)

To State(light)

Event(switch)

on turnOff off

off turnOn on

1. “Sequence diagram” (alternative fragment) for switch and light interaction

2. “State transition table”for light with switch events

lighton

lightoff

turnOff

turnOn

3. “State transition diagram”for light with switch events

Page 5: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

A little “more” on the light switch

off

turnOff

turnOn

onoff

turnOff

turnOn

on

What happens if we turn ona light that is already on?

turnOn

turnOff

state can “transition” to itscurrent state

Page 6: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Using State Transition Diagram

• Model the entity at the “abstraction” level where the number of states is “manageable.”

1. List (design) the states (should not be large)2. List (design) events that will trigger the state transition

(should not be big)3. There must be a starting state4. There must be a terminating state or states5. Design the transition rules (the bulk of your design work

is thinking through the transition rules)

1.The above is not necessarily performed in sequence; iterate through these.

2. Even with a modest number of states and events, the state transitiondiagram, which really depicts the transition rules, can be enormous.

Page 7: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Designing the Voice Recorder(similar to example on page 397 of text)

• For the entity, voice recorder, design the states of this entity:– How many states does ( somewhat based on requirement ) a voice

recorder have?• Set of recorder states = { off, on, play, record, erase, stop, error-msg }

– What are the initial and terminating states?• Initial state = {off}• Terminating state = {off}

• List the events that will change the state of the entity: – What are the events that will change the recorder’s states?

• “Input signals” = { 1, 2, 3, 4, 5, 6 } which corresponds to: {off, on, play, record, erase, stop}

Note: no “rewind” “go-back” state

Page 8: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Designing the Voice Recorder(example form page 397 of text) – cont.

• Design the transition rules for the voice recorder

from state to stateeventsoff 2 on

on 1 off

on 3 play

erase 6 stop

on 4 record

on 5 erase

play 6 stop

record 6 stop

stop 3 play

stop 4 record

stop 5 erase

stop 1 off

More transition rules:

1. Any state transition not describedhere will transition to error-msg state

2. once in error-msg state, the system“displays a message” andautomatically transitions to stop state.

off 1 off

Events : {1=off, 2=on, 3=play, 4=record, 5=erase, 6=stop}

Page 9: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

State Transition Diagram ofVoice Recorder

On

play

erase

record

stop

Error-msg

2

1

1

3 5

4

6

66

3

5

[always true]

1

4

Couple things to note:

1. All non-specified events for all the states go to “Error-msg” state - - - is that o.k?2. What happens when a signal of 6 comes in when in state “ON” - - - error message?3. How do you specify “display error message” in Error-msg state ?4. How do you specify “Starting” and “Terminating” state ?

Off

{1=off, 2=on, 3=play, 4=record, 5=erase, 6=stop}

Page 10: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Finite State Machine (more “classical” usage)

S1 S2

S3S4

0

0

0

0 1

111

“Formal” Definition of M1

M1 = {event, States, T, I, Term}where: event = {0, 1} States = {s1, s2, s3, s4} T = {(s1,0)-> s2; (s1,1) -> s4; -----} I = s1 Term = s1

Pseudo code for the design of M1:

Read input string;If input string includes non - 0’s or 1’s, error msg; break;if input string length is odd number, error msg; break;Set state = s1;While (the string is non-empty) { ele = first element from the string; delete the first element from the string; if (state = s1 & ele = 0) then state = s2; if (state = s1 & ele = 1) then state = s4; if (state = s2 & ele = 0) then state = s1; if (state = s2 & ele = 1) then state = s3; if (state = s3 & ele = 0) then state = s4; if (state = s3 & ele = 1) then state = s2; if (state = s4 & ele = 0) then state = s3; if (state = s4 & ele = 1) then state = s1; } if (state = s1) string is accepted else error msg ; end

M1 is a string checker that “accepts” all strings that contain even number of 0’s and 1’s

State Transition diagram of M1

Page 11: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

UML’s “improvements” on State Transition Diagrams

• Event may be expressed with a string:

event-signature [guard] / action-expression

• State symbol may contain several compartments:• Name• Processing activities in the form of :

• Action-label / Action-expressions• Action-labels:

• Entry – specifies activities upon state entry• Exit – specifies activities upon state exit• Do – specifies activities to be performed after entry but before exit• Include – names another state diagram to be used for “nested” state

transition diagram for further refinement • Action-expression specifies the activities to be performed.

• Instead of include, there may be a “stubbed” state which can be expanded later.

• The state diagram may contain another state diagram (nested diagram) in its compartment

Page 12: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

UML State Transition Composite Diagram Example(cruise control system - similar to page 401 of text)

off

onBtnclick offBtnclick

On

pedalSpeed

entry / spLock:= undefined

setSpeed

entry/ spLock := current speeddo / lockState := on

engage pressBreak

freeSpeedentry / spLock := undefineddo / lockState:= offexit / set offBtnclick

Note:1. the startand terminatingstates 2. When entering acomposite diagram, the start state must beclearly defined.3. How we exit this composite diagram fromthe freeSpeed state andactually enter “off” state

Page 13: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Some Deeper (Advanced) UML State Diagrams

• Concurrent Composite State Diagram contains two or more composite state diagrams that may be executed in parallel; state diagram is divided up into compartments by parallel doted lines.

Concurrent Composite State

StateZ

StateW

Note:1. Transition to a concurrent composite state boundary willenter initial states of all the concurrent state diagrams

2. Or one can specify the substates which will be entered, along with the one specified start state

3. similarly exits can be specified

Event-K

Event-A

Page 14: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

More Advanced UML State Transition Diagramwith Synchronization Mechanism

SignalOperationState

EW Green

NS Red

EW RedEW Yellow

NS YellowNS Green

after 40 sec

after 45 sec

after 5 sec

after 35 sec

after 30 sec

after 5 sec

SynchronizedSignalOperation

EW Green

NS Red

EW Red

EW Yellow

NS Yellow NS Green

after 40 sec

after 45 sec

after 5 sec

after 35 sec

after 30 sec

after 5 sec

11

Synchsignalused to helpregulatetransition

Synchsignalused to helpregulatetransition

EW= east-westNS=north-south

Page 15: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Transition Junction & Compound Transition

at_home

on-lineshopping

far_awaymall

near_bystore

shopping_urge[cold=yes]

[cold=no]

[cold=extreme]

Page 16: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Advanced Mechanism to set History

RadioOn

off turnOn

turnoff

H

Preset cdmusic

Preset AMstation

Preset FMstation

setFM

setCD

setAM

The history of presetstate is“remembered” ifthe radio is turnedon after it isturned off

History state, H, says re-enter the state thatwas last active

Page 17: State Transition Diagram & UML’s enhancements Another “tool” for depicting the Design An integral part of the DeSCRIPTR –State –Transition.

Read at least one example from the text on your own.

(Especially the Dialog Map and UI diagram)