StateEngine 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package StateEngine --version 1.1.0
NuGet\Install-Package StateEngine -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StateEngine" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StateEngine --version 1.1.0
#r "nuget: StateEngine, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StateEngine as a Cake Addin
#addin nuget:?package=StateEngine&version=1.1.0

// Install StateEngine as a Cake Tool
#tool nuget:?package=StateEngine&version=1.1.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:

  1. Global Level Leave events, in the order they were registered
  2. State Level Leave events, in the order they were registered
  3. Transition Level Leave eventse, in the order they were registered.
  4. Global Level Enter events, in the order they were registered
  5. State Level Enter events, in the order they were registered
  6. Transition 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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.Visualizer

StateMachine visualization implementation

StateEngine.Validation

StateMachine Validation implementation

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 111 4/25/2024
1.1.0 342 3/5/2023
1.0.0 473 11/14/2022

Fixed action registration bugs
Simplified interfaces