/// <summary> /// Transition to a new state /// </summary> /// <param name="transition"></param> protected virtual void ExecuteTransition(Transition transition) { // Default checking, if this is a valid transition. if (CurrentState.StateName != transition.SourceStateName) { var message = string.Format( "Transition has wrong source state {0}, when system is in {1}", transition.SourceStateName, CurrentState.StateName); RaiseStateMachineSystemEvent( "State machine: Default guard execute transition.", message); return; } if (StateList.ContainsKey(transition.TargetStateName) == false) { var message = string.Format( "Transition has wrong target state {0}, when system is in {1}. State not in global state list", transition.SourceStateName, CurrentState.StateName); RaiseStateMachineSystemEvent( "State machine: Default guard execute transition.", message); return; } // Run all exit actions of the old state CurrentState.ExitActions.ForEach(a => a.Execute()); // Run all guards of the transition transition.GuardList.ForEach(g => g.Execute()); string info = transition.GuardList.Count + " guard actions executed!"; RaiseStateMachineSystemEvent( "State machine: ExecuteTransition", info); // Run all actions of the transition transition.TransitionActionList.ForEach(a => a.Execute()); ////////////////// // Stage change ////////////////// info = transition.TransitionActionList.Count + " transition actions executed!"; RaiseStateMachineSystemEvent( "State machine: Begin state change!", info); // First resolve the target state with the help of its name var targetState = GetStateFromStateList(transition.TargetStateName); // Transition successful - Change state PreviousState = CurrentState; CurrentState = targetState; // Run all entry actions of new state CurrentState.EntryActions.ForEach(a => a.Execute()); RaiseStateMachineSystemEvent( "State machine: State change completed successfully!", $"Previous state: {PreviousState.StateName} - New state = {CurrentState.StateName}"); }
public void ExecuteTransistion(Transistion transition) { if (CurrenState.StateName != transition.SourceStateName) { string message = String.Format("Transisition has wrong source state {0}, when system is in {1}", transition.SourceStateName, CurrenState.StateName); RaiseStateMachineSystemEvent("State machine: default guard execute transistion", message); return; } if (!StateList.ContainsKey(transition.TargetStateName)) { string message = String.Format("Transisition has wrong target state {0},when system is in {1}. State not in global", transition.SourceStateName, CurrenState.StateName); RaiseStateMachineSystemEvent("State machine: Default guard execute transition ", message); return; } CurrenState.ExitActions.ForEach(a => a.Execute()); transition.GuardList.ForEach(g => g.Execute()); string info = transition.GuardList.Count + " guard actions executed"; RaiseStateMachineSystemEvent("State machine: ExecuteTransistion", info); //Run all transistion action list transition.TransistionActionList.ForEach(t => t.Execute()); //State change info = transition.TransistionActionList.Count + " transistion actions executed"; RaiseStateMachineSystemEvent("State machine: Begin state change", info); var targetState = GetStateFromStateList(transition.TargetStateName); PreviouState = CurrenState; CurrenState = targetState; foreach (var entryActions in CurrenState.EntryActions) { entryActions.Execute(); } RaiseStateMachineSystemEvent("State machine: State change completed succesfully", "Previous State" + PreviouState.StateName + " New State= " + CurrenState.StateName); }