Exemplo n.º 1
0
        public static FSM GenerateTestSequenceAutomaton(string testcaseName, Sequence<Sequence<CompoundTerm>> testseqs, Set<Symbol> actionSymbols)
        {
            Set<Term> acceptingStates = Set<Term>.EmptySet;
            Set<Term> states = Set<Term>.EmptySet;

            Symbol testCaseActionSymbol = new Symbol(testcaseName);
            Literal initialState = new Literal(0);
            states = states.Add(initialState);

            #region generate transitions and accepting states
            Set<Triple<Term, CompoundTerm, Term>> transitions =
                Set<Triple<Term, CompoundTerm, Term>>.EmptySet;
            for (int i = 0; i < testseqs.Count; i++)
            {
                //the i'th test sequence start action
                CompoundTerm startTestAction =
                    new CompoundTerm(testCaseActionSymbol,
                                     new Sequence<Term>(new Literal(i)));

                transitions = transitions.Add(new Triple<Term, CompoundTerm, Term>(
                    initialState,startTestAction, IntermediateState.State(i,0)));

                Sequence<CompoundTerm> testseq = testseqs[i];

                //the final step state of the i'th test sequence is an accepting state
                acceptingStates = acceptingStates.Add(IntermediateState.State(i, testseq.Count));
                states = states.Add(IntermediateState.State(i, testseq.Count));

                for (int j = 0; j < testseq.Count; j++)
                {
                    if (!actionSymbols.Contains(testseq[j].Symbol))
                        throw new ArgumentException("Not all action symbols in test sequences appear in actionSymbols", "actionSymbols");
                    states = states.Add(IntermediateState.State(i, j));
                    transitions = transitions.Add(new Triple<Term, CompoundTerm, Term>(
                        IntermediateState.State(i, j), testseq[j], IntermediateState.State(i, j + 1)));
                }

            }
            #endregion

            return new FSM(initialState, states, transitions,
                acceptingStates, actionSymbols.Add(testCaseActionSymbol));
        }
Exemplo n.º 2
0
        FSM CreateSampleFA()
        {
            Literal[] states = new Literal[5];
            for (int i = 0; i < 5; i++)
                states[i] = new Literal(i);
            CompoundTerm a = new CompoundTerm(new Symbol("a"));
            CompoundTerm b = new CompoundTerm(new Symbol("b"));
            CompoundTerm c = new CompoundTerm(new Symbol("c"));

            Set<Term> faStates = new Set<Term>(states);
            Set<Term> accStates = new Set<Term>(states[2], states[3]);
            Set<Symbol> vocab = new Set<Symbol>(a.Symbol, b.Symbol, c.Symbol);

            Set<Transition> transitions =
                new Set<Transition>(
                    new Transition(states[0], a, states[1]),
                    new Transition(states[1], b, states[2]),
                    new Transition(states[1], c, states[3]),
                    new Transition(states[3], a, states[4]));

            return new FSM(states[0], faStates, transitions, accStates, vocab);
        }
Exemplo n.º 3
0
 // This method has side effects on this.stateMap, this.nodeMap, this.nodes, this.acceptingNodes, this.actionsExploredFromNode
 private bool TryGetTargetNode(Node sourceNode, CompoundTerm action, out Node targetNode)
 {
     IState targetState;
     if (this.actionsExploredFromNode.ContainsKey(sourceNode))
     {
         if (!this.actionsExploredFromNode[sourceNode].TryGetValue(action, out targetNode))
         {
             //this action has not been explored yet from the given node
             TransitionProperties transitionProperties;
             targetState = this.modelProgram.GetTargetState(stateMap[sourceNode], action, Set<string>.EmptySet, out transitionProperties);
             if (!this.nodeMap.TryGetValue(targetState, out targetNode))
             {
                 targetNode = new Literal(this.nodes.Count);
                 this.stateMap[targetNode] = targetState;
                 this.nodeMap[targetState] = targetNode;
                 this.nodes = this.nodes.Add(targetNode);
                 if (this.modelProgram.IsAccepting(targetState))
                     this.acceptingNodes = this.acceptingNodes.Add(targetNode);
                 //if (!this.modelProgram.SatisfiesStateInvariant(targetState))
                 //    this.errorNodes = this.errorNodes.Add(targetNode);
             }
         }
         else
         {
             targetState = this.stateMap[targetNode];
         }
     }
     else //the state has not yet been explored at all
     {
         TransitionProperties transitionProperties;
         targetState = this.modelProgram.GetTargetState(stateMap[sourceNode], action, Set<string>.EmptySet, out transitionProperties);
         if (!this.nodeMap.TryGetValue(targetState, out targetNode))
         {
             targetNode = new Literal(this.nodes.Count);
             this.stateMap[targetNode] = targetState;
             this.nodeMap[targetState] = targetNode;
             this.nodes = this.nodes.Add(targetNode);
             if (this.modelProgram.IsAccepting(targetState))
                 this.acceptingNodes = this.acceptingNodes.Add(targetNode);
             //if (!this.modelProgram.SatisfiesStateInvariant(targetState))
             //    this.errorNodes = this.errorNodes.Add(targetNode);
         }
         Dictionary<CompoundTerm, Node> actionsFromState = new Dictionary<CompoundTerm, Node>();
         actionsFromState[action] = targetNode;
         this.actionsExploredFromNode[sourceNode] = actionsFromState;
     }
     return this.modelProgram.SatisfiesStateFilter(targetState);
 }
Exemplo n.º 4
0
 internal ExploredTransitions(ModelProgram modelProgram, int initTransitions, int maxTransitions)
 {
     this.modelProgram = modelProgram;
     this.transitions = Set<Transition>.EmptySet;
     this.groupingTransitions = Set<Transition>.EmptySet;
     Node initNode = new Literal(0);
     this.initialNode = initNode;
     this.nodes = new Set<Node>(initNode);
     this.acceptingNodes = (modelProgram.IsAccepting(modelProgram.InitialState) ?
         new Set<Node>(initNode) :
         Set<Node>.EmptySet);
     //this.errorNodes = (!modelProgram.SatisfiesStateInvariant(modelProgram.InitialState) ?
     //    new Set<Node>(initNode) :
     //    Set<Node>.EmptySet);
     Dictionary<Node, IState> initialStateMap =
         new Dictionary<Node, IState>();
     initialStateMap[initNode] = modelProgram.InitialState;
     this.stateMap = initialStateMap;
     actionsExploredFromNode = new Dictionary<Node, Dictionary<CompoundTerm,Node>>();
     Dictionary<IState, Node> initialNodeMap =
         new Dictionary<IState, Node>();
     initialNodeMap[modelProgram.InitialState] = initNode;
     this.nodeMap = initialNodeMap;
     this.hiddenTransitions = Set<Transition>.EmptySet;
     this.maxTransitions = maxTransitions;
     this.initTransitions = initTransitions;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Explores the model associated with this instance.
        /// The dictionary stateMap (if not null) is used to record the mapping 
        /// from generated finite automata states to IStates.
        /// Explore(null) is the same as Explore()
        /// </summary>
        /// <returns>A list of transitions. Each transition is a start state,
        /// an action label and an end state.</returns>
        public FSM Explore(Dictionary<Term,IState> generatedStateMap)
        {
            Set<Symbol> actionSymbols = modelProgram.ActionSymbols();
            IState initialState = modelProgram.InitialState;

            Dictionary<IState, int> states = new Dictionary<IState, int>();
            LinkedList<IState> frontier = new LinkedList<IState>();
            frontier.AddFirst(initialState);
            int nextStateId = 0;

            Dictionary<Transition, Transition> transitions = new Dictionary<Transition, Transition>();

            while (frontier.Count > 0)
            {
                IState startState = frontier.First.Value;
                frontier.RemoveFirst();
                if (!states.ContainsKey(startState)) states.Add(startState, nextStateId++);

                foreach (Symbol actionSymbol in actionSymbols)
                {
                    if (modelProgram.IsPotentiallyEnabled(startState, actionSymbol))
                    {
                        IEnumerable<CompoundTerm> actions = modelProgram.GetActions(startState, actionSymbol);
                        foreach (CompoundTerm action in actions)
                        {
                            TransitionProperties transitionProperties;
                            IState targetState = modelProgram.GetTargetState(startState, action, Set<string>.EmptySet, out transitionProperties);
                            if (IncludeTransition(startState, action, targetState, transitionProperties.Properties))
                            {
                                Transition t = new Transition(startState, action, targetState);
                                transitions.Add(t, t);
                                if (!states.ContainsKey(targetState) && !frontier.Contains(targetState))
                                    frontier.AddFirst(targetState);
                            }
                        }
                    }

                }
            }
            Term automatonInitialState = new Literal(states[initialState]);

            Set<Term> automatonStates = Set<Term>.EmptySet;
            Set<Term> acceptingStates = Set<Term>.EmptySet;
            foreach (KeyValuePair<IState, int> kv in states)
            {
                Term automatonState = new Literal(kv.Value);
                automatonStates = automatonStates.Add(automatonState);
                if (modelProgram.IsAccepting(kv.Key))
                    acceptingStates = acceptingStates.Add(automatonState);
                if (generatedStateMap != null)
                    generatedStateMap[automatonState] = kv.Key;
            }

            Set<Triple<Term, CompoundTerm, Term>> automatonTransitions = Set<Triple<Term, CompoundTerm, Term>>.EmptySet;
            foreach (KeyValuePair<Transition, Transition> kv in transitions)
            {
                Transition t = kv.Key;
                automatonTransitions = automatonTransitions.Add(new Triple<Term, CompoundTerm, Term>(new Literal(states[t.startState]),
                                                                                             t.action,
                                                                                             new Literal(states[t.targetState])));
            }
            return new FSM(automatonInitialState, automatonStates, automatonTransitions, acceptingStates);
        }
Exemplo n.º 6
0
        public static FSM ProjectFromProduct(FSM finiteAutomaton, Set<Symbol> projectedSymbols, Sequence<Branch> treePosition, Dictionary<Term, IState>/*?*/ stateMap, out Dictionary<Term, IState>/*?*/ reductStateMap)
        {
            Set<IState> projectedIStates = finiteAutomaton.States.Convert<IState>(delegate(Term t) { return GetReduct(treePosition, stateMap[t]); });
            Set<IState> projectedAcceptingIStates = finiteAutomaton.AcceptingStates.Convert<IState>(delegate(Term t) { return GetReduct(treePosition, stateMap[t]); });

            reductStateMap = new Dictionary<Term, IState>();
            Dictionary<IState, Term>  reverseMap = new Dictionary<IState, Term>();

            Term initialState = new Literal(0);
            IState initialStateReduct = GetReduct(treePosition, stateMap[finiteAutomaton.InitialState]);
            reductStateMap[initialState] = initialStateReduct;
            reverseMap[initialStateReduct] = initialState;

            int i = 1;
            foreach (IState s in projectedIStates.Remove(initialStateReduct))
            {
                Term t = new Literal(i++);
                reductStateMap[t] = s;
                reverseMap[s] = t;
            }
            Set<Term> states = new Set<Term>(reductStateMap.Keys);
            Set<Term> acceptingStates = projectedAcceptingIStates.Convert<Term>(delegate(IState x){return reverseMap[x];});

            Set<Triple<Term, CompoundTerm, Term>> transitions =
                finiteAutomaton.Transitions.Convert<Triple<Term, CompoundTerm, Term>>(
                delegate(Triple<Term, CompoundTerm, Term> t)
                {
                    return new Triple<Term, CompoundTerm, Term>(reverseMap[GetReduct(treePosition, stateMap[t.First])],
                                                          t.Second,
                                                          reverseMap[GetReduct(treePosition, stateMap[t.Third])]);
                });
            Set<Triple<Term, CompoundTerm, Term>> transitions1 =
                transitions.Select(delegate(Triple<Term, CompoundTerm, Term> t)
                    { return projectedSymbols.Contains(t.Second.Symbol); });
            return new FSM(initialState, states, transitions1, acceptingStates, projectedSymbols);
        }
Exemplo n.º 7
0
            Term Term()
            {
                Term result;
                switch (nextToken.kind)
                {
                    case Token.Kind.Integer:
                        result = new Literal(int.Parse(nextToken.value));
                        Next();
                        return result;

                    case Token.Kind.String:
                        result = new Literal(nextToken.value);
                        Next();
                        return result;

                    case Token.Kind.Boolean:
                        result = new Literal(bool.Parse(nextToken.value));
                        Next();
                        return result;

                    case Token.Kind.Null:
                        result = new Literal((string)null);
                        Next();
                        return result;

                    case Token.Kind.Wildcard:
                        Next();
                        return Any.Value;

                    case Token.Kind.Symbol:
                        {
                            Symbol symbol = ParseSymbol();

                            // take care of literals
                            if (AbstractValue.GetLiteralTypes().ContainsValue(symbol.Name)
                                && symbol.Name != "string"
                                && symbol.Name != "int"
                                && symbol.Name != "bool")
                            {
                                Expect(Token.Kind.Open);

                                if (nextToken.kind != Token.Kind.String)
                                    throw new SyntaxErrorException("saw " + nextToken.kind.ToString() + " where " +
                                                   Token.Kind.String.ToString() + " was expected");

                                string literalString = nextToken.value;
                                Next();

                                IComparable value;
                                bool flag = false;
                                switch (symbol.Name)
                                {
                                    case "byte":
                                        {
                                            byte v;
                                            flag = byte.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "char":
                                        {
                                            char v;
                                            flag = char.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "double":
                                        {
                                            double v;
                                            flag = double.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "float":
                                        {
                                            float v;
                                            flag = float.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "long":
                                        {
                                            long v;
                                            flag = long.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "sbyte":
                                        {
                                            sbyte v;
                                            flag = sbyte.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "short":
                                        {
                                            short v;
                                            flag = short.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "uint":
                                        {
                                            uint v;
                                            flag = uint.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "ulong":
                                        {
                                            ulong v;
                                            flag = ulong.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    case "ushort":
                                        {
                                            ushort v;
                                            flag = ushort.TryParse(literalString, out v);
                                            value = v;
                                            break;
                                        }
                                    default:
                                        throw new SyntaxErrorException("saw unexpected token " + symbol.Name + " where " +
                                                   "name of literal type was expected (internal error)");

                                }
                                if (!flag)
                                    throw new SyntaxErrorException("could not parse " + literalString + " as .NET type " + symbol.Name);

                                Expect(Token.Kind.Close);
                                return new Literal(value);
                            }
                            else if (nextToken.kind != Token.Kind.Open)
                            {
                                if (null != symbol.DomainParameters &&
                                       symbol.DomainParameters != Sequence<Symbol>.EmptySequence)
                                    throw new SyntaxErrorException("domain parameters not allowed in variable " + symbol.FullName);

                                string variableName = symbol.Name;
                                return new Variable(variableName);
                            }
                            else
                            {
                                Expect(Token.Kind.Open);
                                bool expectComma = false;

                                LinkedList<Term> arguments = new LinkedList<Term>();
                                while (nextToken.kind != Token.Kind.Close)
                                {
                                    if (expectComma) Expect(Token.Kind.Comma);
                                    Term arg = Term();
                                    arguments.AddLast(arg);
                                    expectComma = true;
                                }

                                Expect(Token.Kind.Close);
                                Term[] argsArray = new Term[arguments.Count];
                                arguments.CopyTo(argsArray, 0);
                                return new CompoundTerm(symbol, argsArray);
                            }
                        }
                    default:
                        throw new SyntaxErrorException("saw unexpected " + nextToken.kind.ToString());
                }
            }