Exemplo n.º 1
0
        //protected QueryConstraintType ConstraintType;
        //protected DRA DRA;
        //public DRA PositiveDRA;
        //public DRA NegativeDRA;
        //public BuchiAutomata PositiveBA;
        //protected double Min = -1;
        //protected double Max = -1;
        //protected MDP mdp;
        //protected Dictionary<string, int> MDPState2DRAStateMapping;
        //private bool HasDeadLock;
        ////private DefinitionRef Process;
        //public const string DUMMY_INIT = "dummy";

        //private bool AlmostFair = false;



        protected void BuildMD_ImprovedTarjan()
        {
            //int counter = 0;
            MDPState dummyInit = new MDPState(DUMMY_INIT);

            mdp = new MDP(dummyInit, Ultility.Ultility.DEFAULT_PRECISION, Ultility.Ultility.MAX_DIFFERENCE);


            List <PCSPEventDRAPairMEC> initials = GetInitialPairsEMC(InitialStep as MDPConfiguration);
            Stack <KeyValuePair <PCSPEventDRAPairMEC, MDPState> > working = new Stack <KeyValuePair <PCSPEventDRAPairMEC, MDPState> >(1024);
            //Stack<PCSPEventDRAPairMEC> stepStack = new Stack<PCSPEventDRAPairMEC>(1024);
            List <PCSPEventDRAPairMEC> stepStack = new List <PCSPEventDRAPairMEC>(1024);

            MDPState2DRAStateMapping = new Dictionary <string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE);
            //HashSet<string> MDPStateMapping = new HashSet<string>();
            //build the MDP while identifying the SCCs
            List <List <string> > SCCs = new List <List <string> >(64);

            for (int z = 1; z <= initials.Count; z++)
            {
                PCSPEventDRAPairMEC initState = initials[z - 1];
                string   stringID             = initState.ID;
                MDPState newinit = new MDPState(stringID);
                mdp.AddState(newinit);
                dummyInit.AddDistribution(new Distribution(stringID, newinit));
                stepStack.Add(initState);
                //newinit.AddDistribution(new Distribution(Constants.TAU, newinit));
                working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(initState, newinit));
            }

            Dictionary <string, int> preorder    = new Dictionary <string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE);
            Dictionary <string, int> lowlink     = new Dictionary <string, int>(Common.Classes.Ultility.Ultility.MC_INITIAL_SIZE);
            Dictionary <string, int> distrRecord = new Dictionary <string, int>();

            int preorderCounter = 0;
            Dictionary <string, List <PCSPEventDRAPairMEC> > ExpendedNode = new Dictionary <string, List <PCSPEventDRAPairMEC> >();

            do
            {
                if (CancelRequested)
                {
                    this.VerificationOutput.NoOfStates = this.VerificationOutput.NoOfStates + mdp.States.Count;
                    return;
                }

                KeyValuePair <PCSPEventDRAPairMEC, MDPState> pair = working.Peek();
                MDPConfiguration evt         = pair.Key.configuration;
                int    DRAState              = pair.Key.state;
                string currentID             = pair.Key.ID;
                List <Distribution> outgoing = pair.Value.Distributions;

                if (!preorder.ContainsKey(currentID))
                {
                    preorder.Add(currentID, preorderCounter);
                    distrRecord.Add(currentID, 0);
                    //stepStack.Add(new PCSPEventDRAPairMEC(currentID + "separate" + 0));
                    preorderCounter++;
                }

                bool done = true;

                if (ExpendedNode.ContainsKey(currentID))
                {
                    List <PCSPEventDRAPairMEC> list = ExpendedNode[currentID];
                    //int counter = 0;
                    if (list.Count > 0)
                    {
                        int k = list.Count - 1;
                        //for (int k = list.Count - 1; k >= 0; k--)

                        PCSPEventDRAPairMEC step = list[k];
                        if (step == null)
                        {
                            stepStack.Add(new PCSPEventDRAPairMEC(currentID + "separate" + (distrRecord[currentID]++)));
                            list.RemoveAt(k);
                            continue;
                        }
                        else
                        {
                            string stepID = step.ID;
                            if (!preorder.ContainsKey(stepID))
                            {
                                //if (done)
                                //{
                                working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(step, mdp.States[stepID]));
                                stepStack.Add(step);
                                done = false;
                                list.RemoveAt(k);
                                //}
                            }
                            else
                            {
                                //stepStack.Add(step.ID);
                                stepStack.Add(step);
                                list.RemoveAt(k);
                                done = false;
                            }
                        }
                    }
                    //else
                    //{
                    //    stepStack.Add(new PCSPEventDRAPairMEC(currentID + "end"));
                    //}
                }
                else
                {
                    int          currentDistriIndex = -1;
                    Distribution newDis             = new Distribution(Constants.TAU);

                    IEnumerable <MDPConfiguration> steps = evt.MakeOneMoveLocal();
                    //int counter = 0;
                    //NOTE: here we play a trick for deadlock case: if a deadlock exist in the MDP, we will make a
                    //self loop transition to remove the deadlock. Deadlock is meaningless in MDP.
                    if (evt.IsDeadLock)
                    {
                        List <MDPConfiguration> stepsList = new List <MDPConfiguration>(steps);

                        stepsList.Add(CreateSelfLoopStep(evt));
                        steps       = stepsList;
                        HasDeadLock = true;
                    }

                    List <PCSPEventDRAPairMEC> product = NextMEC(steps.ToArray(), DRAState);
                    this.VerificationOutput.Transitions += product.Count;

                    for (int k = product.Count - 1; k >= 0; k--)
                    {
                        PCSPEventDRAPairMEC step = product[k];
                        string tmp = step.ID;
                        //int nextIndex = VisitedWithID.Count;
                        MDPState nextState;

                        if (mdp.States.TryGetValue(tmp, out nextState))
                        {
                            if (!preorder.ContainsKey(tmp))
                            {
                                if (done)
                                {
                                    working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(step, nextState));
                                    //stepStack.Add(step.ID);
                                    stepStack.Add(step);
                                    product.RemoveAt(k);
                                    done = false;
                                }
                                else
                                {
                                    product[k] = step;
                                }
                            }
                            else
                            {
                                if (done)
                                {
                                    product.RemoveAt(k);
                                    done = false;
                                }
                            }
                        }
                        else
                        {
                            nextState = new MDPState(tmp);
                            mdp.States.Add(tmp, nextState);

                            if (done)
                            {
                                working.Push(new KeyValuePair <PCSPEventDRAPairMEC, MDPState>(step, nextState));
                                //stepStack.Add(step.ID);
                                stepStack.Add(step);
                                done = false;
                                product.RemoveAt(k);
                            }
                            else
                            {
                                product[k] = step;
                            }
                        }

                        MDPConfiguration pstep = step.configuration;

                        if (pstep.DisIndex == -1)
                        {
                            if (currentDistriIndex >= 0)
                            {
                                pair.Value.AddDistribution(newDis);
                                product.Insert(k + 1, null);//separator
                                newDis = new Distribution(Constants.TAU);
                            }

                            Distribution newTrivialDis = new Distribution(pstep.Event, nextState);
                            pair.Value.AddDistribution(newTrivialDis);
                            if (k != 0)
                            {
                                product.Insert(k, null);//separator
                            }
                        }
                        else if (currentDistriIndex != -1 && pstep.DisIndex != currentDistriIndex)
                        {
                            pair.Value.AddDistribution(newDis);
                            product.Insert(k + 1, null);//separator
                            newDis = new Distribution(Constants.TAU);
                            newDis.AddProbStatePair(pstep.Probability, nextState);
                        }
                        else
                        {
                            newDis.AddProbStatePair(pstep.Probability, nextState);
                        }

                        currentDistriIndex = pstep.DisIndex;
                    }

                    if (currentDistriIndex >= 0)
                    {
                        pair.Value.AddDistribution(newDis);
                    }

                    ExpendedNode.Add(currentID, product);
                }

                if (done)
                {
                    int lowlinkV  = preorder[currentID];
                    int preorderV = preorder[currentID];

                    bool selfLoop = false;

                    //List<bool> temp = new List<bool>(outgoing.Count);
                    int length = outgoing.Count;

                    for (int i = 0; i < length; i++)//Distribution list in outgoing)
                    {
                        //temp[i] = true;
                        Distribution list         = outgoing[i];//note the order of distribution is reversed
                        int          templowlinkV = int.MaxValue;
                        //bool tempUpdate = false;
                        foreach (KeyValuePair <double, MDPState> state in list.States)
                        {
                            string w = state.Value.ID;

                            if (w == currentID && list.States.Count == 1)
                            {
                                selfLoop = true;
                            }

                            if (!MDPState2DRAStateMapping.ContainsKey(w))
                            {
                                //tempUpdate = true;
                                if (preorder[w] > preorderV)
                                {
                                    templowlinkV = Math.Min(templowlinkV, lowlink[w]);
                                    //lowlinkV = Math.Min(lowlinkV, lowlink[w]);
                                }
                                else
                                {
                                    templowlinkV = Math.Min(templowlinkV, preorder[w]);
                                    //lowlinkV = Math.Min(lowlinkV, preorder[w]);
                                }
                            }
                            else
                            {
                                templowlinkV = int.MaxValue;
                                break;
                            }
                        }

                        //if (tempUpdate)
                        //{

                        //}

                        if (templowlinkV == int.MaxValue)
                        {
                            //int index = stepStack.IndexOf(currentID + "separate" + i);
                            //for (int index = stepStack.IndexOf(currentID + "separate" + (i)); index < stepStack.Count; )
                            //{
                            //    if (stepStack[index] != currentID + "separate" + (i+1).ToString() && stepStack[index] != currentID + "end")
                            //    {
                            //        stepStack.RemoveAt(index);
                            //    }
                            //    else
                            //    {
                            //        break;
                            //    }
                            //}
                            bool remove = false;

                            List <int> toRemove = new List <int>();

                            for (int index = stepStack.Count - 1; index >= 0; index--)
                            {
                                //counter++;
                                if (stepStack[index].ID == currentID + "separate" + i)
                                {
                                    remove = true;
                                }
                                if (remove)
                                {
                                    if (stepStack[index].ID != currentID + "separate" + (i - 1) && stepStack[index].ID != currentID)
                                    {
                                        stepStack.RemoveAt(index);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    if (stepStack[index].ID == currentID)
                                    {
                                        foreach (var ind in toRemove)
                                        {
                                            stepStack.RemoveAt(ind);
                                        }
                                        break;
                                    }
                                    toRemove.Add(index);
                                }
                            }
                        }
                        else
                        {
                            lowlinkV = Math.Min(templowlinkV, lowlinkV);
                        }
                    }

                    lowlink[currentID] = lowlinkV;
                    working.Pop();

                    if (lowlinkV == preorderV)
                    {
                        List <string> SCC = new List <string>(1024);
                        SCC.Add(currentID);
                        MDPState2DRAStateMapping.Add(currentID, DRAState);
                        //while (stepStack.Count > 0 && preorder[stepStack[stepStack.Count - 1]] > preorderV)
                        while (stepStack[stepStack.Count - 1].ID != currentID)
                        {
                            PCSPEventDRAPairMEC s = stepStack[stepStack.Count - 1];
                            string sID            = s.ID;
                            stepStack.RemoveAt(stepStack.Count - 1);
                            if (!sID.Contains("separate") && !SCC.Contains(sID))
                            {
                                SCC.Add(sID);
                                MDPState2DRAStateMapping.Add(sID, s.state);
                                //MDPState2DRAStateMapping.Add(sID, );
                            }
                        }

                        stepStack.RemoveAt(stepStack.Count - 1);

                        if (SCC.Count > 1 || selfLoop) //evt.IsDeadLock ||
                        {
                            SCCs.Add(SCC);
                        }
                    }
                    //else
                    //{
                    //    stepStack.Push(pair.Key);
                    //}
                }
            } while (working.Count > 0);


            Debug.WriteLine(mdp.States.Count);
            List <string> EndComponents = new List <string>(SCCs.Count);
            int           count         = DRA.acceptance().size();
            int           helper        = 0;

            foreach (List <string> scc in SCCs)
            {
                //for debug
                //List<MDPState> debug = new List<MDPState>();
                //List<int> drastates = new List<int>();
                //foreach(string state in scc)
                //{
                //    debug.Add(mdp.States[state]);
                //    drastates.Add(MDPState2DRAStateMapping[state]);
                //}
                //for debug
                //if(sentence.Count == 0)
                //{

                //}
                for (int index = 0; index < count; index++)
                {
                    List <string> newSCC  = new List <string>();
                    List <string> targets = new List <string>();

                    if (AlmostFair)
                    {
                        //bool bottom = true;
                        ////int SCCcount = scc.Count;
                        ////note that as long as one SCC(might not be a real MEC) has a U state, the whole SCC cannot be targets.
                        ////RemoveNonECStates(scc, targets);
                        //foreach (string i in scc)
                        //{
                        //    int draState = MDPState2DRAStateMapping[i];
                        //    if (bottom)
                        //    {
                        //        if (DRA.acceptance().isStateInAcceptance_U(index, draState))
                        //        {
                        //            bottom = false;
                        //        }
                        //    }

                        //    newSCC.Add(i);

                        //    if (DRA.acceptance().isStateInAcceptance_L(index, draState))
                        //    {
                        //        targets.Add(i);
                        //    }

                        //}
                        //if (bottom)
                        //{
                        //    if (!BottomECStates(newSCC, targets))
                        //    {
                        //        if (newSCC.Count > 0)
                        //        {
                        //            GroupMEC(newSCC);
                        //        }
                        //    }
                        //    else
                        //    {
                        //        Common.Classes.Ultility.Ultility.AddList(EndComponents, scc);
                        //    }
                        //}
                        //else
                        //{
                        //    RemoveNonECStates(newSCC);
                        //    if (newSCC.Count > 0)
                        //    {
                        //        GroupMEC(newSCC);
                        //    }
                        //}
                    }
                    else
                    {
                        foreach (string i in scc)
                        {
                            int draState = MDPState2DRAStateMapping[i];
                            if (!DRA.acceptance().isStateInAcceptance_U(index, draState))
                            {
                                newSCC.Add(i);
                                //note add by ssz
                                if (DRA.acceptance().isStateInAcceptance_L(index, draState))
                                {
                                    targets.Add(i);
                                }
                                //note add by ssz
                            }
                        }
                        if (targets.Count > 0)
                        {
                            RemoveNonECStates(newSCC, targets);
                        }
                        if (targets.Count > 0)
                        {
                            //List<string> endComponent = TarjanModelChecking(index, newSCC);
                            Debug.WriteLine(helper++);
                            //Common.Classes.Ultility.Ultility.AddList(EndComponents, endComponent);
                            Common.Classes.Ultility.Ultility.AddList(EndComponents, scc);//note here newSCC changed to scc
                        }
                        else
                        {
                            if (scc.Count > 1)
                            {
                                GroupMEC(scc);
                            }
                        }
                    }
                }
            }

            foreach (string s in EndComponents)
            {
                mdp.AddTargetStates(mdp.States[s]);
            }
            Debug.WriteLine(mdp.States.Count);
            VerificationOutput.NoOfStates = VerificationOutput.NoOfStates + mdp.States.Count;
        }
Exemplo n.º 2
0
        //typedef typename DA_t::state_type da_state_t;
        //typedef typename Algorithm_t::state_t algo_state_t;
        //typedef typename Algorithm_t::result_t algo_result_t;
        //typedef TreeWithAcceptance<algo_state_t, typename Acceptance::signature_type> stuttered_state_t;
        //typedef typename stuttered_state_t::ptr stuttered_state_ptr_t;

        //typedef std::pair<stuttered_state_ptr_t, da_state_t*> unprocessed_value_t;
        //typedef std::stack<unprocessed_value_t> unprocessed_stack_t;

        /** Convert the NBA to the DA */
        public void convert()
        {
            APSet ap_set = _da_result.getAPSet();

            if (_algo.checkEmpty())
            {
                _da_result.constructEmpty();
                return;
            }

            _algo.prepareAcceptance(_da_result.acceptance());

            TreeWithAcceptance s_start     = new TreeWithAcceptance(_algo.getStartState());
            DA_State           start_state = _da_result.newState();

            s_start.generateAcceptance(start_state.acceptance());
            if (_detailed_states)
            {
                //start_state->setDescription(s_start->toHTML());
                start_state.setDescription("hahahahah");
            }

            _state_mapper.add(s_start, start_state);
            _da_result.setStartState(start_state);

            Stack <KeyValuePair <TreeWithAcceptance, DA_State> > unprocessed;

            _unprocessed.Push(new KeyValuePair <TreeWithAcceptance, DA_State>(s_start, start_state));

            bool   all_insensitive     = _stutter_information.isCompletelyInsensitive();
            BitSet partial_insensitive = _stutter_information.getPartiallyInsensitiveSymbols();

            while (_unprocessed.Count > 0)
            {
                KeyValuePair <TreeWithAcceptance, DA_State> top = _unprocessed.Pop();
                //_unprocessed.pop();

                TreeWithAcceptance from    = top.Key;
                DA_State           da_from = top.Value;

                //for (APSet::element_iterator it_elem=ap_set.all_elements_begin();it_elem!=ap_set.all_elements_end();++it_elem)
                for (int it_elem = ap_set.all_elements_begin(); it_elem != ap_set.all_elements_end(); ++it_elem)
                {
                    APElement elem = new APElement(it_elem);

                    if (da_from.edges().get(elem) == null)
                    {
                        // the edge was not yet calculated...

                        if (!all_insensitive && partial_insensitive.get(it_elem) == null)
                        {
                            // can't stutter for this symbol, do normal step
                            SafraTree          next_tree  = _algo.delta(from.getTree() as SafraTree, elem).getState();
                            TreeWithAcceptance next_state = new TreeWithAcceptance(next_tree);
                            add_transition(da_from, next_state, elem);

                            continue;
                        }

                        // normal stuttering...

                        calc_delta(from, da_from, elem);

                        if (_limit != 0 && _da_result.size() > _limit)
                        {
                            //THROW_EXCEPTION(LimitReachedException, "");
                            throw new Exception("LimitReachedException");
                        }
                    }
                }
            }
        }