Beispiel #1
0
        public void Transition_Event_Cond()
        {
            State root = new State("root");

            root.Initial = "s1";
            State s1 = new State("s1");

            s1.AddParamerter(new Parameter(typeof(bool), "isTrue"));
            s1.AddTransition(new Transition("s2", "to.s2", Variable <int>("isTrue")));
            root.AddChild(s1);

            State s2 = new State("s2");

            root.AddChild(s2);

            FSMachine fsm = GetFSM();

            fsm.SetRoot(root);

            fsm.Start();
            Assert.AreEqual("s1", fsm.Current.Name);

            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s1", fsm.Current.Name);

            fsm.GetState("s1").SetParameter("isTrue", true);
            fsm.SendEvent("to.s2");
            fsm.Update();

            Assert.AreEqual("s2", fsm.Current.Name);
        }
Beispiel #2
0
        public void If()
        {
            FSMachine fsm = GetExprFSM();

            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:0", fsm.Logger.ToString());

            fsm = GetExprFSM();
            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm["n"] = 1;
            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:1", fsm.Logger.ToString());

            fsm = GetExprFSM();
            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm["n"] = 2;
            fsm.SendEvent("to.s2");
            fsm.Update();
            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:2", fsm.Logger.ToString());

            fsm = GetExprFSM();
            fsm.LoadXml(TestUtils.LoadText("xml.actions.if.xml"));
            fsm.Logger = new LogMessageTrace();
            fsm.Start();
            fsm["n"] = 3;
            fsm.SendEvent("to.s2");
            fsm.Update();

            Assert.AreEqual("s2", fsm.Current.Name);
            Assert.AreEqual("s2:3", fsm.Logger.ToString());
        }
Beispiel #3
0
        public void Transition_Event()
        {
            State root = new State("root");

            root.Initial = "s1";

            State s1 = new State("s1");

            s1.AddTransition(new Transition("s2", "to.s2"));
            root.AddChild(s1);

            State s2 = new State("s2");

            root.AddChild(s2);

            StringBuilder sb = new StringBuilder();

            FSMachine fsm = new FSMachine();

            fsm.SetRoot(root);
            fsm.StateTransition += (object o, TransitionEventArgs e) =>
            {
                sb.Append(e.FromState == null ? "null" : e.FromState.Name)
                .Append("=>")
                .Append(e.ToState.Name);
            };


            fsm.Start();
            var current = fsm.Current;

            Assert.IsNotNull(current);
            Assert.AreEqual("s1", current.Name);

            fsm.SendEvent("to.s2");
            fsm.Update();

            current = fsm.Current;
            Assert.IsNotNull(current);
            Assert.AreEqual("s2", current.Name);
            Console.WriteLine(sb.ToString());
        }