StateEngine 2.0.0
dotnet add package StateEngine --version 2.0.0
NuGet\Install-Package StateEngine -Version 2.0.0
<PackageReference Include="StateEngine" Version="2.0.0" />
paket add StateEngine --version 2.0.0
#r "nuget: StateEngine, 2.0.0"
// Install StateEngine as a Cake Addin #addin nuget:?package=StateEngine&version=2.0.0 // Install StateEngine as a Cake Tool #tool nuget:?package=StateEngine&version=2.0.0
StateEngine
StateEngine is a library for building state machines.
Installation
Install via Nuget
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Examples
Refer to the following enums for the following examples
public enum State
{
Start,
Middle,
End
}
public enum Stimulus
{
GoToStart,
GoToMiddle,
GoToEnd
}
Builder
A fluent style api is provided to build your state-machine.
using StateEngine;
var engine = new Builder<State, Stimulus>(State.Start)
.WithState(State.Start, startState =>
{
startState.CanTransitionTo(State.Middle, Stimulus.GoToMiddle);
})
.WithState(State.Middle, middleState =>
{
middleState.CanTransitionTo(State.End, Stimulus.GoToEnd);
middleState.CanTransitionTo(State.Start, Stimulus.GoToStart);
})
.WithState(State.End, endState =>
{
// Final state, don't allow any transition out of end
})
.Build<StateMachineFactory<State, Stimulus>>();
The frame of reference for registering transitions can be changed to suite your needs. In the above example, .CanTransitionTo(...)
was used. You can use .CanTransitionFrom(...)
if that feels more natural when registering state transitions. In some cases, it may be easier to use .CanTransitionFrom(...)
because the Stimulus
used in the registration is logically related to the StateBuilder
section you are in.
var engine = new StateEngineBuilder<State, Stimulus>(State.Start)
.WithState(State.Start, startState =>
{
startState.CanTransitionFrom(State.Middle, Stimulus.GoToStart);
})
.WithState(State.Middle, middleState =>
{
middleState.CanTransitionFrom(State.Start, Stimulus.GoToMiddle);
})
.WithState(State.End, endState =>
{
// Final state, don't allow any transition out of end
endState.CanTransitionFrom(State.Middle, Stimulus.GoToEnd);
})
.Build<StateMachineFactory<State, Stimulus>>();
You can specify actions at varying levels within the state engine
using StateEngine;
var engine = new StateEngineBuilder<State, Stimulus>(State.Start)
// Global level actions
// Will trigger whenever any state is entered/left.
.WithEnterAction(t => Console.WriteLine($"{t.From} -> {t.To} : {t.Reason}"))
.WithLeaveAction(t => Console.WriteLine($"{t.From} -> {t.To} : {t.Reason}"))
.WithState(State.Start, startState =>
{
// State level actions
// Will trigger whenever this state is entered or left
startState.WithEnterAction(t => Console.WriteLine($"{t.From} -> {t.To} : {t.Reason}"));
startState.WithLeaveAction(t => Console.WriteLine($"{t.From} -> {t.To} : {t.Reason}"));
// Transition level actions
// Will trigger whenever the specific transition occurs
startState.WithEnterAction(
State.Middle, // The state being left
Stimulus.GoToStart, // The reason the transition occurred
t => Console.WriteLine($"{t.From} -> {t.To} : {t.Reason}"));
startState.WithLeaveAction(
State.Middle, // The state being entered
Stimulus.GoToMiddle, // The reason the transition occurred
t => Console.WriteLine($"{t.From} -> {t.To} : {t.Reason}"));
startState.CanTransitionTo(State.Middle, Stimulus.GoToMiddle);
})
.WithState(State.Middle, middleState =>
{
middleState.CanTransitionTo(State.End, Stimulus.GoToEnd);
middleState.CanTransitionTo(State.Start, Stimulus.GoToStart);
})
.WithState(State.End, endState =>
{
// Final state, don't allow any transition out of end
})
.Build<StateMachineFactory<State, Stimulus>>();
Actions registered to Leave
events are triggered prior to the state transition. Actions registered to Enter
events are triggered immediately after the state transition.
Actions are run in the following order:
Global Level
Leave
events, in the order they were registeredState Level
Leave
events, in the order they were registeredTransition Level
Leave
eventse, in the order they were registered.Global Level
Enter
events, in the order they were registeredState Level
Enter
events, in the order they were registeredTransition Level
Enter
events, in the order they were registered
You can register guards to disallow transitions within the engine. All guards are registered at the transition level. All guards are run in the order they are registered.
.WithState(State.Middle, middleState =>
{
middleState.CanTransitionTo(State.End, Stimulus.GoToEnd);
middleState.CanTransitionTo(State.Start, Stimulus.GoToStart);
// This guard will always allow transitioning in to this state
middleState.WithEnterGuard(State.Start, Stimulus.GoToMiddle, t => { return true; });
// This guard will always block transitioning out of this state to the State.Start state
middleState.WithLeaveGuard(State.Start, Stimulus.GoToStart, t => { return false; });
})
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on StateEngine:
Package | Downloads |
---|---|
StateEngine.Deferred
Deferred StateMachine implementation |
|
StateEngine.Validation
StateMachine Validation implementation |
|
StateEngine.Visualizer
StateMachine visualization implementation |
GitHub repositories
This package is not used by any popular GitHub repositories.
Fixed action registration bugs
Simplified interfaces