Create state machines and lightweight state machine-based workflows directly in Go code:
phoneCall := stateless.NewStateMachine(stateOffHook)
phoneCall.Configure(stateOffHook).Permit(triggerCallDialed, stateRinging)
phoneCall.Configure(stateRinging).
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
onDialed(args[0].(string))
return nil
}).
Permit(triggerCallConnected, stateConnected)
phoneCall.Configure(stateConnected).
OnEntry(func(_ context.Context, _ ...interface{}) error {
startCallTimer()
return nil
}).
OnExit(func(_ context.Context, _ ...interface{}) error {
stopCallTimer()
return nil
}).
Permit(triggerLeftMessage, stateOffHook).
Permit(triggerPlacedOnHold, stateOnHold)
// ...
phoneCall.Fire(triggerCallDialed, "qmuntal")
This project, as well as the example above, is almost a direct, yet idiomatic, port of dotnet-state-machine/stateless, which is written in C#.
Most standard state machine constructs are supported:
- Support for states and triggers of any comparable type (int, strings, boolean, structs, etc.)
- Hierarchical states
- Entry/exit events for states
- Guard clauses to support conditional transitions
- Introspection
Some useful extensions are also provided:
- Ability to store state externally (for example, in a property tracked by an ORM)
- Parameterised triggers
- Reentrant states
- Thread-safe
- Export to DOT graph
In the example below, the OnHold
state is a substate of the Connected
state. This means that an OnHold
call is still connected.
phoneCall.Configure(stateOnHold).SubstateOf(stateConnected).
Permit(triggerTakenOffHold, stateConnected).
Permit(triggerPhoneHurledAgainstWall, statePhoneDestroyed)
In addition to the StateMachine.State
property, which will report the precise current state, an IsInState(State)
method is provided. IsInState(State)
will take substates into account, so that if the example above was in the OnHold
state, IsInState(State.Connected)
would also evaluate to true
.
In the example, the StartCallTimer()
method will be executed when a call is connected. The StopCallTimer()
will be executed when call completes (by either hanging up or hurling the phone against the wall.)
The call can move between the Connected
and OnHold
states without the StartCallTimer()
and StopCallTimer()
methods being called repeatedly because the OnHold
state is a substate of the Connected
state.
Entry/Exit event handlers can be supplied with a parameter of type Transition
that describes the trigger, source and destination states.
Stateless is designed to be embedded in various application models. For example, some ORMs place requirements upon where mapped data may be stored, and UI frameworks often require state to be stored in special "bindable" properties. To this end, the StateMachine
constructor can accept function arguments that will be used to read and write the state values:
stateMachine := stateless.NewStateMachineWithExternalStorage(func(_ context.Context) (stateless.State, error) {
return myState.Value, nil
}, func(_ context.Context, state stateless.State) error {
myState.Value = state
return nil
}, stateless.FiringQueued)
In this example the state machine will use the myState
object for state storage.
The state machine can provide a list of the triggers that can be successfully fired within the current state via the StateMachine.PermittedTriggers
property.
The state machine will choose between multiple transitions based on guard clauses, e.g.:
phoneCall.Configure(stateOffHook).
Permit(triggerCallDialled, stateRinging, func(_ context.Context, _ ...interface{}) bool {return IsValidNumber()}).
Permit(triggerCallDialled, stateBeeping, func(_ context.Context, _ ...interface{}) bool {return !IsValidNumber()})
Guard clauses within a state must be mutually exclusive (multiple guard clauses cannot be valid at the same time). Substates can override transitions by respecifying them, however substates cannot disallow transitions that are allowed by the superstate.
The guard clauses will be evaluated whenever a trigger is fired. Guards should therefor be made side effect free.
Strongly-typed parameters can be assigned to triggers:
stateMachine.SetTriggerParameters(triggerCallDialed, reflect.TypeOf(""))
stateMachine.Configure(stateRinging).OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
fmt.Println(args[0].(string))
return nil
})
stateMachine.Fire(triggerCallDialed, "qmuntal")
It is runtime safe to cast parameters to the ones specified in SetTriggerParameters
. If the parameters passed in Fire
do not match the ones specified it will panic.
Trigger parameters can be used to dynamically select the destination state using the PermitDynamic()
configuration method.
Firing a trigger that does not have an allowed transition associated with it will cause a panic to be thrown.
To ignore triggers within certain states, use the Ignore(Trigger)
directive:
phoneCall.Configure(stateConnected).
Ignore(triggerCallDialled)
Alternatively, a state can be marked reentrant so its entry and exit events will fire even when transitioning from/to itself:
stateMachine.Configure(stateAssigned).
PermitReentry(triggerAssigned).
OnEntry(func(_ context.Context, _ ...interface{}) error {
startCallTimer()
return nil
})
By default, triggers must be ignored explicitly. To override Stateless's default behaviour of throwing a panic when an unhandled trigger is fired, configure the state machine using the OnUnhandledTrigger
method:
stateMachine.OnUnhandledTrigger( func (_ context.Context, state State, _ Trigger, _ []string) {})
It can be useful to visualize state machines on runtime. With this approach the code is the authoritative source and state diagrams are by-products which are always up to date.
sm := stateMachine.Configure(stateOffHook).
Permit(triggerCallDialed, stateRinging, isValidNumber)
graph := sm.ToGraph()
The StateMachine.ToGraph() method returns a string representation of the state machine in the DOT graph language, e.g.:
digraph {
OffHook -> Ringing [label="CallDialled [isValidNumber]"];
}
This can then be rendered by tools that support the DOT graph language, such as the dot command line tool from graphviz.org or viz.js. See webgraphviz.com for instant gratification. Command line example: dot -T pdf -o phoneCall.pdf phoneCall.dot to generate a PDF file.
This is the complete Phone Call graph as builded in example_test.go
.
This page is an almost-complete description of Stateless, and its explicit aim is to remain minimal.
Please use the issue tracker or the if you'd like to report problems or discuss features.
(Why the name? Stateless implements the set of rules regarding state transitions, but, at least when the delegate version of the constructor is used, doesn't maintain any internal state itself.)