Esempio n. 1
0
        public void InitialStateSetViaDefinitionBuilder()
        {
            var stateMachineSaver = new StateMachineSaver <string>();

            var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();

            stateMachineDefinitionBuilder
            .In("A")
            .On(1)
            .Goto("B");
            stateMachineDefinitionBuilder
            .In("B")
            .On(2)
            .Goto("C");
            stateMachineDefinitionBuilder
            .WithInitialState("A");
            var machine = stateMachineDefinitionBuilder
                          .Build()
                          .CreatePassiveStateMachine();

            machine.Start();

            machine.Fire(1);

            machine.Save(stateMachineSaver);

            stateMachineSaver
            .CurrentStateId
            .Should()
            .Match <Initializable <string> >(currentState =>
                                             currentState.IsInitialized &&
                                             currentState.ExtractOrThrow() == "B");
        }
Esempio n. 2
0
        private void startFSM(FSMConfiguration fsmConfiguration)
        {
            mainFSMConfiguration = fsmConfiguration;
            fSMData.tcaTSLPath   = fsmConfiguration.tslPath;
            fSMData.comPortName  = fsmConfiguration.defaultComPortName;
            fSMData.ruSerialPort = new RuSerialPort();
            fSMData.tCACommand   = new TCAControl.TCACommandWarpper(fSMData.tcaTSLPath);
            proxyRunMachine      = new StateMachineDefinitionBuilder <States, Events>();
            proxyRunMachine.WithInitialState(States.Connncted);
            proxyRunMachine.In(States.Connncted).On(Events.CT11Command).Goto(States.CT11Mode).Execute(ct11Mode);
            proxyRunMachine.In(States.Connncted).On(Events.RuCommand).Goto(States.RuMode).Execute(ruMode);
            proxyRunMachine.In(States.Connncted).On(Events.TCAIcolishCommand).Goto(States.TCAIcolishMode).Execute(tCAIcolishMode);
            proxyRunMachine.In(States.TCAIcolishMode).On(Events.GoBack).Goto(States.Connncted).Execute(connected);
            proxyRunMachine.In(States.CT11Mode).On(Events.RuCommand).Goto(States.RuMode).Execute(ruMode);
            proxyRunMachine.In(States.CT11Mode).On(Events.GoBack).Goto(States.Connncted).Execute(connected);
            proxyRunMachine.In(States.RuMode).On(Events.CT11Command).Goto(States.CT11Mode).Execute(ct11Mode);
            proxyRunMachine.In(States.RuMode).On(Events.GoBack).Goto(States.Connncted).Execute(connected);
            var definition = proxyRunMachine.Build();

            fSMData.elevator = definition.CreateActiveStateMachine();
            fSMData.elevator.Start();

            //Action must Instantiate after FSM member Instantiate
            connectedAction  = new ConnectedAction(ref fSMData);
            ruModeAction     = new RuModeAction(ref fSMData);
            ct11ModeAction   = new CT11ModeAction(ref fSMData);
            tCAIcolishAction = new TCAIcolishAction(ref fSMData);
        }
        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();
        }
Esempio n. 4
0
        public void BuildingAStateMachineWithoutInitialStateThenInvalidOperationException()
        {
            var testee = new StateMachineDefinitionBuilder <int, int>();

            Action action = () => testee.Build();

            action
            .Should()
            .Throw <InvalidOperationException>()
            .WithMessage("Initial state is not configured.");
        }
Esempio n. 5
0
        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();
        }
Esempio n. 6
0
        internal StackViewStateMachine()
        {
            var builder = new StateMachineDefinitionBuilder <States, Events>();

            builder
            .In(States.Normal)
            .On(Events.Select)
            .Goto(States.Selected)
            .Execute <StackView>(TransitionToSelected);

            builder
            .In(States.Selected)
            .On(Events.Explore)
            .Goto(States.Exploring)
            .Execute <(StackView, WorldView)>(TransitionToExploring);

            builder
            .In(States.Selected)
            .On(Events.Move)
            .Goto(States.Moving)
            .Execute <StackView>(TransitionToMoving);

            builder
            .In(States.Selected)
            .On(Events.Unselect)
            .Goto(States.Normal)
            .Execute <StackView>(TransitionToNormal);

            builder
            .In(States.Selected)
            .On(Events.Patrol)
            .Goto(States.Patrolling)
            .Execute <StackView>(TransitionToPatrolling);

            builder
            .In(States.Selected)
            .On(Events.Fortify)
            .Goto(States.Fortified)
            .Execute <StackView>(TransitionToFortified);

            builder
            .In(States.Selected)
            .On(Events.ShowPotentialMovement)
            .Goto(States.ShowingPotentialMovement)
            .Execute <StackView>(TransitionToShowingPotentialMovement);

            builder
            .In(States.Moving)
            .On(Events.Select)
            .Goto(States.Selected)
            .Execute <StackView>(TransitionToSelected);

            builder
            .In(States.Moving)
            .On(Events.Unselect)
            .Goto(States.Normal)
            .Execute <StackView>(TransitionToNormal);

            builder
            .In(States.Exploring)
            .On(Events.Unselect)
            .Goto(States.Normal)
            .Execute <StackView>(TransitionToNormal);

            builder
            .In(States.Exploring)
            .On(Events.Move)
            .Goto(States.Moving)
            .Execute <StackView>(TransitionToMoving);

            builder
            .In(States.Patrolling)
            .On(Events.Select)
            .Goto(States.Selected)
            .Execute <StackView>(TransitionToSelected);

            builder
            .In(States.Fortified)
            .On(Events.Select)
            .Goto(States.Selected)
            .Execute <StackView>(TransitionToSelected);

            builder
            .In(States.ShowingPotentialMovement)
            .On(Events.ResetPotentialMovement)
            .Goto(States.Selected)
            .Execute <StackView>(TransitionToSelected);

            builder
            .WithInitialState(States.Normal);

            Machine = builder
                      .Build()
                      .CreatePassiveStateMachine();

            Machine.Start();
        }
Esempio n. 7
0
        public static void ini()
        {
            var builder = new StateMachineDefinitionBuilder <States, Events>();

            /*
             *  None: The state enters into its initial sub state. The sub state itself enters its initial sub state and so on until the innermost nested state is reached.
             *  Deep: The state enters into its last active sub state. The sub state itself enters into its last active state and so on until the innermost nested state is reached.
             *  Shallow: The state enters into its last active sub state. The sub state itself enters its initial sub state and so on until the innermost nested state is reached.
             */
            builder.DefineHierarchyOn(States.Program_Ready)      // 准备编程
            .WithHistoryType(HistoryType.Shallow)
            .WithInitialSubState(States.Program_Start)           // 开始编程
            .WithSubState(States.Program_Camera_Ready)           // 准备相机
            .WithSubState(States.Program_Camera_Runing)          // 拍摄
            .WithSubState(States.Program_Cmaera_Completed)       // 拍摄完成
            .WithSubState(States.Program_Completed);             // 编程完成

            builder.DefineHierarchyOn(States.Program_Test_Ready) // 准备测试
            .WithHistoryType(HistoryType.Deep)
            .WithInitialSubState(States.Work_Ready)              // 准备运行
            .WithSubState(States.Program_Test_Completed);        // 测试完成

            builder.DefineHierarchyOn(States.Work_Ready)         // 准备运行
            .WithHistoryType(HistoryType.Shallow)
            .WithInitialSubState(States.Work_Start)              // 开始运行
            .WithSubState(States.Work_Camera_Ready)              // 准备相机
            .WithSubState(States.Work_Camera_Runing)             // 拍摄
            .WithSubState(States.Work_Camera_Completed)          // 相机拍摄完成
            .WithSubState(States.Work_Done);                     // 一次运行完成


            builder.In(States.WaitingFor)                       // 登录完成后
            .On(Events.OnNewProject).Goto(States.Program_Ready) // 准备编程
            .On(Events.OnWork).Goto(States.Work_Ready);         // 准备运行

            builder.In(States.Program_Completed)                // 编程完成后
            .On(Events.OnTest).Goto(States.Program_Test_Ready)  // 准备测试
            .On(Events.OnWork).Goto(States.Work_Ready);         // 准备运行

            #region 异常 停止 恢复
            builder.In(States.Program_Ready)
            .On(Events.OnError).Goto(States.Error)
            .On(Events.OnStop).Goto(States.Stoped)
            .On(Events.OnResume).Goto(States.Program_Ready);

            builder.In(States.Program_Test_Ready)
            .On(Events.OnError).Goto(States.Error)
            .On(Events.OnStop).Goto(States.Stoped)
            .On(Events.OnResume).Goto(States.Program_Test_Ready);

            builder.In(States.Work_Ready)
            .On(Events.OnError).Goto(States.Program_Ready)
            .On(Events.OnStop).Goto(States.Stoped)
            .On(Events.OnResume).Goto(States.Work_Ready);
            #endregion

            builder.In(States.None)
            //.ExecuteOnEntry(logineddddddd)
            //.ExecuteOnExit(logineddddddd)
            //.ExecuteOnExit(Beep) // just beep a second time

            .On(Events.OnLogin).Goto(States.Logined).Execute(logineddddddd);
            //.On(Events.OnLogin).If<string>(Login.translation).Goto(States.Logined).Execute(logineddddddd)
            //.Otherwise().Execute(logine2ddddddd);
            //.On(Events.OnAutoCkeck)
            //    .If().Goto(States.WaitingFor)
            //    .Otherwise().Goto(States.Logined).Execute(this.AnnounceCheckFail);



            builder.WithInitialState(States.None);

            var definition = builder
                             .Build();

            machine = definition
                      .CreatePassiveStateMachine("Elevator");

            machine.Start();
        }
Esempio n. 8
0
        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)
            .On(Events.Error).Goto(States.Error);

            builder.In(States.Error)
            .On(Events.Reset).Goto(States.Healthy)
            .On(Events.Error);

            builder.In(States.OnFloor)
            .On(Events.CloseDoor).Goto(States.DoorClosed)
            .On(Events.OpenDoor).Goto(States.DoorOpen)
            .On(Events.GoUp).Goto(States.MovingUp)
            .On(Events.GoDown).Goto(States.MovingDown);

            builder.In(States.Moving)
            .On(Events.Stop).Goto(States.OnFloor);

            builder.In(States.Healthy).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.Healthy");
            });

            builder.In(States.DoorClosed).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.DoorClosed");
            });

            builder.In(States.DoorOpen).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.DoorOpen");
            });

            builder.In(States.Error).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.Error");
            });

            builder.In(States.Moving).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.Moving");
            });

            builder.In(States.MovingDown).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.MovingDown");
            });

            builder.In(States.MovingUp).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.MovingUp");
            });

            builder.In(States.OnFloor).ExecuteOnEntry(
                () =>
            {
                AddMessage("Entry States.OnFloor");
            });



            builder.WithInitialState(States.OnFloor);

            var definition = builder
                             .Build();

            elevator = definition
                       .CreatePassiveStateMachine("Elevator");

            elevator.Start();
        }