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();
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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"));
        }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        public void Start(
            PassiveStateMachine <int, int> machine,
            bool entryActionExecuted)
        {
            "establish an initialized state machine".x(() =>
            {
                machine = new PassiveStateMachine <int, int>();

                machine.AddExtension(this.testExtension);

                machine.In(TestState)
                .ExecuteOnEntry(() => entryActionExecuted = true);

                machine.Initialize(TestState);
            });

            "when starting the state machine".x(() =>
                                                machine.Start());

            "should set current state of state machine to state to which it is initialized".x(() =>
                                                                                              this.testExtension.CurrentState.Should().Be(TestState));

            "should execute entry action of state to which state machine is initialized".x(() =>
                                                                                           entryActionExecuted.Should().BeTrue());
        }
Exemplo n.º 7
0
        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"));
        }
Exemplo n.º 8
0
        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(() =>
            {
                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();
                stateMachineDefinitionBuilder
                .In("0")
                .On(1)
                .Goto("1");
                machine = stateMachineDefinitionBuilder
                          .WithInitialState("0")
                          .Build()
                          .CreatePassiveStateMachine(Name);

                machine.AddExtension(extension);

                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.ExtractOrThrow() == "1"),
                                                                                                       A <IStateDefinition <string, int> > .That.Matches(x => x.Id == "1"),
                                                                                                       A <ITransitionContext <string, int> > .That.Matches(x => x.EventId.Value == 1)))
                                                                                       .MustHaveHappened());
        }
Exemplo n.º 9
0
        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());
        }
Exemplo n.º 10
0
        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 SetUp()
        {
            _loggerMock   = new Mock <ILogger>();
            _stateMachine = new PassiveStateMachine <States, Events>("Test");
            _stateMachine.AddExtension(new StateMachineLoggerExtension <States, Events>(_loggerMock.Object));

            _stateMachine.In(States.Off)
            .On(Events.TurnOn)
            .Goto(States.On);
            _stateMachine.In(States.On)
            .On(Events.TurnOff)
            .Goto(States.Off)
            .Execute(() => { throw new NotImplementedException("test exception"); });
        }
Exemplo n.º 12
0
        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();
        }
Exemplo n.º 13
0
        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 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 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 Background()
        {
            "establish a hierarchical state machine"._(() =>
                {
                    testExtension = new CurrentStateExtension();

                    machine = new PassiveStateMachine<int, int>();

                    machine.AddExtension(testExtension);

                    machine.DefineHierarchyOn(SuperState)
                        .WithHistoryType(HistoryType.None)
                        .WithInitialSubState(LeafState);

                    machine.In(SuperState)
                        .ExecuteOnEntry(() => entryActionOfSuperStateExecuted = true);
                    machine.In(LeafState)
                        .ExecuteOnEntry(() => entryActionOfLeafStateExecuted = true);
                });
        }
Exemplo n.º 17
0
        public void Initialize(
            PassiveStateMachine <int, int> machine,
            bool entryActionExecuted)
        {
            "establish a state machine".x(() =>
            {
                machine = new PassiveStateMachine <int, int>();

                machine.AddExtension(this.testExtension);

                machine.In(TestState)
                .ExecuteOnEntry(() => entryActionExecuted = true);
            });

            "when state machine is initialized".x(() =>
                                                  machine.Initialize(TestState));

            "should not yet execute any entry actions".x(() =>
                                                         entryActionExecuted.Should().BeFalse());
        }
Exemplo n.º 18
0
        public void Initialize(
            PassiveStateMachine<int, int> machine,
            bool entryActionExecuted)
        {
            "establish a state machine"._(() =>
                {
                    machine = new PassiveStateMachine<int, int>();

                    machine.AddExtension(this.testExtension);

                    machine.In(TestState)
                        .ExecuteOnEntry(() => entryActionExecuted = true);
                });

            "when state machine is initialized"._(() =>
                machine.Initialize(TestState));

            "should not yet execute any entry actions"._(() =>
                entryActionExecuted.Should().BeFalse());
        }
Exemplo n.º 19
0
        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());
        }
Exemplo n.º 20
0
        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(() =>
            {
                machine = new PassiveStateMachine <string, string>(Name);

                machine.AddExtension(extension);

                machine.DefineHierarchyOn("A")
                .WithHistoryType(HistoryType.None)
                .WithInitialSubState("A0");

                machine.In("0")
                .On("A0").Goto("A0");

                machine.Initialize("0");
                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 == "A0"),
                                                                                                                               A <IState <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 == "A0"),
                                                                                                                    A <IState <string, string> > .That.Matches(x => x.Id == "A0"),
                                                                                                                    A <ITransitionContext <string, string> > .That.Matches(x => x.EventId.Value == "A0")))
                                                                                                    .MustHaveHappened());
        }
Exemplo n.º 21
0
        public void ClearingExtensions(
            IStateMachine<string, int> machine,
            IExtension<string, int> extension)
        {
            "establish a state machine with an extension"._(() =>
                {
                    machine = new PassiveStateMachine<string, int>();

                    extension = A.Fake<IExtension<string, int>>();
                    machine.AddExtension(extension);
                });

            "when clearing all extensions from the state machine"._(() =>
                {
                    machine.ClearExtensions();
                    machine.Initialize("initial");
                });

            "it should not anymore notify extension about internal events"._(() =>
                A.CallTo(extension)
                    .MustNotHaveHappened());
        }
Exemplo n.º 22
0
        public void ClearingExtensions(
            IStateMachine <string, int> machine,
            IExtension <string, int> extension)
        {
            "establish a state machine with an extension".x(() =>
            {
                machine = new PassiveStateMachine <string, int>();

                extension = A.Fake <IExtension <string, int> >();
                machine.AddExtension(extension);
            });

            "when clearing all extensions from the state machine".x(() =>
            {
                machine.ClearExtensions();
                machine.Initialize("initial");
            });

            "it should not anymore notify extension about internal events".x(() =>
                                                                             A.CallTo(extension)
                                                                             .MustNotHaveHappened());
        }
Exemplo n.º 23
0
        private PassiveStateMachine <States, Events> createStateMachine(Timer timer)
        {
            var fsm = new PassiveStateMachine <States, Events>();

            fsm.AddExtension(new ConsoleLoggingExtension <States, Events>());

            fsm.Initialize(States.None);


            fsm.In(States.None).On(Events.SingleSlotPressed).Goto(States.Holding);

            fsm.In(States.Holding).ExecuteOnEntry(timer.Start);
            fsm.In(States.Holding).ExecuteOnExit(timer.Stop);
            fsm.In(States.Holding)
            .On(Events.MultipleSlotPressed).Goto(States.None);
            fsm.In(States.Holding)
            .On(Events.NoSlotPressed).Goto(States.None);
            fsm.In(States.Holding)
            .On(Events.Timer).Goto(States.Complete).Execute(SelectTargetedInventoryItem);

            fsm.In(States.Complete).On(Events.NoSlotPressed).Goto(States.None);

            return(fsm);
        }
Exemplo n.º 24
0
        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();
        }
Exemplo n.º 25
0
        public void Start(
            PassiveStateMachine<int, int> machine,
            bool entryActionExecuted)
        {
            "establish an initialized state machine"._(() =>
                {
                    machine = new PassiveStateMachine<int, int>();

                    machine.AddExtension(this.testExtension);

                    machine.In(TestState)
                        .ExecuteOnEntry(() => entryActionExecuted = true);

                    machine.Initialize(TestState);
                });

            "when starting the state machine"._(() =>
                machine.Start());

            "should set current state of state machine to state to which it is initialized"._(() =>
                this.testExtension.CurrentState.Should().Be(TestState));

            "should execute entry action of state to which state machine is initialized"._(() =>
                entryActionExecuted.Should().BeTrue());
        }
Exemplo n.º 26
0
        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));
        }