State Machine Diagram Tutorial

A state machine diagram is used to model the dynamic behavior of individual class objects, use cases, and entire systems. In other words, when a state machine created where the object it is attached to, that object becomes the owner of the state machine, for example, the object to be attached by the state machine could be a class, use case or even the entire system.

State Machine Diagram Example

What is a State Machine Diagram in UML?

A state machine diagram is a behavior that specifies the sequences of states an object goes through during its lifetime in response to events. A state machine are used to specify the behavior of objects that must respond to asynchronous stimulus or whose current behavior depends on their past. A state machines are used to model the behavior of objects, use cases, or even entire systems, especially reactive systems, which must respond to signals from actors outside the system.

In UML, state machines introduce the two new concepts in additional to traditional start chart notation:

  1. Composite and nested states
  2. Orthogonal regions

Graphically, a state is rendered as a rectangle with rounded corners. A transition is rendered as a solid directed line.

Try it!

Finding an online State Machine Diagram tool? Just click the Draw button below to create your State Machine Diagram online. Visual Paradigm Online is free* and intuitive. You can also go through this State Machine Diagram tutorial to learn about State Machine Diagram before you get started.

Draw Now

State Machine Diagram Notations

State

A state is a condition during the life of an object which it may either satisfy some condition for performing some activities, or waiting for some events to be received.

A state has five parts:

  1. State Name – Name of State
  2. Entry – Action performed on entry to state
  3. Do Activity – Action performed on entry to state
  4. Exit State – Action performed on leaving state
  5. Deferrable Trigger – A list of events that are not handled in that state but, rather, are postponed and queued for handling by the object in another state
Description of state body

An object remains in a state for a finite amount of time. For example, a Heater in a home might be in any of four states: Idle, Cooling, Heating, Initiating and Active.

Transition

  1. A transition is a relationship between two states indicating that an object in the first state will perform certain actions and enter the second state when a specified event occurs and specified conditions are satisfied.
  2. Transition fires means change of state occurs. Until transition fires, the object is in the source state; after it fires, it is said to be in the target state.
  3. A transition has five parts:
    1. Source state – The state affected by the transition
    2. Event trigger – a stimulus that can trigger a source state to fire on satisfying guard condition,
    3. Guard condition – Boolean expression that is evaluated when the transition is triggered by the reception of the event trigger,
    4. Action – An executable atomic computation that may directly act on the object that owns the state machine, and indirectly on other objects that are visible to the object,
    5. Target state – The state that is active after the completion of the transition.
State Transition

Source and Target State

Source State: The state affected by the transition; if an object is in the source state, an outgoing transition may fire when the object receives the trigger event of the transition and if the guard condition, if any, is satisfied.

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

Events

Event is a discrete signal that happens at a point in time. It also known as a stimulus and in a kind of  input to an object. Here is the characteristics of events:

  1. May cause a change in state
  2. May trigger actions –  Actions can be internal or external
  3. May have associated conditions
  4. Signal events can be used to communicate between state machines

Guard Condition

  1. State transition label –  Event [Guard Condition]
  2. Condition is a Boolean function
    1. Conditions are optional on state machines
    2. Condition is true for finite period of time
  3. When event occurs, condition must be true for state transition to occur. If condition is false, state transition does not occur.

Actions

Action is executed as a result of instantaneously of state transition. State transition label can be expressed as the following format

  1. Event / action(s)
  2. Event [condition] / action(s)
  3. Entry/exit actions

Decision Node

A Decision ode is used to represent a test condition to ensure that the control flow or object flow only goes down one path.

Fork node is a pseudo state used to split an incoming transition into two or more transitions terminating on orthogonal target vertices. The segments outgoing from a fork vertex must not have guards or triggers and it must have exactly one incoming and at least two outgoing transitions.

Join node is a pseudo state used to merge several transitions emanating from source vertices in different orthogonal regions. The transitions entering a join vertex cannot have guards or triggers and it must have at least two incoming transitions and exactly one outgoing transition.

State Machine Diagram: Decision Flow

Merge node is used to bring back together different decision paths that ware created using a decision node.

Choice is a pseudo state which, when reached, result in the dynamic evaluation of the guards of the triggers of its outgoing transitions. This realizes a dynamic conditional branch. It allows splitting of transitions into multiple outgoing paths such that the decision on which path to take.

State Machine Diagram: Guard

Example: Choice Node for State Machine Diagram

State Machine Diagram: Choice

Terminate is a pseudo state indicates that the lifeline of the state machine has ended. A terminate pseudo-state is represented by a cross.

Unlike a final state, a terminate pseudo state implies that the state machine is ended due to the context object is terminated. There is no exit of any states nor does the state machine perform any exit actions other than the actions associated with the transition that leads to the terminate state.

State Machine Diagram: Terminate

Composite State

A simple state is one which has no substructure. Composite States can be further broken down into substates (either within the state or in a separate diagram). A state which has substates (nested states) is called a composite state.

  1. Substates may be nested to any level.
  2. A nested state machine may have at most one initial state and one final state.
  3. Substates are used to simplify complex flat state machines by showing that some states are only possible within a particular context (the enclosing state).
State Machine Diagram: Composite State

Composite State vs Submachine State

Besides composite state, there is another symbol called submachine state, which is semantically equivalent to a composite state.

State Machine Diagram: Composite State vs Submachine State

Orthogonal State

A composite state with two or more regions is called orthogonal. Unlike composite states, submachine states are intended to group states, so you can reuse them. Orthogonal state is divided into two or more regions separated by a dashed line:

  1. One state of each region is always active at any point in time, i.e., concurrent substrates
  2. Entry: transition to the boundary of the orthogonal state activates the initial states of all regions
  3. Exit: final state must be reached in all regions to trigger completion event
State Machine Diagram: Orthogonal State

Note That:

You can use parallel and synchronized node to ordinate different substates. Concurrent Substates are independent and can complete at different time.

History State – Shallow / Deep

History states allow the state machine to re­enter the last substate that was active prior to leaving the composite state. An example of history state usage is presented in the figure below:

State Machine Diagram: History State

When to draw State Machine Diagram?

You can use state machines in the following situations:

  1. During business modeling, you can create state machines to model a use-case scenario.
  2. During analysis and design, you can model event-driven objects that react to events outside an object's context.
  3. During analysis and design, you can use several state machine diagrams to show different aspects of the same state machine and its behavior.

How to Draw a State Machine Diagram?

A Use Case model can be developed by following the steps below.

  1. Identify entities that have complex behavior or identify a class participating in behavior whose lifecycle is to be specified
  2. Model states – Determine the initial and final states of the entity
  3. Model transitions
  4. Model events – Identify the events that affect the entity
  5. Working from the initial state, trace the impact of events and identify intermediate states
  6. Identify any entry and exit actions on the states
  7. Expand states using substates where necessary
  8. If the entity is a class, check that the action in the state are supported by the operations and relationships of the class, and if not extend the class
  9. Refine and elaborate as required

You can also:

  1. Draw substates for logical categorization of states with abstraction for reuse purposes

State Machine Diagram Examples

The Digital Clock State Machine diagram example below shows the interface of a simple digital clock:

Digital clock

The state machine diagram where the class it is attached:

A UML class

The state Diagram for modeling the behavior of the DigitalClock:

State Machine Diagram Example: Digital Clock

Some more State Machine Diagram examples are provided below.

State Machine Diagram example: Toaster

State Machine Diagram Example: Toaster

State Machine Diagram example: Oven

State Machine Diagram Example: Oven

State Machine Diagram example: Computer Testing

This example represents two sets of concurrent substates by using two regions.

State Machine Diagram Example: Concurrent sub state

State Machine Diagram example: Human Life

This example represents two sets of concurrent substates by using two regions.

State Machine Diagram Example: Human life

Want to draw a State Machine Diagram?

You've learned what a State Machine Diagram is and how to draw a State Machine Diagram step-by-step. It's time to get your hands dirty by drawing a State Machine Diagram of your own. Draw UML diagrams free* with Visual Paradigm Online. It's easy-to-use, intuitive.

Draw Now

* The Free edition supports free usage of Visual Paradigm Online for non-commercial use only.