Skip to content
DiaGO

State Machine Diagrams

States with entry, do and exit behaviours, transitions labelled event [guard] / effect, composite states, choices, history and the other pseudostates - how to draw the life of one object.

A state machine diagram shows the life of one object: the situations it can be in, the events it reacts to, and how each event moves it from one situation to the next. Where an activity diagram follows the steps of a process, a state machine follows a single thing — a book copy, an order, an ATM session — through everything that can happen to it.

The notation

ElementDrawn asMeans
StateA rectangle with rounded cornersA situation the object is in until an event moves it on
Composite stateA large rounded rectangle with substates insideA state made of substates; exactly one of them is active
Initial stateA small filled circleWhere a region starts
Final stateA bull's-eye (a filled circle inside a ring)The region has finished
TransitionA solid arrow labelled event [guard] / effectOn the event, if the guard holds: leave, run the effect, enter
ChoiceA hollow diamond with guarded branchesOne branch is taken, by guards evaluated when it is reached
JunctionA small filled dotTransition segments that branch or meet
Shallow historyA circle with HRe-enter the composite in its last active substate
Deep historyA circle with H*The same, at every nesting level
Entry / exit pointA small circle (with an X for exit) on the borderA named way into or out of a composite state
TerminateA large XThe whole state machine stops at once

States and their behaviours

Name a state after the situation, not after what happens: Waiting for payment, On loan, Paid — not Pay. A state may list behaviours in a compartment under its name:

  • entry / stamp the due date runs every time the state is entered,
  • do / count the notes runs for as long as the object stays in the state,
  • exit / eject the card runs every time the state is left.

An internal transition sits in the same compartment, written like a transition label: renew [renewals < 3] / extend the due date. It reacts to the event without leaving the state, so neither the exit nor the entry behaviour runs. A self-transition — an arrow from a state back to itself — does leave and re-enter: exit, effect, entry, in that order. Pick the one that says what you mean.

Transitions: event [guard] / effect

Every part of a transition label is optional, but each has one job:

On loan --return [reserved by another member] / hold the copy--> Reserved
  • The event (trigger) is what happens: return, card inserted, after(30 s) (a time event).
  • The guard in brackets must be true for the transition to fire. Two transitions on the same event out of one state need guards that never both hold.
  • The effect after the slash is what the object does on the way.

A transition with no event is a completion transition: it fires as soon as the state has finished its entry and do behaviours. Out of a composite state it fires when the composite reaches its final state, which is exactly what you want there; out of a plain state it usually means an event was forgotten.

Composite states

When several states share behaviour — a Cancel that ends the session from any step — group them in a composite state. While the object is in the composite, it is in exactly one of its substates. A transition into the composite starts at the composite's own initial state; a transition out of the composite's border leaves whichever substate is active, so you draw cancel once instead of once per substate. When a substate reaches the composite's final state, the composite's completion transition fires.

History remembers where the composite was left. A transition into a shallow history H re-enters the substate that was active last time; deep history H* restores the nested substates too. In the ATM example, a network outage leaves the Session composite, and network back returns through H to whatever the customer was doing.

Entry and exit points sit on the composite's border and name the ways in and out that do not go through its initial or final state — for example an exit point aborted next to the normal completion.

In DiaGO a state belongs to the composite it is placed or dropped in, and moving a composite moves everything inside it. Orthogonal regions (a composite split into parallel regions by dashed lines) are not drawn by the studio yet.

Reach for it

  • An object reacts differently to the same event depending on what has happened before
  • Showing the life cycle of a business object (an order, a loan, an account)
  • Specifying controllers and devices that react to events and timeouts

Avoid it

  • Following the steps of a process across several participants - use an activity diagram
  • Showing which objects call which operations - use a sequence diagram
  • Objects that only store data and react the same way in every situation

Common mistakes

No initial state, or several. Every region — the state machine itself and each composite with substates — has exactly one initial state. Its transition may have an effect, but no event and no guard: it fires as soon as the region starts. (A composite that is only ever entered through entry points, history or straight to a substate may leave it out; the studio then just suggests one.)

Transitions out of a final state. A final state means the region is done. If the object goes on, it was not the end.

States named as actions. Ship is something that happens on a transition (ship / send the tracking number); Shipped is the state the order is in afterwards.

Branches without guards. A choice with [paid in full] and an unguarded second branch says nothing about when the second is taken. Guard every branch, and end with [else].

Forgotten events. An arrow between two plain states with no event fires immediately, so the first state is never really occupied. Name the event that ends the state.

Unreachable states. A state no chain of transitions leads to from the initial state never happens. Connect it or remove it.

Practice

Draw the life of a library book copy: it starts Available; borrow [member in good standing] puts it On loan; return [no reservation] brings it back, while return [reserved by another member] / hold the copy makes it Reserved; a reservation that is not collected expires with after(3 days). Add an internal transition renew [renewals < 3] / extend the due date to On loan, then a Lost state that ends at a final state when the copy is written off.