Ejemplo n.º 1
0
        public void SMG_01_01_BooleanExpression()
        {
            var eval = new StateMachineCompiler();

            eval.CompileString("smg test declare BOOLEAN A, B, C");
            var c = eval.EvaluateCondition("A AND NOT B").Decompose(ConditionMode.Pre);

            Assert.AreEqual("A!B", c.ToString());
        }
Ejemplo n.º 2
0
        public void SMG_01_02_BooleanRules()
        {
            var eval = new StateMachineCompiler();

            var dict = new Dictionary <string, string>();

            // fix static variable order
            var sb = new StringBuilder();

            sb.Append("SMG test DECLARE BOOLEAN ");
            for (char c = 'A'; c <= 'Z'; ++c)
            {
                if (c != 'A')
                {
                    sb.Append(",");
                }
                sb.Append(c);
            }

            eval.CompileString(sb.ToString());

            dict.Add("A AND B", "AB");
            dict.Add("A OR B", "A + B");
            dict.Add("A AND NOT A", "0");
            dict.Add("A OR NOT A", "1");
            dict.Add("A AND NOT A AND B", "0");
            dict.Add("A OR (A AND B)", "A");
            dict.Add("A OR (NOT A AND B)", "A + B");

            dict.Add("(A AND B AND C) OR (B AND C)", "BC");
            dict.Add("(A AND B AND C) OR (A AND B AND NOT C)", "AB");

            // check ordering by address
            dict.Add("M OR NOT ((X AND Y) OR Z) OR (A AND B) OR F", "AB + F + M + !X!Z + !Y!Z");

            foreach (var pair in dict)
            {
                Trace("\n--- testing {0} ...", pair.Key);
                var cond = eval.EvaluateCondition(pair.Key).Decompose(ConditionMode.Pre);
                Assert.AreEqual(pair.Value, cond.ToString());
            }
        }
Ejemplo n.º 3
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);
        }