Brain Dump

State Machine Diagram

Tags
modelling

Is a UML time-view diagram, to represent state-based (reactive) systems.

\begin{figure}
  \centering
  \begin{tikzpicture}
    \umlstateinitial[name=Init]
    \umlbasicstate[y=-2.5]{Idle}
    \umlbasicstate[x=5, y=-4]{Accepting Coins}
    \umlbasicstate[y=-6]{Dispensing}

    \umltrans{Init}{Idle}
    \umlHVtrans[arg=insert coin, pos=0.5]{Idle}{Accepting Coins}
    \umltrans[arg=insert coin, recursive=-10|10|3cm, pos=1.5, recursive direction=right to right]{Accepting Coins}{Accepting Coins}
    \umlVHtrans[arg=insert coin/ready, pos=0.5]{Accepting Coins}{Dispensing}
    \umlHVtrans[arg=cancel/eject coins, pos=0.5]{Accepting Coins}{Idle}
    \umltrans[arg=cancel/eject coins, arm1=2cm, pos=0.3]{Dispensing}{Idle}
    \umltrans[arg=select/dispense drink, pos=0.6]{Dispensing}{Idle}
  \end{tikzpicture}
  \label{fig:state-machine}
  \caption{Example state machine diagram for a vending machine.}
\end{figure}

The [see page 4, semantics] of a state machine are simple. We enter the machine through it's initial state. Events are loaded into a queue and passed into the machine one at a time. If a transition exists in the current state for the current event then we trigger it and go to the next state. If a transition doesn't exist we ignore the event. While you can explicitly add recursive transitions for all the events you want to ignore, you should avoid overloading the model with unnecessary details.

Constructs uml

States are visualised as rounded boxes that can contain sub-states.

The umlbasicstate macro is used to create a simple state and the umlstate section used to define a state that can contain sub-states.

\begin{figure}
  \centering
  \begin{tikzpicture}
    \umlbasicstate{Basic State}
    \begin{umlstate}[x=4]{Nested State}
    \end{umlstate}
  \end{tikzpicture}
  \caption{Example umlstate}
\end{figure}

You can see an example state-machine diagram in fig:state-machine.

Table 1: Listing of extra macros used to define special states.
MacroSymbolDescription
umlstateinitial\tikz \umlstateinitial[name=Initial];The first state a state diagram enters into.
umlstatefinal\tikz \umlstatefinal[name=Final];The last state a diagrams enters before halting.
umlstatejoin\tikz \umlstatejoin[name=Join];
umlstatedecision\tikz \umlstatedecision[name=Branch];
umlstateenter\tikz \umlstateenter[name=Enter];
umlstateexit\tikz \umlstateexit[name=Exit];
umlstateend\tikz \umlstateend[name=End];
umlstatehistory\tikz \umlstatehistory[name=History];
umlstatedeephistory\tikz \umlstatedeephistory[name=DeepHist];

Preconditions

You can attach [see page 11, preconditions] to a state transition such that a single state can handle the same event in multiple ways depending on the result of the precondition.

Preconditioned transitions just have the transition name followed by the condition in guards (surrounded square brackets). For example issue [loans < max - 1] is a transition that only triggers on the event issue when the maximum number of loans haven't been exceeded.

Post Conditions

The special [see page 11, decision] pseudo-state (\tikz \umlstatedecision;) is a state that can have multiple outgoing transitions based on one or more pre-conditions.

The decision state is usually entered into by some activity done as a consequence of the transition. This activity would modify some variable that the decision state can compare against to come to a decision. We denote such activities in the transition name as event [pre-condition] / activity.

Extended Regions

State Machine Composition

State machines by themselves are difficult to scale because of a large amount of state being presented all at once. The way around this is to [see page 13, compose] one or more machines into each other.

Methods of composition include:

  • Place a state machine inside a sub-state machine (running within its super-state.)
  • Place two orthogonal machines inside a state: concurrent sub-state machines.

Both these variant composition methods can be shown to be equivalent to a more complex expanded flat state-machine.