State

A state in a state machine describes a particular behaviour of the machine. When we say that a machine is “in” a state, it means that the machine behaves in the way that state describes.

The behaviour of a state is defined as how the state reacts to events. Each state can specify a number of events that it “understands”, and specifies, for each event, any number of transitions that could be taken, if that event happens. A state also describes a set of actions to execute when the state is entered or exited, and in some implementations, activities that should be happening for the entire duration of time that the machine is in the state.

A state usually has a name, but some systems allow for anonymous states.

When a state machine is executed, it enters the initial state, and adopts the behaviour associated with that state. As the machine handles events, it transitions to other states, thus changing the behaviour of the machine over time.

States in statcharts

There are a few special types of states in statecharts:

  • Atomic (or simple) states, are states that have no substates.
  • Compound (or composite) states, are states that have one or more substates. The substates can be said to be a refinement of their parent state. Coversely, the parent state can be said to provide a default behaviour of the substates.
  • Parallel (or orthogonal) states, are states that (like a compound states) have substates, but where the child states are grouped into orthogonal regions, and those regions are all active simultaneously.

Hierarchy

These constructs allow statecharts to be hierarchically organized. When a compound state is active (when the machine is in that state), exactly one of its child states are also active. When a parallel state is active, by definition, all of the child states (called regions) are also active. This implies that in a hierarchically organized statechart, many states can be active at any point in time. Specifically, for each active atomic state, all the compound and parallel parents are also deemed to be “active”. When an event happens, the “deepest” of the states get to handle the event, pre-empting the behaviour of the parent states that contain it.

Notation

A state is generally depicted as a rounded box, with the name of the state centered at the top of the box, with a horizontal line sparating the name of the state from the actions and activities. The original statechart paper did not have these lines, they come from the UML Statechart standard. It is common to drop this line in states that have no substates and no actions.

Transitions from this state are depicted as curvy lines pointing away. Transitions to this state are depicted as curved lines that point to this state.

Entry and Exit actions are written in text form inside the state.

A rounded rectangle with a horizontal line, on top of which the word "On", and below which the words "entry / turn on"

SCXML

In SCXML, a state is defined by the <state> element, with the name of the state in its id attribute. An empty state:

<state id="my_state"/>

The child elements of the <state> include entry and exit actions, transitions, along with substates:

<state id="my_state">
  <onentry>
    <script>do_something()</script>
  </onentry>
  <transition event="some_event" target="some_other_state"/>
  <state id="my_substate">
    <transition event="some_event" target="my_other_substate"/>
  </state>
  <state id="my_other_substate"/>
</state>

XState

In XState, a state node is specified as a named object in the states property of a state machine or compound state node. The key becomes the name of the state node (and is used to construct the state node’s ID), and the object defines the state’s behaviour.

Here’s an empty state called my_state:

{
  states: {
    my_state: {}
  }
}

Here’s the my_state with an action, transitions, and substates:

{
  states: {
    my_state: {
      onEntry: "do_something",
      on: {
        some_event: "some_other_state"
      },
      states: {
        my_substate: {
          on: {
            some_event: "my_other_substate"
          }
        },
        my_other_substate: {}
      }
    }
  }
}

SCION-CORE

In SCION Core, a state is described by an object with an id property, denoting the identifier of the state.

{
  id: 'my_state';
}

Here’s the same state with an action, transitions, and substates:

{
  id: "my_state",
  onEntry: function(event) {
    do_something();
  },
  transitions: [
    {
      event: "some_event",
      target: "some_other_state"
    }
  ],
  states: [
    {
      id: "my_substate",
      transitions: [
        {
          event: "some_event",
          target: "my_other_substate"
        }
      ]
    },
    {
      id: "my_other_substate"
    }
  ]
}