Beispiel #1
0
        /// <summary>
        /// Adds a field to an unsuccessful code location.
        /// </summary>
        /// <param name="location"></param>
        /// <param name="fields"></param>
        public void AddFieldsOfUncoveredCodeLocations(CodeLocation location, SafeList <Field> fields, FieldModificationType fmt,
                                                      Term condition, string terms, int fitnessval, TypeEx explorableType, SafeList <TypeEx> allFieldTypes)
        {
            //No need to process this location.
            if (fields.Count == 0)
            {
                return;
            }

            Field  targetField;
            TypeEx declaringType;   //This declaring type is considered as explorable type in the rest of the analysis

            if (!PexMeFactoryGuesser.GetTargetExplorableField(this, fields, out targetField, out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "factoryguesser",
                                  "Failed to retrieve the target field for uncovered location " + location.ToString());
                return;
            }

            //Compare the declaring type and actual explorable type.
            //If there is a inheritance relation, use the actual one
            if (explorableType.IsAssignableTo(declaringType))
            {
                declaringType = explorableType;
            }

            var uclskey = UncoveredCodeLocationStore.GetKey(location.ToString(), declaringType.ToString(), condition.UniqueIndex);
            UncoveredCodeLocationStoreList uclslist;

            if (!this.unCoveredLocationDic.TryGetValue(uclskey, out uclslist))
            {
                uclslist                           = new UncoveredCodeLocationStoreList();
                uclslist.Location                  = location;
                uclslist.ExplorableType            = declaringType.ToString();
                uclslist.TermIndex                 = condition.UniqueIndex;
                this.unCoveredLocationDic[uclskey] = uclslist;
            }

            var ucls = new UncoveredCodeLocationStore();

            ucls.Location       = location;
            ucls.ExplorableType = declaringType;
            ucls.TargetField    = targetField;
            ucls.AllFields.AddRange(fields);
            ucls.AllFieldTypes.AddRange(allFieldTypes);
            ucls.TermIndex = condition.UniqueIndex;
            //add the sequence of method calls
            ucls.MethodCallSequence = new MethodSignatureSequence();
            foreach (var m in this.LastExecutedFactoryMethodCallSequence)
            {
                ucls.MethodCallSequence.Sequence.Add(MethodOrFieldAnalyzer.GetMethodSignature(m));
            }
            ucls.IsADefectDetectingSequence = this.DefectDetectingSequence;
            ucls.CUTMethodCallSequence      = this.LastExecutedCUTMethodCallSequence;

            if (!uclslist.StoreList.Contains(ucls))
            {
                ucls.TextualTerms.Add(terms);
                ucls.DesiredFieldModificationType = fmt;
                ucls.Fitnessvalue = fitnessval;
                uclslist.StoreList.Add(ucls);
            }
        }
        /// <summary>
        /// Adds a field to an unsuccessful code location.         
        /// </summary>
        /// <param name="location"></param>
        /// <param name="fields"></param>
        public void AddFieldsOfUncoveredCodeLocations(CodeLocation location, SafeList<Field> fields, FieldModificationType fmt, 
            Term condition, string terms, int fitnessval, TypeEx explorableType, SafeList<TypeEx> allFieldTypes)
        {
            //No need to process this location.
            if (fields.Count == 0)
            {
                return;
            }

            Field targetField;
            TypeEx declaringType;   //This declaring type is considered as explorable type in the rest of the analysis
            if (!PexMeFactoryGuesser.GetTargetExplorableField(this, fields, out targetField, out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "factoryguesser",
                   "Failed to retrieve the target field for uncovered location " + location.ToString());
                return;
            }

            //Compare the declaring type and actual explorable type.
            //If there is a inheritance relation, use the actual one
            if (explorableType.IsAssignableTo(declaringType))
            {
                declaringType = explorableType;
            }

            var uclskey = UncoveredCodeLocationStore.GetKey(location.ToString(), declaringType.ToString(), condition.UniqueIndex);
            UncoveredCodeLocationStoreList uclslist;
            if (!this.unCoveredLocationDic.TryGetValue(uclskey, out uclslist))
            {
                uclslist = new UncoveredCodeLocationStoreList();
                uclslist.Location = location;
                uclslist.ExplorableType = declaringType.ToString();
                uclslist.TermIndex = condition.UniqueIndex;
                this.unCoveredLocationDic[uclskey] = uclslist;
            }

            var ucls = new UncoveredCodeLocationStore();
            ucls.Location = location;
            ucls.ExplorableType = declaringType;
            ucls.TargetField = targetField;
            ucls.AllFields.AddRange(fields);
            ucls.AllFieldTypes.AddRange(allFieldTypes);
            ucls.TermIndex = condition.UniqueIndex;
            //add the sequence of method calls
            ucls.MethodCallSequence = new MethodSignatureSequence();
            foreach (var m in this.LastExecutedFactoryMethodCallSequence)
            {
                ucls.MethodCallSequence.Sequence.Add(MethodOrFieldAnalyzer.GetMethodSignature(m));
            }
            ucls.IsADefectDetectingSequence = this.DefectDetectingSequence;
            ucls.CUTMethodCallSequence = this.LastExecutedCUTMethodCallSequence;

            if (!uclslist.StoreList.Contains(ucls))
            {
                ucls.TextualTerms.Add(terms);
                ucls.DesiredFieldModificationType = fmt;
                ucls.Fitnessvalue = fitnessval;
                uclslist.StoreList.Add(ucls);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Targets to remove those sequences with low or the same fitness
        /// </summary>
        /// <param name="ucovLocList"></param>
        /// <param name="persistentUncoveredLocationStore"></param>
        private void RemoveSuggestionsWithLowFitness(UncoveredCodeLocationStoreList ucovLocList,
                                                     PersistentUncoveredLocationStore pucls, List <MethodSignatureSequence> putspecificsequences)
        {
            pucls.NumberOfUnsuccessfulAttempts++;
            int bestFitness = pucls.Fitnessvalue;

            foreach (var ucovloc in ucovLocList.StoreList)
            {
                if (ucovloc.Fitnessvalue < bestFitness)
                {
                    bestFitness = ucovloc.Fitnessvalue;
                }
            }

            var explorableType         = ucovLocList.ExplorableType;
            var tempSuggestedSequences = new List <MethodSignatureSequence>();

            tempSuggestedSequences.AddRange(pucls.SuggestedMethodSequences);

            //remove those suggestions that have lower fitness values
            foreach (var ucovloc in ucovLocList.StoreList)
            {
                //Get matching sequence from pucls
                MethodSignatureSequence matchingseq;
                if (!FactorySuggestionStore.TryGetMatchingSequence(ucovloc.MethodCallSequence, tempSuggestedSequences, out matchingseq))
                {
                    //This sequence is not there in our suggested sequence. This should have happened
                    //due to the case that it came from other uncovered location stores, and is helping
                    //the current store.
                    if (ucovloc.Fitnessvalue <= bestFitness)
                    {
                        var mss = new MethodSignatureSequence();
                        foreach (var methodinucov in ucovloc.MethodCallSequence.Sequence)
                        {
                            if (methodinucov.Contains("..ctor(")) //Don't add constructors
                            {
                                continue;
                            }
                            if (!methodinucov.Contains(explorableType)) //Ignore the method calls from other types
                            {
                                continue;
                            }
                            mss.Sequence.Add(methodinucov);
                        }

                        if (mss.Sequence.Count > 0)
                        {
                            pucls.SuggestedMethodSequences.Add(mss);
                        }
                    }

                    continue;
                }

                tempSuggestedSequences.Remove(matchingseq);
                if (ucovloc.Fitnessvalue > bestFitness)
                {
                    //Previous sequence is of no use as it is leading to higher fitness value
                    pucls.SuggestedMethodSequences.Remove(matchingseq);
                }
            }

            if (tempSuggestedSequences.Count != 0)
            {
                //Not all sequences are assigned fitness values. Raise warnings for other sequences
                this.Log.LogWarning(WikiTopics.MissingWikiTopic, "sequencematching",
                                    "Fitness values are not available for some previous sequences, Needs to handle this case!!!");

                //Remove those suggestions whose fitness value is not evaluated at all. Ideally
                //this case should not happen. Needs additional debugging.
                //foreach (var seq in tempSuggestedSequences)
                //    pucls.SuggestedMethodSequences.Remove(seq);
            }

            //Update the previous fitness value with the current. If there is any improvement in the fitness, reset the number
            //of unsuccessful attempts
            if (pucls.Fitnessvalue != bestFitness)
            {
                pucls.NumberOfUnsuccessfulAttempts = 0;
                pucls.Fitnessvalue = bestFitness;
            }
        }
        /// <summary>
        /// Targets to remove those sequences with low or the same fitness
        /// </summary>
        /// <param name="ucovLocList"></param>
        /// <param name="persistentUncoveredLocationStore"></param>
        private void RemoveSuggestionsWithLowFitness(UncoveredCodeLocationStoreList ucovLocList, 
            PersistentUncoveredLocationStore pucls, List<MethodSignatureSequence> putspecificsequences)
        {
            pucls.NumberOfUnsuccessfulAttempts++;
            int bestFitness = pucls.Fitnessvalue;

            foreach (var ucovloc in ucovLocList.StoreList)
            {
                if (ucovloc.Fitnessvalue < bestFitness)
                    bestFitness = ucovloc.Fitnessvalue;
            }

            var explorableType = ucovLocList.ExplorableType;
            var tempSuggestedSequences = new List<MethodSignatureSequence>();
            tempSuggestedSequences.AddRange(pucls.SuggestedMethodSequences);

            //remove those suggestions that have lower fitness values
            foreach (var ucovloc in ucovLocList.StoreList)
            {
                //Get matching sequence from pucls
                MethodSignatureSequence matchingseq;
                if (!FactorySuggestionStore.TryGetMatchingSequence(ucovloc.MethodCallSequence, tempSuggestedSequences, out matchingseq))
                {
                    //This sequence is not there in our suggested sequence. This should have happened
                    //due to the case that it came from other uncovered location stores, and is helping
                    //the current store.
                    if (ucovloc.Fitnessvalue <= bestFitness)
                    {
                        var mss = new MethodSignatureSequence();
                        foreach (var methodinucov in ucovloc.MethodCallSequence.Sequence)
                        {
                            if (methodinucov.Contains("..ctor(")) //Don't add constructors
                                continue;
                            if (!methodinucov.Contains(explorableType)) //Ignore the method calls from other types
                                continue;
                            mss.Sequence.Add(methodinucov);
                        }

                        if(mss.Sequence.Count > 0)
                            pucls.SuggestedMethodSequences.Add(mss);
                    }

                    continue;
                }

                tempSuggestedSequences.Remove(matchingseq);
                if (ucovloc.Fitnessvalue > bestFitness)
                {
                    //Previous sequence is of no use as it is leading to higher fitness value
                    pucls.SuggestedMethodSequences.Remove(matchingseq);
                }
            }

            if (tempSuggestedSequences.Count != 0)
            {
                //Not all sequences are assigned fitness values. Raise warnings for other sequences
                this.Log.LogWarning(WikiTopics.MissingWikiTopic, "sequencematching",
                    "Fitness values are not available for some previous sequences, Needs to handle this case!!!");

                //Remove those suggestions whose fitness value is not evaluated at all. Ideally
                //this case should not happen. Needs additional debugging.
                //foreach (var seq in tempSuggestedSequences)
                //    pucls.SuggestedMethodSequences.Remove(seq);
            }

            //Update the previous fitness value with the current. If there is any improvement in the fitness, reset the number
            //of unsuccessful attempts
            if (pucls.Fitnessvalue != bestFitness)
            {
                pucls.NumberOfUnsuccessfulAttempts = 0;
                pucls.Fitnessvalue = bestFitness;
            }
        }