public void Foreach() { FSMachine fsm = GetExprFSM(); fsm.Logger = new LogMessageTrace(); int[] intArray = new int[] { 1, 2, 3 }; fsm.GlobalContext.AddParameter(intArray.GetType(), "array"); fsm.GlobalContext.SetParameter("array", intArray); fsm.LoadXml(TestUtils.LoadText("xml.actions.foreach.xml")); fsm.Start(); Assert.AreEqual("index:0, value:1_index:1, value:2_index:2, value:3", fsm.Logger.ToString()); fsm = GetExprFSM(); fsm.Logger = new LogMessageTrace(); List <int> listArray = new List <int>(new int[] { 1, 2, 3 }); fsm.GlobalContext.AddParameter(listArray.GetType(), "array"); fsm.GlobalContext.SetParameter("array", listArray); fsm.LoadXml(TestUtils.LoadText("xml.actions.foreach.xml")); fsm.Start(); Assert.AreEqual("index:0, value:1_index:1, value:2_index:2, value:3", fsm.Logger.ToString()); //System.Func<A, B> aa=null; //System.Func<int, object> cc = null; //System.Func<B, A> bb = null; //bb = aa; }
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); }
public void Transition_Multi() { State root = new State("root"); root.Initial = "s1"; State s1 = new State("s1"); s1.AddTransition(new Transition("s2")); root.AddChild(s1); State s2 = new State("s2"); s2.AddTransition(new Transition("s3")); root.AddChild(s2); State s3 = new State("s3"); s3.AddTransition(new Transition("s4")); root.AddChild(s3); State s4 = new State("s4"); root.AddChild(s4); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); fsm.Start(); Assert.AreEqual("s4", fsm.Current.Name); }
public void Parameter_Child() { State root = new State("root"); root.Initial = "s1"; root.AddParamerter <int>("a"); State s1 = new State("s1"); s1.AddParamerter <int>("b"); root.AddChild(s1); root.AddParamerter <int>("result"); FSMachine fsm = GetFSM(); fsm.SetRoot(root); //Assert.IsFalse(fsm.Root.ContainsParameter("a")); fsm.Start(); Assert.IsTrue(fsm.Root.ContainsParameter("a")); Assert.IsFalse(fsm.Root.ContainsParameter("b")); fsm["a"] = 1; fsm.GetState("s1")["b"] = 2; Assert.IsTrue(fsm.GetState("s1").ContainsParameter("b")); fsm.GetState("s1").GetContext().EvalExpression(Assign(Variable <int>("result"), Add(Variable <int>("a"), Variable <int>("b")))); Assert.AreEqual(1, fsm["a"]); Assert.AreEqual(2, fsm.GetState("s1")["b"]); Assert.AreEqual(3, fsm.GetState("s1")["result"]); }
public void Transition_NotTransitionParent() { State root = new State("root"); root.Initial = "s1"; State s1 = new State("s1"); s1.Initial = "s11"; State s11 = new State("s11"); s11.AddTransition(new Transition("s2")); s1.AddChild(s11); root.AddChild(s1); State s2 = new State("s2"); root.AddChild(s2); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); fsm.Start(); Assert.AreEqual("s1", fsm.Current.Name); }
public void Parameter() { State root = new State(); root.Initial = "s1"; root.AddParamerter <int>("a"); root.AddParamerter <int>("b"); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); //Assert.IsFalse(fsm.Root.ContainsParameter("a")); fsm.Start(); Assert.IsTrue(fsm.Root.ContainsParameter("a")); fsm["a"] = 1; fsm["b"] = 2; Assert.AreEqual(1, fsm["a"]); Assert.AreEqual(2, fsm["b"]); try { fsm.Root.GetParameter("1"); Assert.Fail(); } catch (FSMParameterException ex) { } }
public void Cancel() { FSMachine fsm = new FSMachine(); fsm.LoadXml(TestUtils.LoadText("xml.actions.cancel.xml")); fsm.Start(); Assert.AreEqual("s1", fsm.Current.Name); }
public void Raise() { FSMachine fsm = new FSMachine(); fsm.LoadXml(TestUtils.LoadText("xml.actions.raise.xml")); fsm.Start(); Assert.AreEqual("s3", fsm.Current.Name); }
public void Assign() { FSMachine fsm = GetExprFSM(); fsm.LoadXml(TestUtils.LoadText("xml.actions.assign.xml")); fsm.Start(); Assert.AreEqual(1, fsm["int32ByValue"]); Assert.AreEqual("Hello World", fsm["stringByExpr"]); }
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()); }
public void Raise_Data() { FSMachine fsm = GetExprFSM(); fsm.LoadXml(TestUtils.LoadText("xml.actions.raise_data.xml")); fsm.Logger = new LogMessageTrace(); fsm.Start(); Assert.AreEqual("s2", fsm.Current.Name); Assert.AreEqual("Hello World", fsm.Logger.ToString()); }
public void Log() { FSMachine fsm = GetExprFSM(); fsm.LoadXml(TestUtils.LoadText("xml.actions.log.xml")); StringTraceLog log = new StringTraceLog(); fsm.Logger = log; fsm.Start(); Assert.AreEqual(":root.msg_string:root.type.string_format:root.format_:s1.onEntry_expr:s1.type.expr_format:1+2=3", log.ToString()); }
public void Empty() { State root = new State(); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); Assert.IsFalse(fsm.Root.IsActive); Assert.IsNull(fsm.Current); fsm.Start(); Assert.IsNull(fsm.Current); Assert.IsTrue(fsm.Root.IsActive); }
public void Custom() { FSMachine fsm = new FSMachine(); var reader = new Model.Xml.FSMXmlReader(); reader.AddActionReader("myAction", "urn:test", typeof(MyAction), (Model.Xml.FSMXmlReader r) => { MyAction action = new MyAction(); action.Text = r.ReadAttributeValue <string>("text", null); return(action); }); fsm.SetRoot(reader.Read(TestUtils.LoadText("xml.actions.custom.xml"))); fsm.Logger = new LogMessageTrace(); fsm.Start(); Assert.AreEqual("hello world", fsm.Logger.ToString()); }
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()); }
public void SendEvent() { State root = new State(); State s1 = new State(); s1.AddTransition(new Transition("s2", "s1.to.s2")); State s2 = new State(); s2.AddTransition(new Transition("s1", "s2.to.s1")); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); Assert.IsFalse(fsm.Root.IsActive); Assert.IsNull(fsm.Current); fsm.Start(); Assert.IsNull(fsm.Current); Assert.IsTrue(fsm.Root.IsActive); }
public void Base() { State root = new State(); root.Initial = "s1"; State s1 = new State(); s1.Name = "s1"; root.AddChild(s1); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); Assert.IsNull(fsm.Current); fsm.Start(); var current = fsm.Current; Assert.IsNotNull(current); Assert.AreEqual("s1", current.Name); }
public void Transition_Entry() { State root = new State("root"); EntryState entry = new EntryState(); entry.AddTransition(new Transition("s2")); root.EntryState = entry; State s1 = new State("s1"); root.AddChild(s1); State s2 = new State("s2"); root.AddChild(s2); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); fsm.Start(); Assert.AreEqual("s2", fsm.Current.Name); }
public void Parameter_ExprEval() { State root = new State(); root.Initial = "s1"; root.AddParamerter <int>("a"); root.AddParamerter <int>("b"); root.AddParamerter <int>("result"); //FSMachine fsm = GetFSM(); FSMachine fsm = new FSMachine(); fsm.SetRoot(root); fsm.Start(); Assert.IsTrue(fsm.Root.ContainsParameter("a"), "not contains param a"); //fsm["a"] = 1; //fsm["b"] = 2; //fsm.Root.EvalExpression(Assign(Variable("result"), Add(Variable("a"), Variable("b")))); //Assert.AreEqual(1, fsm["a"]); //Assert.AreEqual(2, fsm["b"]); //Assert.AreEqual(3, fsm["result"]); }