Compound State

Also known as Composite state and Or state

A compound state is a state that includes one or more substates. It is the main differentiating factor between traditional state machines and statecharts. A compound state can be thought of as containing its own state machine.

Notation

A compound state is a normal state with its substates depicted inside the borders of the state itself:

A state "Off" with substates A and B

Here, the state called Off is a compound state. It has two states A and B as its substates. Note how the substates constitute their own state machine, even with an initial state.

Description

If there are more than one substates, one of them is usually designated as the initial state of that compound state.

When a compound state is active, its substates behave as though they were an active state machine: Exactly one child state must also be active. This means that:

  • When a compound state is entered, it must also enter exactly one of its substates, usually its initial state.
  • When an event happens, the substates have priority when it comes to selecting which transition to follow. If a substate happens to handles an event, the event is consumed, it isn’t passed to the parent compound state.
  • When a substate transitions to another substate, both “inside” the compound state, the compound state does not exit or enter; it remains active.
  • When a compound state exits, its substate is simultaneously exited too. (Ttechnically, the substate exits first, then its parent.)

Compound states may be nested, or include parallel states.

The opposite of a compound state is an atomic state, which is a state with no substates.

A compound state is allowed to define transitions to its child states. Normally, when a transition leads from a state, it causes that state to be exited. For transitions from a compound state to one of its descendantes, it is possible to define a transition that avoids exiting and entering the compound state itself, such transitions are called local transitions.

Usage

An atomic state is often converted to a compound state in order to change its behaviour slightly. The newly introduced substates get a chance to override the behaviour by defining how to react to different events. The substates only need to define the differences in behaviour. The (now) compound state defines the general behaviour, and substates define deviations from this behaviour.

The act of changing an atomic state into a compound state (by introducing a substate or two) is called refining the state. The refinement alludes to the different behaviour encoded in the substates as being a refinement, or specialization of the general behavour of the compound state.

A group of states can be collected into a compound state to factor out common transitions. For example, if five sibling states all have the same transitions to a particular target, it can be beneficial to move those five states into a single compound state, and then move the individual transitions to the compound state.

The act of grouping states with commonalities into a compound state is called clustering.

Technically a statechart itself is usually (at the top level) a compound state itself. Some systems allow the machine to be a parallel state.

Zooming in and out

The nesting of states in a hierarchy can lead to complicated charts when it is visualized. It is possible to conceal the internals of a compound state by excluding substates from the visualization. This technique is called zooming out. Zooming in would then reveal the details.

SCXML

In Statechart XML, a compound state is any state with nested state elements as direct children; this includes <parallel>, <initial> elements too, as these are also state elements.

<state id="off">
  <transition event="flick" target="on" />
  <state id="A"/>
  <state id="B"/>
</state>

A compound state’s initial state is either identified by the initial attribute of the compound state, or the <initial> state, and if none of these is specified, the first state in document order.

XState

An XState compound state is declared using the states property of the state, holding an object containing substates. Each key value pair declares the name and definition of the state, respectively.

"off": {
  "on": {
    "flick": "on"
  }
  "initial": "A",
  "states": {
    "A": {  },
    "B": {  }
  }
}

The initial property must specify the initial state.

SCION-CORE

In SCION-CORE, a compound state is declared by specifying the states property of the state in question, containing an array of state objects.

{
  id: "off",
  states: [
    {
      id: "A",
    },
    {
      id: "B",
    }
  ]
}

Unless explicitly specified using the initial property, the first item in the state becomes the initial state.