statecharts library
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 countedLightswitch = RootState<Lightbulb>('lightswitch2', [
State<Lightbulb>('off',
transitions: [
Transition(
targets: ['on'],
event: turnOn,
],
onEntry: (b, _) => b!.isOn = false),
State<Lightbulb>('on',
transitions: [
Transition(targets: ['off'], event: turnOff),
],
onEntry: (b, _) => b!.isOn = true),
]);
Execution
final engine = Engine<Lightbulb>(lightswitch, bulb);
// Execute an event
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> -
History<
T> -
HistoryBase<
T> -
HistoryBuilder<
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> -
StateSet<
T> - Fast ordered set implementation using State.order.
-
Transition<
T> - Base class for all transitions.
Enums
- HistoryDepth
- TransitionType
- Where the transition should receive its events from.
Extensions
-
ID
on Iterable<
State< T> > -
Sort
on Iterable<
State< T> > -
SortTransitions
on Iterable<
Transition< T> >
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