Exemple #1
0
 public void CreateTrigger(StateMachine sm)
 {
     if (null != Condition)
     {
         var trigger = new Trigger(Event, Condition);
         trigger.AddEffects(Effects);
         sm.AddTrigger(trigger);
     }
     else if(Effects.Any())
     {
         var trigger = new Trigger(Event, new AnyCondition());
         trigger.AddEffects(Effects);
         sm.AddTrigger(trigger);
     }
 }
Exemple #2
0
        public void SMG_05_01_CodeGeneration()
        {
            var cc = new StateMachineCompiler();
            cc.CompileString(ReadEmbeddedScript("CodeGeneration.smg"));

            cc.GenerateCode();
            Assert.AreEqual(2, cc.SM.Events.Count());

            var u = cc.SM.AddEvent("u");
            var trigger = new Trigger(u, cc.EvaluateCondition("s(b => a)"));
            trigger.AddEffects(new[] { new CallEffect(cc.SM, "m") });
            cc.SM.AddTrigger(trigger);

            Assert.IsFalse(cc.SM.IsPrepared);

            cc.Parameters.IsProcessEventPublic = true;

            cc.GenerateCode();

            Assert.IsTrue(cc.SM.IsPrepared);
            Assert.AreEqual(3, cc.SM.Events.Count());

            Trace("output:\n{0}", cc.Output);

            var csharp = new CSharpCodeProvider();
            var options = new CompilerParameters();

            var result = csharp.CompileAssemblyFromSource(options, cc.Output);

            if (result.Errors.Count > 0)
            {
                foreach (var e in result.Errors)
                {
                    Trace("{0}", e);
                }

                Assert.Fail("generated code failed to compile.");
            }

            var dll = result.CompiledAssembly;
            var type = dll.GetType("CodeGeneration");
            var eventtype = dll.GetType("EventCode");
            var events = Enum.GetValues(eventtype);

            var x = Activator.CreateInstance(type);

            var tostatestring = type.GetMethod("ToStateString");
            Trace("initial state [{0}].", tostatestring.Invoke(x, new object[0]));

            var processevent = type.GetMethod("ProcessEvent");
            var sendevent = events.GetValue(0);
            Trace("sending event '{0}' ...", sendevent);
            processevent.Invoke(x, new object[] { sendevent });

            var statestring = tostatestring.Invoke(x, new object[0]).ToString();
            Trace("state after [{0}].", statestring);

            Assert.AreEqual("s(b) t(b) f(0)", statestring);

            sendevent = events.GetValue(1);
            Trace("sending event '{0}' ...", sendevent);
            processevent.Invoke(x, new object[] { sendevent });

            statestring = tostatestring.Invoke(x, new object[0]).ToString();
            Trace("state after [{0}].", statestring);

            Assert.AreEqual("s(b) t(a) f(1)", statestring);
        }