Пример #1
0
 static void WriteFSMFile(DummyGraphViewForm form, string machineFileName)
 {
     System.IO.StreamWriter sw = new System.IO.StreamWriter(machineFileName);
     string fsmAsString = form.View.finiteAutomatonContext.fa.ToString();
     sw.Write(fsmAsString);
     sw.Close();
 }
Пример #2
0
        /// <summary>
        /// Provides programmatic access to the commandline utility 'mp2dot.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

            #region load the state variables for state tooltip if any
            if (!String.IsNullOrEmpty(settings.StateVarsFile))
            {
                try
                {
                    System.IO.StreamReader stateVarsReader =
                        new System.IO.StreamReader(settings.StateVarsFile);
                    StateTooltipVars = stateVarsReader.ReadToEnd();
                    stateVarsReader.Close();
                }
                catch (Exception e)
                {
                    throw new ModelProgramUserException("Cannot create state variables list: " + 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);
                }
            }

            // must replace form.View with something that doesn't depend on Windows.Forms or GLEE
            //ModelProgramGraphViewForm form = new ModelProgramGraphViewForm("Model Program Viewer");
            DummyGraphViewForm form = new DummyGraphViewForm("mp2dot");
            //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.SetModelProgram(mp);
            form.View.graphWorker();
            form.View.WriteExplorationStatistics();

            if (!String.IsNullOrEmpty(settings.dotFileName))
            {
                WriteDotFile(form, settings.dotFileName);
            }

            if (!String.IsNullOrEmpty(settings.machineFileName))
            {
                WriteFSMFile(form, settings.machineFileName);
            }
        }
Пример #3
0
 static void WriteDotFile(DummyGraphViewForm form, string dotFileName)
 {
     // Console.WriteLine("Preparing to write dot file {0}", dotFileName);
     System.IO.StreamWriter sw = new System.IO.StreamWriter(dotFileName);
     string dot = form.View.ToDot();
     sw.Write(dot);
     sw.Close();
 }