public void MultipleEntryActions( PassiveStateMachine <int, int> machine, bool entryAction1Executed, bool entryAction2Executed) { "establish a state machine with several entry actions on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnEntry(() => entryAction1Executed = true) .ExecuteOnEntry(() => entryAction2Executed = true); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when entering the state".x(() => { machine.Start(); }); "It should execute all entry actions".x(() => { entryAction1Executed .Should().BeTrue("first action should be executed"); entryAction2Executed .Should().BeTrue("second action should be executed"); }); }
public void ExceptionHandling( PassiveStateMachine <int, int> machine, bool entryAction1Executed, bool entryAction2Executed, bool entryAction3Executed) { var exception2 = new Exception(); var exception3 = new Exception(); var receivedExceptions = new List <Exception>(); "establish a state machine with several entry actions on a state and some of them throw an exception".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnEntry(() => entryAction1Executed = true) .ExecuteOnEntry(() => { entryAction2Executed = true; throw exception2; }) .ExecuteOnEntry(() => { entryAction3Executed = true; throw exception3; }); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); machine.TransitionExceptionThrown += (s, e) => receivedExceptions.Add(e.Exception); }); "when entering the state".x(() => { machine.Start(); }); "it should execute all entry actions on entry".x(() => { entryAction1Executed .Should().BeTrue("action 1 should be executed"); entryAction2Executed .Should().BeTrue("action 2 should be executed"); entryAction3Executed .Should().BeTrue("action 3 should be executed"); }); "it should handle all exceptions of all throwing entry actions by firing the TransitionExceptionThrown event".x(() => receivedExceptions .Should() .HaveCount(2) .And .Contain(exception2) .And .Contain(exception3)); }
public void NoExceptionHandlerRegistered( PassiveStateMachine <int, int> machine, Exception catchedException) { "establish an exception throwing state machine without a registered exception handler".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(Values.Source) .On(Values.Event) .Execute(() => throw Values.Exception); machine = stateMachineDefinitionBuilder .WithInitialState(Values.Source) .Build() .CreatePassiveStateMachine(); machine.Start(); }); "when an exception occurs".x(() => catchedException = Catch.Exception(() => machine.Fire(Values.Event))); "should (re-)throw exception".x(() => catchedException.InnerException .Should().BeSameAs(Values.Exception)); }
public void NoMatchingGuard( PassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { bool declined = false; "establish state machine with no matching guard".x(() => { machine = new PassiveStateMachine <int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => false).Goto(ErrorState); machine.TransitionDeclined += (sender, e) => declined = true; machine.Initialize(SourceState); machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should notify about declined transition".x(() => declined.Should().BeTrue("TransitionDeclined event should be fired")); }
public SimpleStateMachine() { var builder = new StateMachineDefinitionBuilder <States, Events>(); builder .In(States.Off) .On(Events.TurnOn) .Goto(States.On) .Execute(SayHello); builder .In(States.On) .On(Events.TurnOff) .Goto(States.Off) .Execute(SayBye); builder .WithInitialState(States.Off); machine = builder .Build() .CreatePassiveStateMachine(); machine.Start(); }
public void ExitActionException(PassiveStateMachine <int, int> machine) { "establish an exit action throwing an exception".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(Values.Source) .ExecuteOnExit(() => throw Values.Exception) .On(Values.Event) .Goto(Values.Destination); machine = stateMachineDefinitionBuilder .WithInitialState(Values.Source) .Build() .CreatePassiveStateMachine(); machine.TransitionExceptionThrown += (s, e) => this.receivedTransitionExceptionEventArgs = e; }); "when executing the transition".x(() => { machine.Start(); machine.Fire(Values.Event, Values.Parameter); }); this.ItShouldHandleTransitionException(); }
public void Start( PassiveStateMachine <int, int> machine, bool entryActionExecuted, CurrentStateExtension currentStateExtension) { "establish a state machine".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(TestState) .ExecuteOnEntry(() => entryActionExecuted = true); machine = stateMachineDefinitionBuilder .WithInitialState(TestState) .Build() .CreatePassiveStateMachine(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); }); "when starting the state machine".x(() => machine.Start()); "should set current state of state machine to state to which it is initialized".x(() => currentStateExtension.CurrentState.Should().Be(TestState)); "should execute entry action of state to which state machine is initialized".x(() => entryActionExecuted.Should().BeTrue()); }
public void StartingException(PassiveStateMachine <int, int> machine) { const int State = 1; "establish a entry action for the initial state that throws an exception".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnEntry(() => throw Values.Exception); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); machine.TransitionExceptionThrown += (s, e) => this.receivedTransitionExceptionEventArgs = e; }); "when starting the state machine".x(() => machine.Start()); "should catch exception and fire transition exception event".x(() => this.receivedTransitionExceptionEventArgs.Exception.Should().NotBeNull()); "should pass thrown exception to event arguments of transition exception event".x(() => this.receivedTransitionExceptionEventArgs.Exception.Should().BeSameAs(Values.Exception)); }
public void MatchingGuard( PassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with guarded transitions".x(() => { machine = new PassiveStateMachine <int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => false).Goto(ErrorState) .If(() => true).Goto(DestinationState) .If(() => true).Goto(ErrorState) .Otherwise().Goto(ErrorState); machine.Initialize(SourceState); machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should take transition guarded with first matching guard".x(() => currentStateExtension.CurrentState.Should().Be(DestinationState)); }
private void ConfigureStateMachine() { mToiletStateMachine = new PassiveStateMachine <ToiletStates, ToiletEvents>(); mCurrentStateExtension = new CurrentStateExtension(); mToiletStateMachine.AddExtension(mCurrentStateExtension); mToiletStateMachine.In(ToiletStates.WaitingForStart) .ExecuteOnEntry(StopStateMachine) .On(ToiletEvents.StartStateMachine).Goto(ToiletStates.WaitingForMan); mToiletStateMachine.In(ToiletStates.WaitingForMan) .ExecuteOnEntry(WaitForMan) .On(ToiletEvents.ManArrived).Goto(ToiletStates.ManInfrontOfToilet) .On(ToiletEvents.StopStateMachine).Goto(ToiletStates.WaitingForStart); mToiletStateMachine.In(ToiletStates.ManInfrontOfToilet) .ExecuteOnEntry(ManArrived) .On(ToiletEvents.ManPees).Goto(ToiletStates.ManIsPeeing) .On(ToiletEvents.ManLeft).Goto(ToiletStates.WaitingForMan) .On(ToiletEvents.StopStateMachine).Goto(ToiletStates.WaitingForStart); mToiletStateMachine.In(ToiletStates.ManIsPeeing). ExecuteOnEntry(ManIsPeeing) .On(ToiletEvents.ManLeft).Goto(ToiletStates.PeeingFinished) .On(ToiletEvents.StopStateMachine).Goto(ToiletStates.WaitingForStart); mToiletStateMachine.In(ToiletStates.PeeingFinished) .ExecuteOnEntry(Flush) .On(ToiletEvents.FlushFinished).Goto(ToiletStates.WaitingForMan) .On(ToiletEvents.StopStateMachine).Goto(ToiletStates.WaitingForStart); mToiletStateMachine.Initialize(ToiletStates.WaitingForStart); mToiletStateMachine.Start(); }
public void BeforeExecutingEntryActions( PassiveStateMachine <string, int> machine, IExtension <string, int> extension) { "establish an extension".x(() => extension = A.Fake <IExtension <string, int> >()); "establish a state machine using the extension".x(() => { machine = new PassiveStateMachine <string, int>(Name); machine.AddExtension(extension); machine.In("0") .On(1).Goto("1"); machine.Initialize("0"); machine.Start(); }); "when firing an event onto the state machine".x(() => machine.Fire(1)); "it should call EnteringState on registered extensions for target state".x(() => A.CallTo(() => extension.EnteringState( A <IStateMachineInformation <string, int> > .That.Matches(x => x.Name == Name && x.CurrentStateId == "1"), A <IState <string, int> > .That.Matches(x => x.Id == "1"), A <ITransitionContext <string, int> > .That.Matches(x => x.EventId.Value == 1))) .MustHaveHappened()); }
public void ExitActionWithParameter( PassiveStateMachine <int, int> machine, string parameter) { const string Parameter = "parameter"; "establish a state machine with exit action with parameter on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExitParametrized(p => parameter = p, Parameter) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(() => { machine.Start(); machine.Fire(Event); }); "it should execute the exit action".x(() => parameter.Should().NotBeNull()); "it should pass parameter to the exit action".x(() => parameter.Should().Be(Parameter)); }
public void MultipleExitActions( PassiveStateMachine <int, int> machine, bool exitAction1Executed, bool exitAction2Executed) { "establish a state machine with several exit actions on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit(() => exitAction1Executed = true) .ExecuteOnExit(() => exitAction2Executed = true) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(() => { machine.Start(); machine.Fire(Event); }); "It should execute all exit actions".x(() => { exitAction1Executed .Should().BeTrue("first action should be executed"); exitAction2Executed .Should().BeTrue("second action should be executed"); }); }
public void EventArgument( PassiveStateMachine <int, int> machine, int passedArgument) { const int Argument = 17; "establish a state machine with an exit action taking an event argument".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit((int argument) => passedArgument = argument) .On(Event).Goto(AnotherState); stateMachineDefinitionBuilder .In(AnotherState) .ExecuteOnEntry((int argument) => passedArgument = argument); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(() => { machine.Start(); machine.Fire(Event, Argument); }); "it should pass event argument to exit action".x(() => passedArgument.Should().Be(Argument)); }
public void ExitAction( PassiveStateMachine <int, int> machine, bool exitActionExecuted) { "establish a state machine with exit action on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit(() => exitActionExecuted = true) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(() => { machine.Start(); machine.Fire(Event); }); "it should execute the exit action".x(() => exitActionExecuted.Should().BeTrue()); }
public void NoMatchingGuard( PassiveStateMachine <int, int> machine) { var declined = false; "establish state machine with no matching guard".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(SourceState) .On(Event) .If(() => false).Goto(ErrorState); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); machine.TransitionDeclined += (sender, e) => declined = true; machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should notify about declined transition".x(() => declined.Should().BeTrue("TransitionDeclined event should be fired")); }
public void OtherwiseGuard( PassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with otherwise guard and no machting other guard".x(() => { machine = new PassiveStateMachine <int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => false).Goto(ErrorState) .Otherwise().Goto(DestinationState); machine.Initialize(SourceState); machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should_take_transition_guarded_with_otherwise".x(() => currentStateExtension.CurrentState.Should().Be(DestinationState)); }
public void MatchingGuard( PassiveStateMachine<int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with guarded transitions"._(() => { machine = new PassiveStateMachine<int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => false).Goto(ErrorState) .If(() => true).Goto(DestinationState) .If(() => true).Goto(ErrorState) .Otherwise().Goto(ErrorState); machine.Initialize(SourceState); machine.Start(); }); "when an event is fired"._(() => machine.Fire(Event)); "it should take transition guarded with first matching guard"._(() => currentStateExtension.CurrentState.Should().Be(DestinationState)); }
public void CustomTypesForStatesAndEvents( PassiveStateMachine <MyState, MyEvent> machine, bool arrivedInStateB) { "establish a state machine with custom types for states and events".x(() => { machine = new PassiveStateMachine <MyState, MyEvent>(); machine.In(new MyState("A")) .On(new MyEvent(1)).Goto(new MyState("B")); machine.In(new MyState("B")) .ExecuteOnEntry(() => arrivedInStateB = true); machine.Initialize(new MyState("A")); machine.Start(); }); "when using the state machine".x(() => machine.Fire(new MyEvent(1))); "it should use equals to compare states and events".x(() => arrivedInStateB.Should().BeTrue("state B should be current state")); }
public void CustomTypesForStatesAndEvents( PassiveStateMachine<MyState, MyEvent> machine, bool arrivedInStateB) { "establish a state machine with custom types for states and events"._(() => { machine = new PassiveStateMachine<MyState, MyEvent>(); machine.In(new MyState("A")) .On(new MyEvent(1)).Goto(new MyState("B")); machine.In(new MyState("B")) .ExecuteOnEntry(() => arrivedInStateB = true); machine.Initialize(new MyState("A")); machine.Start(); }); "when using the state machine"._(() => machine.Fire(new MyEvent(1))); "it should use equals to compare states and events"._(() => arrivedInStateB.Should().BeTrue("state B should be current state")); }
public QuestEditorController(InventoryController inventoryController, HotbarController hotbarController, PlayerRobotSimulator playerRobotSimulator) { this.inventoryController = inventoryController; this.hotbarController = hotbarController; this.playerRobotSimulator = playerRobotSimulator; createStateMachine(); fsm.Start(); }
public void CanStartFSM() { fsm = new PassiveStateMachine <SM.ProcessState, SM.Command>(); fsm.Initialize(SM.ProcessState.Initial); fsm.Fire(SM.Command.OnOpenBattleFront); fsm.Start(); Assert.IsTrue(fsm.IsRunning); }
public void Loading( StateMachineSaver <State> saver, StateMachineLoader <State> loader, FakeExtension extension, State sourceState, State targetState) { "establish a saved state machine with history".x(() => { var machine = new PassiveStateMachine <State, Event>(); DefineMachine(machine); machine.Initialize(State.A); machine.Start(); machine.Fire(Event.S2); // set history of super state S machine.Fire(Event.B); // set current state to B saver = new StateMachineSaver <State>(); loader = new StateMachineLoader <State>(); machine.Save(saver); }); "when state machine is loaded".x(() => { loader.SetCurrentState(saver.CurrentStateId); loader.SetHistoryStates(saver.HistoryStates); extension = new FakeExtension(); var loadedMachine = new PassiveStateMachine <State, Event>(); loadedMachine.AddExtension(extension); DefineMachine(loadedMachine); loadedMachine.Load(loader); loadedMachine.TransitionCompleted += (sender, args) => { sourceState = args.StateId; targetState = args.NewStateId; }; loadedMachine.Start(); loadedMachine.Fire(Event.S); }); "it should reset current state".x(() => sourceState.Should().Be(State.B)); "it should reset all history states of super states".x(() => targetState.Should().Be(State.S2)); "it should notify extensions".x(() => extension.LoadedCurrentState .Should().BeEquivalentTo(State.B)); }
public void LoadingEvents( PassiveStateMachine <string, int> machine) { "establish a state machine".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>(); stateMachineDefinitionBuilder .In("A") .On(1) .Goto("B"); stateMachineDefinitionBuilder .In("B") .On(2) .Goto("C"); stateMachineDefinitionBuilder .In("C") .On(3) .Goto("A"); machine = stateMachineDefinitionBuilder .WithInitialState("A") .Build() .CreatePassiveStateMachine(); }); "when it is loaded with Events".x(() => { var loader = new StateMachineLoader <string, int>(); loader.SetEvents(new List <EventInformation <int> > { new EventInformation <int>(1, null), new EventInformation <int>(2, null), }); machine.Load(loader); }); "it should process those events".x(() => { var transitionRecords = new List <TransitionRecord>(); machine.TransitionCompleted += (sender, args) => transitionRecords.Add( new TransitionRecord(args.EventId, args.StateId, args.NewStateId)); machine.Start(); transitionRecords .Should() .HaveCount(2) .And .IsEquivalentInOrder(new List <TransitionRecord> { new TransitionRecord(1, "A", "B"), new TransitionRecord(2, "B", "C") }); }); }
public Elevator() { var builder = new StateMachineDefinitionBuilder <States, Events>(); builder.DefineHierarchyOn(States.Healthy) .WithHistoryType(HistoryType.Deep) .WithInitialSubState(States.OnFloor) .WithSubState(States.Moving); builder.DefineHierarchyOn(States.Moving) .WithHistoryType(HistoryType.Shallow) .WithInitialSubState(States.MovingUp) .WithSubState(States.MovingDown); builder.DefineHierarchyOn(States.OnFloor) .WithHistoryType(HistoryType.None) .WithInitialSubState(States.DoorClosed) .WithSubState(States.DoorOpen); builder.In(States.Healthy) .ExecuteOnEntry(() => { }) .On(Events.Error).Goto(States.Error); builder.In(States.Error) .On(Events.Reset).Goto(States.Healthy) .On(Events.Error); builder.In(States.OnFloor) .ExecuteOnEntry(this.AnnounceFloor) .ExecuteOnExit(Beep) .ExecuteOnExit(Beep) // just beep a second time .On(Events.CloseDoor).Goto(States.DoorClosed) .On(Events.OpenDoor).Goto(States.DoorOpen) .On(Events.GoUp) .If(CheckOverload).Goto(States.MovingUp) .Otherwise().Execute(this.AnnounceOverload) .On(Events.GoDown) .If(CheckOverload).Goto(States.MovingDown) .Otherwise().Execute(this.AnnounceOverload); builder.In(States.Moving) .On(Events.Stop).Goto(States.OnFloor); builder.WithInitialState(States.OnFloor); var definition = builder .Build(); elevator = definition .CreatePassiveStateMachine("Elevator"); elevator.Start(); }
public void CommonAncestor( PassiveStateMachine <int, int> machine) { const int CommonAncestorState = 0; const int SourceState = 1; const int ParentOfSourceState = 2; const int SiblingOfSourceState = 3; const int DestinationState = 4; const int ParentOfDestinationState = 5; const int SiblingOfDestinationState = 6; const int Event = 0; var commonAncestorStateLeft = false; "establish a hierarchical state machine".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .DefineHierarchyOn(CommonAncestorState) .WithHistoryType(HistoryType.None) .WithInitialSubState(ParentOfSourceState) .WithSubState(ParentOfDestinationState); stateMachineDefinitionBuilder .DefineHierarchyOn(ParentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(SourceState) .WithSubState(SiblingOfSourceState); stateMachineDefinitionBuilder .DefineHierarchyOn(ParentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(DestinationState) .WithSubState(SiblingOfDestinationState); stateMachineDefinitionBuilder .In(SourceState) .On(Event).Goto(DestinationState); stateMachineDefinitionBuilder .In(CommonAncestorState) .ExecuteOnExit(() => commonAncestorStateLeft = true); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); machine.Start(); }); "when firing an event resulting in a transition with a common ancestor".x(() => machine.Fire(Event)); "the state machine should remain inside common ancestor state".x(() => commonAncestorStateLeft .Should().BeFalse()); }
public void SimpleTransition() { fsm = new PassiveStateMachine <SM.ProcessState, SM.Command>(); fsm.Initialize(SM.ProcessState.Initial); fsm.Fire(SM.Command.OnOpenBattleFront); fsm.Start(); fsm.Fire(SM.Command.OnOuterDoorDown); Assert.IsTrue(fsm.IsRunning); }
public void CommonAncestor( PassiveStateMachine<int, int> machine) { const int CommonAncestorState = 0; const int SourceState = 1; const int ParentOfSourceState = 2; const int SiblingOfSourceState = 3; const int DestinationState = 4; const int ParentOfDestinationState = 5; const int SiblingOfDestinationState = 6; const int Event = 0; bool commonAncestorStateLeft = false; "establish a hierarchical state machine"._(() => { machine = new PassiveStateMachine<int, int>(); machine.DefineHierarchyOn(CommonAncestorState) .WithHistoryType(HistoryType.None) .WithInitialSubState(ParentOfSourceState) .WithSubState(ParentOfDestinationState); machine.DefineHierarchyOn(ParentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(SourceState) .WithSubState(SiblingOfSourceState); machine.DefineHierarchyOn(ParentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(DestinationState) .WithSubState(SiblingOfDestinationState); machine.In(SourceState) .On(Event).Goto(DestinationState); machine.In(CommonAncestorState) .ExecuteOnExit(() => commonAncestorStateLeft = true); machine.Initialize(SourceState); machine.Start(); }); "when firing an event resulting in a transition with a common ancestor"._(() => machine.Fire(Event)); "the state machine should remain inside common ancestor state"._(() => commonAncestorStateLeft .Should().BeFalse()); }
public static void Main() { // configure basic logging (all levels enabled, messages are written to the console) log4net.Config.BasicConfigurator.Configure(); var elevator = new PassiveStateMachine <States, Events>("Elevator"); elevator.AddExtension(new Extensions.Log4NetExtension <States, Events>("Elevator")); elevator.DefineHierarchyOn(States.Healthy, States.OnFloor, HistoryType.Deep, States.OnFloor, States.Moving); elevator.DefineHierarchyOn(States.Moving, States.MovingUp, HistoryType.Shallow, States.MovingUp, States.MovingDown); elevator.DefineHierarchyOn(States.OnFloor, States.DoorClosed, HistoryType.None, States.DoorClosed, States.DoorOpen); elevator.In(States.Healthy) .On(Events.ErrorOccured).Goto(States.Error); elevator.In(States.Error) .On(Events.Reset).Goto(States.Healthy); elevator.In(States.OnFloor) .ExecuteOnEntry(AnnounceFloor) .On(Events.CloseDoor).Goto(States.DoorClosed) .On(Events.OpenDoor).Goto(States.DoorOpen) .On(Events.GoUp) .If(CheckOverload).Goto(States.MovingUp) .Otherwise().Execute(AnnounceOverload) .On(Events.GoDown) .If(CheckOverload).Goto(States.MovingDown) .Otherwise().Execute(AnnounceOverload); elevator.In(States.Moving) .On(Events.Stop).Goto(States.OnFloor); elevator.Initialize(States.OnFloor); elevator.Fire(Events.ErrorOccured); elevator.Fire(Events.Reset); elevator.Start(); elevator.Fire(Events.OpenDoor); elevator.Fire(Events.CloseDoor); elevator.Fire(Events.GoUp); elevator.Fire(Events.Stop); elevator.Fire(Events.OpenDoor); elevator.Stop(); Console.ReadLine(); }
public void ExecutingTransition( PassiveStateMachine <int, int> machine, string actualParameter, bool exitActionExecuted, bool entryActionExecuted) { "establish a state machine with transitions".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(SourceState) .ExecuteOnExit(() => exitActionExecuted = true) .On(Event) .Goto(DestinationState) .Execute <string>(p => actualParameter = p); stateMachineDefinitionBuilder .In(DestinationState) .ExecuteOnEntry(() => entryActionExecuted = true); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); machine.AddExtension(CurrentStateExtension); machine.Start(); }); "when firing an event onto the state machine".x(() => machine.Fire(Event, Parameter)); "it should_execute_transition_by_switching_state".x(() => CurrentStateExtension.CurrentState.Should().Be(DestinationState)); "it should_execute_transition_actions".x(() => actualParameter.Should().NotBeNull()); "it should_pass_parameters_to_transition_action".x(() => actualParameter.Should().Be(Parameter)); "it should_execute_exit_action_of_source_state".x(() => exitActionExecuted.Should().BeTrue()); "it should_execute_entry_action_of_destination_state".x(() => entryActionExecuted.Should().BeTrue()); }
public void BeforeExecutingEntryActionsHierarchical( PassiveStateMachine <string, string> machine, IExtension <string, string> extension) { "establish an extension".x(() => extension = A.Fake <IExtension <string, string> >()); "establish a hierarchical state machine using the extension".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, string>(); stateMachineDefinitionBuilder .DefineHierarchyOn("A") .WithHistoryType(HistoryType.None) .WithInitialSubState("A0"); stateMachineDefinitionBuilder .In("0") .On("A0") .Goto("A0"); machine = stateMachineDefinitionBuilder .WithInitialState("0") .Build() .CreatePassiveStateMachine(Name); machine.AddExtension(extension); machine.Start(); }); "when firing an event onto the state machine".x(() => machine.Fire("A0")); "it should call EnteringState on registered extensions for entered super states of target state".x(() => A.CallTo(() => extension.EnteringState( A <IStateMachineInformation <string, string> > .That.Matches(x => x.Name == Name && x.CurrentStateId.ExtractOrThrow() == "A0"), A <IStateDefinition <string, string> > .That.Matches(x => x.Id == "A"), A <ITransitionContext <string, string> > .That.Matches(x => x.EventId.Value == "A0"))) .MustHaveHappened()); "it should call EnteringState on registered extensions for entered leaf target state".x(() => A.CallTo(() => extension.EnteringState( A <IStateMachineInformation <string, string> > .That.Matches(x => x.Name == Name && x.CurrentStateId.ExtractOrThrow() == "A0"), A <IStateDefinition <string, string> > .That.Matches(x => x.Id == "A0"), A <ITransitionContext <string, string> > .That.Matches(x => x.EventId.Value == "A0"))) .MustHaveHappened()); }
public void InitializationInSuperState( PassiveStateMachine <int, int> machine, CurrentStateExtension testExtension, bool entryActionOfLeafStateExecuted, bool entryActionOfSuperStateExecuted) { "establish a hierarchical state machine with super state as initial state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .DefineHierarchyOn(SuperState) .WithHistoryType(HistoryType.None) .WithInitialSubState(LeafState); stateMachineDefinitionBuilder .In(SuperState) .ExecuteOnEntry(() => entryActionOfSuperStateExecuted = true); stateMachineDefinitionBuilder .In(LeafState) .ExecuteOnEntry(() => entryActionOfLeafStateExecuted = true); machine = stateMachineDefinitionBuilder .WithInitialState(SuperState) .Build() .CreatePassiveStateMachine(); testExtension = new CurrentStateExtension(); machine.AddExtension(testExtension); }); "when initializing to a super state and starting the state machine".x(() => { machine.Start(); }); "it should_set_current_state_of_state_machine_to_initial_leaf_state_of_the_state_to_which_it_is_initialized".x(() => testExtension.CurrentState .Should().Be(LeafState)); "it should_execute_entry_action_of_super_state_to_which_state_machine_is_initialized".x(() => entryActionOfSuperStateExecuted .Should().BeTrue()); "it should_execute_entry_actions_of_initial_sub_states_until_a_leaf_state_is_reached".x(() => entryActionOfLeafStateExecuted .Should().BeTrue()); }
public void InitializationInLeafState( PassiveStateMachine <int, int> machine, CurrentStateExtension testExtension, bool entryActionOfLeafStateExecuted, bool entryActionOfSuperStateExecuted) { "establish a hierarchical state machine with leaf state as initial state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .DefineHierarchyOn(SuperState) .WithHistoryType(HistoryType.None) .WithInitialSubState(LeafState); stateMachineDefinitionBuilder .In(SuperState) .ExecuteOnEntry(() => entryActionOfSuperStateExecuted = true); stateMachineDefinitionBuilder .In(LeafState) .ExecuteOnEntry(() => entryActionOfLeafStateExecuted = true); machine = stateMachineDefinitionBuilder .WithInitialState(LeafState) .Build() .CreatePassiveStateMachine(); testExtension = new CurrentStateExtension(); machine.AddExtension(testExtension); }); "when starting the state machine".x(() => { machine.Start(); }); "it should set current state of state machine to state to which it is initialized".x(() => testExtension.CurrentState .Should().Be(LeafState)); "it should execute entry action of state to which state machine is initialized".x(() => entryActionOfLeafStateExecuted .Should().BeTrue()); "it should execute entry action of super states of the state to which state machine is initialized".x(() => entryActionOfSuperStateExecuted .Should().BeTrue()); }
public void ExecutingTransition( PassiveStateMachine<int, int> machine, string actualParameter, bool exitActionExecuted, bool entryActionExecuted) { "establish a state machine with transitions"._(() => { machine = new PassiveStateMachine<int, int>(); machine.AddExtension(CurrentStateExtension); machine.In(SourceState) .ExecuteOnExit(() => exitActionExecuted = true) .On(Event).Goto(DestinationState).Execute<string>(p => actualParameter = p); machine.In(DestinationState) .ExecuteOnEntry(() => entryActionExecuted = true); machine.Initialize(SourceState); machine.Start(); }); "when firing an event onto the state machine"._(() => machine.Fire(Event, Parameter)); "it should_execute_transition_by_switching_state"._(() => CurrentStateExtension.CurrentState.Should().Be(DestinationState)); "it should_execute_transition_actions"._(() => actualParameter.Should().NotBeNull()); "it should_pass_parameters_to_transition_action"._(() => actualParameter.Should().Be(Parameter)); "it should_execute_exit_action_of_source_state"._(() => exitActionExecuted.Should().BeTrue()); "it should_execute_entry_action_of_destination_state"._(() => entryActionExecuted.Should().BeTrue()); }
public void NoExceptionHandlerRegistered( Exception catchedException) { "establish an exception throwing state machine without a registered exception handler"._(() => { machine = new PassiveStateMachine<int, int>(); machine.In(Values.Source) .On(Values.Event).Execute(() => { throw Values.Exception; }); machine.Initialize(Values.Source); machine.Start(); }); "when an exception occurs"._(() => catchedException = Catch.Exception(() => this.machine.Fire(Values.Event))); "should (re-)throw exception"._(() => catchedException.InnerException .Should().BeSameAs(Values.Exception)); }
public void OtherwiseGuard( PassiveStateMachine<int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with otherwise guard and no machting other guard"._(() => { machine = new PassiveStateMachine<int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => false).Goto(ErrorState) .Otherwise().Goto(DestinationState); machine.Initialize(SourceState); machine.Start(); }); "when an event is fired"._(() => machine.Fire(Event)); "it should_take_transition_guarded_with_otherwise"._(() => currentStateExtension.CurrentState.Should().Be(DestinationState)); }
public void NoMatchingGuard( PassiveStateMachine<int, int> machine, CurrentStateExtension currentStateExtension) { bool declined = false; "establish state machine with no matching guard"._(() => { machine = new PassiveStateMachine<int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => false).Goto(ErrorState); machine.TransitionDeclined += (sender, e) => declined = true; machine.Initialize(SourceState); machine.Start(); }); "when an event is fired"._(() => machine.Fire(Event)); "it should notify about declined transition"._(() => declined.Should().BeTrue("TransitionDeclined event should be fired")); }
public void NoCommonAncestor( PassiveStateMachine<string, int> machine) { const string SourceState = "SourceState"; const string ParentOfSourceState = "ParentOfSourceState"; const string SiblingOfSourceState = "SiblingOfSourceState"; const string DestinationState = "DestinationState"; const string ParentOfDestinationState = "ParentOfDestinationState"; const string SiblingOfDestinationState = "SiblingOfDestinationState"; const string GrandParentOfSourceState = "GrandParentOfSourceState"; const string GrandParentOfDestinationState = "GrandParentOfDestinationState"; const int Event = 0; string log = string.Empty; "establish a hierarchical state machine"._(() => { machine = new PassiveStateMachine<string, int>(); machine.DefineHierarchyOn(ParentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(SourceState) .WithSubState(SiblingOfSourceState); machine.DefineHierarchyOn(ParentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(DestinationState) .WithSubState(SiblingOfDestinationState); machine.DefineHierarchyOn(GrandParentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(ParentOfSourceState); machine.DefineHierarchyOn(GrandParentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(ParentOfDestinationState); machine.In(SourceState) .ExecuteOnExit(() => log += "exit" + SourceState) .On(Event).Goto(DestinationState); machine.In(ParentOfSourceState) .ExecuteOnExit(() => log += "exit" + ParentOfSourceState); machine.In(DestinationState) .ExecuteOnEntry(() => log += "enter" + DestinationState); machine.In(ParentOfDestinationState) .ExecuteOnEntry(() => log += "enter" + ParentOfDestinationState); machine.In(GrandParentOfSourceState) .ExecuteOnExit(() => log += "exit" + GrandParentOfSourceState); machine.In(GrandParentOfDestinationState) .ExecuteOnEntry(() => log += "enter" + GrandParentOfDestinationState); machine.Initialize(SourceState); machine.Start(); }); "when firing an event resulting in a transition without a common ancestor"._(() => machine.Fire(Event)); "it should execute exit action of source state"._(() => log.Should().Contain("exit" + SourceState)); "it should execute exit action of parents of source state (recursively)"._(() => log .Should().Contain("exit" + ParentOfSourceState) .And.Contain("exit" + GrandParentOfSourceState)); "it should execute entry action of parents of destination state (recursively)"._(() => log.Should().Contain("enter" + ParentOfDestinationState) .And.Contain("enter" + GrandParentOfDestinationState)); "it should execute entry action of destination state"._(() => log.Should().Contain("enter" + DestinationState)); "it should execute actions from source upwards and then downwards to destination state"._(() => { string[] states = { SourceState, ParentOfSourceState, GrandParentOfSourceState, GrandParentOfDestinationState, ParentOfDestinationState, DestinationState }; var statesInOrderOfAppearanceInLog = states .OrderBy(s => log.IndexOf(s.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)); statesInOrderOfAppearanceInLog .Should().Equal(states); }); }
public static void Main() { // configure basic logging (all levels enabled, messages are written to the console) log4net.Config.BasicConfigurator.Configure(); var elevator = new PassiveStateMachine<States, Events>("Elevator"); elevator.AddExtension(new Extensions.Log4NetExtension<States, Events>("Elevator")); elevator.DefineHierarchyOn(States.Healthy, States.OnFloor, HistoryType.Deep, States.OnFloor, States.Moving); elevator.DefineHierarchyOn(States.Moving, States.MovingUp, HistoryType.Shallow, States.MovingUp, States.MovingDown); elevator.DefineHierarchyOn(States.OnFloor, States.DoorClosed, HistoryType.None, States.DoorClosed, States.DoorOpen); elevator.In(States.Healthy) .On(Events.ErrorOccured).Goto(States.Error); elevator.In(States.Error) .On(Events.Reset).Goto(States.Healthy); elevator.In(States.OnFloor) .ExecuteOnEntry(AnnounceFloor) .On(Events.CloseDoor).Goto(States.DoorClosed) .On(Events.OpenDoor).Goto(States.DoorOpen) .On(Events.GoUp) .If(CheckOverload).Goto(States.MovingUp) .Otherwise().Execute(AnnounceOverload) .On(Events.GoDown) .If(CheckOverload).Goto(States.MovingDown) .Otherwise().Execute(AnnounceOverload); elevator.In(States.Moving) .On(Events.Stop).Goto(States.OnFloor); elevator.Initialize(States.OnFloor); elevator.Fire(Events.ErrorOccured); elevator.Fire(Events.Reset); elevator.Start(); elevator.Fire(Events.OpenDoor); elevator.Fire(Events.CloseDoor); elevator.Fire(Events.GoUp); elevator.Fire(Events.Stop); elevator.Fire(Events.OpenDoor); elevator.Stop(); Console.ReadLine(); }