Esempio n. 1
0
        public void TransitionEvents()
        {
            StateMachine stateMachine = new StateMachine();
            SimpleState  s0 = new SimpleState(), s1 = new SimpleState();

            stateMachine.AddState(s0);
            stateMachine.AddState(s1);
            stateMachine.AddEntryTransition(0, null);
            stateMachine.AddExitTransition(1, null);
            TransitionTrigger trigger = new TransitionTrigger();

            stateMachine.AddTransition(0, 1, trigger);
            List <string> result = new List <string>();

            s0.StateLeave            += () => result.Add("leave: 0");
            s1.StateEnter            += () => result.Add("enter: 1");
            trigger.BeforeTransition += () =>
            {
                result.Add("before transition");
                Assert.AreEqual("0.-1", stateMachine.GenerateActiveStateDescriptor());
            };
            trigger.AfterTransition += () =>
            {
                result.Add("after transition");
                Assert.AreEqual("-1", stateMachine.GenerateActiveStateDescriptor());
            };
            stateMachine.Run();
            trigger.Fire();
            Assert.AreEqual(4, result.Count);
            Assert.AreEqual("before transition", result[0]);
            Assert.AreEqual("leave: 0", result[1]);
            Assert.AreEqual("enter: 1", result[2]);
            Assert.AreEqual("after transition", result[3]);

            trigger.BeforeTransition += () => trigger.StopTransition();
            result.Clear();
            stateMachine.Reset();
            stateMachine.Run();
            trigger.Fire();
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("before transition", result[0]);
        }
Esempio n. 2
0
        /*
         *  | state id |    state name    |
         *  |    0     |    unconnected   |
         *  |    1     |    connected     |
         *  |    2     |    recovery      |
         */
        private void BuildStateMachine(ITransportationStateProvider transportationStateProvider)
        {
            stateMachine.AddState(unconnectedState);
            stateMachine.AddState(connectedState);
            stateMachine.AddState(recoveryState);

            transportationStateProvider.ConnectionEstablished += () => transportationReadyTrigger.Fire();
            transportationStateProvider.ConnectionLost        += () => transportationLostTrigger.Fire();

            unconnectedState.StateEnter += () =>
            {
                if (!started)
                {
                    started = true;
                    return;
                }
                Logger.Log($"unconnectedState.StateEnter, stop timers, state={StateDescr()}", "FT");
                silenceTimer.Stop();
                heartBeatTimer.Stop();
                Logger.Log($"invoking connection lost event, state={StateDescr()}", "FT");
                ConnectionLost?.Invoke();
            };
            unconnectedState.StateLeave += () =>
            {
                Logger.Log($"unconnectedState.StateLeave, start silence timer, state={StateDescr()}", "FT");
                silenceTimer.Start();
            };
            connectedState.StateEnter += () =>
            {
                Logger.Log($"connectedState.StateEnter, start heart beat, state={StateDescr()}", "FT");
                heartBeatTimer.Start();
            };

            stateMachine.AddEntryTransition(0, null);
            stateMachine.AddTransition(1, 0, silenceTimeoutTrigger);
            stateMachine.AddTransition(1, 0, transportationLostTrigger);

            AddCustomTransitionsAndActions();
            stateMachine.Run();
        }
Esempio n. 3
0
        public void MultiEdges()
        {
            StateMachine stateMachine = new StateMachine();
            SimpleState  s0 = new SimpleState(), s1 = new SimpleState();

            stateMachine.AddState(s0);
            stateMachine.AddState(s1);
            stateMachine.AddEntryTransition(0, null);
            stateMachine.AddExitTransition(1, null);
            TransitionTrigger trigger0 = new TransitionTrigger();
            TransitionTrigger trigger1 = new TransitionTrigger();

            stateMachine.AddTransition(0, 1, trigger0);
            stateMachine.AddTransition(0, 1, trigger1);

            stateMachine.Run();
            trigger0.Fire();
            Assert.AreEqual("-1", stateMachine.GenerateActiveStateDescriptor());
            stateMachine.Reset();
            stateMachine.Run();
            trigger1.Fire();
            Assert.AreEqual("-1", stateMachine.GenerateActiveStateDescriptor());
        }