Exemple #1
0
        public void SMG_01_04_ConditionTransitions()
        {
            TraceFlags.ShowDepencencyAnalysis = true;
            // TraceFlags.ShowLabel = true;

            var cc = new StateMachineCompiler();
            var sm = cc.CompileString(ReadEmbeddedScript("ConditionTransitions.smg"));

            foreach (var trigger in sm.Events.SelectMany(e => e.Triggers))
            {
                PrintMonitorTransitions(trigger);
            }

            foreach (var guard in sm.Guards)
            {
                PrintMonitorTransitions(guard);
            }

            // Trace("{0}", GateCache.Instance.ToDebugString());

            //cc.GenerateCode();
            sm.Calculate();

            PrintEventEffectConditions(sm);
        }
Exemple #2
0
        public void SMG_04_00_Basic()
        {
            var cc = new StateMachineCompiler();

            cc.CompileString(
                "SMG BasicTest " +
                "DECLARE State (a, b) s, t " +
                "DECLARE BOOLEAN f " +
                "TRIGGER e1 WHEN s(a => b) " +
                "TRIGGER e2 WHEN t(a => b) " +
                "GUARD WHEN ENTER t(b) AND s(b) AND f CALL q1 " +
                "TRIGGER e4 WHEN f(0 => 1) " +
                ""
                );

            var sm = cc.SM;

            sm.Calculate();

            PrintEventEffectConditions(sm);

            ValidateEventEffectConditionPre(sm, "e1", "q1", "s(a)t(b)f");
            ValidateEventEffectConditionPre(sm, "e2", "q1", "s(b)t(a)f");
            ValidateEventEffectConditionPre(sm, "e4", "q1", "s(b)t(b)!f");
        }
Exemple #3
0
        public void SMG_04_04_ConflictingTriggers()
        {
            TraceFlags.ShowDepencencyAnalysis = true;
            var cc = new StateMachineCompiler();

            try
            {
                var sm = cc.CompileString(
                    "SMG test " +
                    "DECLARE State (a, b, c) s, t " +
                    "TRIGGER e1 WHEN s(a => b) OR s(a => c) CALL m1" +
                    ""
                    );

                Assert.Fail("exception expected.");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(AggregateException));
                ex = ex.InnerException;

                Assert.IsInstanceOfType(ex, typeof(CompilerException));
                var cex = (CompilerException)ex;

                Assert.AreEqual(ErrorCode.AmbigousPostCondition, cex.Code);
            }
        }
Exemple #4
0
        public void SMG_04_06_StandardConditions()
        {
            // TraceFlags.ShowDepencencyAnalysis = true;

            var cc      = new StateMachineCompiler();
            var smgtext = ReadEmbeddedScript("StandardConditions.smg");
            var sm      = cc.CompileString(smgtext);

            sm.Calculate();


            ValidateEventEffectConditionPre(sm, "e1", "ge1", "s(a) + t(a)");
            ValidateEventEffectConditionPre(sm, "e1", "gprod", "s(a) + t(a)");
            ValidateEventEffectConditionPre(sm, "e1", "gsum", "s(a) + t(a)");

            var ce1 = "s(a)t(a)";

            ValidateEventEffectConditionPre(sm, "e2", "ge2", ce1);
            ValidateEventEffectConditionPre(sm, "e2", "gprod", ce1);
            ValidateEventEffectConditionPre(sm, "e2", "gsum", ce1);

            ValidateEventEffectConditionPre(sm, "e3", "gsum", "0");
            ValidateEventEffectConditionPre(sm, "e4", "gsum", "0");

            PrintEventEffectConditions(sm);
        }
Exemple #5
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());
        }
Exemple #6
0
        public void SMG_05_03_PseudoCode()
        {
            var cc = new StateMachineCompiler();

            cc.CompileString(ReadEmbeddedScript("StandardConditions.smg"));

            cc.Parameters.Language = "pseudo";

            cc.GenerateCode();

            Trace("output:\n{0}", cc.Output);
        }
Exemple #7
0
        public void SMG_04_07_MixedConditions()
        {
            TraceFlags.ShowDepencencyAnalysis = true;

            var cc      = new StateMachineCompiler();
            var smgtext = ReadEmbeddedScript("MixedConditions.smg");
            var sm      = cc.CompileString(smgtext);

            sm.Calculate();

            ValidateEventEffectConditionPre(sm, "e1", "m1", "s(a)t(a)");
        }
Exemple #8
0
        public void SMG_01_03_ConditionTransitions()
        {
            TraceFlags.ShowDepencencyAnalysis = true;
            var cc = new StateMachineCompiler();
            var sm = cc.CompileString(ReadEmbeddedScript("TransitionCondition.smg"));

            cc.GenerateCode();

            PrintEventEffectConditions(sm);

            Trace("{0}", cc.Output);
        }
Exemple #9
0
        public void SMG_04_06_SumGuard()
        {
            var cc      = new StateMachineCompiler();
            var smgtext = ReadEmbeddedScript("SumGuard.smg");
            var sm      = cc.CompileString(smgtext);

            sm.Calculate();

            PrintEventEffectConditions(sm);

            cc.GenerateCode();
            Trace("{0}", cc.Output);
        }
Exemple #10
0
        public void SMG_05_04_SyntaxCases()
        {
            TraceFlags.ShowDepencencyAnalysis = true;

            var cc = new StateMachineCompiler();

            cc.CompileString(ReadEmbeddedScript("SyntaxCases.smg"));

            cc.Parameters.Language = "pseudo";

            cc.GenerateCode();

            Trace("output:\n{0}", cc.Output);
        }
Exemple #11
0
        public void SMG_04_Program()
        {
            TraceFlags.ShowDepencencyAnalysis = true;
            var cc = new StateMachineCompiler();

            var sm = cc.CompileString(ReadEmbeddedScript("script3.smg"));

            cc.GenerateCode();

            PrintEventEffectConditions(cc.SM);

            Trace("{0}", cc.Output);

            // Trace("gatecache: \n{0}", GateCache.Instance.ToDebugString());
        }
Exemple #12
0
        public void SMG_04_08_SyntaxErrors()
        {
            var cc = new StateMachineCompiler();

            cc.SM.SourceFile = "testfile.smg";

            try
            {
                var sm = cc.CompileString("SMG Fails TRIGGER x AND TRIGGER y ");
                Assert.Fail("exception expected.");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(AggregateException));
                var cex = (CompilerException)ex.InnerException;
                Assert.AreEqual(ErrorCode.SyntaxError, cex.Code);
                Assert.AreEqual(cc.SM.SourceFile, cex.Location.SourceFile);
            }
        }
Exemple #13
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());
            }
        }
Exemple #14
0
        public void SMG_04_03_CodeLabels()
        {
            GateCache.Instance.Purge();

            var cc = new StateMachineCompiler();
            var sm = cc.CompileString(
                "SMG test " +
                "DECLARE State (a, b, c) s, t " +
                "TRIGGER e WHEN s(a => b) AND t(b) " +
                "GUARD WHEN s(* => b) AND t(b) CALL m1 " +
                ""
                );

            sm.Calculate();

            PrintEventEffectConditions(sm);

            var ev = sm.Events.Where(e => e.Name == "e").First();

            using (var gc = new GateConverter())
            {
                ev.EffectsAfter.Schedule(gc);

                var cle = new SimpleCodeLabelEvaluator();

                // Trace("gc: \n{0}", gc.ToDebugString());
                gc.Emit(cle, 0);
            }

            var eclist = ev.EffectsAfter.GetEffectConditions();
            var ec     = eclist.Where(a => a.Effect.UniqueID == "CALL m1").First();

            Assert.AreEqual("<_c0><_c1>", ec.ConditionLabel.ToString());

            /*var sb = new StringBuilder();
             * using (var w = new StringWriter(sb))
             * {
             *  w.Write(sm.GenerateCode());
             * }
             *
             * Trace("code:\n{0}", sb);*/
        }
Exemple #15
0
        public void SMG_05_02_CodeGeneration()
        {
            // TraceFlags.ShowLabel = true;

            var cc = new StateMachineCompiler();

            cc.CompileString(ReadEmbeddedScript("CodeGenerationOptions.smg"));

            Assert.AreEqual(true, cc.Parameters.IsPartial);
            Assert.AreEqual("Test.Code", cc.Parameters.Namespace);

            cc.GenerateCode();

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

            /*using(var writer = new StreamWriter(@"c:\users\tc\repositories\igra3\prototype\html\smg.js"))
             * {
             *  writer.Write(cc.Output);
             * }*/
        }
Exemple #16
0
        public void SMG_04_01_EnterLeaveGuards()
        {
            var cc = new StateMachineCompiler();

            cc.CompileString(
                "SMG test \n" +
                "DECLARE State (a, b) s, t, x\n" +
                "TRIGGER e1 WHEN s(a => b) " +
                "TRIGGER e2 WHEN t(a => b) " +
                "TRIGGER e3 WHEN s(a => b) AND t(a => b) " +
                "GUARD WHEN ENTER t(b) AND s(b) CALL q1 " +
                "GUARD WHEN LEAVE t(a) AND s(a) CALL q2 " +
                ""
                );

            var sm = cc.SM;

            sm.Calculate();

            ValidateEventEffectConditionPre(sm, "e1", "q1", "s(a)t(b)");
            ValidateEventEffectConditionPre(sm, "e2", "q1", "s(b)t(a)");
            ValidateEventEffectConditionPre(sm, "e3", "q1", "s(a)t(a)");
        }
Exemple #17
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);
        }