statecharts library Null safety

Extensible statechart library.

This contains the tools to build an executable statechart, essentially a state machine with nested substates and well-defined behaviors.

Each statechart is a tree with a RootState and one or more substates ( State). Each State can contain Transition objects that are triggered by a message (event) or by a timer.

Each statechart may also have an associated data structure (context) that can be altered by the state machine.

Let's take a simple lightswitch example.

The data model

class Lightbulb {
 bool isOn = false;
}

The statechart

const turnOn = 'turnOn';
const turnOff = 'turnOff';
final res = StateResolver<Lightbulb>();

final countedLightswitch = RootState.newRoot<Lightbulb>('lightswitch2', [
 State<Lightbulb>('off',
     transitions: [
       res.transition(
           targets: ['on'],
           event: turnOn,
     ],
     onEntry: (b, _) => b!.isOn = false),
 State<Lightbulb>('on',
     transitions: [
       res.transition(targets: ['off'], event: turnOff),
     ],
     onEntry: (b, _) => b!.isOn = true),
]);

Execution

final engine = await Future.value(lightswitch)
           .then((ls) => Engine.initial<Lightbulb>(ls, bulb));
// Execute an event
await engine.execute(anEvent: 'turnOn');

Classes

Engine<T>
Steps through the statechart, calling the State.onEntry and State.onExit methods to alter the data in the context. [...]
EngineCallback
Extend for a communications link back to a custom engine.
EventTransition<T>
A transition based on a named event such as 'turnOn'. [...]
ExecutionStep<T>
Contains the results of one execution step. [...]
ExecutionStepBuilder<T>
HistoryState<T>
Signals the engine to return to a previous configuration of active states. [...]
NonEventTransition<T>
A transition based on time or the condition field. [...]
RootState<T>
Defines the starting node for a statechart. [...]
State<T>
StateResolver<T>
Used during construction to refer to a state by ID and resolve it to a concrete state later. [...]
Transition<T>
Base class for all transitions.

Enums

HistoryDepth
TransitionType
Where the transition should receive its events from.

Typedefs

Action<T> = void Function(T? context, EngineCallback? callback)
Defines entry and exit actions (in and out of a state or container)
Condition<T> = bool Function(T context)
Tests whether or not to take a transition