当前位置: 首页>>代码示例>>C#>>正文


C# StateMachine.Trigger方法代码示例

本文整理汇总了C#中StateMachine.Trigger方法的典型用法代码示例。如果您正苦于以下问题:C# StateMachine.Trigger方法的具体用法?C# StateMachine.Trigger怎么用?C# StateMachine.Trigger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StateMachine的用法示例。


在下文中一共展示了StateMachine.Trigger方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: StateMachine_Trigger_ValidTriggerAndTrans_ChoosesFirstTranWthPassGuard_DiffAsCurrentState_Raised_Sets

        public void StateMachine_Trigger_ValidTriggerAndTrans_ChoosesFirstTranWthPassGuard_DiffAsCurrentState_Raised_Sets()
        {
            var model = new StubStateModel();
            var machine = new StateMachine<StubStateModel>();

            var transitioningEventArgs = new List<TransitionEventArgs<StubStateModel>>();
            machine.Transitioning += (s, e) => { transitioningEventArgs.Add(e); };

            var transitionedEventArgs = new List<TransitionEventArgs<StubStateModel>>();
            machine.Transitioned += (s, e) => { transitionedEventArgs.Add(e); };

            var trigger1 = new Trigger("t1");
            var trigger2 = new Trigger("t2");

            var state1Mock = new Mock<State<StubStateModel>>("s1");
            var state2Mock = new Mock<State<StubStateModel>>("s2");
            var state3Mock = new Mock<State<StubStateModel>>("s3");
            var state4Mock = new Mock<State<StubStateModel>>("s4");

            var state1 = state1Mock.Object;
            var state2 = state2Mock.Object;
            var state3 = state3Mock.Object;
            var state4 = state4Mock.Object;

            var statetrans1 = new Transition<StubStateModel>(trigger1, state1, state2, m => 1 == 2);
            var statetrans2 = new Transition<StubStateModel>(trigger1, state1, state3, m => 1 == 3);
            var statetrans3 = new Transition<StubStateModel>(trigger2, state1, state3);

            var globaltrans1 = new Transition<StubStateModel>(trigger1, null, state2, m => 1 == 1);

            machine.AddGlobalTransition(globaltrans1);
            state1.AddTransition(statetrans1);
            state1.AddTransition(statetrans2);
            state1.AddTransition(statetrans3);

            model.CurrentState = state1;

            // so when trigger 1 is triggered on state model whose current state is state1,
            // it will fall through to the first maching transition with a passing guard, the global transition to state 2
            // it will then transition to state2.

            // make sure that transition events are called for moving from state 1 to state 2
            state1Mock.Setup(s => s.RaiseExiting(model, state2, trigger1)).Verifiable();
            state2Mock.Setup(s => s.RaiseEntered(model, state1, trigger1)).Verifiable();

            machine.Trigger(trigger1, model);

            // verify that no transition events were called that shouldn't have been
            state1Mock.Verify(s =>
                s.RaiseEntered(
                    It.IsAny<StubStateModel>(),
                    It.IsAny<State<StubStateModel>>(),
                    It.IsAny<Trigger>()), Times.Never());
            state2Mock.Verify(s =>
                s.RaiseExiting(
                    It.IsAny<StubStateModel>(),
                    It.IsAny<State<StubStateModel>>(),
                    It.IsAny<Trigger>()), Times.Never());
            Assert.Equal(1, transitioningEventArgs.Count);
            Assert.Equal(1, transitionedEventArgs.Count);

            Assert.Equal(state1, transitioningEventArgs[0].From);
            Assert.Equal(state2, transitioningEventArgs[0].To);
            Assert.Equal(trigger1, transitioningEventArgs[0].Trigger);
            Assert.Equal(model, transitioningEventArgs[0].Model);
            Assert.Equal(state1, transitionedEventArgs[0].From);
            Assert.Equal(state2, transitionedEventArgs[0].To);
            Assert.Equal(trigger1, transitionedEventArgs[0].Trigger);
            Assert.Equal(model, transitionedEventArgs[0].Model);
            Assert.Equal(state2, model.CurrentState);
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:71,代码来源:StateMachineTests.cs

示例2: StateMachine_Trigger_ValidTriggerAndTrans_ChoosesFirstTranWthPassGuard_SameAsCurrentState_NoRaised

        public void StateMachine_Trigger_ValidTriggerAndTrans_ChoosesFirstTranWthPassGuard_SameAsCurrentState_NoRaised()
        {
            var model = new StubStateModel();
            var machine = new StateMachine<StubStateModel>();
            TransitionEventArgs<StubStateModel> transitioningArgs = null;
            machine.Transitioning += (s, e) => { transitioningArgs = e; };
            TransitionEventArgs<StubStateModel> transitionedArgs = null;
            machine.Transitioned += (s, e) => { transitionedArgs = e; };

            var trigger1 = new Trigger("t1");
            var trigger2 = new Trigger("t2");

            var state1Mock = new Mock<State<StubStateModel>>("s1");
            var state2Mock = new Mock<State<StubStateModel>>("s2");
            var state3Mock = new Mock<State<StubStateModel>>("s3");
            var state4Mock = new Mock<State<StubStateModel>>("s4");

            var state1 = state1Mock.Object;
            var state2 = state2Mock.Object;
            var state3 = state3Mock.Object;
            var state4 = state4Mock.Object;

            var statetrans1 = new Transition<StubStateModel>(trigger1, state1, state2, m => 1 == 2);
            var statetrans2 = new Transition<StubStateModel>(trigger1, state1, state3, m => 1 == 3);
            var statetrans3 = new Transition<StubStateModel>(trigger2, state1, state3);

            var globaltrans1 = new Transition<StubStateModel>(trigger1, null, state1, m => 1 == 1);

            machine.AddGlobalTransition(globaltrans1);
            state1.AddTransition(statetrans1);
            state1.AddTransition(statetrans2);
            state1.AddTransition(statetrans3);

            model.CurrentState = state1;

            // so when trigger1 is triggered on state model whose current state is state1,
            // it will fall through to the first matching transition with a passing guard, the global transition to state 1
            // but since this is same state as current state, it shouldn't actually transition.  no events should be called.

            machine.Trigger(trigger1, model);

            // verify that no transition events were calledb
            state1Mock.Verify(s =>
                s.RaiseEntered(
                    It.IsAny<StubStateModel>(),
                    It.IsAny<State<StubStateModel>>(),
                    It.IsAny<Trigger>()), Times.Never());
            state1Mock.Verify(s =>
                s.RaiseExiting(
                    It.IsAny<StubStateModel>(),
                    It.IsAny<State<StubStateModel>>(),
                    It.IsAny<Trigger>()), Times.Never());
            Assert.Null(transitioningArgs);
            Assert.Null(transitionedArgs);
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:55,代码来源:StateMachineTests.cs

示例3: StateMachine_Trigger_TransitionsToSameState_ConfigRaiseOnSameStateTranTrue_ThrowsInvalidTrigger

        public void StateMachine_Trigger_TransitionsToSameState_ConfigRaiseOnSameStateTranTrue_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = true };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state1);
            model.CurrentState = state1;

            // set up scenario where state would transition from current to same state.
            // so no true transition would not occur

            Assert.Throws<InvalidTriggerException>(() =>
                machine.Trigger(trigger1, model));
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:17,代码来源:StateMachineTests.cs

示例4: StateMachine_Trigger_ValidlyTransitions_RaisesAllEventsInCorrectOrder

        public void StateMachine_Trigger_ValidlyTransitions_RaisesAllEventsInCorrectOrder()
        {
            var calls = new List<string>();
            var model = new TrackableStateModel();
            var state1 = new State<TrackableStateModel>("s1");
            var state2 = new State<TrackableStateModel>("s2");
            var trigger = new Trigger("t1");
            var transition = new Transition<TrackableStateModel>(trigger, state1, state2);
            state1.AddTransition(transition);
            model.CurrentState = state1;

            var stateMachine = new StateMachine<TrackableStateModel>();

            Action<object, TransitionEventArgs<TrackableStateModel>> entering = (s, e) => calls.Add(String.Format("entering {0} from {1} on {2}", e.To, e.From, e.Trigger));
            Action<object, TransitionEventArgs<TrackableStateModel>> entered = (s, e) => calls.Add(String.Format("entered {0} from {1} on {2}", e.To, e.From, e.Trigger));
            Action<object, TransitionEventArgs<TrackableStateModel>> exiting = (s, e) => calls.Add(String.Format("exiting {0} to {1} on {2}", e.From, e.To, e.Trigger));
            Action<object, TransitionEventArgs<TrackableStateModel>> exited = (s, e) => calls.Add(String.Format("exited {0} to {1} on {2}", e.From, e.To, e.Trigger));

            model.Setting += (s, e) => calls.Add("setting new current state on model");

            state1.Entering += (s, e) => entering(s, e);
            state1.Entered += (s, e) => entered(s, e);
            state1.Exiting += (s, e) => exiting(s, e);
            state1.Exited += (s, e) => exited(s, e);

            state2.Entering += (s, e) => entering(s, e);
            state2.Entered += (s, e) => entered(s, e);
            state2.Exiting += (s, e) => exiting(s, e);
            state2.Exited += (s, e) => exited(s, e);

            stateMachine.Transitioning += (s, e) => calls.Add(String.Format("transitioning from {0} to {1} on {2}", e.From, e.To, e.Trigger));
            stateMachine.Transitioned += (s, e) => calls.Add(String.Format("transitioned from {0} to {1} on {2}", e.From, e.To, e.Trigger));

            stateMachine.Trigger(trigger, model);

            Assert.Equal(7, calls.Count);
            Assert.Equal("transitioning from s1 to s2 on t1", calls[0]);
            Assert.Equal("exiting s1 to s2 on t1", calls[1]);
            Assert.Equal("entering s2 from s1 on t1", calls[2]);
            Assert.Equal("setting new current state on model", calls[3]);
            Assert.Equal("exited s1 to s2 on t1", calls[4]);
            Assert.Equal("entered s2 from s1 on t1", calls[5]);
            Assert.Equal("transitioned from s1 to s2 on t1", calls[6]);
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:44,代码来源:StateMachineTests.cs

示例5: StubStateModel

        public void StateMachine_Trigger_TransitionFromCurrentToSame_ConfiggedNotToRaiseExcep_DoesNotRaiseTransitionChangeEvents()
        {
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");

            var state1 = new State<StubStateModel>("state1");
            TransitionEventArgs<StubStateModel> transitioningArgs = null;
            state1.Entered += (s, e) => { transitioningArgs = e; };
            TransitionEventArgs<StubStateModel> transitionedArgs = null;
            state1.Exiting += (s, e) => { transitionedArgs = e; };

            state1.AddTransition(trigger1, state1);
            model.CurrentState = state1;

            // set up scenario where state would transition from current to same state.
            // so no true transition would not occur

            // should not throw exception and that's good enough for us
            machine.Trigger(trigger1, model);

            // verify that no transition events occurred
            Assert.Null(transitioningArgs);
            Assert.Null(transitionedArgs);
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:26,代码来源:StateMachineTests.cs

示例6: StateMachine_Trigger_TransitionsToSameState_ConfigRaiseOnSameStateTranFalse_ThrowsInvalidTrigger

        public void StateMachine_Trigger_TransitionsToSameState_ConfigRaiseOnSameStateTranFalse_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state1);
            model.CurrentState = state1;

            // set up scenario where state would transition from current to same state.
            // so no true transition would not occur

            // should not throw exception and that's good enough for us
            machine.Trigger(trigger1, model);
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:17,代码来源:StateMachineTests.cs

示例7: StateMachine_Trigger_NoStateOrGlobalTransOnTrigger_ConfigRaiseOnNoTransTrue_ThrowsInvalidTrigger

        public void StateMachine_Trigger_NoStateOrGlobalTransOnTrigger_ConfigRaiseOnNoTransTrue_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoTransition = true };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var trigger2 = new Trigger("trigger2");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2);
            model.CurrentState = state1;

            // set up so that current state of state 1 doesn't' define a trans for "trigger2", only "trigger1"

            Assert.Throws<InvalidTriggerException>(() =>
                machine.Trigger(trigger2, model));
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:17,代码来源:StateMachineTests.cs

示例8: StateMachine_Trigger_NoStateOrGlobalTransOnTrigger_ConfigRaiseOnNoTransFalse_NotThrowsInvalidTrigger

        public void StateMachine_Trigger_NoStateOrGlobalTransOnTrigger_ConfigRaiseOnNoTransFalse_NotThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoTransition = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var trigger2 = new Trigger("trigger2");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2);
            model.CurrentState = state1;

            // no exception shoudl happen, and that's good enough for us.
            machine.Trigger(trigger2, model);
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:15,代码来源:StateMachineTests.cs

示例9: StateMachine_Trigger_NoPassingTransitionOnTrigger_ConfigRaiseOnNoPassingTranTrue_ThrowsInvalidTrigger

        public void StateMachine_Trigger_NoPassingTransitionOnTrigger_ConfigRaiseOnNoPassingTranTrue_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoPassingTransition = true };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2, m => 1 == 2);
            model.CurrentState = state1;

            // set up so that there's a matching transition, but the guard would fail when run

            Assert.Throws<InvalidTriggerException>(() =>
                machine.Trigger(trigger1, model));
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:16,代码来源:StateMachineTests.cs

示例10: StateMachine_Trigger_ModelStateNotState_ThrowsInvalidStateModel

        public void StateMachine_Trigger_ModelStateNotState_ThrowsInvalidStateModel()
        {
            var machine = new StateMachine<StubStateModel>();
            var model = new StubStateModel();
            var trigger = new Trigger("trigger");

            Assert.Throws<InvalidStateModelException>(() =>
                machine.Trigger(trigger, model));
        }
开发者ID:mmonteleone,项目名称:nate,代码行数:9,代码来源:StateMachineTests.cs


注:本文中的StateMachine.Trigger方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。