Here is a toy console app example of a basic FSM.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// DESC: QAD-FSM (Quick And Dirty Finite State Machine)
//
// Notes: In its simplest form a state machine is
// • A set of states
// • A set of events
// • A set of transitions that define
// the next state given the current state and event.
// • A method of tracking current state
//
// Example:
//
// I want to create a different kind of door lock that
// has the following states:
//
// 1. LBS - Locked_Both_Sides
// 2. UBS - Unlocked_Both_Sides
// 3. LFO - Locked_From_Outside
//
// and has the following events:
//
// 1. OKT - Outside Key Turn
// 2. IKT - Inside Key Turn
//
// Transistions will be as follows:
//
// CurrState Event NextState Desc
// ========================================================
// LBS OKT UBS When both sides locked, outside key turn unlocks both sides
// LBS IKT LFO When both sides locked, inside key turn unlocks inside
// UBS OKT LFO When both sides unlocked, outside key turn locks outside
// UBS IKT LBS When both sides unlocked, inside key turn locks both sides
// LFO OKT UBS When only outside locked, outside key turn unlocks outside
// LFO IKT LBS When only outside locked, inside key turn locks both sides.
namespace FSM
{
// The FSM states
enum State
{
LBS,
UBS,
LFO
}
// The FSM events
enum Event
{
IKT,
OKT
}
class Transition
{
public State currState { get; set; }
public Event evnt { get; set; }
public State nextState { get; set; }
}
class Program
{
static void Main(string[] args)
{
var fsm = new FSM();
System.Console.WriteLine("Current State: " + fsm.StateDesc[fsm.CurrentState]);
string input = "";
while (input != "x")
{
System.Console.Write("Enter key turn [IKT, OKT] or x to exit: ");
input = System.Console.ReadLine();
if (input == "x") break;
Event evnt;
if (!Enum.TryParse(input, out evnt))
{
System.Console.WriteLine("Invalid input: " + input + ", enter one of [IKT,OKT,x]");
continue;
}
fsm.ChangeState(evnt);
System.Console.WriteLine("New State: " + fsm.StateDesc[fsm.CurrentState]);
}
System.Console.WriteLine("");
System.Console.WriteLine("History");
System.Console.WriteLine("===============================================");
System.Console.WriteLine("CurrState(Event) => NextState");
System.Console.WriteLine("===============================================");
fsm.hist
.Select(h => h.currState.ToString() + "(" + h.evnt.ToString() + ") => " + h.nextState.ToString())
.ToList()
.ForEach(h => System.Console.WriteLine(h));
}
}
class FSM
{
public Dictionary<State, String> StateDesc = new Dictionary<State, String>()
{
{State.LBS, "Both Sides Locked"},
{State.LFO, "Locked From Outside"},
{State.UBS, "Both Sides Unlocked"}
};
public List<Transition> hist = new List<Transition>();
// Create FSM transitions.
List<Transition> trans = new List<Transition>
{
new Transition() { currState = State.LBS, evnt = Event.OKT, nextState = State.UBS },
new Transition() { currState = State.LBS, evnt = Event.IKT, nextState = State.LFO },
new Transition() { currState = State.UBS, evnt = Event.OKT, nextState = State.LFO },
new Transition() { currState = State.UBS, evnt = Event.IKT, nextState = State.LBS },
new Transition() { currState = State.LFO, evnt = Event.OKT, nextState = State.UBS },
new Transition() { currState = State.LFO, evnt = Event.IKT, nextState = State.LBS },
};
public State CurrentState { get { var lt = hist.FirstOrDefault(); return lt == null ? State.UBS : lt.nextState; } }
public State? ChangeState(Event evnt)
{
var t = trans.Find(r => r.currState == CurrentState && r.evnt == evnt);
if (t == null) return null; // If you don't create transitions that cover all combinations this could happen.
hist.Insert(0, t);
return t.nextState;
}
}
}