//internal bool collapseExcludedIsomorphicStates; //int initTransitions; //CompoundTerm goal; /// <summary> /// Show all transitions from the given node and from the nodes that are reached from /// the given node etc., until the state space is exhausted or the maximum number /// of transitions is reached. /// </summary> public ReachabilityResult CheckReachability() { uint transCnt = 0; uint stateCount = 0; //(firstExploration ? (initTransitions < 0 ? maxTransitions : initTransitions) : maxTransitions); //firstExploration = false; //excludeIsomorphicStates = false; if (excludeIsomorphicStates) { Set <IState> frontier = new Set <IState>(modelProgram.InitialState); //stateCount++; //Set<IState> visited = Set<IState>.EmptySet; StateContainer <IState> visited = new StateContainer <IState>(this.modelProgram, modelProgram.InitialState); stateCount++; // need to add a check about the initial state. Set <string> transitionPropertyNames = Set <string> .EmptySet; TransitionProperties transitionProperties; while (!frontier.IsEmpty) { IState sourceIState = frontier.Choose(0); frontier = frontier.Remove(sourceIState); foreach (Symbol aSymbol in this.modelProgram.PotentiallyEnabledActionSymbols(sourceIState)) { foreach (CompoundTerm action in this.modelProgram.GetActions(sourceIState, aSymbol)) { //Node targetNode = GetTargetNode(sourceNode, action); IState targetIState = modelProgram.GetTargetState(sourceIState, action, transitionPropertyNames, out transitionProperties); //if (this.modelProgram.IsAccepting(targetIState)) // this.acceptingNodes = this.acceptingNodes.Add(targetNode); //if (!this.modelProgram.SatisfiesStateInvariant(targetState)) // this.errorNodes = this.errorNodes.Add(targetNode); //IState isomorphicState; //Transition t; transCnt++; IState isomorphicState; if (!visited.HasIsomorphic(targetIState, out isomorphicState)) { frontier = frontier.Add(targetIState); visited.Add(targetIState); stateCount++; //visited.Add(targetIState); //t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); } //else //{ // //if (collapseExcludedIsomorphicStates) // // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, nodeMap[isomorphicState]); // //else // { // Term isoNode = nodeMap[isomorphicState]; // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // if (!targetNode.Equals(sourceNode) && !targetNode.Equals(isoNode)) // groupingTransitions = groupingTransitions.Add(new Triple<Term, CompoundTerm, Term>(targetNode, new CompoundTerm(new Symbol("IsomorphicTo"), new Sequence<Term>()), isoNode)); // } //} //this.transitions = this.transitions.Add(t); //this.hiddenTransitions = this.hiddenTransitions.Remove(t); } } } //Set<IState> frontier = new Set<IState>(stateMap[node]); //StateContainer<IState> visited = new StateContainer<IState>(this.modelProgram, stateMap[node]); //while (!frontier.IsEmpty && this.transitions.Count < transCnt) //{ // IState sourceIState = frontier.Choose(0); // Node sourceNode = nodeMap[sourceIState]; // frontier = frontier.Remove(sourceIState); // foreach (Symbol aSymbol in this.modelProgram.PotentiallyEnabledActionSymbols(sourceIState)) // { // foreach (CompoundTerm action in this.modelProgram.GetActions(sourceIState, aSymbol)) // { // Node targetNode = GetTargetNode(sourceNode, action); // IState targetIState = stateMap[targetNode]; // IState isomorphicState; // Transition t; // if (!visited.HasIsomorphic(targetIState, out isomorphicState)) // { // frontier = frontier.Add(targetIState); // //visited = visited.Add(targetIState); // visited.Add(targetIState); // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // } // else // { // if (collapseExcludedIsomorphicStates) // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, nodeMap[isomorphicState]); // else // { // Term isoNode = nodeMap[isomorphicState]; // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // if (!targetNode.Equals(sourceNode) && !targetNode.Equals(isoNode)) // groupingTransitions = groupingTransitions.Add(new Triple<Term, CompoundTerm, Term>(targetNode, new CompoundTerm(new Symbol("IsomorphicTo"), new Sequence<Term>()), isoNode)); // } // } // this.transitions = this.transitions.Add(t); // this.hiddenTransitions = this.hiddenTransitions.Remove(t); // } // } //} ////Console.WriteLine(dashedTransitions.ToString()); ////Console.WriteLine(visited.ToString()); } else { Set <IState> frontier = new Set <IState>(modelProgram.InitialState); stateCount++; // need to add a check about the initial state. Set <IState> visited = new Set <IState>(modelProgram.InitialState); Set <string> transitionPropertyNames = Set <string> .EmptySet; TransitionProperties transitionProperties; while (!frontier.IsEmpty) { IState sourceIState = frontier.Choose(0); frontier = frontier.Remove(sourceIState); foreach (Symbol aSymbol in this.modelProgram.PotentiallyEnabledActionSymbols(sourceIState)) { foreach (CompoundTerm action in this.modelProgram.GetActions(sourceIState, aSymbol)) { //Node targetNode = GetTargetNode(sourceNode, action); IState targetIState = modelProgram.GetTargetState(sourceIState, action, transitionPropertyNames, out transitionProperties); //Console.WriteLine(sourceIState.ToString()); //Console.WriteLine ("--- " + action + " --->"); //Console.WriteLine (targetIState.ToString()); //Console.WriteLine(); //Console.WriteLine(); //if (this.modelProgram.IsAccepting(targetIState)) // this.acceptingNodes = this.acceptingNodes.Add(targetNode); //if (!this.modelProgram.SatisfiesStateInvariant(targetState)) // this.errorNodes = this.errorNodes.Add(targetNode); //IState isomorphicState; //Transition t; transCnt++; if (!visited.Contains(targetIState)) { frontier = frontier.Add(targetIState); visited = visited.Add(targetIState); stateCount++; //visited.Add(targetIState); //t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); } //else //{ // //if (collapseExcludedIsomorphicStates) // // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, nodeMap[isomorphicState]); // //else // { // Term isoNode = nodeMap[isomorphicState]; // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // if (!targetNode.Equals(sourceNode) && !targetNode.Equals(isoNode)) // groupingTransitions = groupingTransitions.Add(new Triple<Term, CompoundTerm, Term>(targetNode, new CompoundTerm(new Symbol("IsomorphicTo"), new Sequence<Term>()), isoNode)); // } //} //this.transitions = this.transitions.Add(t); //this.hiddenTransitions = this.hiddenTransitions.Remove(t); } } } } //Console.WriteLine("Checker results"); //Console.WriteLine("Reached states: "+stateCount); //Console.WriteLine("Number of transitions: "+transCnt); ReachabilityResult result = new ReachabilityResult(stateCount, transCnt); return(result); }
/// <summary> /// Check if the term represented by goal is reachable. Empty string results in traversing the whole state space. /// </summary> /// <param name="goal">The goal term involving the model program name as outer function symbol.</param> public ReachabilityResult CheckReachability(string goal) { uint transCnt = 0; uint stateCount = 0; //(firstExploration ? (initTransitions < 0 ? maxTransitions : initTransitions) : maxTransitions); //firstExploration = false; //excludeIsomorphicStates = false; Set<CompoundTerm> goals = processGoal(modelProgram, goal); Pair<Set<CompoundTerm>, Set<Term>> simpleAndFsmGoals = splitGoals(goals); if (GoalsSatisfied(simpleAndFsmGoals, modelProgram.InitialState)) goto end; if (excludeIsomorphicStates) { Set<IState> frontier = new Set<IState>(modelProgram.InitialState); //stateCount++; //Set<IState> visited = Set<IState>.EmptySet; StateContainer<IState> visited = new StateContainer<IState>(this.modelProgram, modelProgram.InitialState); stateCount++; // need to add a check about the initial state. Set<string> transitionPropertyNames = Set<string>.EmptySet; TransitionProperties transitionProperties; while (!frontier.IsEmpty) { IState sourceIState = frontier.Choose(0); frontier = frontier.Remove(sourceIState); foreach (Symbol aSymbol in this.modelProgram.PotentiallyEnabledActionSymbols(sourceIState)) { foreach (CompoundTerm action in this.modelProgram.GetActions(sourceIState, aSymbol)) { //Node targetNode = GetTargetNode(sourceNode, action); IState targetIState = modelProgram.GetTargetState(sourceIState, action, transitionPropertyNames, out transitionProperties); //if (this.modelProgram.IsAccepting(targetIState)) // this.acceptingNodes = this.acceptingNodes.Add(targetNode); //if (!this.modelProgram.SatisfiesStateInvariant(targetState)) // this.errorNodes = this.errorNodes.Add(targetNode); //IState isomorphicState; //Transition t; transCnt++; IState isomorphicState; if (!visited.HasIsomorphic(targetIState, out isomorphicState)) { if (GoalsSatisfied(simpleAndFsmGoals, targetIState)) goto end; frontier = frontier.Add(targetIState); visited.Add(targetIState); stateCount++; //visited.Add(targetIState); //t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); } //else //{ // //if (collapseExcludedIsomorphicStates) // // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, nodeMap[isomorphicState]); // //else // { // Term isoNode = nodeMap[isomorphicState]; // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // if (!targetNode.Equals(sourceNode) && !targetNode.Equals(isoNode)) // groupingTransitions = groupingTransitions.Add(new Triple<Term, CompoundTerm, Term>(targetNode, new CompoundTerm(new Symbol("IsomorphicTo"), new Sequence<Term>()), isoNode)); // } //} //this.transitions = this.transitions.Add(t); //this.hiddenTransitions = this.hiddenTransitions.Remove(t); } } } //Set<IState> frontier = new Set<IState>(stateMap[node]); //StateContainer<IState> visited = new StateContainer<IState>(this.modelProgram, stateMap[node]); //while (!frontier.IsEmpty && this.transitions.Count < transCnt) //{ // IState sourceIState = frontier.Choose(0); // Node sourceNode = nodeMap[sourceIState]; // frontier = frontier.Remove(sourceIState); // foreach (Symbol aSymbol in this.modelProgram.PotentiallyEnabledActionSymbols(sourceIState)) // { // foreach (CompoundTerm action in this.modelProgram.GetActions(sourceIState, aSymbol)) // { // Node targetNode = GetTargetNode(sourceNode, action); // IState targetIState = stateMap[targetNode]; // IState isomorphicState; // Transition t; // if (!visited.HasIsomorphic(targetIState, out isomorphicState)) // { // frontier = frontier.Add(targetIState); // //visited = visited.Add(targetIState); // visited.Add(targetIState); // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // } // else // { // if (collapseExcludedIsomorphicStates) // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, nodeMap[isomorphicState]); // else // { // Term isoNode = nodeMap[isomorphicState]; // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // if (!targetNode.Equals(sourceNode) && !targetNode.Equals(isoNode)) // groupingTransitions = groupingTransitions.Add(new Triple<Term, CompoundTerm, Term>(targetNode, new CompoundTerm(new Symbol("IsomorphicTo"), new Sequence<Term>()), isoNode)); // } // } // this.transitions = this.transitions.Add(t); // this.hiddenTransitions = this.hiddenTransitions.Remove(t); // } // } //} ////Console.WriteLine(dashedTransitions.ToString()); ////Console.WriteLine(visited.ToString()); } else { Set<IState> frontier = new Set<IState>(modelProgram.InitialState); Console.Out.WriteLine(frontier.ToString()); Console.Out.WriteLine(modelProgram.ToString()); stateCount++; // need to add a check about the initial state. Set<IState> visited = new Set<IState>(modelProgram.InitialState); Set<string> transitionPropertyNames = Set<string>.EmptySet; TransitionProperties transitionProperties; while (!frontier.IsEmpty) { IState sourceIState = frontier.Choose(0); frontier = frontier.Remove(sourceIState); foreach (Symbol aSymbol in this.modelProgram.PotentiallyEnabledActionSymbols(sourceIState)) { foreach (CompoundTerm action in this.modelProgram.GetActions(sourceIState, aSymbol)) { //Node targetNode = GetTargetNode(sourceNode, action); IState targetIState = modelProgram.GetTargetState(sourceIState, action, transitionPropertyNames, out transitionProperties); //Console.WriteLine(sourceIState.ToString()); //Console.WriteLine ("--- " + action + " --->"); //Console.WriteLine (targetIState.ToString()); //Console.WriteLine(); //Console.WriteLine(); //if (this.modelProgram.IsAccepting(targetIState)) // this.acceptingNodes = this.acceptingNodes.Add(targetNode); //if (!this.modelProgram.SatisfiesStateInvariant(targetState)) // this.errorNodes = this.errorNodes.Add(targetNode); //IState isomorphicState; //Transition t; transCnt++; if (!visited.Contains(targetIState)) { if (GoalsSatisfied(simpleAndFsmGoals, targetIState)) goto end; frontier = frontier.Add(targetIState); visited = visited.Add(targetIState); stateCount++; //visited.Add(targetIState); //t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); } //else //{ // //if (collapseExcludedIsomorphicStates) // // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, nodeMap[isomorphicState]); // //else // { // Term isoNode = nodeMap[isomorphicState]; // t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode); // if (!targetNode.Equals(sourceNode) && !targetNode.Equals(isoNode)) // groupingTransitions = groupingTransitions.Add(new Triple<Term, CompoundTerm, Term>(targetNode, new CompoundTerm(new Symbol("IsomorphicTo"), new Sequence<Term>()), isoNode)); // } //} //this.transitions = this.transitions.Add(t); //this.hiddenTransitions = this.hiddenTransitions.Remove(t); } } } } //Console.WriteLine("Checker results"); //Console.WriteLine("Reached states: "+stateCount); //Console.WriteLine("Number of transitions: "+transCnt); ReachabilityResult result = new ReachabilityResult(stateCount, transCnt); return result; end: ReachabilityResult resReached = new ReachabilityResult(stateCount, transCnt); resReached.Goal = goal; resReached.GoalReached = true; return resReached; }
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); }