Rajinder Yadav - Windows C++ Development Tools & Resources :Design, Code, Test, Deploy
UML Basics
Introduction
Object Oriented Software
Substitution Principle
Structural Diagrams
Behavioral Diagrams
Design by Contract
Interface Based Development
Diagram Elements
Note fixture
Constraint fixture
Stereotype fixture
Tagged Value fixture
Path fixture
Relationships
Dependency
Association
Aggregation
Composition
Multiplicity
Structural Diagrams
Class
Component
Deployment
Object
Package
Behavioral Diagrams
Activity
Collaboration
State Machine
Sequence
Use Case
State Machine Diagram - The Unified Modeling Language (UML)

PreviousNext

UML Guide v2.1

Author: Rajinder Yadav
Date: Oct 12, 2007

State Machine Diagram
A state machine can used to model the behaviour from a single object to something more complex like an entire system. Generally a state graph will consist of each state the system can be in along with the events that lead to those states. The level of details to depict various states and events can vary relative to the scope of the diagram. In a meta-model state diagram of a sub-system, adding the state information for each object would be too much detail, while modeling the different states the sub-system can be in and the different events that can lead to those states would be ideal.

A simple object state diagram shows the different states an object has during it's lifetime. I am using the term object loosely here to depict many things including a class instance, a washing machine, a network sub-system, or traffic lights. A state diagram usually shows a initial state, an intermediate state, and a final state.

Note: If you want to show the flow of control from one activity to another activity, do not use a state machine diagram, instead use an activity diagram to model this.

States and Events
A UML state element is shown as a round rectangle with a label of the state placed inside. A state has one or more transition paths which are shown by directed arrows and are called events. The event is drawn using a solid line from the current state with the arrow pointing to the transition state.

The next diagram shows the life of a class object, the initial state shows the object getting created, after which the object enters a intermediate state and then a final state when the object is destroyed. The initial state is represented by a dot pointing to a state and the final state is shown as a dot with a circle around it.


Diagram 1 - Initial, intermediate and final states

Not all state machine will have a final state. Below is a state diagram of the life of a traffic light.


Diagram 2 - States of a traffic light

The above diagram shows the initial state to be green, but it could have easily been any other state. In the case of a traffic light, there is no final end state.

Events Actions
Actions represent atomic operations that must be preformed when an event is triggered, or when the state machine enters a new state. To denote an action that gets executed when an event is triggered, the action is placed after the event label and is separated by a "/" forward slash.


Diagram 3 - Event and Action

Internal State Event
A state may have an event that causes an action to execute without a change in state. This type of event is called an internal event and is labeled in the format, "event / action" inside the state. For example, an opened state may specify that another open event will result in a no operation using the label, "open/nop". The following two state diagrams show how to model an internal event in different ways.


Diagram 4 - Internal State Event

State Actions
The actions for a state are listed inside the state with the format, "action context / action". Each state can list the atomic action that is executed under the following contexts:

  • state is entered when an event is triggered
  • state is exited when an event is triggered
  • while in a given state


Diagram 5 - State and Actions

Below is a list of action types shown in the above diagram that are possible inside a UML state element. The do action is used to declare an operation that will continue to execute while the state machine remain in a given state. For example, a connected state in a server may perform an operation to listen for incoming connection request.

Action QualifierDescription
enterThe action that is executed when a state is entered.
exitThe action that is executed when a state is exited.
doAn operation that is performed while the state machine is in a particular state.

Table 1 -State Event types

You may define your own action qualifier for a given state as long as everyone is aware of the definition. For example, you may define an initialization action that only executes once the first time a state is entered as,"init/action". Think of a singleton server object that gets created and initialized once.


Diagram 6 - Action Qualifier

The state machine above models a sub-system that uses the init/action qualifier to show what action is executed once and only once when the first connection is made. The initialization phase creates and starts a worker thread. If the connection is closed and then opened again, the state machine would re-enter the connected state but the action for init/ would not be executed the second time around.

Note: the order the actions are listed inside the state is important. The actions should be listed in a top-down manner. In the above diagram, the init/ action is listed before the enter/ action because we need to create a worker thread before we can start it. Likewise an enter/ action will take place before an exit/ action.

Event Guards
A event guard is used to define a boolean condition when the event is valid and can take place. If the condition is not satisfied, the event will not trigger and the state machine will not transition to a new state. Each event guard must be mutually exclusive.

An event guard is denoted inside a pair of brackets after the name of the event, such as "Event[Guard]".

Event and Action Data
Like function calls, an event or action can carry data when an event is triggered. Data that is sent with an event is listed inside a pair of round bracket following the name of the event, such as, "event(data). Likewise any action data that is sent to execute an action is denote as, "event/action(data)".

The next diagram shows a partial state machine of a web browser with the states for creating a new browsing document and reading a web page to be presented to the User. It shows how an event guard states that in an opened state, the web page can only be read when there is a connection. In the open state, the connect action gets executed when the state is entered, also the connect action passes some https data structure. The close state executes the action to release the web_doc when the state is exited.


Diagram 7 - Event Guard
PreviousNext

Copyright © 2007 Rajinder Yadav, All rights reserved

UML logo are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries