You are on page 1of 47

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

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

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

A generalised state machine 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

Example : CD Player

Power switch on Stopped state

Insert CD
Ready state Eject CD

Power switch on
Press Play button Press stop button

Playing state

Stat Machine
A state machine is a graph of states and transitions Usually a state machine is attached to a class and it describes the response of object of the class to event it receives. The object is examined in an isolated manner How the object responds to an event depends on the current state of the object One starting point shown by filled circle Several end points shown bulls eye symbol A state is shown by rectangle with rounded corners Arrow between states shows the transition of object from one state to another states

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

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

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

Change Event
Change event is evaluated continuously till it becomes true. When it is true transition fires

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)

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.

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.

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

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

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

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)

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 Telephones 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 objects class because
All instances of the same class live in same state space which encompasses an indefinite yet finite no. of possible states

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

Example : Simple state


Close Opened Open

Closed

locked

Unlocked
Locked

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.

Example for Concurrent Composite states


In Service
Pump gas

Filling Gas tank

Depart

Begin Wash
Begin Wash

Sequential Composite state Transmission


Transmission
Neutral
Push N Push R

Reverse
Push F

Push N

Forward
up shift up shift up shift up shift

First
Down shift

Second
Down shift

Third
Down shift

Fourth
Down shift

Fifth

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

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

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.

Entry, exit and do actions


As well as labelling transitions with actions, it is possible to define actions within a state
entry defines an action to be performed on entering a state updating exit defines an action to be performed when leaving a state entry/getBalance() do defines an action to be performed do/addToBalance() while in a state
exit/tellBalance()

ATM example (with

send event)

Customer at ATM

Authentication

Check PIN [correct]

Withdraw

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

Example
Event State Transition

digit (n)

digit (n)

dial tone
on-hook off-hook

dialling

on-hook

idle

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

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 of an 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

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.

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).

States
A state has several parts: 1. Name 2. Entry/exit actions 3. Internal transitions 4. Sub states 5. 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)

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).

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

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.

Transition out of a state


An automatic state transition occurs when the activity of the originating state completesthere 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.

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

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.

State Transition Details


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 privatethat is, they are used only by the object itself.

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