SimpleStateMachineLibrary NuGet Pre Release
A C# library for realization simple state-machine on .Net
If you like or are using this project please give it a star. Thanks!
State machine properties:
- Start state
- Entry/exit events for state
- Invoke event for transition
- Parameters for transitions
- Parameters for entry/exit for state
Useful extensions for work:
- State changed event for state machine
- Data for sharing between states
- Change event for data
- Export/Import to/from XML
- Logging
StateMachine stateMachine = new StateMachine(); //Add states State state1 = stateMachine.AddState("State1"); State state2 = stateMachine.AddState("State2"); State state3 = stateMachine.AddState("State3"); State state4 = stateMachine.AddState("State4"); //Add transitions three ways: //Standart way Transition transition1 = stateMachine.AddTransition("Transition1", state1, state2); //From state Transition transition2 = state2.AddTransitionFromThis("Transition2", state3); //To state Transition transition3 = state4.AddTransitionToThis("Transition3", state3); //Add action on entry or/and exit state1.OnExit(Action1); state2.OnEntry(Action2); state3.OnExit(Action3); state4.OnExit(Action4); //Set start state state1.SetAsStartState(); //Start work stateMachine.Start();
void ActionOnEtnry(State state, Dictionary<string, object> parameters) { //you need invoke transition in entry or exit action, differently work state machine will be end state.StateMachine.InvokeTransition("Transition1"); }
void ActionOnChangeState(State stateFrom, State stateTo) { }
void ActionOnTransitionInvoke(Transition transition, Dictionary<string, object> parameters) { }
- StateMachine
- State
- Transition
- Data
Copyright (c) SimpleStateMachine
Licensed under the MIT license.