Finite 4.2.0

dotnet add package Finite --version 4.2.0                
NuGet\Install-Package Finite -Version 4.2.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="Finite" Version="4.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Finite --version 4.2.0                
#r "nuget: Finite, 4.2.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 Finite as a Cake Addin
#addin nuget:?package=Finite&version=4.2.0

// Install Finite as a Cake Tool
#tool nuget:?package=Finite&version=4.2.0                

Finite

A simple finite state machine written in C#

Installation

PM> Install-Package Finite

Usage

Declare a state arg object which is used to control available state transitions:

public class StateArgs
{
	public boolean OnBattery { get; set; }
}

A class for each of the states to use in your state machine:

public class LightOff : State<StateArgs>
{
	public LightOff()
	{
		LinkTo<LightOnFull>(l => l.OnBattery == false);
		LinkTo<LightOnDim>(l => l.OnBattery);
	}
}

public class LightOnDim : State<StateArgs>
{
	public LightOnDim()
	{
		LinkTo<LightOnFull>(l => l.OnBattery == false);
		LinkTo<LightOff>();
	}
}

public class LightOnFull : State<StateArgs>
{
	public LightOnFull()
	{
		LinkTo<LightOnDim>(l => l.OnBattery);
		LinkTo<LightOff>();
	}
}

Configure the machine with the states:

var allStates = new State<LightsSwitches>[]
{
	new LightOff(),
	new LightOnDim(),
	new LightOnFull(),
};

var machine = new StateMachine<LightSwitches>(
	new ManualStateProvider<LightsSwitches>(allStates),
	new LightSwitches());

The state machine can then be used:

//reset the state machine to an initial state:
machine.ResetTo<LightOff>();
machine.CurrentState.ShouldBeOfType<LightOff>();

//all of the possible transitions
machine.AllTargetStates
	.Select(state => state.GetType())
	.ShouldBe(new[] { typeof(LightOnDim), typeof(LightOnFull) });

//all of the states which can currently be transitioned to
machine.ActiveTargetStates
	.Select(state => state.GetType())
	.ShouldBe(new[] { typeof(LightOnFull) });

//all of the states which can't be currently transitioned to
machine.InactiveTargetStates
	.Select(state => state.GetType())
	.ShouldBe(new[] { typeof(LightOnDim) });

//LightOnDim is not a valid transition at the moment:
Should.Throw<InvalidTransitionException>(() => machine.TransitionTo<LightOnDim>());
machine.CurrentState.ShouldBeOfType<LightOff>();

//LightOnFull is valid:
machine.TransitionTo<LightOnFull>();
machine.CurrentState.ShouldBeOfType<LightOnFull>();

State Discovery

There are a couple of ways of adding states to the state machine. The first seen in the previous examples is to specify them by an array (or IEnumerable<Type>):

var allStates = new State<LightsSwitches>[]
{
	new LightOff(),
	new LightOnDim(),
	new LightOnFull(),
};

var machine = new StateMachine<LightSwitches>(
	new ManualStateProvider<LightsSwitches>(allStates),
	switches);

This method is fine if you have just few states to specify, but when you get into larger state machines, it can become unweildy.

The alternate built in way is to use the ScanningStateProvider class, which will look for all States in the assembly which can be used by your state machine (in this example, anything inheriting State<LightSwitches>):

var machine = new StateMachine<LightSwitches>(
	new ScanningStateProvider<LightSwitches>(),
	switches);

This StateProvider also supports custom state creation, by means of a lambda:

var stateProvider = new ScanningStateProvider<LightSwitches>(state => (State<LightSwitches>)_container.GetInstance(state));

To create your own StateProvider, you just need to implement IStateProvider<TSwitches>. The source for both the ManualStateProvider and ScanningStateProvider are [viewable here][https://github.com/Pondidum/Finite/tree/master/Finite/StateProviders].

State Transition Notifications

The state machine supports several notification events. Each state can receive an OnEnter and OnLeave notification, and the state machine itself supports OnEnter, OnLeave and OnReset.

When machine.TransitionTo<TState>() is called, the OnEnter and OnLeave methods get called, in the following order:

  1. Configuration.OnLeave
  2. OldState.OnLeave
  3. NewState.OnEnter
  4. Configuration.OnEnter

When machine.ResetTo<TState>() is called, only the Configuration.OnReset() method is called.

You can do anything in the handlers you want, but in general the handlers on the individual states are used to trigger behaviour in your application, such as toggling display elements, and the handlers on the configuration are used for logging, auditing, etc of all state changes.

Each handler is passed a StateChangeEventArgs object, which contains properties for the switches, previous state and current state.

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 was computed. 
.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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.2.0 881 1/20/2019
4.1.0 1,207 10/9/2015
3.1.0 1,091 6/12/2015
3.0.0 994 6/8/2015
2.1.0 996 6/7/2015
1.0.13 1,036 6/5/2015
1.0.10 1,189 5/16/2015
1.0.9 1,072 3/22/2015