47
State Transition Diagram

State Transition Diagram

Embed Size (px)

Citation preview

Page 1: State Transition Diagram

State Transition Diagram

Page 2: State Transition Diagram

State Transition Diagram

• State machine visualizes an object of a class as an isolated entity

• Describes how the object communicates with the rest of the world by detecting events and responding to them

• State diagram also models the dynamic aspect of the system

• Sequence and communication also fulfill the same purpose but in a different manner

Page 3: State Transition Diagram

State Transition Diagram

• Sequence and Colloboration deals with community of objects and describe the messages passed between them

• Whereas state diagram treats the particular object in an isolated manner

• Describes all possible states that an object can have during its life cycle

• How the object responds to an event when it is in a particular state

Page 4: State Transition Diagram

State Machine – characteristic features

• An object has various states during its life cycle• The arrival of an event triggers the transition of

object from one state to another state• An object behaves different in different states

Page 5: State Transition Diagram

A generalised state machine diagram

Page 6: State Transition Diagram

Example printer

• Consider a printer is switched on• Command is given to the printer to print a document• If there are no papers loaded in the printer, it will give

a signal which will compel the computer to display a message to load paper

• If the printer is jammed, it will refuse to print, even if paper is loaded in the printer, till the problem is resolved

• If the printer is fine and paper is loaded properly, then the printouts can be taken without any problem

Page 7: State Transition Diagram

Example : CD Player

Stopped state Ready state

Playing state

Insert CD

Eject CD

Press stop buttonPress Play button

Power switch on

Power switch on

Page 8: State Transition Diagram

Stat Machine

Page 9: State Transition Diagram

Events

• A noteworthy occurrence which has consequences is called an event

• Types– Time event– Guard condition becoming true event– Change event– Call event– Signal event

Page 10: State Transition Diagram

Time event

• Time is calculated as absolute time or the passage of relative amount of time

• eg. Antivirus software to run at 1.00pm daily.– State – non scanned, under scanning

• Set the machine in locked ode if idle for 2 minutes

Page 11: State Transition Diagram

Guard condition becoming true event

• When transition occurs , condition is being tested, if it is true then the state changes

• Conditional Event– Debit if current balance is > debit amount else cancel

Page 12: State Transition Diagram

Change Event

• Change event is evaluated continuously till it becomes true.

• When it is true transition fires

Page 13: State Transition Diagram

Call event

• Call event represents the dispatch of an operation, the event may trigger a

state transition in a state machine.

• A signal is asynchronous, a call event is synchronous

• When an object invokes an operation on another object that has a state

machine, control passes from the sender to the receiver, the transition is

triggered by the event, the operation is completed, the receiver transitions to a

new state, and control returns to the sender. (Exam hall, malpractice)

Page 14: State Transition Diagram

Signal

• A signal represents a named object that is dispatched (thrown)

asynchronously by one object and then received (caught) by another.

Exceptions are supported by all programming languages.

• A signal may be sent as the action of a state transition in a state machine or

the sending or a message in an interaction

• In UML you can model the relationship between an operation and the events

that it can send by using a dependency relationship, stereotypes as send

• You can use a dependency, stereotypes as send, to indicate that an operation

sends a particular signal.

Page 15: State Transition Diagram

Sending and Receiving Events

• Signal events and call events involve at least two objects

• Object sends a signal, the sender dispatches the signal and then continues

along its flow of control, not waiting for any return from the receiver

• For example, an actor sends push-Button signal to the ATM system, the

actor may continue along its way independent of the system to which the

signal was sent

• The sender dispatches the operation and then waits for the receiver. Here

the sender is put in lock step with the flow of control of the receiver until

the activity of the operation is carried out.

Page 16: State Transition Diagram

Eg. Customer Arrival in a Bank

• Customer Arrival in a bank branch is an event• Customer does a transaction that is the action• Object concerned with is the bank branch• If no customer is in waiting state, then the Teller is is not

busy state. i.e if zero customers are ready, teller is in idle state

• If n customers are there in the queue, then the teller is in busy state

• Completion of transaction process take to next state

Page 17: State Transition Diagram

State Transition Diagram

• Not every state classes exhibit such behavior• We can draw a state diagram to show event-ordered

behavior of the system as a whole• During Analysis:

– We use state diagram to indicate dynamic behavior of the system

• During Design:– We capture dynamic behavior of the individual classes or

collection of collaboration classes

Page 18: State Transition Diagram

State

• State of an Object represents the cumulative results of its behavior

• Transition– Progression from one state to another and will be

triggered by event– Event may be external or internal

Page 19: State Transition Diagram

Eg. Telephone

1. Telephone first installed Idle State(No previous behavior is of great interest, the phone is ready to initiate or receive calls)

2. Phone is off-hook Dialing state(Someone picks of the hand-set , Phone is not expected to ring, we expect to initiate conversation on to another phone)

3. Phone is on-hook Receiving state (if it rings we are expected to converse with the initiating

phone)

Page 20: State Transition Diagram

cumulative results of its behavior

• At a given point of time the state of the object encompasses all of its properties together with the current value of each of those properties

• Telephone’s property: can dial, can on-hook, can-off hook• Current: if on-hook , can receive calls

• Here the property mans the totality of the object ‘s attributes and relationship with other objects

• We can generalize the concept of individual object's state to apply to object’s class because– All instances of the same class live in same state space which

encompasses an indefinite yet finite no. of possible states

Page 21: State Transition Diagram

State

• Simple state– A state that has no sub states

• Super or Composite state– A state that has sub states

• Concurrent Composite State

• Sequential Composite state

Page 22: State Transition Diagram

Opened Closed

Locked

Close

Open

Unlockedlocked

Example : Simple state

Page 23: State Transition Diagram

Superstate or Composite Substate

• Objects with complex behaviour can be modeled at different levels of abstraction

• So we need composite states to express the same• Composite states further consists of elaborate

internal state or substates inside individual composite states.

Page 24: State Transition Diagram

Example for Concurrent Composite states

In Service

Filling Gas tank

Begin WashBegin Wash

Pump gas

Depart

Page 25: State Transition Diagram

Sequential Composite state Transmission

TransmissionNeutral

Forward

First Second Third Fourth Fifth

Reverse

up shift

Down shift Down shift Down shift Down shift

up shift up shift up shift

Push R

Push N

Push N Push F

Page 26: State Transition Diagram

State Chart diagram for Car Transmission

• Car Transmission has three states– Neutral– Reverse– Forward

• Forward state is sequential composite state showing First, second, Third, fourth.

• Substate: A state nested in another state is called substate

Page 27: State Transition Diagram

Concurrent Composite states

• Useful when object has sets of independent behaviors

• However there should not be too many sets of concurrent behaviors

• If concurrent composite state is active then one of the nested states from which each concurrent state is active

Page 28: State Transition Diagram

• An Event is the specification of a significant occurrence that has a location in time and space.

• In the context of state machines, an event is an occurrence of a stimulus that can trigger a state transition.

• A signal is a kind of event that represents the specification of an asynchronous stimulus communicated between instances.

Page 29: State Transition Diagram

Entry, exit and do actions• As well as labelling transitions with

actions, it is possible to define actionswithin a state– entry – defines an action to be performed

on entering a state– exit – defines an action to be performed

when leaving a state– do – defines an action to be performed

while in a state

updating

entry/getBalance()

do/addToBalance()

exit/tellBalance()

Page 30: State Transition Diagram

ATM example (with send event)

Customer at ATM

Authentication WithdrawCheck PIN [correct]

Check PIN [incorrect] / incrementErrorCounter

Rejection

Check PIN [incorrect and ErrCounter >=Limit]/authentication Failed

Page 31: State Transition Diagram

Example

dial tone

idle

diallingdigit (n)

on-hookon-hook

digit (n)

State

TransitionEvent

off-hook

Page 32: State Transition Diagram

Activities

• Activities ultimately result in some action, which is made up of executable atomic computations that result in a change in state of the model or a return of a value

• The state of an object is a condition or situation during the life of an object during which it satisfies some condition, performs some activity, or waits for some event

Page 33: State Transition Diagram

You can visualize a state machine in two ways:

1. By emphasizing the flow of control from activity to activity using Activity Diagrams

2. By emphasizing the potential states of the objects and the transitions among these state using State chart Diagrams

State diagrams are used to model different states ofan object during its lifetime; from the time it is created until it is destroyed. It shows the flow of control from one state to another

Page 34: State Transition Diagram

• Use Cases and scenarios provide a way to describe system behavior; that is, the interaction between objects in the system.

• Sometimes it is necessary to look at the behavior inside an object.

• A state chart diagram shows the states of a single object, the events or messages that cause a transition from one state to another, and the actions that result from a state change

• A state chart diagram will not be created for every class in the system, only for classes with significant" dynamic behavior.

Page 35: State Transition Diagram

States

• A state is a condition during the life of an object during which it satisfies some condition, performs some action, or waits for an event.

• The state of an object may be characterized by the value of one or more of the attributes of the class.

• The Heater in a home might be in any of four states: Idle, Activating, Active, and ShuttingDown

• For example, a CourseOffering object may be open (able to add a student) or closed (maximum number of students already assigned to the CourseOffering object).

Page 36: State Transition Diagram

States

A state has several parts:1. Name2. Entry/exit actions3. Internal transitions4. Sub states5. Deferred events: a list of events not handled in that state,

but are postponed and queued for handling by the object in another state ( Message option during conversation in mobile phone)

Page 37: State Transition Diagram

Example

• Objects in CourseOffering class can be in one of the following states:– Initialization (created prior to registration but students

have not been added to it),– Open (able to accept students),– Closed (maximum number of students already

registered for it),– Canceled (no longer offered).

Page 38: State Transition Diagram
Page 39: State Transition Diagram

State Details• actions that accompany all state transitions into a state

may be placed as an entry action within the state. • Likewise, actions that accompany all state transitions out

of a state may be placed as exit actions within the state.• Behavior that occurs within the state is called an activity. • An activity starts when the state is entered and either

completes or is interrupted by an outgoing state transition.• As with actions and guards, this behavior typically is

mapped to operations on the object

Page 40: State Transition Diagram
Page 41: State Transition Diagram

State transition• Represents a change from an originating state to a

successor state (which may be the same as the originating state).

• An action can accompany a state transition. • There are two ways to transition out of a state—

– automatic and nonautomatic.

Page 42: State Transition Diagram

Transition out of a state• An automatic state transition occurs when the activity of

the originating state completes—there is no named event associated with the state transition.

• A non-automatic state transition is caused by a named event (either from another object or from outside the system).

• Both types of state transitions are considered to take zero time and cannot be interrupted.

• A state transition is represented by an arrow that points from the originating state to the successor state.

Page 43: State Transition Diagram

A transition has five parts:

• Source state

• Event trigger: The event whose reception by the object in the source state

makes the transition eligible to fire, providing its guard condition is satisfied

• Guard condition: A Boolean expression that is evaluated when the transition

is triggered by the reception of the event trigger. If true, the transition is

eligible for fire. If false, the transition is not fired

• Action: An execution atomic computations that may directly act on the object

that owns the state machine, and indirectly on other objects that are visible to

the object

• Target state: The state that is active after the completion of the transition

Page 44: State Transition Diagram

Special States

• There are two special states that are added to the state chart

diagram.

• The first is a start state. Each diagram must have one and

only one start state since the object must be in a consistent

state when it is created. The UML notation for a start state is a

small solid-filled circle.

• The second special state is a stop state. An object can have

multiple stop states. The UML notation for a stop state is a

bull's eye.

Page 45: State Transition Diagram
Page 46: State Transition Diagram

• A state Transition may have an action and/or a guard condition associated

with it and may also trigger an event.

• An action is behavior that occurs when the state transition occurs.

• An event is a message that is sent to another object in the system.

• A guard condition is a Boolean expression of attribute values that allows a

state transition only if the condition is true.

• Both actions and guards are behaviors of the object and typically become

operations.

• Often, these operations are private—that is, they are used only by the

object itself.

State Transition Details

Page 47: State Transition Diagram

event name [ guard condition ] / action^Class name.Send event