예제 #1
0
        void server()
        {
            if (Config.logEnabled)
            {
                Logger.log("start UDP server on port " + Config.asyncPort);
            }
            IPEndPoint ipep    = new IPEndPoint(IPAddress.Any, Config.asyncPort);
            Socket     newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            newsock.Bind(ipep);
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint   Remote = (EndPoint)(sender);

            while (true)
            {
                byte[] data = new byte[Config.bufferSize];
                int    recv = newsock.ReceiveFrom(data, ref Remote);
                string s    = Encoding.UTF8.GetString(data, 0, recv);
                if (Config.logEnabled)
                {
                    Logger.log("obsv " + s);
                }
                if (observer != null)
                {
                    observer(CompoundTerm.Parse(s));
                }
            }
        }
예제 #2
0
        internal Set <CompoundTerm> processGoal(ModelProgram mp, Set <CompoundTerm> goals)
        {
            Set <CompoundTerm> processedGoals = Set <CompoundTerm> .EmptySet;

            if (typeof(LibraryModelProgram) == mp.GetType())
            {
                //we ignore it for the moment
                Console.Error.WriteLine("Goals involving LibraryModelPrograms currently not supported. ");
                Console.Error.WriteLine("Currently searching for a match for '" + goals.ToString() + "'. ");
            }
            else if (typeof(FsmModelProgram) == mp.GetType())
            {
                FsmModelProgram fsm = (FsmModelProgram)mp;
                foreach (CompoundTerm ct in goals)
                {
                    Console.WriteLine("Checking FSM: " + ct.ToString() + "; " + fsm.Name);
                    if (ct.FunctionSymbol.ToString() == fsm.Name)
                    {
                        processedGoals = processedGoals.Add(CompoundTerm.Parse("FsmState(Set(" + ct.Arguments[0].ToString() + "))"));
                        goals          = goals.Remove(ct);
                    }
                    Console.WriteLine("Current processedGoals: " + processedGoals.ToString());
                }
            }
            else if (typeof(ProductModelProgram) == mp.GetType())
            {
                ProductModelProgram pmp = (ProductModelProgram)mp;
                processedGoals = processedGoals.Union(processGoal(pmp.M1, goals));
                processedGoals = processedGoals.Union(processGoal(pmp.M2, goals));
            }
            return(processedGoals);
        }
예제 #3
0
        internal static StringBuilder FSM2Dot(string name, string fsm)
        {
            FSM         fa = FSM.FromTerm(CompoundTerm.Parse(fsm));
            GraphParams gp = new GraphParams(name, fa);

            return(DotWriter.ToDot(gp));
        }
예제 #4
0
        /// <summary>
        /// Process the goal in the format "ModelProgramName1(stateName1(value1)),ModelProgramName2(stateName2(value2)" to a compound term of the corresponding model program.
        /// In the case of FSMs the format is "ModelProgramName(stateNumber)".
        /// </summary>
        /// <param name="mp">model program</param>
        /// <param name="goalString">goal as string</param>
        /// <returns>A set of compound terms corresponding to the goal string.</returns>
        internal Set <CompoundTerm> processGoal(ModelProgram mp, string goalString)
        {
            Set <CompoundTerm> processedGoals = Set <CompoundTerm> .EmptySet;

            if (goalString == "")
            {
                return(processedGoals);
            }
            string[]           subgoals = goalString.Split(',');
            Set <CompoundTerm> goals    = Set <CompoundTerm> .EmptySet;

            try
            {
                foreach (string g in subgoals)
                {
                    CompoundTerm ct = CompoundTerm.Parse(g);
                    goals = goals.Add(ct);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("The goal '" + goalString + "' seems not to be a term. " +
                                        "Goal should be in the form ModelProgramName(state) " +
                                        "(or several such terms separated by commas).");
                Console.Error.WriteLine("The error message: ", e.Message);
                return(processedGoals);
            }
            return(processGoal(mp, goals));
        }
예제 #5
0
        public void FSM2Dot1()
        {
            FSM           fa    = FSM.FromTerm(CompoundTerm.Parse(FSMTRIANGLE));
            GraphParams   gp    = new GraphParams("Test1", fa);
            StringBuilder sb    = DotWriter.ToDot(gp);
            string        s     = sb.ToString();
            Regex         r     = new Regex("\\[ label = \"AssignColor\" \\];", RegexOptions.Multiline);
            int           count = 0;

            count = r.Matches(s).Count;
            Assert.AreEqual(42, count);
        }
예제 #6
0
        /// <summary/>
        public CompoundTerm DoAction(CompoundTerm action)
        {
            Config.init();
            string s = action.ToString();

            if (!c.isConnected())
            {
                c.Socket();
                if (Config.logEnabled)
                {
                    Logger.log("connect to " + Config.host + ":" + Config.port);
                }
                c.Connect(Config.host, Config.port, Config.bufferSize);
            }
            if (resetDelayed)
            {
                if (Config.logEnabled)
                {
                    Logger.log("delayed Reset on " + s);
                }
                reset();
            }
            if (Config.logEnabled)
            {
                Logger.log("send " + s);
            }
            c.Send(s + "\n");
            s = receive();
            if (Config.logEnabled)
            {
                Logger.log("rcvd " + s);
            }
            if (s.Length == 0)
            {
                return(null);
            }
            else
            {
                return(CompoundTerm.Parse(s));
            }
        }
예제 #7
0
        public static void RunWithCommandLineArguments(string[] args)
        {
            //System.Diagnostics.Debugger.Break();
            ConformanceTester confTester = null;

            try
            {
                ConfTesterCommandLineSettings settings = new ConfTesterCommandLineSettings();
                if (!Parser.ParseArgumentsWithUsage(args, settings))
                {
                    //Console.ReadLine();
                    return;
                }

                #region load the libraries
                List <Assembly> libs = new List <Assembly>();
                try
                {
                    if (settings.reference != null)
                    {
                        foreach (string l in settings.reference)
                        {
                            libs.Add(System.Reflection.Assembly.LoadFrom(l));
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException(e.Message);
                }
                #endregion

                #region create the implementation stepper using the factory method
                string implStepperMethodName;
                string implStepperClassName;
                ReflectionHelper.SplitFullMethodName(settings.iut, out implStepperClassName, out implStepperMethodName);
                Type       implStepperType   = ReflectionHelper.FindType(libs, implStepperClassName);
                MethodInfo implStepperMethod = ReflectionHelper.FindMethod(implStepperType, implStepperMethodName, Type.EmptyTypes, typeof(IStepper));
                IStepper   implStepper       = null;
                try
                {
                    implStepper = (IStepper)implStepperMethod.Invoke(null, null);
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Invocation of '" + settings.iut + "' failed: " + e.ToString());
                }
                #endregion

                #region create a model program for each model using the factory method and compose into product
                string       mpMethodName;
                string       mpClassName;
                ModelProgram mp = null;
                if (settings.model != null && settings.model.Length > 0)
                {
                    ReflectionHelper.SplitFullMethodName(settings.model[0], out mpClassName, out mpMethodName);
                    Type       mpType   = ReflectionHelper.FindType(libs, mpClassName);
                    MethodInfo mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                    try
                    {
                        mp = (ModelProgram)mpMethod.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Invocation of '" + settings.model[0] + "' failed: " + e.ToString());
                    }
                    for (int i = 1; i < settings.model.Length; i++)
                    {
                        ReflectionHelper.SplitFullMethodName(settings.model[i], out mpClassName, out mpMethodName);
                        mpType   = ReflectionHelper.FindType(libs, mpClassName);
                        mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                        ModelProgram mp2 = null;
                        try
                        {
                            mp2 = (ModelProgram)mpMethod.Invoke(null, null);
                        }
                        catch (Exception e)
                        {
                            throw new ModelProgramUserException("Invocation of '" + settings.model[i] + "' failed: " + e.ToString());
                        }
                        mp = new ProductModelProgram(mp, mp2);
                    }
                }
                #endregion

                #region load the test cases if any
                Sequence <Sequence <CompoundTerm> > testcases = Sequence <Sequence <CompoundTerm> > .EmptySequence;
                if (!String.IsNullOrEmpty(settings.testSuite))
                {
                    try
                    {
                        System.IO.StreamReader testSuiteReader =
                            new System.IO.StreamReader(settings.testSuite);
                        string testSuiteAsString = testSuiteReader.ReadToEnd();
                        testSuiteReader.Close();
                        CompoundTerm testSuite = (CompoundTerm)Term.Parse(testSuiteAsString);
                        foreach (CompoundTerm testCaseTerm in testSuite.Arguments)
                        {
                            Sequence <CompoundTerm> testCase =
                                testCaseTerm.Arguments.Convert <CompoundTerm>(delegate(Term t) { return((CompoundTerm)t); });
                            testcases = testcases.AddLast(testCase);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Cannot create test suite: " + e.Message);
                    }
                }
                #endregion

                #region load the fsms if any
                Dictionary <string, FSM> fsms = new Dictionary <string, FSM>();
                if (settings.fsm != null && settings.fsm.Length > 0)
                {
                    try
                    {
                        foreach (string fsmFile in settings.fsm)
                        {
                            System.IO.StreamReader fsmReader = new System.IO.StreamReader(fsmFile);
                            string fsmAsString = fsmReader.ReadToEnd();
                            fsmReader.Close();
                            fsms[fsmFile] = FSM.FromTerm(CompoundTerm.Parse(fsmAsString));
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Cannot create fsm: " + e.Message);
                    }
                }
                #endregion

                if (mp == null && testcases.IsEmpty && fsms.Count == 0)
                {
                    throw new ModelProgramUserException("No model, fsm, or test suite was given.");
                }

                if (fsms.Count > 0)
                {
                    foreach (string fsmName in fsms.Keys)
                    {
                        ModelProgram fsmmp = new FsmModelProgram(fsms[fsmName], fsmName);
                        if (mp == null)
                        {
                            mp = fsmmp;
                        }
                        else
                        {
                            mp = new ProductModelProgram(mp, fsmmp);
                        }
                    }
                }

                #region create the model stepper
                IStrategy ms;

                if (!testcases.IsEmpty)
                {
                    ms = new TestSuiteStepper(settings.startTestAction, testcases, mp);
                }
                else
                {
                    ms = CreateModelStepper(libs, mp, settings.modelStepper, settings.coverage);
                }

                #endregion

                confTester = new ConformanceTester(ms, implStepper);

                #region configure conformance tester settings

                confTester.ContinueOnFailure = settings.continueOnFailure;
                confTester.StepsCnt          = (testcases.IsEmpty ? settings.steps : 0);
                confTester.MaxStepsCnt       = (testcases.IsEmpty ? settings.maxSteps : 0);
                confTester.RunsCnt           = (testcases.IsEmpty ? settings.runs : testcases.Count);
                confTester.WaitAction        = settings.waitAction;
                confTester.TimeoutAction     = settings.timeoutAction;

                Symbol waitActionSymbol    = confTester.waitActionSet.Choose();
                Symbol timeoutActionSymbol = confTester.timeoutAction.FunctionSymbol1;

                Set <Symbol> obs = new Set <string>(settings.observableAction).Convert <Symbol>(delegate(string s) { return(Symbol.Parse(s)); });
                confTester.ObservableActionSymbols = obs;

                Set <Symbol> cleanup = new Set <string>(settings.cleanupAction).Convert <Symbol>(delegate(string s) { return(Symbol.Parse(s)); });
                confTester.CleanupActionSymbols = cleanup;

                if (confTester.IsAsync)
                {
                    //remove the wait and timeout action symbol from tester action symbols
                    if (confTester.testerActionSymbols.Contains(waitActionSymbol) ||
                        confTester.testerActionSymbols.Contains(timeoutActionSymbol))
                    {
                        confTester.testerActionSymbols =
                            confTester.testerActionSymbols.Remove(waitActionSymbol).Remove(timeoutActionSymbol);
                    }
                }

                Set <Symbol> internals = new Set <string>(settings.internalAction).Convert <Symbol>(delegate(string s) { return(Symbol.Parse(s)); });

                confTester.InternalActionSymbols =
                    (testcases.IsEmpty || settings.startTestAction != "Test" ?
                     internals :
                     internals.Add(Symbol.Parse("Test")));

                TimeSpan timeout = new TimeSpan(0, 0, 0, 0, settings.timeout);
                confTester.TesterActionTimeout = delegate(IState s, CompoundTerm a) { return(timeout); };
                confTester.Logfile             = settings.logfile;
                confTester.OverwriteLog        = settings.overwriteLog;
                if (settings.randomSeed != 0)
                {
                    confTester.RandomSeed = settings.randomSeed;
                }
                #endregion

                //finally, run the application
                confTester.Run();
            }
            catch (ModelProgramUserException)
            {
                throw;
            }
            catch (ConformanceTesterException e)
            {
                throw new ModelProgramUserException(e.Message);
            }
            finally
            {
                if (confTester != null)
                {
                    confTester.Dispose();
                }
            }
        }
예제 #8
0
 /// <summary>
 /// Create an FSM from a string representation of a compound term fsm of the form
 /// FSM(initialState, acceptingStates, transitions [, vocabulary])
 /// where:
 ///  - initialState is a term,
 ///  - acceptingStates is a compound term of the form AcceptingStates(p1,...,pk)
 ///  - transitions is a compound term of the form Transitions(t1,...,tl)
 ///    where each ti is compound term of the form t(q,a,q') where
 ///    q and q' are states and a is a compound term
 ///  - vocabulary is optional, if present it must be
 ///    a compound term of the form Vocabulary(s1,...,sn) where
 ///    each si is a string literal
 /// </summary>
 /// <param name="fsm">string representation of an FSM</param>
 /// <returns>FSM corresponding to fsm</returns>
 public static FSM FromString(string fsm)
 {
     return(FromTerm(CompoundTerm.Parse(fsm)));
 }
예제 #9
0
        /// <summary>
        /// Provides programmatic access to the ModelProgramViewer commandline utility 'mpv.exe'.
        /// </summary>
        /// <param name="args">command line arguments: model program(s), optional settings for the viewer</param>
        /// <remarks>The settings are displayed when 'mpv.exe /?' is executed from the command line without arguments.</remarks>
        public static void RunWithCommandLineArguments(params string[] args)
        {
            ProgramSettings settings = new ProgramSettings();

            if (!Parser.ParseArgumentsWithUsage(args, settings))
            {
                return;
            }

            #region load the libraries
            List <Assembly> libs = new List <Assembly>();
            try
            {
                if (settings.reference != null)
                {
                    foreach (string l in settings.reference)
                    {
                        libs.Add(System.Reflection.Assembly.LoadFrom(l));
                    }
                }
            }
            catch (Exception e)
            {
                throw new ModelProgramUserException(e.Message);
            }
            #endregion

            #region create a model program for each model using the factory method and compose into product
            string       mpMethodName;
            string       mpClassName;
            ModelProgram mp = null;
            if (settings.model != null && settings.model.Length > 0)
            {
                if (libs.Count == 0)
                {
                    throw new ModelProgramUserException("No reference was provided to load models from.");
                }
                ReflectionHelper.SplitFullMethodName(settings.model[0], out mpClassName, out mpMethodName);
                Type       mpType   = ReflectionHelper.FindType(libs, mpClassName);
                MethodInfo mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                try
                {
                    mp = (ModelProgram)mpMethod.Invoke(null, null);
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Invocation of '" + settings.model[0] + "' failed: " + e.ToString());
                }
                for (int i = 1; i < settings.model.Length; i++)
                {
                    ReflectionHelper.SplitFullMethodName(settings.model[i], out mpClassName, out mpMethodName);
                    mpType   = ReflectionHelper.FindType(libs, mpClassName);
                    mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                    ModelProgram mp2 = null;
                    try
                    {
                        mp2 = (ModelProgram)mpMethod.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Invocation of '" + settings.model[i] + "' failed: " + e.ToString());
                    }
                    mp = new ProductModelProgram(mp, mp2);
                }
            }
            #endregion

            #region create a model program from given namespace and feature names
            if (settings.mp != null && settings.mp.Length > 0)
            {
                if (libs.Count == 0)
                {
                    throw new ModelProgramUserException("No reference was provided to load models from.");
                }
                //parse the model program name and the feature names for each entry
                foreach (string mps in settings.mp)
                {
                    //the first element is the model program, the remaining ones are
                    //feature names
                    string[] mpsSplit = mps.Split(new string[] { "[", "]", "," },
                                                  StringSplitOptions.RemoveEmptyEntries);
                    if (mpsSplit.Length == 0)
                    {
                        throw new ModelProgramUserException("Invalid model program specifier '" + mps + "'.");
                    }
                    string       mpName     = mpsSplit[0];
                    Assembly     mpAssembly = ReflectionHelper.FindAssembly(libs, mpName);
                    Set <string> mpFeatures = new Set <string>(mpsSplit).Remove(mpName);
                    ModelProgram mp1        = new LibraryModelProgram(mpAssembly, mpName, mpFeatures);
                    mp = (mp == null ? mp1 : new ProductModelProgram(mp, mp1));
                }
            }

            #endregion

            #region load the test cases if any
            Sequence <Sequence <CompoundTerm> > testcases = Sequence <Sequence <CompoundTerm> > .EmptySequence;
            if (!String.IsNullOrEmpty(settings.testSuite))
            {
                try
                {
                    System.IO.StreamReader testSuiteReader =
                        new System.IO.StreamReader(settings.testSuite);
                    string testSuiteAsString = testSuiteReader.ReadToEnd();
                    testSuiteReader.Close();
                    CompoundTerm testSuite = CompoundTerm.Parse(testSuiteAsString);
                    foreach (CompoundTerm testCaseTerm in testSuite.Arguments)
                    {
                        Sequence <CompoundTerm> testCase =
                            testCaseTerm.Arguments.Convert <CompoundTerm>(delegate(Term t) { return((CompoundTerm)t); });
                        testcases = testcases.AddLast(testCase);
                    }
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Cannot create test suite: " + e.Message);
                }
            }
            #endregion

            #region load the fsms if any
            Dictionary <string, FSM> fsms = new Dictionary <string, FSM>();
            if (settings.fsm != null && settings.fsm.Length > 0)
            {
                try
                {
                    foreach (string fsmFile in settings.fsm)
                    {
                        System.IO.StreamReader fsmReader = new System.IO.StreamReader(fsmFile);
                        string fsmAsString = fsmReader.ReadToEnd();
                        fsmReader.Close();
                        fsms[fsmFile] = FSM.FromTerm(CompoundTerm.Parse(fsmAsString));
                    }
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Cannot create fsm: " + e.Message);
                }
            }
            #endregion

            if (mp == null && testcases.IsEmpty && fsms.Count == 0)
            {
                throw new ModelProgramUserException("No model, fsm, or test suite was given.");
            }

            if (!testcases.IsEmpty)
            {
                FSM fa = FsmTraversals.GenerateTestSequenceAutomaton(
                    settings.startTestAction, testcases, GetActionSymbols(testcases));
                ModelProgram famp = new FsmModelProgram(fa, settings.testSuite);
                if (mp == null)
                {
                    mp = famp;
                }
                else
                {
                    mp = new ProductModelProgram(mp, famp);
                }
            }

            if (fsms.Count > 0)
            {
                foreach (string fsmName in fsms.Keys)
                {
                    ModelProgram fsmmp = new FsmModelProgram(fsms[fsmName], fsmName);
                    if (mp == null)
                    {
                        mp = fsmmp;
                    }
                    else
                    {
                        mp = new ProductModelProgram(mp, fsmmp);
                    }
                }
            }

            ModelProgramGraphViewForm form = new ModelProgramGraphViewForm("Model Program Viewer");
            //configure the settings of the viewer
            form.View.AcceptingStatesMarked   = settings.acceptingStatesMarked;
            form.View.TransitionLabels        = settings.transitionLabels;
            form.View.CombineActions          = settings.combineActions;
            form.View.Direction               = settings.direction;
            form.View.UnsafeStateColor        = Color.FromName(settings.unsafeStateColor);
            form.View.HoverColor              = Color.FromName(settings.hoverColor);
            form.View.InitialStateColor       = Color.FromName(settings.initialStateColor);
            form.View.LoopsVisible            = settings.loopsVisible;
            form.View.MaxTransitions          = settings.maxTransitions;
            form.View.NodeLabelsVisible       = settings.nodeLabelsVisible;
            form.View.SelectionColor          = Color.FromName(settings.selectionColor);
            form.View.MergeLabels             = settings.mergeLabels;
            form.View.StateShape              = settings.stateShape;
            form.View.DeadStateColor          = Color.FromName(settings.deadStateColor);
            form.View.InitialTransitions      = settings.initialTransitions;
            form.View.LivenessCheckIsOn       = settings.livenessCheckIsOn;
            form.View.ExcludeIsomorphicStates = settings.excludeIsomorphicStates;
            form.View.SafetyCheckIsOn         = settings.safetyCheckIsOn;
            form.View.DeadstatesVisible       = settings.deadStatesVisible;
            form.View.StateViewVisible        = settings.stateViewVisible;

            //show the view of the product of all the model programs
            form.View.SetModelProgram(mp);

            form.OnSaveSettings += new EventHandler(settings.SaveSettings);
            form.ShowDialog();
        }
예제 #10
0
        public static void RunWithCommandLineArguments(string[] args)
        {
            MCCmdLineParams settings = new MCCmdLineParams();

            if (!Parser.ParseArgumentsWithUsage(args, settings))
            {
                return;
            }



            #region load the libraries
            List <Assembly> libs = new List <Assembly>();
            try
            {
                if (settings.reference != null)
                {
                    foreach (string l in settings.reference)
                    {
                        libs.Add(System.Reflection.Assembly.LoadFrom(l));
                    }
                }
            }
            catch (Exception e)
            {
                throw new ModelProgramUserException(e.Message);
            }
            #endregion

            #region load the test cases if any
            CompoundTerm goal = null;
            if (!String.IsNullOrEmpty(settings.goal))
            {
                try
                {
                    System.IO.StreamReader goalReader =
                        new System.IO.StreamReader(settings.goal);
                    string goalAsString = goalReader.ReadToEnd();
                    goalReader.Close();
                    goal = CompoundTerm.Parse(goalAsString);
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Cannot create goal: " + e.Message);
                }
            }
            else
            {
                Console.WriteLine("No goal was specified, counting distinct states and transitions.");
                Console.WriteLine("Invalid end states check currently not enabled.");
            }
            #endregion

            #region create a model program for each model using the factory method and compose into product
            string       mpMethodName;
            string       mpClassName;
            ModelProgram mp = null;
            if (settings.model != null && settings.model.Length > 0)
            {
                if (libs.Count == 0)
                {
                    throw new ModelProgramUserException("No reference was provided to load models from.");
                }
                ReflectionHelper.SplitFullMethodName(settings.model[0], out mpClassName, out mpMethodName);
                Type       mpType   = ReflectionHelper.FindType(libs, mpClassName);
                MethodInfo mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                try
                {
                    mp = (ModelProgram)mpMethod.Invoke(null, null);
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Invocation of '" + settings.model[0] + "' failed: " + e.ToString());
                }
                for (int i = 1; i < settings.model.Length; i++)
                {
                    ReflectionHelper.SplitFullMethodName(settings.model[i], out mpClassName, out mpMethodName);
                    mpType   = ReflectionHelper.FindType(libs, mpClassName);
                    mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                    ModelProgram mp2 = null;
                    try
                    {
                        mp2 = (ModelProgram)mpMethod.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Invocation of '" + settings.model[i] + "' failed: " + e.ToString());
                    }
                    mp = new ProductModelProgram(mp, mp2);
                }
            }
            #endregion

            if (mp == null)
            {
                Console.WriteLine("ModelProgram was null");
                Console.WriteLine("Tried to instantiate:");
                if (settings.model != null)
                {
                    foreach (string s in settings.model)
                    {
                        Console.WriteLine(s);
                    }
                }
                return;
            }
            Reachability mc = new Reachability();
            mc.excludeIsomorphicStates = settings.excludeIsomorphic;
            mc.modelProgram            = mp;
            DateTime           before = DateTime.Now;
            ReachabilityResult result = mc.CheckReachability();
            DateTime           after  = DateTime.Now;
            Console.WriteLine("Results of reachability checking:");
            Console.WriteLine();
            Console.WriteLine(" States reached: " + result.StateCount);
            Console.WriteLine(" Transitions covered: " + result.TransitionCount);
        }
예제 #11
0
        /// <summary>
        /// Provides programmatic access to the offline test generator 'otg.exe'.
        /// </summary>
        /// <param name="args">command line arguments: references (required), model program(s) (required), name of test suite file and other settings (optional)</param>
        /// <remarks>The settings are displayed when 'otg.exe /?' is executed from the command line.</remarks>
        public static void RunWithCommandLineArguments(string[] args)
        {
            OfflineTestGenCommandLineSettings settings = new OfflineTestGenCommandLineSettings();

            if (!Parser.ParseArgumentsWithUsage(args, settings))
            {
                //Console.ReadLine();
                return;
            }

            if ((settings.reference == null || settings.reference.Length == 0) &&
                settings.model != null && settings.model.Length > 0)
            {
                throw new ModelProgramUserException("Reference missing for model programs.");
            }

            #region load the libraries
            List <Assembly> libs = new List <Assembly>();
            try
            {
                if (settings.reference != null)
                {
                    foreach (string l in settings.reference)
                    {
                        libs.Add(System.Reflection.Assembly.LoadFrom(l));
                    }
                }
            }
            catch (Exception e)
            {
                throw new ModelProgramUserException(e.Message);
            }
            #endregion

            #region create a model program for each model using the factory method and compose into product
            string       mpMethodName;
            string       mpClassName;
            ModelProgram mp = null;
            if (settings.model.Length > 0)
            {
                ReflectionHelper.SplitFullMethodName(settings.model[0], out mpClassName, out mpMethodName);
                Type       mpType   = ReflectionHelper.FindType(libs, mpClassName);
                MethodInfo mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                try
                {
                    mp = (ModelProgram)mpMethod.Invoke(null, null);
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Invocation of '" + settings.model[0] + "' failed: " + e.ToString());
                }
                for (int i = 1; i < settings.model.Length; i++)
                {
                    ReflectionHelper.SplitFullMethodName(settings.model[i], out mpClassName, out mpMethodName);
                    mpType   = ReflectionHelper.FindType(libs, mpClassName);
                    mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                    ModelProgram mp2 = null;
                    try
                    {
                        mp2 = (ModelProgram)mpMethod.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Invocation of '" + settings.model[i] + "' failed: " + e.ToString());
                    }
                    mp = new ProductModelProgram(mp, mp2);
                }
            }
            #endregion

            #region create a model program from given namespace and feature names
            if (settings.mp != null && settings.mp.Length > 0)
            {
                if (libs.Count == 0)
                {
                    throw new ModelProgramUserException("No reference was provided to load models from.");
                }
                //parse the model program name and the feature names for each entry
                foreach (string mps in settings.mp)
                {
                    //the first element is the model program, the remaining ones are
                    //feature names
                    string[] mpsSplit = mps.Split(new string[] { "[", "]", "," },
                                                  StringSplitOptions.RemoveEmptyEntries);
                    if (mpsSplit.Length == 0)
                    {
                        throw new ModelProgramUserException("Invalid model program specifier '" + mps + "'.");
                    }
                    string       mpName     = mpsSplit[0];
                    Assembly     mpAssembly = ReflectionHelper.FindAssembly(libs, mpName);
                    Set <string> mpFeatures = new Set <string>(mpsSplit).Remove(mpName);
                    ModelProgram mp1        = new LibraryModelProgram(mpAssembly, mpName, mpFeatures);
                    mp = (mp == null ? mp1 : new ProductModelProgram(mp, mp1));
                }
            }

            #endregion

            #region load the fsms if any
            Dictionary <string, FSM> fsms = new Dictionary <string, FSM>();
            if (settings.fsm != null && settings.fsm.Length > 0)
            {
                try
                {
                    foreach (string fsmFile in settings.fsm)
                    {
                        System.IO.StreamReader fsmReader = new System.IO.StreamReader(fsmFile);
                        string fsmAsString = fsmReader.ReadToEnd();
                        fsmReader.Close();
                        fsms[fsmFile] = FSM.FromTerm(CompoundTerm.Parse(fsmAsString));
                    }
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Cannot create fsm: " + e.Message);
                }
            }
            #endregion

            if (mp == null && fsms.Count == 0)
            {
                throw new ModelProgramUserException("No model program or fsm was given.");
            }

            if (fsms.Count > 0)
            {
                foreach (string fsmName in fsms.Keys)
                {
                    ModelProgram fsmmp = new FsmModelProgram(fsms[fsmName], fsmName);
                    if (mp == null)
                    {
                        mp = fsmmp;
                    }
                    else
                    {
                        mp = new ProductModelProgram(mp, fsmmp);
                    }
                }
            }

            OfflineTestGenerator gen = new OfflineTestGenerator(mp);

            #region configure offline test generator settings

            gen.FileName = settings.file;
            gen.Append   = settings.append;
            //gen.StartTest = settings.startTest;

            #endregion

            //finally, run the offline test generator
            gen.GenerateTestSuite();
        }
예제 #12
0
        public static void RunWithCommandLineArguments(string[] args)
        {
            //System.Diagnostics.Debugger.Break();
            ConformanceTester confTester = null;

            try
            {
                ConfTesterCommandLineSettings settings = new ConfTesterCommandLineSettings();
                if (!Parser.ParseArgumentsWithUsage(args, settings))
                {
                    //Console.ReadLine();
                    return;
                }

                #region load the libraries
                List <Assembly> libs = new List <Assembly>();
                try
                {
                    if (settings.reference != null)
                    {
                        foreach (string l in settings.reference)
                        {
                            libs.Add(System.Reflection.Assembly.LoadFrom(l));
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException(e.Message);
                }
                #endregion

                #region create the implementation stepper using the factory method
                string implStepperMethodName;
                string implStepperClassName;
                ReflectionHelper.SplitFullMethodName(settings.iut, out implStepperClassName, out implStepperMethodName);
                Type       implStepperType   = ReflectionHelper.FindType(libs, implStepperClassName);
                MethodInfo implStepperMethod = ReflectionHelper.FindMethod(implStepperType, implStepperMethodName, Type.EmptyTypes, typeof(IStepper));
                IStepper   implStepper       = null;
                try
                {
                    implStepper = (IStepper)implStepperMethod.Invoke(null, null);
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Invocation of '" + settings.iut + "' failed: " + e.ToString());
                }
                #endregion

                #region create a model program for each model using the factory method and compose into product
                string       mpMethodName;
                string       mpClassName;
                ModelProgram mp = null;
                if (settings.model != null && settings.model.Length > 0)
                {
                    ReflectionHelper.SplitFullMethodName(settings.model[0], out mpClassName, out mpMethodName);
                    Type       mpType   = ReflectionHelper.FindType(libs, mpClassName);
                    MethodInfo mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                    try
                    {
                        mp = (ModelProgram)mpMethod.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Invocation of '" + settings.model[0] + "' failed: " + e.ToString());
                    }
                    for (int i = 1; i < settings.model.Length; i++)
                    {
                        ReflectionHelper.SplitFullMethodName(settings.model[i], out mpClassName, out mpMethodName);
                        mpType   = ReflectionHelper.FindType(libs, mpClassName);
                        mpMethod = ReflectionHelper.FindMethod(mpType, mpMethodName, Type.EmptyTypes, typeof(ModelProgram));
                        ModelProgram mp2 = null;
                        try
                        {
                            mp2 = (ModelProgram)mpMethod.Invoke(null, null);
                        }
                        catch (Exception e)
                        {
                            throw new ModelProgramUserException("Invocation of '" + settings.model[i] + "' failed: " + e.ToString());
                        }
                        mp = new ProductModelProgram(mp, mp2);
                    }
                }
                #endregion

                #region create a model program from given namespace and feature names
                if (settings.mp != null && settings.mp.Length > 0)
                {
                    if (libs.Count == 0)
                    {
                        throw new ModelProgramUserException("No reference was provided to load models from.");
                    }
                    //parse the model program name and the feature names for each entry
                    foreach (string mps in settings.mp)
                    {
                        //the first element is the model program, the remaining ones are
                        //feature names
                        string[] mpsSplit = mps.Split(new string[] { "[", "]", "," },
                                                      StringSplitOptions.RemoveEmptyEntries);
                        if (mpsSplit.Length == 0)
                        {
                            throw new ModelProgramUserException("Invalid model program specifier '" + mps + "'.");
                        }
                        string       mpName     = mpsSplit[0];
                        Assembly     mpAssembly = ReflectionHelper.FindAssembly(libs, mpName);
                        Set <string> mpFeatures = new Set <string>(mpsSplit).Remove(mpName);
                        ModelProgram mp1        = new LibraryModelProgram(mpAssembly, mpName, mpFeatures);
                        mp = (mp == null ? mp1 : new ProductModelProgram(mp, mp1));
                    }
                }

                #endregion

                #region load the test cases if any
                Sequence <Sequence <CompoundTerm> > testcases = Sequence <Sequence <CompoundTerm> > .EmptySequence;
                if (!String.IsNullOrEmpty(settings.testSuite))
                {
                    try
                    {
                        System.IO.StreamReader testSuiteReader =
                            new System.IO.StreamReader(settings.testSuite);
                        string testSuiteAsString = testSuiteReader.ReadToEnd();
                        testSuiteReader.Close();
                        CompoundTerm testSuite = (CompoundTerm)Term.Parse(testSuiteAsString);
                        foreach (CompoundTerm testCaseTerm in testSuite.Arguments)
                        {
                            Sequence <CompoundTerm> testCase =
                                testCaseTerm.Arguments.Convert <CompoundTerm>(delegate(Term t) { return((CompoundTerm)t); });
                            testcases = testcases.AddLast(testCase);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Cannot create test suite: " + e.Message);
                    }
                }
                #endregion

                #region load the fsms if any
                Dictionary <string, FSM> fsms = new Dictionary <string, FSM>();
                if (settings.fsm != null && settings.fsm.Length > 0)
                {
                    try
                    {
                        foreach (string fsmFile in settings.fsm)
                        {
                            System.IO.StreamReader fsmReader = new System.IO.StreamReader(fsmFile);
                            string fsmAsString = fsmReader.ReadToEnd();
                            fsmReader.Close();
                            fsms[fsmFile] = FSM.FromTerm(CompoundTerm.Parse(fsmAsString));
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Cannot create fsm: " + e.Message);
                    }
                }
                #endregion


                // Requirements metrics

                #region load all the requirements from an external file - if any
                if (!String.IsNullOrEmpty(settings.RequirementsFile))
                {
                    try
                    {
                        System.IO.StreamReader reqsReader =
                            new System.IO.StreamReader(settings.RequirementsFile);
                        string   line;
                        char[]   splitchars = { '|' };
                        string[] splitedLine;
                        while ((line = reqsReader.ReadLine()) != null)
                        {
                            if (line.Length > 5)
                            {
                                splitedLine = line.Split(splitchars);
                                // The format of a requirement line is:
                                // action (ignore by the parser) | id | description
                                AllRequirements.Add(new KeyValuePair <string, string>(splitedLine[1].Trim().ToLower(), splitedLine[2].Trim().ToLower()));
                            }
                        }
                        reqsReader.Close();
                    }
                    catch (Exception e)
                    {
                        throw new ModelProgramUserException("Cannot create all-requirements list: " + e.Message);
                    }
                }
                #endregion


                if (mp == null && testcases.IsEmpty && fsms.Count == 0)
                {
                    throw new ModelProgramUserException("No model, fsm, or test suite was given.");
                }

                if (fsms.Count > 0)
                {
                    foreach (string fsmName in fsms.Keys)
                    {
                        ModelProgram fsmmp = new FsmModelProgram(fsms[fsmName], fsmName);
                        if (mp == null)
                        {
                            mp = fsmmp;
                        }
                        else
                        {
                            mp = new ProductModelProgram(mp, fsmmp);
                        }
                    }
                }

                #region create the model stepper
                IStrategy    ms;
                Set <Symbol> obs = new Set <string>(settings.observableAction).Convert <Symbol>(delegate(string s) { return(Symbol.Parse(s)); });
                if (!testcases.IsEmpty)
                {
                    ms = new TestSuiteStepper(settings.startTestAction, testcases, mp);
                }
                else
                {
                    ms = CreateModelStepper(libs, mp, settings.modelStepper, settings.coverage, obs);
                }

                #endregion

                confTester = new ConformanceTester(ms, implStepper);

                #region configure conformance tester settings

                confTester.ContinueOnFailure = settings.continueOnFailure;
                confTester.StepsCnt          = (testcases.IsEmpty ? settings.steps : 0);
                confTester.MaxStepsCnt       = (testcases.IsEmpty ? settings.maxSteps : 0);
                confTester.RunsCnt           = (testcases.IsEmpty ? settings.runs : testcases.Count);
                confTester.WaitAction        = settings.waitAction;
                confTester.TimeoutAction     = settings.timeoutAction;
                confTester.ShowTestCaseCoveredRequirements = settings.showTestCaseCoveredRequirements;
                confTester.showMetrics = settings.showMetrics;

                Symbol waitActionSymbol    = confTester.waitActionSet.Choose();
                Symbol timeoutActionSymbol = confTester.timeoutAction.Symbol;


                confTester.ObservableActionSymbols = obs;

                Set <Symbol> cleanup = new Set <string>(settings.cleanupAction).Convert <Symbol>(delegate(string s) { return(Symbol.Parse(s)); });
                confTester.CleanupActionSymbols = cleanup;

                if (confTester.IsAsync)
                {
                    //remove the wait and timeout action symbol from tester action symbols
                    if (confTester.testerActionSymbols.Contains(waitActionSymbol) ||
                        confTester.testerActionSymbols.Contains(timeoutActionSymbol))
                    {
                        confTester.testerActionSymbols =
                            confTester.testerActionSymbols.Remove(waitActionSymbol).Remove(timeoutActionSymbol);
                    }
                }

                Set <Symbol> internals = new Set <string>(settings.internalAction).Convert <Symbol>(delegate(string s) { return(Symbol.Parse(s)); });

                confTester.InternalActionSymbols =
                    (testcases.IsEmpty || settings.startTestAction != "Test" ?
                     internals :
                     internals.Add(Symbol.Parse("Test")));

                TimeSpan timeout = new TimeSpan(0, 0, 0, 0, settings.timeout);
                confTester.TesterActionTimeout = delegate(IState s, CompoundTerm a) { return(timeout); };
                confTester.Logfile             = settings.logfile;
                confTester.OverwriteLog        = settings.overwriteLog;
                if (settings.randomSeed != 0)
                {
                    confTester.RandomSeed = settings.randomSeed;
                }
                #endregion

                //finally, run the application
                confTester.Run();
            }
            catch (ModelProgramUserException)
            {
                throw;
            }
            catch (ConformanceTesterException e)
            {
                throw new ModelProgramUserException(e.Message);
            }
            finally
            {
                if (confTester != null)
                {
                    confTester.Dispose();
                }
            }
        }