1. Overview

UML (Unified Modeling Language) is a modeling language that is used for concisely describing a system. UML diagrams are widely used in the software engineering field.

In this tutorial, we’ll learn how a UML state diagram describes a system and its different states.

2. What Is a State Diagram?

A state diagram models the behavior of a system or an object in response to events. A state diagram mainly consists of states, events, guards, transitions, and actions:

Example State Diagram

The above diagram shows a state diagram for the booting process of a computer.

3. States

States describe the system’s phase or condition. They are shown by a rectangle with the state’s name written inside it:

State Example

4. Events

An event can be a signal or a change in the environment of the system. Events may or may not lead to a change in the system’s state:

Event Example

In the above diagram, power button pressed is the event that triggers the transition from the initial state to the Running BIOS state.

5. Transitions

Transitions are shown as arrows from one state to another. They may have three indicators written on them:

  • The event that triggers the transition
  • The guard statement which is a condition that must be true for the transition to take place
  • An action statement that will happen as a result of the transition
Transition Example

In the above diagram, entered credentials is the event that triggers the transitions, [not valid] and [valid] are guard statements, and showLoginSuccessful() and showLoginUnsuccessful() are actions that are invoked after each transition.

6. Actions

Actions are specific functions that are invoked by the system. There are two types of actions:

6.1. Transition Actions

They are functions that are invoked after a transition is finished:

Transition Action Example

In the above transition, showLoginSuccessful() is a transition action.

6.2. State Actions

State actions are invoked when the system is in a specific state. There are 3 types of state actions:

  • Entry actions: they are executed when the system enters the state
  • Do actions: they are continuously executed while the system is in the state
  • Exit actions: they are executed when the system exits the state

 

State Actions Example

7. Pseudo-States

In addition to the normal states in the state diagram, we may have pseudo-states. Pseudo-states don’t have any specific actions associated with them.

7.1. Initial State

It shows the starting point of the system. It’s represented as a filled circle:

Initial State Example

7.2. Final State

It shows the ending point of the system. It’s represented as a circle with a dot inside:

Final State Example

7.3. Choice State

The choice state is represented as a diamond with one arrow arriving and multiple arrows leaving the state. It shows a decision point in the system where the result depends on a condition that is written on every outgoing arrow:

Choice State Example

In the above diagram, if the password is correct, the system enters the welcome state, and if the password is incorrect, the system enters the error state.

7.4. Junction State

A junction state combines multiple incoming transitions into one transition. A junction state may have multiple outgoing transitions with different conditions on each one:

Junction State Example

In the above diagram, both Email Login and Username Login states enter the same junction state, and if the credentials are correct, the system enters the Welcome state, otherwise it enters the Error state

7.5. Fork and Join States

Fork and join states are used when we have concurrency in the system. Before the concurrent threads are created, a fork state is placed that has one incoming arrow and multiple outgoing arrows that lead to each thread. After the concurrent threads, a join state is placed that links together multiple arrows from each thread into one arrow:

Fork and Join States

In the above diagram, the first small rectangle is the fork state that leads to the Welcome state. In addition, the second small rectangle is the join state and it’s placed after the concurrent threads of the Welcome state.

8. Composite States

Composite states are states that have sub-states. The sub-states form another state diagram inside the composite state:

Composite State Example

Here, the Login state is a composite state that contains the Entering Credentials and Logged In sub-states.

8.1. Entry Point and Exit Point

A composite state may not necessarily start at the initial state and can have a different entry point. Moreover, a composite state may not necessarily end at the final state and can have a different exit point:

Entry Point ExampleExit Point Example

Here, Username Login is an entry point to the Welcome state, and Failed is an exit point from the Login state. The Welcome and Login composite states can look something like this:

Entry Point and Exit Point Composite State

Here, the Login composite state will have a different final point if the entered credentials were incorrect. Moreover, the Welcome composite state may have a different entry point if the user entered their username instead of email.

9. Concurrent States

Concurrent states are sub-states of a composite state that are executed concurrently. As we learned, There’s a fork state before the concurrent states and a join state after them:

Fork and Join States

In the above diagram, the Welcome state has two sub-states that run concurrently: Show Welcome Message and Play Welcome Sound.

10. History States

Sometimes the object must remember where it left off. In these situations, a history state is used to show the memory of the object. If, for any reason, the system is interrupted, it can remember where it left off the next time it starts running.

A history state is shown by a capital H inside a circle:

HIstory State Example

Here, the computer starts running the programs and if an error occurs, it enters the standby state. After the error is resolved, the computer starts where it left off until all the programs are successfully executed.

11. Summary

In this tutorial, we learned about the components and syntax of a UML state diagram.

Comments are closed on this article!