StateMachine

A finite state machine (FSM) comprises a finite set of states, a table of transitions between those states, and a table of actions to be performed when states are entered, exited, or transitioned between. Each transition may have a guard, allowing conditional selection among transitions for the same event. A transition may have null for its event, in which case the transition is traversed immediately after arriving if the guard permits.

The FSM is parametric on the type of states, the type of events which cause transitions between states, the type of action keys, the type of guard keys, and the type of argument that an action will receive. The client provides each parameter for maximum type-safety and code-reuse.

States, events, action keys and guard keys are enumerations, thereby allowing the compiler to check the correctness of usages and the runtime environment to validate the comprehensiveness of the FSM model. In particular, all states must be reachable, all events must occur, and all action keys must be bound to executable actions.

Executable actions are keyed by members of an action key enumeration. That is, states and transitions are not bound directly to actions, but rather indirectly to keys. This allows optimal type-safety and automated validation. An action accepts a single argument of client-specified type. This object is treated as a memento by the FSM, an opaque argument supplied at execution context creation-time and passed through to an action upon its performance.

Executable guards are keyed by members of a guard key enumeration for the same reasons as actions. An executable guard accepts a single argument of client-specified type, namely the same memento passed to executable actions. The executable guard answers a boolean indicating whether the transition may be taken.

A new FSM is obtainable only via an appropriately parameterized factory. This allows incremental and arbitrary-order specification of the FSM independent of any runtime assembly constraints.

An FSM provides a single protocol operation, namely the creation of a new execution context (createExecutionContext). Transitions are client-instigated by notification of event occurrences (ExecutionContext.handleEvent). Not every state will have a valid transition on every event; should an invalid transition occur, an [InvalidTransitionException] will be thrown. This event may be safely discarded to permit continued use of the signaling execution context. Event notification is thread-safe, and multiple contexts may simultaneously execute on the same FSM.

Author

Todd L Smith

Parameters

State

The kind of states.

Event

The kind of events.

ActionKey

The kind of action keys.

GuardKey

The kind of guard keys.

Memento

The kind of argument that actions will receive.

initialState

The state in which a new context will start.

summaries

The collection of state summaries.

See also

Functions

Link copied to clipboard

Create a context for executing this StateMachine.