Example #1
0
        public void Transition_With_Complex_Conditional_Pass_Subcondition( )
        {
            StateMachine stateMachine = ObjectFactory.GetInstance <StateMachine> ( );
            StateMap     map          = ObjectFactory.GetInstance <StateMap> ( );

            State state0 = map.AddState(new State( ))
                           .Configure(x => x.OnEnter(new StateEvent <StateEventLogicHandler> (new OnEnterStateLogicHandler(ObjectFactory.Container)))
                                      .Name = "State 0");

            State state1 = map.AddState(new State( ))
                           .Configure(x => x.OnEnter(new StateEvent <SimpleAction> (new SimpleAction(() => Console.WriteLine(@"Action 1 Fired"))))
                                      .Name = "State 1");

            map.Configure(stateMap => stateMap.AddTransition(() => new ConditionalTransition(state0, state1, null, expression => expression.Evaluate(() => true)
                                                                                             .And(() => true)
                                                                                             .And(() => false)
                                                                                             .Or(subCondition => subCondition.Evaluate(() => true)
                                                                                                 .And(() => true)))));

            stateMachine.Configure(map);

            stateMachine.Start( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 0");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 1");

            stateMachine.Stop( );
        }
Example #2
0
        public void Create_And_Populate_Engine_And_Transitions( )
        {
            StateMachine stateMachine = ObjectFactory.GetInstance <StateMachine> ( );
            StateMap     map          = ObjectFactory.GetInstance <StateMap> ( );

            State initialState = map.AddState(new State( ))
                                 .Configure(x => { x.Name = "Initial State"; });
            State state1 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 1"; });
            State state2 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 2"; });
            State state3 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 3"; });

            const bool isItemPickedUp       = true;
            const bool doesProperty1Match   = true;
            const bool doesProperty2Match   = false;
            const bool ignorePropertyValues = true;

            const bool canTransitionTo2 = true;
            const bool canTransitionTo3 = true;

            map.Configure(stateMap =>
            {
                stateMap.AddTransition(() => new ConditionalTransition(initialState, state1, null, expression => expression.Evaluate(() => isItemPickedUp)
                                                                       .And(() => doesProperty1Match)
                                                                       .And(() => doesProperty2Match)
                                                                       .Or(() => ignorePropertyValues)));

                stateMap.AddTransition(() => new ConditionalTransition(state1, state2, null, expression => expression.Evaluate(() => canTransitionTo2)));

                stateMap.AddTransition(() => new ConditionalTransition(state2, state3, null, expression => expression.Evaluate(() => canTransitionTo3)));
            });

            stateMachine.Configure(map);

            stateMachine.Start(initialState);

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "Initial State");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 1");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 2");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 3");

            stateMachine.Stop( );
        }
Example #3
0
        public void Branch_To_Simultaneous_Active_States_One_State_Transition_False_With_Null_Final_States( )
        {
            StateMachine stateMachine = ObjectFactory.GetInstance <StateMachine> ( );
            StateMap     map          = ObjectFactory.GetInstance <StateMap> ( );

            State state0 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 0"; });
            State state1 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 1"; });
            State state2 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 2"; });
            State state3 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 3"; });

            map.Configure(stateMap =>
            {
                stateMap.AddTransition(() => new ConditionalTransition(state0, state1, state2, expression => expression.Evaluate(() => true)));
                stateMap.AddTransition(() => new ConditionalTransition(state1, state2, state3, expression => expression.Evaluate(() => true)));
                stateMap.AddTransition(() => new ConditionalTransition(state2, state3, null, expression => expression.Evaluate(() => true)));
                stateMap.AddTransition(() => new ConditionalTransition(state3, null, null, expression => expression.Evaluate(() => true)));
            });

            stateMachine.Configure(map);

            stateMachine.Start(state0);

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 0");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 1");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 2");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 3");

            stateMachine.Transition( );

            stateMachine.Stop( );
        }
Example #4
0
        public void Multiple_Branching_State_Transitions( )
        {
            StateMachine stateMachine = ObjectFactory.GetInstance <StateMachine> ( );
            StateMap     map          = ObjectFactory.GetInstance <StateMap> ( );

            State state0 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 0"; });
            State state1 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 1"; });
            State state2 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 2"; });
            State state3 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 3"; });
            State state4 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 4"; });
            State state5 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 5"; });
            State state6 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 6"; });
            State state7 = map.AddState(new State( ))
                           .Configure(x => { x.Name = "State 7"; });

            map.Configure(stateMap =>
            {
                stateMap.AddTransition(() => new ConditionalTransition(state0, state1, state2, expression => expression.Evaluate(() => true)));
                stateMap.AddTransition(() => new ConditionalTransition(state1, state3, state4, expression => expression.Evaluate(() => false)));
                stateMap.AddTransition(() => new ConditionalTransition(state3, state5, state6, expression => expression.Evaluate(() => true)));
                stateMap.AddTransition(() => new ConditionalTransition(state4, state6, state7, expression => expression.Evaluate(() => false)));
            });

            stateMachine.Configure(map);

            stateMachine.Start(state0);

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 0");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 1");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 4");

            stateMachine.Transition( );

            Assert.NotNull(stateMachine.ActiveStates [0]);
            Assert.AreEqual(stateMachine.ActiveStates [0].Name, "State 7");

            stateMachine.Stop( );
        }