Exemple #1
0
 public FSMParser(FSMBuilder theBuilder, string theFileName)
 {
     this.builder              = theBuilder;
     this.fileName             = theFileName;
     this.FSMGeneratorName     = typeof(SMCSharpGenerator).FullName;
     this.builder.ErrorManager = this.errorManager;
 }
        /// <summary>
        /// Generates a testsuite that covers the transitions of the state machine generated from the
        /// model program. The state machine generated from the model program
        /// is assumed to be finite. If a nonempty filename is provided,
        /// writes the generated test suite in the file, otherwise writes the
        /// testsuite to the console.
        /// </summary>
        public void GenerateTestSuite()
        {
            FSM fa = new FSMBuilder(model).Explore();
            Sequence <Sequence <CompoundTerm> > testsuite =
                FsmTraversals.GenerateTestSequences(fa);

            using (StreamWriter sw = GetTestSuiteWriter())
            {
                WriteLine(sw, "TestSuite(");
                int testNr = 0;
                foreach (Sequence <CompoundTerm> testcase in testsuite)
                {
                    WriteLine(sw, "    TestCase(");
                    //WriteLine(sw, "        " + this.startTest + "(" + testNr + "),");
                    int nrofactions = testcase.Count;
                    foreach (CompoundTerm action in testcase)
                    {
                        nrofactions -= 1;
                        WriteLine(sw, "        " + action.ToString() +
                                  (nrofactions > 0 ? "," : ""));
                    }
                    testNr += 1;
                    WriteLine(sw, "    )" + (testNr < testsuite.Count ? "," : ""));
                }
                WriteLine(sw, ")");
            }
        }
        public void PowerSwitchTest()
        {
            LibraryModelProgram mp        = LibraryModelProgram.Create(typeof(SampleModels.PowerSwitch.Contract));
            FSMBuilder          fabuilder = new FSMBuilder(mp);
            FSM fsm = fabuilder.Explore();

            Assert.AreEqual(2, fsm.AcceptingStates.Count, "Unexpected number of accepting states.");
            Assert.AreEqual(2, fsm.States.Count, "Unexpected number of states.");
            Assert.AreEqual(2, fsm.Transitions.Count, "Unexpected number of transitions.");
            Assert.IsTrue(fsm.IsDeterministic, "FSM expected to be deterministic.");
            Set <Symbol> voc = new Set <Symbol>(Symbol.Parse("PowerOn"), Symbol.Parse("PowerOff"));

            Assert.AreEqual(voc, fsm.Vocabulary, "Unexpected vocabulary.");
        }
        public void FanTest2()
        {
            LibraryModelProgram mp = LibraryModelProgram.Create(typeof(SampleModels.Fan.Control),
                                                                "Power", "Control", "Speed", "Filter1");
            FSMBuilder fabuilder = new FSMBuilder(mp);
            FSM        fsm       = fabuilder.Explore();

            Assert.AreEqual(11, fsm.States.Count, "Unexpected number of states.");
            Assert.AreEqual(20, fsm.Transitions.Count, "Unexpected number of transitions.");
            Assert.IsTrue(fsm.IsDeterministic, "FSM expected to be deterministic.");
            Set <Symbol> voc = new Set <Symbol>(Symbol.Parse("PowerOn"),
                                                Symbol.Parse("PowerOff"), Symbol.Parse("ControlPower"),
                                                Symbol.Parse("ControlSpeed"), Symbol.Parse("IncrementSpeed"));

            Assert.AreEqual(voc, fsm.Vocabulary, "Unexpected vocabulary.");
        }
Exemple #5
0
        public void FSM_Events_Lead_To_Valid_Transitions()
        {
            string active   = "active";
            string inactive = "inactive";
            string idle     = "idle";

            var fsmBuilder = new FSMBuilder <FSMContext>();

            fsmBuilder
            .AddState(active, c => c.State   = active)
            .AddState(inactive, c => c.State = inactive)
            .AddState(idle, c => c.State     = idle);

            var turnOffEvent = fsmBuilder.From(active).To(inactive);
            var turnOnEvent  = fsmBuilder.From(inactive).To(active);
            var goIdleEvent  = fsmBuilder.From(active).To(idle);

            var fsm = fsmBuilder.Build(active);

            var context = new FSMContext();

            fsm.Update(context);
            Assert.AreEqual(active, context.State);

            turnOffEvent.Raise();
            fsm.Update(context);
            Assert.AreEqual(inactive, context.State);

            goIdleEvent.Raise();
            fsm.Update(context);
            Assert.AreEqual(inactive, context.State);

            turnOnEvent.Raise();
            fsm.Update(context);
            Assert.AreEqual(active, context.State);

            goIdleEvent.Raise();
            fsm.Update(context);
            Assert.AreEqual(idle, context.State);
        }