public static void RunSimple() { StateBase initialState = new StateA() { A = 1 }; IReadOnlyCollection <ITaskBase <StateBase, StateBase> > tasks = new ITaskBase <StateBase, StateBase>[] { new DeterministicTask(), new DryTask(), new GenericInputTask() }; Logger.Message($"InitialState: {initialState}"); var terminationState = SimpleExecutor.Run(initialState, tasks); Logger.Message($"FinalState: {terminationState}"); initialState = new StateA() { A = 2 }; Logger.Message($"InitialState: {initialState}"); terminationState = SimpleExecutor.Run(initialState, tasks); Logger.Message($"FinalState: {terminationState}"); }
private void Start() { _fsmSystem = new FSMSystem(); IFSMState stateA = new StateA(_fsmSystem); IFSMState stateB = new StateB(); IFSMState stateC = new StateC(_fsmSystem); stateA[(int)State.B] = stateB; stateA[(int)State.C] = stateC; stateB[(int)State.C] = stateC; stateC[(int)State.A] = stateA; stateC[(int)State.B] = stateB; _fsmSystem.AddState(stateA, true); _fsmSystem.AddState(stateB); _fsmSystem.AddState(stateC); }
public void Test_demo() { Record record = new Record(); StateA stateA = new StateA(); StateB stateB = new StateB(); DemoContext context = new DemoContext(stateA); { context.RequestA(record); Assert.That(record.HandlerType, Is.EqualTo(HandlerType.A)); Assert.That(record.StateType, Is.EqualTo(StateType.A)); } { context.RequestB(record); Assert.That(record.HandlerType, Is.EqualTo(HandlerType.B)); Assert.That(record.StateType, Is.EqualTo(StateType.A)); } context.TransitorTo(stateB); { context.RequestA(record); Assert.That(record.HandlerType, Is.EqualTo(HandlerType.A)); Assert.That(record.StateType, Is.EqualTo(StateType.B)); } { context.RequestB(record); Assert.That(record.HandlerType, Is.EqualTo(HandlerType.B)); Assert.That(record.StateType, Is.EqualTo(StateType.B)); } }
public static StateA getInstance() { if (instance == null) { instance = new StateA(); } return(instance); }
// <---Hold--- //Idle Run // ---Push---> static void Main(string[] args) { /*StateMachine<DummyState, DummyEvent> sm = StateMachine<DummyState, DummyEvent>.getInstance(); * * //sm.pushRule(DummyState.Idle, DummyEvent.Push, DummyState.Run, (DummyState last, DummyState curr) => { System.Console.WriteLine("idle--->push:"+last+","+curr); }); * //sm.pushRule(DummyState.Run, DummyEvent.Hold, DummyState.Idle, (DummyState last, DummyState curr) => { System.Console.WriteLine("run--->hold:" + last + "," + curr); }); * * * sm.pushRule(DummyState.Idle, DummyEvent.Push, (DummyState state, DummyEvent evt) => { DummyState result = DummyState.Run; System.Console.WriteLine(state + "," + evt + "->" + result); return result; }); * sm.pushRule(DummyState.Run, DummyEvent.Hold, (DummyState state, DummyEvent evt) => { DummyState result = DummyState.Idle; System.Console.WriteLine(state + "," + evt + "->" + result); return result; }); * * sm.sendEvent(DummyEvent.Push); * sm.sendEvent(DummyEvent.Hold); * sm.sendEvent(DummyEvent.Hold);*/ StateMachine <BaseState, Event> sm2 = StateMachine <BaseState, Event> .getInstance(); sm2.pushRule(null, Event.Enter, (BaseState from, Event evt) => { BaseState to = StateA.getInstance(); if (from != null) { from.exit(to); } to.enter(from); return(to); }); sm2.pushRule(StateA.getInstance(), Event.Enter, (BaseState from, Event evt) => { BaseState to = StateB.getInstance(); if (from != null) { from.exit(to); } to.enter(from); return(to); }); sm2.pushRule(StateB.getInstance(), Event.Enter, (BaseState from, Event evt) => { BaseState to = StateA.getInstance(); if (from != null) { from.exit(to); } to.enter(from); return(to); }); sm2.sendEvent(Event.Enter); sm2.sendEvent(Event.Enter); sm2.sendEvent(Event.Enter); System.Console.ReadKey(); }
void Start() { StateA stateA = new StateA(); StateB stateB = new StateB(); ReactiveStateMachine <Triggers, IState> rsm; rsm = new ReactiveStateMachine <Triggers, IState>(); rsm.OnEnter(stateA).Subscribe(e => Debug.Log(((StateA)e).aString)).AddTo(this); rsm.OnEnter(stateB).Subscribe(e => Debug.Log(((StateB)e).anInt)).AddTo(this); rsm.AddTransition(stateA, Triggers.TriggerB, stateB); rsm.AddTransition(stateB, Triggers.TriggerA, stateA); rsm.Start(stateA); rsm.Trigger(Triggers.TriggerA); rsm.Trigger(Triggers.TriggerB); rsm.Trigger(Triggers.TriggerC); //should not do anything rsm.Trigger(Triggers.TriggerB); //should not do anything rsm.Trigger(Triggers.TriggerA); /* * ReactiveStateMachine<Triggers, States> rsm2; * rsm2 = new ReactiveStateMachine<Triggers, States>(); * * rsm2.AddTransition(States.StateA, Triggers.TriggerB, States.StateB); * rsm2.AddTransition(States.StateB, Triggers.TriggerA, States.StateA); * * rsm2.OnEnter(States.StateA).Subscribe(e => Debug.Log(e + " OnEnter")); * rsm2.OnExit(States.StateA).Subscribe(e => Debug.Log(e + " OnExit")); * * rsm2.OnEnter(States.StateB).Subscribe(e => Debug.Log(e + " OnEnter")); * rsm2.OnExit(States.StateB).Subscribe(e => Debug.Log(e + " OnExit")); * * rsm2.Start(States.StateB); * * rsm2.Trigger(Triggers.TriggerA); * rsm2.Trigger(Triggers.TriggerB); * rsm2.Trigger(Triggers.TriggerC); //should not do anything * rsm2.Trigger(Triggers.TriggerB); //should not do anything * rsm2.Trigger(Triggers.TriggerA); */ }
public ItemA() { //read from somewhere else, pass it in, etc. state = new StateA(); }