version 0.120815 (beta)
Abstract Finite State Machine created via strings and delegates.
Main.cs
Who-to-use example with console Main function.
FiniteStateMachine.cs
FSM class file. License under the terms of the GNU Lesser General Public License (LGPL) version 2.1
Init class member with start condition name and function of this condition:
public void StartStateFunc()
{
// Do anything
}
...
// delegate type here is "void foo()"
var fsm = new FiniteStateMachine("StartState", StartStateFunc);
Add some states and functions:
public void StateAFunc()
{
// Do something else
}
public void StateBFunc()
{
// Do something else
}
...
fsm.AddState("A", StateAFunc);
fsm.AddState("B", StateBFunc);
And if you need you can add some transitions. Transition look like state, which connect two exist state, but they have only one type of events for finishing. You can use FSM without transitions, but it can help you support good logical structure:
public void ABConnect()
{
// Do transition
}
fsm.AddTransition("A", "B", TransferWater);
At the end - add event for control our state conversion:
AddEvent("EventAB", "A", "B");
Let's try our FSM:
fsm.EventHappen("EventAB"); // a -> transition to b
fsm.Invoke(); // Invoke ABConnect()
fsm.EventHappen("finish transition"); // transition -> b
fsm.Invoke(); // Invoke StateBFunc()
If you want, you can back-up you FSM to first state, but use it accurate:
fsm.CanReset = true; // allow back-up to first step, false by default
fsm.Reset();
###FSM Logical Presentation And last one - you can save you FSM like graph with all states, transitions and events in file. At current version only dot format is supported. You can view this file in any type of text editor or like vector image in special application like graphviz
fsm.SaveToFile("dot"); // save *.dot file
Clone repo:
$git clone git://github.com/IslamovDenis/Csharp-FSM-.git
On Windows - open FiniteStateMachineProject -> FiniteStateMachineProject.sln by VS2010 and compile
On Mac/Linux - download mono and In Console/Terminal:
$gmcs FiniteStateMachine.cs Main.cs
$mono FiniteStateMachine.exe