When de­vel­op­ing a product or pro­gram­ming software, UML state machine diagrams help to represent the lifecycle of in­di­vidu­al objects in a clear and visual way. Although the diagram only comprises a few elements, it can be a key con­tri­bu­tion to the success of the end product when used correctly. Learn why and in which cases it can make sense to use this kind of diagram and how you can create your own UML state machine diagram in the sections below.

What is a state machine diagram?

A UML state machine diagram (also known as a state diagram) visu­al­ises the states of a finite automaton, i.e. a be­ha­vi­our­al model com­pris­ing actions and states or state trans­itions. The diagram provides for an initial and final state and at least one in­ter­me­di­ary state for each object within the model. The state machine diagram is, therefore, able to map the complete lifecycle of a system or subsystem or in­di­vidu­al com­pon­ents and classes, re­gard­less of whether these processes concern a coffee machine, an e-book reader or a technical component in a car, for example.

The state machine diagram is one of 14 types of diagrams of the Unified Modelling Language (UML) and the Systems Model Language (SysML). It’s based on a concept by David Harel, published in the sci­entif­ic paper “Stat­e­charts: A Visual Formalism for Complex Systems” in 1987. Other UML diagram types include the ap­plic­a­tion case diagram and the component diagram.

What are the ap­plic­a­tions of UML state machine diagrams?

As mentioned above, state machine diagrams aim to describe the behaviour of a system as precisely as possible. Among other things, the visu­al­isa­tion of in­di­vidu­al processes is designed to answer the following questions:

  • What happens when the object is in a specific state?
  • What state has to occur for a change in its behaviour?
  • What are the trig­ger­ing actions?
  • Which prop­er­ties does the object need to have to switch state?

UML state machine diagrams are used wherever it makes sense to visualise states and trans­ition con­di­tions for an optimised de­vel­op­ment process. They are es­pe­cially popular for designing embedded systems, for example, since these systems handle various automated signals and processes in the back­ground which need to be optimally co­ordin­ated. In this case, a state machine diagram supports de­velopers by visu­al­ising all relevant control and reg­u­lat­ory functions, making them obvious at a glance.

The benefits of state machine diagrams can be il­lus­trated using the example of the washing machine function “Aqua Stop”. The function regulates the in­ter­rup­tion of water supply to a washing machine. It can be un­der­stood as its own system as part of a UML state machine diagram. Here, the graphical rep­res­ent­a­tion shows in which state and under which con­di­tions the “Aqua Stop” function takes effect.

Note

In various industry sectors like medical tech­no­logy or trans­port­a­tion, state machine diagrams are used to explain complex situ­ations. State machine diagrams are also applied in product and project man­age­ment and en­gin­eer­ing.

State machine diagram: overview of structure and com­pon­ents

Although UML state machine diagrams are based on just a few elements, the effective com­bin­a­tion of these com­pon­ents allows complex sequences of states to be mapped. But what are the most important com­pon­ents and what does the fun­da­ment­al structure of a state machine diagram look like?

States

States are the central com­pon­ents of a state machine diagram. All real states are always rep­res­en­ted with a rectangle with rounded corners. For example, a door can occupy two state values:

If we consider the state machine diagram example for visu­al­ising the states of a door, the following condition always needs to be met:

  • The object is always in one of the two states: The door is either open or closed, but never sim­ul­tan­eously open and closed.

In more complex state machine diagrams, the rectangle can be divided into three sections rep­res­ent­ing be­ha­vi­our­al spe­cific­a­tions (see “Trans­ition”).

Trans­ition: how does a state change?

To switch from one state to the next, an action has to be triggered which rep­res­ents a trans­ition. This trans­ition connects the states – visually depicted by an arrow. Con­di­tions can apply for trig­ger­ing such a trans­ition. In principle, a dis­tinc­tion is made between internal and external trans­itions in UML state machine diagrams. While external trans­itions are mandatory when creating a state machine diagram, internal trans­itions don’t ne­ces­sar­ily have to be included in the diagram.

In the state machine diagram for an elevator, for example, the condition for the action “close elevator door” could be that it had to be open for at least five seconds pre­vi­ously before switching state from “closed” to “open”.

External trans­ition: state changes

In the following example of a UML state machine diagram, for example, the trans­itions are known as external trans­itions. The trans­ition, therefore, results in the object changing to a different state (entry/exit).

Example: If the alarm of a radio alarm clock is triggered, the state changes from “alarm activated” to “alarm de­ac­tiv­ated”. The state changes.

Internal trans­ition: state remains unchanged

In the case of an internal trans­ition, an activity rather than a state change is triggered.

Example: In an ac­count­ing system, the automatic sending of an invoice can be triggered for an out­stand­ing invoice (external trans­ition). If a reminder is sent in response to an out­stand­ing invoice, the dispatch of the reminder is con­sidered an internal trans­ition. Although the activity “send reminder” occurs, the invoice continues to be in the state “unpaid” until set­tle­ment.

Actions: why do states change?

Actions can describe in greater detail under which con­di­tions a state is left to switch to the new state. When trans­ition­ing from the initial state to the first real state, this isn’t necessary; further action in­form­a­tion is optional. If no action is specified, this means that it occurs auto­mat­ic­ally as soon as all activ­it­ies in the previous states have been completed.

A NONE action (trigger / ANY trigger) means that the action is always occurring. Actions can be stated as a be­ha­vi­our­al spe­cific­a­tion within the state or within the state trans­ition (see “Trans­ition”).

A trig­ger­ing action must fulfil the following three con­di­tions:

  • entry: The action is auto­mat­ic­ally triggered when a state is entered, i.e. in all incoming trans­itions.
  • exit: The action is triggered when leaving a state, i.e. in all outgoing trans­itions.
  • do: The action is re­peatedly triggered when the state isn’t changed.

These be­ha­vi­our­al spe­cific­a­tions can be noted within the state to simply show in which be­ha­vi­our­al patterns the state changes. There are two ways to visually represent these triggers. First of all, they can be listed within the cor­res­pond­ing state as demon­strated in the state machine diagram example below:

Al­tern­at­ively, actions can be indicated above the trans­ition arrow:

Pseudo States

If control elements influence the sequence of a state auto­ma­tion but do not possess any value para­met­ers, these are called pseudo states. UML 2, the current version of the Unified Modelling Language, offers ten such pseudo states:

  • Initial state: no incoming trans­itions and exactly one outgoing trans­ition revealed by the starting state
  • Final state: no outgoing trans­ition and the end of the sequence
  • Fork: split into multiple parallel states
  • Join: merger of multiple parallel states
  • Junction: hub for the con­nec­tion of multiple trans­itions
  • Choice: node from which multiple al­tern­at­ive trans­itions can be started based on the decision taken
  • Entry point: summary of ho­mo­gen­ous trans­itions that enter a composite state
  • Exit point: summary of ho­mo­gen­ous trans­itions that leave a composite state
  • Shallow history: archive of the last active sub-state of a composite state
  • Deep history: archive of the last active sub-state of all hierarchy levels of a composite state

Special: sub­or­din­ate diagram

Depending on the com­plex­ity, sub-states are possible that provide a more detailed picture of the object state and possible be­ha­vi­our­al patterns. These complex ex­plan­a­tions in UML state machine diagrams are par­tic­u­larly common in technical contexts.

  • Composite state: Here, a state can be defined in greater depth.

Example: A door can be in the state “open” or “closed”. Sub-states of the state “closed” could be “unlocked” and “locked”.

  • Sub-machine state: Here a sub­or­din­ate state machine diagram is added to a state. Sub-states that comprise multiple sub-states are known as complex states. The various sub-states can be run in­de­pend­ently of each other, although they stand in relation to one another.

Example: In the case of a smart­phone, the alarm function is just one of many features that can be as­so­ci­ated with multiple states. If multiple alarms are pro­grammed for different days of the week and times, they concern sub-states that run in­de­pend­ently of each other.

Creating state machine diagrams: a simple example

A UML state machine diagram can be applied to a range of different objects. The example below shows the various elements by visu­al­ising the states of an invoice. The most important elements of a UML state machine diagram are depicted as follows:

When assembled and applied to the example, a simple diagram looks like this:

After the initial state as a pseudo state, the invoice occupies the state “written”. Ideally, the process should trans­ition to the state “paid”. This trans­ition can be described in greater detail by adding the action name “send”.

Once the invoice is paid, the invoice enters the state “paid”. Before this state is reached, it may be necessary to send a “reminder”. Since the invoice doesn’t change state in this case, this rep­res­ents an activity or an internal trans­ition. If the invoice is not settled, up to three reminders are sent.

Go to Main Menu