Пример #1
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);
     }
 }
Пример #2
0
        public Action DoAction(Action action)
        {
            switch (action.Name)
            {
            case ("CheckView"):
                Set <string> modelView = (Set <string>)action[0];
                Set <string> implView  = new Set <string>(bag.table.Keys);
                if (!modelView.Equals(implView))
                {
                    throw new Exception("Inconsistent views of state: model:" +
                                        modelView + " iut:" + implView);
                }
                return(null);

            case ("Add"):
                bag.Add((string)action[0]);
                return(null);

            case ("Delete"):
                bag.Delete((string)action[0]);
                return(null);

            case ("Lookup_Start"):
                return(Action.Create("Lookup_Finish", bag.Lookup((string)action[0])));

            case ("Count_Start"):
                return(Action.Create("Count_Finish", bag.Count));

            default:
                throw new Exception("Unexpected action " + action);
            }
        }
Пример #3
0
        //ParameterizedThreadStart CallTheImplementationThreadStart = new ParameterizedThreadStart(CallTheImplementation);

        Action DoAction(Action action, TimeSpan t)
        {
            ImplementationResultWrapper implRes = new ImplementationResultWrapper();
            //Thread CallTheImplementationThread = new Thread(CallTheImplementationThreadStart);
            //CallTheImplementationThread.Start(new object[] { impl, action, implRes});
            //bool ok = CallTheImplementationThread.Join(t);
            bool ok = worker.StartWork(CallTheImplementation,
                                       new object[] { impl, action, implRes }, t);

            if (!ok)
            {
                //CallTheImplementationThread.Abort();

                throw new ConformanceTesterException("Action timed out");  //conformance failure
            }
            else
            {
                if (implRes.exception != null)
                {
                    //rethrow the exception thrown by the implementation
                    //include only the message in the exception
                    throw new ConformanceTesterException(MakeQuotedString(implRes.exception.Message)); //conformance failure
                }
                else
                {
                    return(implRes.implAction);           //return either null or the following implementation action
                }
            }
        }
Пример #4
0
 public Action DoAction(Action action)
 {
     switch (action.Name)
     {
         case ("CheckView"):
             Set<string> modelView = (Set<string>)action[0];
             Set<string> implView = new Set<string>(bag.table.Keys);
             if (!modelView.Equals(implView))
                 throw new Exception("Inconsistent views of state: model:" +
                                     modelView + " iut:" + implView);
             return null;
         case ("Add"):
             bag.Add((string)action[0]);
             return null;
         case ("Delete"):
             bag.Delete((string)action[0]);
             return null;
         case ("Lookup_Start"):
             return Action.Create("Lookup_Finish", bag.Lookup((string)action[0]));
         case ("Count_Start"):
             return Action.Create("Count_Finish", bag.Count);
         default:
             throw new Exception("Unexpected action " + action);
     }
 }
Пример #5
0
 /// <summary>
 /// Update the current state to the target state of the action from the current state.
 /// If the action is the current action in the current test case, consume that action.
 /// </summary>
 /// <param name="action">given action</param>
 public override void DoAction(CompoundTerm action)
 {
     base.DoAction(action);
     if (!this.currentTest.IsEmpty && this.currentTest.Head.Equals(action))
     {
         this.currentTest = this.currentTest.Tail;
         this.currentTestInProgress = true;
     }
 }
Пример #6
0
 /// <summary>
 /// Get coverage points from a given state and given action
 /// </summary>
 /// <param name="state">from given state</param>
 /// <param name="action">given action</param>
 /// <returns>coverage points</returns>
 public Bag<Term> GetCoveragePoints(IState state, Action action)
 {
     TransitionProperties tprops;
     mp.GetTargetState(state, action, transitionPropertyNames, out tprops);
     Bag<Term> res = Bag<Term>.EmptyBag;
     foreach (Bag<Term> props in tprops.Properties.Values)
         res = res.Union(props);
     return res;
 }
Пример #7
0
 internal MapViewer(string keyName, CompoundTerm t, bool isBag)
 {
     this.isBag = isBag;
     this.t = t;
     this.keyName = keyName;
     this.argumentViewers = new TermViewer[t.Arguments.Count/2];
     for (int i = 0; i < argumentViewers.Length; i++)
     {
         argumentViewers[i] = new MapletViewer(t.Arguments[2 * i], t.Arguments[(2 * i) + 1], isBag);
     }
 }
Пример #8
0
 /// <summary>
 /// Add a relational (i.e. unordered) transition into the graph.
 /// </summary>
 /// <param name="from"></param>
 /// <param name="to"></param>
 /// <param name="label"></param>
 public void AddRelationalTransition(Vertex from, Vertex to, String label)
 {
     VertexData vd = vertexRecords[from];
     CompoundTerm edgeLabel = new CompoundTerm(new Symbol(label), new Sequence<Term>());
     if (vd.unorderedOutgoingEdges.ContainsKey(edgeLabel))
     {
         vd.unorderedOutgoingEdges = vd.unorderedOutgoingEdges.Override(edgeLabel, vd.unorderedOutgoingEdges[edgeLabel].Add(to));
     }
     else
     {
         vd.unorderedOutgoingEdges = vd.unorderedOutgoingEdges.Add(edgeLabel, new Set<Vertex>(to));
     }
 }
Пример #9
0
        /// <summary>
        /// Get coverage points from a given state and given action
        /// </summary>
        /// <param name="state">from given state</param>
        /// <param name="action">given action</param>
        /// <returns>coverage points</returns>
        public Bag <Term> GetCoveragePoints(IState state, Action action)
        {
            TransitionProperties tprops;

            mp.GetTargetState(state, action, transitionPropertyNames, out tprops);
            Bag <Term> res = Bag <Term> .EmptyBag;

            foreach (Bag <Term> props in tprops.Properties.Values)
            {
                res = res.Union(props);
            }
            return(res);
        }
Пример #10
0
        static void CallTheImplementation(object implAndActionAndRes)
        {
            object[] args = (object[])implAndActionAndRes;
            try
            {
                Action implAction = ((IStepper)args[0]).DoAction((Action)args[1]);
                ((ImplementationResultWrapper)args[2]).implAction = implAction;
            }

            catch (Exception e)
            {
                ((ImplementationResultWrapper)args[2]).exception = e;
            }
        }
Пример #11
0
        double GetReward(IState s, Action a)
        {
            Bag <CoveragePoint> cps = this.coveragePointProvider(s, a);
            double totreward        = 0;

            foreach (CoveragePoint cp in cps)
            {
                int    newCoverage = cps.CountItem(cp);            //note this is >= 1
                int    oldCoverage = coveragePoints.CountItem(cp); //note this is >= 0
                double reward      = (newCoverage / (newCoverage + (oldCoverage * oldCoverage)));
                totreward = totreward + reward;
            }
            return(totreward);
        }
Пример #12
0
 /// <summary>
 /// Parses a symbol from the given string
 /// </summary>
 public static Symbol Parse(string representation)
 {
     if (representation == null)
     {
         throw new ArgumentNullException("representation");
     }
     try
     {
         Term         term = Term.Parse(representation + "()");
         CompoundTerm ct   = (CompoundTerm)term;
         return(ct.Symbol);
     }
     catch (ArgumentException)
     {
         throw;
     }
 }
Пример #13
0
        public override CompoundTerm DoStep(InterpretationContext c, CompoundTerm action)
        {
            // Result of invocation must be a value term (must support IComparable)

            IComparable/*?*/ thisArg;
            IComparable/*?*/[] methodArgs = this.ConvertTermArgumentsToMethodArguments(c, action.Arguments, out thisArg);

            foreach (IComparable/*?*/ o in methodArgs)
                AbstractValue.FinalizeImport(o);

            object/*?*/ resultObj = this.method.methodInfo.Invoke(thisArg, methodArgs);
            CompoundTerm/*?*/ finishAction = null;

            // Handle output args and return value
            if (null != this.finishActionMethod)
            {
                int nOutputs = this.finishActionMethod.actionLabel.Arguments.Count;
                Sequence<Term> outputs = Sequence<Term>.EmptySequence;

                for (int i = 0; i < nOutputs; i += 1)
                {
                    int outputArgIndex = this.finishActionMethod.outputArgumentIndices[i];
                    if (-2 == outputArgIndex) // "any" placeholder
                        outputs = outputs.AddLast(Any.Value);
                    else
                    {
                        object output = (-1 == outputArgIndex ? resultObj : methodArgs[outputArgIndex]);

                        IComparable outputAsComparable;
                        if (null == output)
                            outputAsComparable = null;
                        else
                        {
                            outputAsComparable = output as IComparable;
                            if (null == outputAsComparable)
                                throw new InvalidOperationException(MessageStrings.LocalizedFormat(MessageStrings.ComparableResultRequired, action.ToString(), output.ToString()));
                        }
                        outputs = outputs.AddLast(AbstractValue.GetTerm(outputAsComparable));
                    }
                }
                finishAction = new CompoundTerm(this.FinishAction, outputs);
            }

            return finishAction;
        }
Пример #14
0
 /// <summary>
 /// Perform the action
 /// </summary>
 /// <param name="action">the given action</param>
 /// <returns>the returned action (or null)</returns>
 public CompoundTerm DoAction(CompoundTerm action)
 {
     switch (action.FunctionSymbol.ToString())
     {
         case "Insert":
             {
                 Coin coin = GetCoin(action.Arguments[0]);
                 EmptyCoffeeMachineImpl.InsertACoin(coin);
                 return null;
             }
         case "Cancel":
             {
                 Term coin = CompoundValue.GetTerm(EmptyCoffeeMachineImpl.Cancel());
                 return new CompoundTerm(Symbol.Parse("Return"), coin);
             }
         default:
             throw new InvalidOperationException("Unrecognized action: " + action.ToString());
     }
 }
Пример #15
0
        /// <summary>
        /// Update the current state to the target state of the action from the current state.
        /// Records coverage points of this transition.
        /// </summary>
        /// <param name="action">given action</param>
        public override void DoAction(Action action)
        {
            System.Console.WriteLine("Action " + action.ToString());

            int srcHash = this.currState.GetHashCode();

            base.DoAction(action);
            int targetHash = this.currState.GetHashCode();

            if (this.ObservableActionSymbols.Contains(action.FunctionSymbol1))
            {
                AddPassiveEdge(srcHash, targetHash);
            }
            else
            {
                AddActiveEdge(srcHash, targetHash);
            }
            doValueIteration(bdt);
        }
Пример #16
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));
        }
Пример #17
0
        public CompoundTerm DoAction(CompoundTerm action)
        {
            System.Console.WriteLine("robot" + action.Name + action.Arguments[0].ToString());
            int id;
            switch (action.FunctionSymbol.ToString())
            {
                case ("CreateRobot"):
                    id = (int)((CompoundTerm)action[0])[0];
                    impl.AddRobot(id);
                    return null;

                case ("DeleteRobot"):
                    id = (int)((CompoundTerm)action[0])[0];
                    impl.KillRobot(id);
                    return null;

                case ("Search"):
                    id = (int)((CompoundTerm)action[0])[0];
                    impl.Search(id);
                    return impl.Recharge(id);

                case("Wait"):
                    id = (int)((CompoundTerm)action[0])[0];
                    impl.Wait(id);
                    return null;

               /* case("Recharge"):
                    id = (int)((CompoundTerm)action[0])[0];
                    impl.Recharge(id);
                    return null;
            */
                case("CheckState"):
                    id = (int)action[0];
                    if (id != impl.maxId)
                        throw new Exception("Wrong Number of Robots in Model and Implementation");
                    else
                        return null;

                default:
                    throw new Exception("Unexpected action " + action);
            }
        }
Пример #18
0
        public CompoundTerm DoAction(CompoundTerm action)
        {
            switch (action.Name)
            {
            case("Tests"): return null; // first action in test seq.

            case("ServerSocket"):
            s.Socket(); return null;
            case("ServerBind"):
            s.Bind(host,port); return null;
            case("ServerListen"):
            s.Listen(); return null;
            case("ServerAccept"):
            s.Accept(); return null;
             	        case("ServerReceive"):
                    s.Receive(); return null;
             	        case("ServerSend"):
            // s.Send sends a double, not a string!
                    s.Send((double)((Literal) action.Arguments[0]).Value);
            return null;
            case("ServerCloseConnection"):
            s.CloseConnection(); return null;
            case("ServerClose"):
            s.Close(); return null;

            case("ClientSocket"):
            c.Socket(); return null;
                case("ClientConnect"):
            c.Connect(host,port); return null;
            case("ClientSend"):
                c.Send("T"); return null;
            case("ClientReceive_Start"):
            // c.Receive returns a double, not a string
            return CompoundTerm.Create("ClientReceive_Finish",
                           c.Receive());
            case("ClientClose"):
            c.Close(); return null;

                default: throw new Exception("Unexpected action " + action);
            }
        }
Пример #19
0
 public CompoundTerm DoAction(CompoundTerm compoundTerm)
 {
     Console.Out.Write("Press ");
     switch (compoundTerm.Name)
     {
         case "PressMin_Sec":
             Console.Out.WriteLine("Both Min and Sec");
             return null;
         case "PressMin":
             Console.Out.WriteLine("Min");
             return null;
         case "PressSec":
             Console.Out.WriteLine("Sec");
             return null;
         case "PressStartStop":
             Console.Out.WriteLine("Start/Stop");
             return null;
     }
     Console.Out.WriteLine("DoAction(" + compoundTerm.Name + ")");
     return null;
 }
Пример #20
0
 /// <summary>
 /// Perform the action
 /// </summary>
 /// <param name="action">the given action</param>
 /// <returns>the returned action (or null)</returns>
 public CompoundTerm DoAction(CompoundTerm action)
 {
     switch (action.FunctionSymbol.ToString())
     {
         case "SetPlayer1":
             wh.Set(); // Signal the waiting thread to proceed (test-cases continue, don't exit)
             Console.WriteLine("\nSetting Player1 to " + (string)action[0]);
             runner.setPlayer("Player1", (string)action[0]);
             return null;
         case "SetPlayer2":
             Console.WriteLine("\nSetting Player2 to " + (string)action[0]);
             runner.setPlayer("Player2", (string)action[0]);
             return null;
         case "ReadLastResult_Start":
             Console.WriteLine("\nChecking the results list-box");
             Thread.Sleep(200);
             return CompoundTerm.Create("ReadLastResult_Finish", runner.getLastResult());
         default:
             throw new InvalidOperationException("Unrecognized action: " + action.ToString());
     }
 }
Пример #21
0
        /// <summary>
        /// Update the current state to the target state of the action from the current state.
        /// Records coverage points of this transition.
        /// </summary>
        /// <param name="action">given action</param>
        public override void DoAction(Action action)
        {
            UpdateCoveragePoints(action);
            int srcHash = this.currState.GetHashCode();

            base.DoAction(action);

            int targetHash = this.currState.GetHashCode();

            if (!v.ContainsKey(targetHash))
            {
                v[targetHash] = 1.0;
            }
            if (this.ObservableActionSymbols.Contains(action.FunctionSymbol1))
            {
                AddPassiveEdge(srcHash, targetHash);
                DoValueIteration(); // do value iteration when the probabilities change
            }
            else
            {
                bool doiter = false;
                // do value iteration when a new active edge is added to the transition graph
                if (!activeEdges.ContainsKey(srcHash))
                {
                    doiter = true;
                }
                else if (!activeEdges[srcHash].Contains(targetHash))
                {
                    doiter = true;
                }
                AddActiveEdge(srcHash, targetHash);
                if (doiter)
                {
                    DoValueIteration();
                }
            }
            //foreach (int i in cov.Keys)
            //    if(cov[i]<1)
            //        System.Console.WriteLine("state : " + i + " cov " + cov[i]);
        }
Пример #22
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);
        }
Пример #23
0
 public CompoundTerm DoAction(CompoundTerm action)
 {
     switch (action.FunctionSymbol.ToString())	    {
         case("Tests"): return null; // first action in test seq.
         case ("SelectMessages"):
             NewsReaderUI.SelectMessages(); return null;
         case ("SelectTopics"):
             NewsReaderUI.SelectTopics(); return null;
         case ("ShowTitles"):
             NewsReaderUI.ShowTitles(); return null;
         case ("ShowText"):
             NewsReaderUI.ShowText();
             return null;
         case ("SortByFirst"):
             NewsReaderUI.SortByFirst();
             return null;
         case ("SortByMostRecent"):
             NewsReaderUI.SortByMostRecent();
             return null;
         default:
             throw new Exception("Unexpected action " + action);
     }
 }
Пример #24
0
        /// <summary>
        /// Select an action that is enabled in the current state
        /// and whose action symbol is in the set <paramref name="actionSymbols"/>.
        /// Use coverage points and reward policy.
        /// </summary>
        /// <param name="actionSymbols">set of candidate action symbols</param>
        /// <returns>the chosen action or null if no choice is possible</returns>
        public override Action SelectAction(Set <Symbol> actionSymbols)
        {
            if (actionSymbols == null)
            {
                throw new ArgumentNullException("actionSymbols");
            }

            if (actionSymbols.IsEmpty)
            {
                return(null);
            }

            Sequence <Action> actions = new Sequence <Action>(this.GetEnabledActions(actionSymbols));

            if (actions.IsEmpty)
            {
                return(null);
            }

            Action a = ChooseAction(actions, this.CurrentState); //choose a tester action

            return(a);
        }
Пример #25
0
        /// <summary>
        /// Select an action that is enabled in the current state
        /// and whose action symbol is in the set <paramref name="actionSymbols"/>.
        /// Use coverage points and reward policy.
        /// </summary>
        /// <param name="actionSymbols">set of candidate action symbols</param>
        /// <returns>the chosen action or null if no choice is possible</returns>
        public override Action SelectAction(Set <Symbol> actionSymbols)
        {
            if (actionSymbols == null)
            {
                throw new ArgumentNullException("actionSymbols");
            }

            if (actionSymbols.IsEmpty)
            {
                return(null);
            }

            Sequence <Action> actions = new Sequence <Action>(this.GetEnabledActions(actionSymbols));

            if (actions.IsEmpty)
            {
                return(null);
            }

            Action a = ChooseAction(actions, this.CurrentState); //choose a tester action

            //System.Console.WriteLine("Chosen Action " + a.ToString());
            return(a);
        }
Пример #26
0
        /// <summary>
        /// Update the current state to the target state of the action from the current state.
        /// Records coverage points of this transition.
        /// </summary>
        /// <param name="action">given action</param>
        public override void DoAction(Action action)
        {
            UpdateCoveragePoints(action);
            int srcHash = this.currState.GetHashCode();

            base.DoAction(action);

            int targetHash = this.currState.GetHashCode();

            if (!v.ContainsKey(targetHash))
            {
                v[targetHash] = 1.0;
            }
            if (this.ObservableActionSymbols.Contains(action.FunctionSymbol1))
            {
                AddPassiveEdge(srcHash, targetHash);
                DoValueIteration(); // do value iteration when the probabilities change
            }
            else
            {
                bool doiter = false;
                // do value iteration when a new active edge is added to the transition graph
                if (!activeEdges.ContainsKey(srcHash))
                    doiter = true;
                else if (!activeEdges[srcHash].Contains(targetHash))
                {
                    doiter = true;
                }
                AddActiveEdge(srcHash, targetHash);
                if (doiter)
                    DoValueIteration();
            }
            //foreach (int i in cov.Keys)
            //    if(cov[i]<1)
            //        System.Console.WriteLine("state : " + i + " cov " + cov[i]);
        }
Пример #27
0
        private CompoundTerm ChooseAction(Sequence <Action> actions, IState iState)
        {
            Action         maxAct = actions.Head;
            int            sState = iState.GetHashCode();
            int            tState;
            IExtendedState iestate = (IExtendedState)iState;
            int            c       = iestate.LocationValuesCount;

            foreach (Action a in actions)
            {
                foreach (string s in this.modelProgram.GetEnablingConditionDescriptions(iState, a, false))
                {
                    Term                 t     = Term.Parse(s);
                    Sequence <Term>      vars  = (t.Arguments[0].Arguments);
                    Map <Variable, Term> subst = ConstructSubst(a, vars);
                    System.Console.WriteLine(a.ToString() + sState + " enabled string " + t.Arguments[1].Substitute(subst));
                }
            }

            /*
             * for (int i = 0; i < c; i++)
             * {
             *  System.Console.WriteLine("name: "+iestate.GetLocationName(i) + " value : "+
             *      iestate.GetLocationValue(i) + " hash" +
             *      iestate.GetLocationValue(i).GetHashCode());
             *  CompoundValue t  = (CompoundValue)iestate.GetLocationValue(i);
             *  foreach (CompoundValue t1 in t.FieldValues())
             *  {
             *      System.Console.WriteLine(" field " + t1.ToString());
             *  }
             * }
             */

            TransitionProperties tp;
            int sum = 0;
            Sequence <Pair <int, Action> > cumulActSum = Sequence <Pair <int, Action> > .EmptySequence;

            Set <int> coveredActs = findCoveredActs(sState, actions);

            if (!cov.ContainsKey(sState))
            {
                cov[sState] = 0.0;
            }
            Set <Action> newStateActs = new Set <Action>();
            Set <Action> newActs      = new Set <Action>();
            Set <Action> oldActs      = new Set <Action>(actions.Head);

            foreach (Action a in actions)
            {
                tState = this.modelProgram.GetTargetState(iState, a, null, out tp).GetHashCode();
                if (!v.ContainsKey(tState))
                {
                    newStateActs = newStateActs.Add(a);
                }
                else if (!coveredActs.Contains(a.GetHashCode()))
                {
                    newActs = newActs.Add(a);
                }
                else
                {
                    // one greedy approach

                    /*
                     * if (v.ContainsKey(tState) && v[tState] > maxv)
                     * {
                     *  maxv = v[tState];
                     *  oldActs = new Set<Action>(a);
                     * }
                     * else if (v.ContainsKey(tState) && v[tState] == maxv)
                     * {
                     *  oldActs = oldActs.Add(a);
                     * }*/

                    // probabilistic greedy approach
                    if (v.ContainsKey(tState))
                    {
                        sum = sum + (int)(v[tState] * Math.Pow(10.0, 9.0));
                        Pair <int, Action> np = new Pair <int, Action>(sum, a);
                        cumulActSum = cumulActSum.AddLast(np);
                    }
                }
            }
            if (!newStateActs.IsEmpty)
            {
                maxAct = newStateActs.Choose();
                System.Console.WriteLine("new action in new state " + maxAct.ToString());
            }
            else if (!newActs.IsEmpty)
            {
                maxAct = newActs.Choose();
                System.Console.WriteLine("new action in old state " + maxAct.ToString());
            }
            else
            {
                //maxAct = oldActs.Choose();
                Random rndNumbers = new Random();
                int    rndNumber  = rndNumbers.Next(sum);
                System.Console.WriteLine(sum + " " + rndNumber);
                foreach (Pair <int, Action> np in cumulActSum)
                {
                    System.Console.WriteLine(np.First + " " + np.Second.ToString());
                    if (rndNumber <= np.First)
                    {
                        maxAct = np.Second;
                        break;
                    }
                    maxAct = np.Second;
                }
                System.Console.WriteLine("old action in old state " + maxAct.ToString());
            }
            coveredActs = coveredActs.Add(maxAct.GetHashCode());
            cov[sState] = (double)coveredActs.Count / (double)actions.Count;
            return(maxAct);
        }
Пример #28
0
 /// <summary>
 /// Parse the string into an action.
 /// </summary>
 /// <param name="s">given string representing of an action</param>
 /// <returns>action represented by the string</returns>
 new public static Action Parse(string s)
 {
     return((Action)CompoundTerm.Parse(s));
 }
Пример #29
0
 /// <summary>
 /// Compute the weight of an action a in state s as 1000 * GetReward(s,a) as integer
 /// </summary>
 int GetWeight(IState s, Action a)
 {
     return Math.Max((int)GetReward(s, a) * 1000, 1);
 }
Пример #30
0
 double GetReward(IState s, Action a)
 {
     Bag<CoveragePoint> cps = this.coveragePointProvider(s, a);
     double totreward = 0;
     foreach (CoveragePoint cp in cps)
     {
         int newCoverage = cps.CountItem(cp); //note this is >= 1
         int oldCoverage = coveragePoints.CountItem(cp);   //note this is >= 0
         double reward = (newCoverage / (newCoverage + (oldCoverage * oldCoverage)));
         totreward = totreward + reward;
     }
     return totreward;
 }
Пример #31
0
 /// <summary>
 /// Returns a bag containing a single coverage point that is a pair of integers 
 /// that are the hashcodes of the state and the action.
 /// </summary>
 static Bag<Term> DefaultCoveragePointProvider(IState state, CompoundTerm action)
 {
     return new Bag<Term>(new Pair<int, int>(state.GetHashCode(), action.GetHashCode()).AsTerm);
 }
Пример #32
0
 /// <summary>
 /// Update the bag of coverage points seen so far, by adding 
 /// the coverage points provided by the transition labeled by action a from state s.
 /// </summary>
 internal void Update(IState s, Action a)
 {
     Bag<Term> cps = this.coveragePointProvider(s, a);
     this.coveragePoints = this.coveragePoints.Union(cps);
 }
Пример #33
0
 /// <summary>
 /// Update the current state to the target state of the action from the current state.
 /// Records coverage points of this transition.
 /// </summary>
 /// <param name="action">given action</param>
 public override void DoAction(Action action)
 {
     Update(this.CurrentState, action);
     base.DoAction(action);
 }
Пример #34
0
            void CreateVariableWiewers()
            {
                //view also the control mode of the root state if the product has more than one machine in it
                //the id of the composed state is -1
                //if (leafStates.Count > 1)
                //    termViewers.Add(TermViewer.Create("[Control Mode]", rootState.ControlMode), -1);

                for (int stateId = 0; stateId < this.leafStates.Count; stateId++)
                {
                    IState leafState = this.leafStates[stateId];
                    //string modelName = this.modelNames[stateId];

                    //each state has a control mode, create a viewer for it if the controlMode is
                    //not an empty sequence
                    if (!leafState.ControlMode.ToCompactString().Equals("Sequence()"))
                        termViewers.Add(TermViewer.Create("[Control Mode]", leafState.ControlMode), stateId);

                    IExtendedState estate = leafState as IExtendedState;
                    if (estate != null)
                    {
                        #region add the domain map viewer, skip this if there are no domains
                        if (estate.DomainMap.Count > 0)
                        {
                            //StringBuilder sb = new StringBuilder();
                            Sequence<Term> args = Sequence<Term>.EmptySequence;
                            foreach (Pair<Symbol, int> si in estate.DomainMap)
                            {
                                args = args.AddLast(new Literal(si.First.FullName));
                                args = args.AddLast(new Literal(si.Second));
                            }
                            Symbol symb = Symbol.Parse("Map<String, Integer>");
                            Term domMap = new CompoundTerm(symb, args);

                            termViewers.Add(TermViewer.Create("[Domain Map]", domMap), stateId);
                        }
                        #endregion

                        //add a viewer for each state variable
                        for (int j = 0; j < estate.LocationValuesCount; j++)
                        {
                            termViewers.Add(TermViewer.Create(estate.GetLocationName(j),
                                               estate.GetLocationValue(j)), stateId);
                        }
                    }
                }
            }
Пример #35
0
        private CompoundTerm ChooseAction(Sequence <Action> actions, IState iState)
        {
            Set <int>            coveredActs;
            double               minCov = 1.0;
            double               maxv   = 0.0;
            IState               tState;
            Action               maxAct = actions.Head;
            TransitionProperties tp;

            if (!cov.ContainsKey(iState))
            {
                maxAct      = actions.Choose();
                cov[iState] = (1.0 / actions.Count);
                v[iState]   = 1.0;
                // System.Console.WriteLine("inside if " + iState.GetHashCode() + " cov"+ cov[iState]);
                return(maxAct);
            }
            else if (cov.ContainsKey(iState) && cov[iState] < 1.0)
            {
                coveredActs = new Set <int>(this.findCoveredActs(iState));
                Set <int> acts = new Set <int>();
                foreach (Action a in actions)
                {
                    if (!coveredActs.Contains(a.GetHashCode()))
                    {
                        System.Console.WriteLine(a.GetHashCode());
                        tState = modelProgram.GetTargetState(iState, a, null, out tp);
                        if (!cov.ContainsKey(tState))
                        {
                            minCov = 0;
                            maxAct = a;
                        }
                        else if (cov[tState] <= minCov)
                        {
                            minCov = cov[tState];
                            maxAct = a;
                        }
                    }
                    else
                    {
                        acts.Add(a.GetHashCode());
                        tState = modelProgram.GetTargetState(iState, a, null, out tp);
                        if (v.ContainsKey(tState))
                        {
                            maxv = Math.Max(maxv, v[tState]);
                        }
                    }
                }
                acts        = acts.Add(maxAct.GetHashCode());
                cov[iState] = (acts.Count / actions.Count);
                if (v.ContainsKey(iState))
                {
                    v[iState] = Math.Min(v[iState], (alpha * maxv));
                }
                else
                {
                    v[iState] = alpha * maxv;
                }
                //System.Console.WriteLine("inside <1.0 " + iState.GetHashCode() + " cov " + cov[iState]);
                return(maxAct);
            }
            else if (cov.ContainsKey(iState) && cov[iState] == 1.0)
            {
                foreach (Action a in actions)
                {
                    tState = modelProgram.GetTargetState(iState, a, null, out tp);
                    if (!cov.ContainsKey(tState))
                    {
                        maxv   = 1.0;
                        maxAct = a;
                    }
                    else if (cov.ContainsKey(tState) && maxv <= v[tState])
                    {
                        maxv   = v[tState];
                        maxAct = a;
                    }
                }
                if (v.ContainsKey(iState))
                {
                    v[iState] = Math.Min(v[iState], (alpha * maxv));
                }
                else
                {
                    v[iState] = alpha * maxv;
                }
                //System.Console.WriteLine("inside else " + iState.GetHashCode() + " " + v[iState]);
                return(maxAct);
            }
            else
            {
                //System.Console.WriteLine("Should not be reachable!! cov" + cov[iState]);
                return(maxAct);
            }
        }
Пример #36
0
 // invariant: this.arity <= aInfo.arity when this in aInfo.actionMethods
 protected ActionMethod(CompoundTerm actionLabel, Method method, int arity)
 {
     this.actionLabel = actionLabel;
     this.method = method;
     this.arity = arity;
 }
Пример #37
0
 /// <summary>
 /// Hide the transition with the given action from the given node
 /// </summary>
 internal void HideTransition(Node node, CompoundTerm action)
 {
     this.hiddenTransitions = this.hiddenTransitions.Union(this.transitions.Select(delegate(Transition t) { return t.First.Equals(node) && t.Second.Equals(action); }));
     this.transitions = this.transitions.Difference(this.hiddenTransitions);
 }
Пример #38
0
 /// <summary>
 /// Show the transition with the given action from the given state
 /// </summary>
 internal void ShowTransition(Node sourceNode, CompoundTerm action)
 {
     Node targetNode;
     if (TryGetTargetNode(sourceNode, action, out targetNode))
     {
         Transition t = new Triple<Term, CompoundTerm, Term>(sourceNode, action, targetNode);
         this.transitions = this.transitions.Add(t);
         this.hiddenTransitions = this.hiddenTransitions.Remove(t);
     }
 }
Пример #39
0
 public abstract CompoundTerm DoStep(InterpretationContext c, CompoundTerm action);
Пример #40
0
        private CompoundTerm ChooseAction(Sequence <Action> actions, IState iState)
        {
            TransitionProperties tp;
            int               targetId    = -1;
            List <double>     MaxV        = bdt.ReturnValue(true);
            List <double>     MinV        = bdt.ReturnValue(false);
            List <Set <int> > abstractMap = bdt.ReturnLeaves();
            Sequence <Pair <int, Action> > cumulActSum = Sequence <Pair <int, Action> > .EmptySequence;
            double epsilon = 0.1;
            Dictionary <int, int> sumTarget = new Dictionary <int, int>();
            Action maxAct      = null;
            int    targetAbsId = -1;
            int    sum         = 0;

            UpdateRequirementMaps(actions, iState);
            Set <Action> newStateActs = new Set <Action>();
            Set <Action> oldActs      = new Set <Action>(actions.Head);

            foreach (Action a in actions)
            {
                int tState = this.modelProgram.GetTargetState(iState, a, null, out tp).GetHashCode();
                targetAbsId = findAbstractId(tState, abstractMap);
                if (targetAbsId == -1)
                {
                    newStateActs = newStateActs.Add(a);
                }
                else
                {
                    sum = sum + (int)(MaxV[targetAbsId] * Math.Pow(10.0, 9.0));
                    Pair <int, Action> np = new Pair <int, Action>(sum, a);
                    sumTarget.Add(sum, targetAbsId);
                    cumulActSum = cumulActSum.AddLast(np);
                }
            }
            if (!newStateActs.IsEmpty)
            {
                maxAct = newStateActs.Choose();
                System.Console.WriteLine("new action in new state " + maxAct.ToString());
                return(maxAct);
            }
            else
            {
                Random rndNumbers = new Random();
                int    rndNumber  = rndNumbers.Next(sum);
                System.Console.WriteLine(sum + " " + rndNumber);
                foreach (Pair <int, Action> np in cumulActSum)
                {
                    System.Console.WriteLine(np.First + " " + np.Second.ToString());
                    if (rndNumber <= np.First)
                    {
                        maxAct   = np.Second;
                        targetId = sumTarget[np.First];
                        break;
                    }
                    targetId = sumTarget[np.First];
                    maxAct   = np.Second;
                }
                System.Console.WriteLine("old action in old state " + maxAct.ToString());
            }
            // Adaptive Refinement
            if (MaxV[targetId] - MinV[targetId] > epsilon)
            {
                if (i < requirementProperties.Count)
                {
                    string s1 = requirementProperties[i++];
                    bdt = bdt.Refine(s1, requireEnabledStateMap[s1]);
                    bdt.PrintTree(0);
                }
            }
            return(maxAct);
        }
Пример #41
0
 /// <summary>
 /// Compute the weight of an action a in state s as 1000 * GetReward(s,a) as integer
 /// </summary>
 int GetWeight(IState s, Action a)
 {
     return(Math.Max((int)GetReward(s, a) * 1000, 1));
 }
Пример #42
0
 /// <summary>
 /// Update the current state to the target state of the action from the current state.
 /// Records coverage points of this transition.
 /// </summary>
 /// <param name="action">given action</param>
 public override void DoAction(Action action)
 {
     Update(this.CurrentState, action);
     base.DoAction(action);
 }
Пример #43
0
 public new CompoundTerm DoAction(CompoundTerm action)
 {
     if (svthread == null) { startServer(); }
     return base.DoAction(action);
 }
Пример #44
0
        /// <summary>
        /// Update the bag of coverage points seen so far, by adding
        /// the coverage points provided by the transition labeled by action a from state s.
        /// </summary>
        internal void Update(IState s, Action a)
        {
            Bag <Term> cps = this.coveragePointProvider(s, a);

            this.coveragePoints = this.coveragePoints.Union(cps);
        }
Пример #45
0
 /// <summary>
 /// Returns true if the action is a label of a visible transition from the given node
 /// </summary>
 internal bool IsActionVisible(Node node, CompoundTerm action)
 {
     return this.transitions.Exists(delegate(Transition t) { return t.First.Equals(node) && t.Second.Equals(action); });
 }
Пример #46
0
        readonly int[] inputArgumentIndices; // method arg position is index[ith action arg];

        #endregion Fields

        #region Constructors

        // -1 means implicit this param
        // -2 means Any.Value ("_")
        public ActionMethodStart(CompoundTerm actionLabel, Method method, int arity, ActionMethodFinish/*?*/ finishActionMethod, int[] inputArgumentIndices)
            : base(actionLabel, method, arity)
        {
            this.finishActionMethod = finishActionMethod;
            this.inputArgumentIndices = inputArgumentIndices;
        }
Пример #47
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);
 }
Пример #48
0
        /// <summary>
        /// Run a single test case.
        /// Returns the result of the test case, containing the action trace
        /// </summary>
        /// <param name="testNr">test case number</param>
        /// <returns>test result</returns>
        internal TestResult RunTestCase(int testNr)
        {
            Sequence <Action> testCase = Sequence <Action> .EmptySequence;
            Action /*?*/      o        = null;

            // Requirements metrics
            Bag <Pair <string, string> > executedRequirements = Bag <Pair <string, string> > .EmptyBag;

            while ((this.stepsCnt <= 0) || testCase.Count < stepsCnt || (!model.IsInAcceptingState && (maxStepsCnt <= 0 || testCase.Count < maxStepsCnt)))
            {
                if (o != null || (IsAsync && !observations.IsEmpty))
                {
                    #region there is an implementation action o to be checked
                    if (o == null)
                    {
                        o = observations.Dequeue();
                    }

                    testCase = testCase.AddLast(o);  //record the action in the testCase

                    string failureReason = "";
                    if (model.IsActionEnabled(o, out failureReason)) //check conformance to the model
                    {
                        model.DoAction(o);
                        o = null;                    //consume the action
                    }
                    else
                    {
                        return(new TestResult(testNr, Verdict.Failure, failureReason, testCase, executedRequirements)); // Requirements metrics: ", executedRequirements"
                    }
                    #endregion
                }
                else
                {
                    //use only cleanup actions when in cleanup phase
                    Set <Symbol> actionSymbols = (((stepsCnt <= 0) || testCase.Count < stepsCnt) ? this.testerActionSymbols : this.cleanupActionSymbols);

                    //select a tester action that is enabled in the current model state
                    Action testerAction = model.SelectAction(actionSymbols);

                    //if a tester action could be chosen
                    if (testerAction != null)
                    {
                        #region execute the tester action

                        //get the timespan within which calling impl with testerAction must return
                        TimeSpan t = (!internalActionSymbols.Contains(testerAction.Symbol) ?
                                      this.testerActionTimeout(model.CurrentState, testerAction) : new TimeSpan());

                        //do the action in the model
                        model.DoAction(testerAction);

                        // Requirements metrics

                        string actionName = testerAction.Name;
                        foreach (string methodName in LibraryModelProgram.AllModeledRequirements.Keys)
                        {
                            // The methods names don't contain "_Start"
                            // when testerAction.Name == actionName_Start, remove the "_Start"
                            // in order to check it in AllModeledRequirements.Keys
                            if (actionName.Contains("_Start"))
                            {
                                actionName = actionName.Replace("_Start", "");
                            }
                            // I use 'Contains' to get all the enabled actions as well
                            if (methodName.Contains(actionName))
                            {
                                foreach (Pair <string, string> req in LibraryModelProgram.AllModeledRequirements[methodName])
                                {
                                    executedRequirements = executedRequirements.Add(req);
                                }
                            }
                        }

                        //record the action in the testCase
                        testCase = testCase.AddLast(testerAction);

                        //call the implementation if the symbol is shared
                        if (!internalActionSymbols.Contains(testerAction.Symbol))
                        {
                            try
                            {
                                DateTime startAction = DateTime.Now; // Performance metrics

                                o = DoAction(testerAction, t);       //if return value is non-null it will be checked next time around

                                // Requirements metrics
                                CalcPerformance(testerAction, startAction);
                            }
                            catch (ConformanceTesterException e)
                            {
                                return(new TestResult(testNr, Verdict.Failure, e.Message, testCase, executedRequirements));  //conformance failure // Requirements : ", executedRequirements"
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        //otherwise, try to get an implementation action
                        if (IsAsync)
                        {
                            //get the Wait action from the model
                            Action w          = model.SelectAction(waitActionSet);
                            int    obsTimeout = (w == null ? 0 : (int)w[0]);
                            if (w != null)
                            {
                                testCase = testCase.AddLast(w);
                                model.DoAction(w);
                            }
                            if (!observations.TryDequeue(new TimeSpan(0, 0, 0, 0, obsTimeout), out o))
                            {
                                //if there are no tester actions and no observables but the
                                //model is in accepting state, the test succeeds
                                if (model.IsInAcceptingState)
                                {
                                    return(new TestResult(testNr, Verdict.Success, "", testCase, executedRequirements));// Requirements metrics: ", executedRequirements"
                                }
                                else
                                {
                                    o = timeoutAction;
                                }
                            }
                        }
                        else
                        {
                            //if there are no tester actions and no observables but the
                            //model is in accepting state, the test succeeds
                            if (model.IsInAcceptingState)
                            {
                                return(new TestResult(testNr, Verdict.Success, "", testCase, executedRequirements));// Requirements metrics: ", executedRequirements"
                            }
                            else
                            {
                                return(new TestResult(testNr, Verdict.Failure, "Run stopped in a non-accepting state", testCase, executedRequirements));// Requirements metrics: ", executedRequirements"
                            }
                        }
                    }
                }
            }
            if (model.IsInAcceptingState)
            {
                return(new TestResult(testNr, Verdict.Success, "", testCase, executedRequirements));// Requirements metrics: ", executedRequirements"
            }
            else
            {
                return(new TestResult(testNr, Verdict.Failure, "Test run did not finish in accepting state", testCase, executedRequirements));// Requirements metrics: ", executedRequirements"
            }
        }