Transition - SimpleStateMachine/SimpleStateMachineLibrary GitHub Wiki

Сontent

Create

You can add transition set states object or states name

//Throw exeption if transition already exist
Transition transition1 = stateMachine.AddTransition("Transition1", state1, state2);
Transition transition1 = stateMachine.AddTransition("Transition1", "state1", state2);
Transition transition1 = stateMachine.AddTransition("Transition1", state1, "state2");
Transition transition1 = stateMachine.AddTransition("Transition1", "state1", "state2");

//Return null if transition already exist
Transition transition1 = stateMachine.TryAddTransition(out bool result, "Transition1", state1, state2);
Transition transition1 = stateMachine.TryAddTransition(out bool result, "Transition1", "state1", state2);
Transition transition1 = stateMachine.TryAddTransition(out bool result, "Transition1", state1, "state2");
Transition transition1 = stateMachine.TryAddTransition(out bool result, "Transition1", "state1", "state2");

You can add transition from this state

//Throw exeption if transition already exist
//Add transition from state1 to state2
Transition transition1 = state1.AddTransitionFromThis("Transition1", state2);
Transition transition1 = state1.AddTransitionFromThis("Transition1", "state2");

//Return null if transition already exist
Transition transition1 = state1.TryAddTransitionFromThis(out bool result, "Transition1", state2);
Transition transition1 = state1.TryAddTransitionFromThis(out bool result, "Transition1", "state2");

You can add transition to this state

//Throw exeption if transition already exist
//Add transition from state1 to state2
Transition transition1 = state2.AddTransitionToThis("Transition1", state1);
Transition transition1 = state2.AddTransitionToThis("Transition1", "state1");

//Return null if transition already exist
Transition transition1 = state2.TryAddTransitionToThis(out bool result, "Transition1", state1);
Transition transition1 = state2.TryAddTransitionToThis(out bool result, "Transition1", "state1");

Get

//Throw exeption if transition not found
Transition transition1 = stateMachine.GetTransition("Transition1");

//Return null if transition not found
Transition transition1 = stateMachine.TryGetTransition("Transition1", out bool result);

You can get all transitions from this state

//From state machine

//Throw exeption if transitions not found
Dictionary<string, Transition> transitions = stateMachine.GetTransitionsFromState(state1);
Dictionary<string, Transition> transitions = stateMachine.GetTransitionsFromState("State1");

//Return empty dictionary if transitions not found
Dictionary<string, Transition> transitions = stateMachine.TryGetTransitionsFromState(state1, out bool result);
Dictionary<string, Transition> transitions = stateMachine.TryGetTransitionsFromState("State1", out bool result);

//From state

//Throw exeption if transitions not found
Dictionary<string, Transition> transitions = state1.GetTransitionsFromThis();

//Return empty dictionary if transitions not found
Dictionary<string, Transition> transitions = state1.TryGetTransitionsFromThis(out bool result);

You can get all transitions to this state

//From state machine

//Throw exeption if transitions not found
Dictionary<string, Transition> transitions = stateMachine.GetTransitionsToState(state2);
Dictionary<string, Transition> transitions = stateMachine.GetTransitionsToState("State2");

//Return empty dictionary if transitions not found
Dictionary<string, Transition> transitions = stateMachine.GetTransitionsToState(state2, out bool result);
Dictionary<string, Transition> transitions = stateMachine.GetTransitionsToState("State2", out bool result);

//From state

//Throw exeption if transitions not found
Dictionary<string, Transition> transitions = state2.GetTransitionsToThis();

//Return empty dictionary if transitions not found
Dictionary<string, Transition> transitions = state2.TryGetTransitionsToThis(out bool result);

Exists

//You can check on exists
bool Transition1IsExists = stateMachine.TransitionExists("Transition1")

Delete

Delete with name
 //Throw exeption if transition not found
stateMachine.DeleteTransition("Transition1");

stateMachine.TryDeleteTransition("Transition1");
Delete with object

//Throw exeption if transition not found
stateMachine.DeleteTransition(transition1);

stateMachine.TryDeleteTransition(transition1);

//Throw exeption if transition already delete from state machine
transition1.Delete();

transition1.TryDelete(out bool result);

Entry

Triggered on transition from state. You can set many action for one transition

Action Syntax
void ActionOnInvoke(Transition transition, Dictionary<string, object> parameters)
{

}
Add action

//you can set action with add
Transition transition1 = stateMachine.AddTransition("Transition1", state1, state2, ActionOnInvoke);

//you can set action after add
Transition transition1 = stateMachine.AddTransition("Transition1", state1, state2).OnInvoke(ActionOnInvoke);

transition1.OnInvoke(ActionOnInvoke);

Invoke

Transition invoke after entry and exit state (state from for transition) events


state.StateMachine.InvokeTransition("Transition1");

state.StateMachine.InvokeTransition(transition1);

transition1.Invoke();

Parameters

You can add parameters for invoke transition.

It's parameters will be use in:

  • Invoke Transition
  • Entry State
  • Exit State

Parameters it's collection of keys and values

//Example
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "Key1", Value1 } };

You can set parameters with invoke

StateMachine.InvokeTransition("Transition1", parameters);

After invoke

StateMachine.InvokeTransition("Transition3").AddParameters(parameters);

You can add parameters one by one

StateMachine.InvokeTransition("Transition3").AddParameter("Key1", Value1).AddParameter("Key2", Value1)

Or mixed addition

StateMachine.InvokeTransition("Transition1", parameters).AddParameter("Key1", Value1).AddParameters(parameters2).AddParameter("Key2", Value2);