Exemplo n.º 1
0
 public void SearchRespectsAccessLevelCriteria()
 {
     var codeSearcher = new CodeSearcher();
     string keywords = "usage type";
     SimpleSearchCriteria searchCriteria = new SimpleSearchCriteria()
     {
         AccessLevels = new SortedSet<AccessLevel>() { AccessLevel.Private },
         SearchByAccessLevel = true,
         SearchTerms = new SortedSet<string>(keywords.Split(' '))
     };
     List<CodeSearchResult> codeSearchResults = codeSearcher.Search(searchCriteria);
     Assert.AreEqual(2, codeSearchResults.Count, "Invalid results number");
     var methodSearchResult = codeSearchResults.Find(el =>
                                                         el.ProgramElement.ProgramElementType == ProgramElementType.Method &&
                                                         (el.ProgramElement.Name == "UsageTypeCriteriaToString"));
     if(methodSearchResult == null)
     {
         Assert.Fail("Failed to find relevant search result for search: " + keywords);
     }
     var method = methodSearchResult.ProgramElement as MethodElement;
     Assert.AreEqual(method.AccessLevel, AccessLevel.Private, "Method access level differs!");
     Assert.AreEqual(method.Arguments, "StringBuilder stringBuilder bool searchByUsageType", "Method arguments differs!");
     Assert.NotNull(method.Body, "Method body is null!");
     //Assert.True(method.ClassId != null && method.ClassId != Guid.Empty, "Class id is invalid!");
     //Assert.AreEqual(method.ClassName, "SimpleSearchCriteria", "Method class name differs!");
     Assert.AreEqual(method.DefinitionLineNumber, 96, "Method definition line number differs!");
     Assert.True(method.FullFilePath.EndsWith("\\TestFiles\\MethodElementTestFiles\\Searcher.cs".ToLowerInvariant()), "Method full file path is invalid!");
     Assert.AreEqual(method.Name, "UsageTypeCriteriaToString", "Method name differs!");
     Assert.AreEqual(method.ProgramElementType, ProgramElementType.Method, "Program element type differs!");
     Assert.AreEqual(method.ReturnType, "void", "Method return type differs!");
     Assert.False(String.IsNullOrWhiteSpace(method.RawSource), "Method snippet is invalid!");
 }
Exemplo n.º 2
0
 private void Initialze(SimpleSearchCriteria searchCriteria)
 {
     if (_searchCriteria == null)
     {
         _searchCriteria = searchCriteria ?? new SimpleSearchCriteria();
     }
 }
Exemplo n.º 3
0
 public CriteriaBuilder Ext(string searchString, SimpleSearchCriteria searchCriteria = null)
 {
     Initialze(searchCriteria);
     _searchCriteria.FileExtensions        = WordSplitter.GetFileExtensions(searchString);
     _searchCriteria.SearchByFileExtension = _searchCriteria.FileExtensions.Any();
     return(this);
 }
Exemplo n.º 4
0
        public CriteriaBuilder AddSearchString(string searchString, SimpleSearchCriteria searchCriteria = null)
        {
            Initialze(searchCriteria);
            var terms = WordSplitter.ExtractSearchTerms(searchString).ToList();

            SearchCriteriaReformer.ReformSearchCriteria(_searchCriteria);
            _searchCriteria.SearchTerms = new SortedSet <string>(terms);
            return(this);
        }
Exemplo n.º 5
0
        public virtual List<CodeSearchResult> Search(SimpleSearchCriteria searchCriteria, bool rerunWithWildcardIfNoResults = false)
        {
            Contract.Requires(searchCriteria != null, "CodeSearcher:Search - searchCriteria cannot be null!");

            var searchResults = _simpleSearcher.Search(searchCriteria).ToList();
            if (!searchResults.Any() && rerunWithWildcardIfNoResults && !QuotesInQuery(searchCriteria))
                searchResults = RerunQueryWithWildcardAtTheEnd(searchCriteria, searchResults);
            return searchResults;
        }
Exemplo n.º 6
0
 public CriteriaBuilder AddFromDescription(SandoQueryDescription description, SimpleSearchCriteria searchCriteria = null)
 {
     Initialze(searchCriteria);
     _searchCriteria.AddAccessLevels(description.AccessLevels);
     _searchCriteria.FileExtensions.UnionWith(description.FileExtensions);
     _searchCriteria.SearchTerms.UnionWith(description.LiteralSearchTerms);
     _searchCriteria.Locations.UnionWith(description.Locations);
     _searchCriteria.AddProgramElementTypes(description.ProgramElementTypes);
     _searchCriteria.SearchTerms.UnionWith(description.SearchTerms);
     return(this);
 }
Exemplo n.º 7
0
 public void SearchCriteria_EqualsReturnTrueWhenObjectIsComparedToItsOwn()
 {
     SearchCriteria simpleSearchCriteria = new SimpleSearchCriteria()
                                             {
                                                 SearchByProgramElementType = true,
                                                 ProgramElementTypes = new SortedSet<ProgramElementType>()
                                                                         {
                                                                             ProgramElementType.Class,
                                                                             ProgramElementType.Method
                                                                         }
                                             };
     Assert.True(simpleSearchCriteria.Equals(simpleSearchCriteria), "Equals should return true when search criteria object is compared to its own!");
 }
 private static void PopulateRecommendations(SimpleSearchCriteria criteria, List<string> terms, List<string> originalTerms, List<IReformedQuery> queries)
 {
     //LogEvents.AddSearchTermsToOriginal(queries.First());
     var query = queries.First();
     var termsToAdd = query.WordsAfterReform.Except(terms);
     foreach (var term in termsToAdd)
         if (term.Contains(" ")&&!term.Contains("\""))
             terms.AddRange(term.Split(' '));
         else
             terms.Add(term);
     criteria.Explanation = GetExplanation(query, originalTerms);
     criteria.Reformed = true;
     criteria.RecommendedQueries = queries.GetRange(1, queries.Count - 1).
         Select(n => n.QueryString).AsQueryable();
     if (queries.Count > 1)
     {
         LogEvents.IssueRecommendedQueries(queries.GetRange(1, queries.Count - 1).
             ToArray());
     }
 }
Exemplo n.º 9
0
 public void SearchCriteria_EqualsReturnFalseWhenObjectsHaveDifferentData()
 {
     SearchCriteria simpleSearchCriteria1 = new SimpleSearchCriteria()
                                             {
                                                 SearchByProgramElementType = true,
                                                 ProgramElementTypes = new SortedSet<ProgramElementType>()
                                                                         {
                                                                             ProgramElementType.Class,
                                                                             ProgramElementType.Property
                                                                         }
                                             };
     SearchCriteria simpleSearchCriteria2 = new SimpleSearchCriteria()
                                             {
                                                 SearchByProgramElementType = true,
                                                 ProgramElementTypes = new SortedSet<ProgramElementType>()
                                                                         {
                                                                             ProgramElementType.Class,
                                                                             ProgramElementType.Method
                                                                         }
                                             };
     Assert.False(simpleSearchCriteria1.Equals(simpleSearchCriteria2), "Equals should return false when search criteria objects have different data!");
 }
Exemplo n.º 10
0
 public void SearchCriteria_EqualsReturnTrueWhenObjectsHaveTheSameData()
 {
     SearchCriteria simpleSearchCriteria1 = new SimpleSearchCriteria()
                                             {
                                                 SearchByProgramElementType = true,
                                                 ProgramElementTypes = new SortedSet<ProgramElementType>()
                                                                         {
                                                                             ProgramElementType.Class,
                                                                             ProgramElementType.Method
                                                                         }
                                             };
     SearchCriteria simpleSearchCriteria2 = new SimpleSearchCriteria()
                                             {
                                                 SearchByProgramElementType = true,
                                                 ProgramElementTypes = new SortedSet<ProgramElementType>()
                                                                         {
                                                                             ProgramElementType.Method,
                                                                             ProgramElementType.Class
                                                                         }
                                             };
     Assert.True(simpleSearchCriteria1.Equals(simpleSearchCriteria2), "Equals should return true when search criteria objects have the same data!");
 }
Exemplo n.º 11
0
        public static void ReformSearchCriteria(SimpleSearchCriteria criteria)
        {
            var specialTerms = GetSpecialTerms(criteria.SearchTerms);
            var terms        = criteria.SearchTerms.Where(t => !t.StartsWith("\"") || !t.EndsWith("\"")).
                               Select(t => t.NormalizeText()).Where(t => !String.IsNullOrWhiteSpace(t)).
                               Distinct().ToList();
            var originalTerms           = terms.ToList();
            var dictionarySplittedTerms = terms.SelectMany
                                              (ServiceLocator.Resolve <DictionaryBasedSplitter>().
                                              ExtractWords).Where(t => t.Length >= TERM_MINIMUM_LENGTH).ToList();

            terms.AddRange(dictionarySplittedTerms.Except(terms));
            var queries = GetReformedQuery(terms.Distinct()).ToList();

            if (queries.Count > 0)
            {
                LogEvents.AddSearchTermsToOriginal(queries.First());
                var query = queries.First();
                terms.AddRange(query.WordsAfterReform.Except(terms));
                criteria.Explanation        = GetExplanation(query, originalTerms);
                criteria.Reformed           = true;
                criteria.RecommendedQueries = queries.GetRange(1, queries.Count - 1).
                                              Select(n => n.QueryString).AsQueryable();
                if (queries.Count > 1)
                {
                    LogEvents.IssueRecommendedQueries(queries.GetRange(1, queries.Count - 1).
                                                      ToArray());
                }
            }
            else
            {
                criteria.Explanation        = String.Empty;
                criteria.Reformed           = false;
                criteria.RecommendedQueries = Enumerable.Empty <String>().AsQueryable();
            }
            terms.AddRange(specialTerms);
            criteria.SearchTerms = ConvertToSortedSet(terms);
        }
Exemplo n.º 12
0
        public static void ReformSearchCriteria(SimpleSearchCriteria criteria)
        {
            //try without splitting search terms
            var specialTerms = GetSpecialTerms(criteria.SearchTerms);
            var terms = criteria.SearchTerms.Where(t => !t.StartsWith("\"")||!t.EndsWith("\"")).
                Select(t => t.NormalizeText()).Where(t => !String.IsNullOrWhiteSpace(t)).
                    Distinct().ToList();
            var originalTerms = terms.ToList();
            var queries = GetReformedQuery(terms.Distinct()).ToList();

            if (queries.Count > 0)
            {
                PopulateRecommendations(criteria, terms, originalTerms, queries);
            }
            else
            {
                criteria.Explanation = String.Empty;
                criteria.Reformed = false;
                criteria.RecommendedQueries = Enumerable.Empty<String>().AsQueryable();
            }
            terms.AddRange(specialTerms);
            criteria.SearchTerms = ConvertToSortedSet(terms);
        }
Exemplo n.º 13
0
 public CriteriaBuilder NumResults(int numResults, SimpleSearchCriteria searchCriteria = null)
 {
     Initialze(searchCriteria);
     _searchCriteria.NumberOfSearchResultsReturned = numResults;
     return(this);
 }
Exemplo n.º 14
0
 public CriteriaBuilder AddCriteria(SimpleSearchCriteria searchCriteria)
 {
     Initialze(searchCriteria);
     return(this);
 }
Exemplo n.º 15
0
 private IEnumerable<CodeSearchResult> GetResults(string searchString)
 {
     var searcher = new SimpleIndexerSearcher();
     var criteria = new SimpleSearchCriteria
         {
             SearchTerms = new SortedSet<string>(searchString.Split(' ').ToList())
         };
     var results = searcher.Search(criteria);
     return results;
 }
 public LuceneQueryStringBuilder(SimpleSearchCriteria simpleSearchCriteria)
 {
     _criteria = simpleSearchCriteria;
 }
Exemplo n.º 17
0
        private void BeginSearch(string searchString)
        {
            this.BeforeSearch(this, new EventArgs());

            AddSearchHistory(searchString);

            SimpleSearchCriteria criteria = new SimpleSearchCriteria();

            var selectedAccessLevels = this.AccessLevels.Where(a => a.Checked).Select(a => a.Access).ToList();
            if (selectedAccessLevels.Any())
            {
                criteria.SearchByAccessLevel = true;
                criteria.AccessLevels = new SortedSet<AccessLevel>(selectedAccessLevels);
            }
            else
            {
                criteria.SearchByAccessLevel = false;
                criteria.AccessLevels.Clear();
            }

            var selectedProgramElementTypes =
                this.ProgramElements.Where(e => e.Checked).Select(e => e.ProgramElement).ToList();
            if (selectedProgramElementTypes.Any())
            {
                criteria.SearchByProgramElementType = true;
                criteria.ProgramElementTypes = new SortedSet<ProgramElementType>(selectedProgramElementTypes);
            }
            else
            {
                criteria.SearchByProgramElementType = false;
                criteria.ProgramElementTypes.Clear();
            }

            SearchAsync(searchString, criteria);
        }
Exemplo n.º 18
0
        public void SearchReturnsElementsUsingCrossFieldMatching()
        {
            var codeSearcher = new CodeSearcher();
            string keywords = "fetch output argument";
            SimpleSearchCriteria searchCriteria = new SimpleSearchCriteria()
            {
                SearchTerms = new SortedSet<string>(keywords.Split(' '))
            };
            List<CodeSearchResult> codeSearchResults = codeSearcher.Search(searchCriteria);

            var methodSearchResult = codeSearchResults.Find(el =>
                                                                el.ProgramElement.ProgramElementType == ProgramElementType.Method &&
                                                                (el.ProgramElement.Name == "FetchOutputStream"));
            if(methodSearchResult == null)
            {
                Assert.Fail("Failed to find relevant search result for search: " + keywords);
            }
            var method = methodSearchResult.ProgramElement as MethodElement;
            Assert.AreEqual(method.AccessLevel, AccessLevel.Public, "Method access level differs!");
            Assert.AreEqual(method.Arguments, "A B string fileName Image image", "Method arguments differs!");
            Assert.NotNull(method.Body, "Method body is null!");
            Assert.True(method.ClassId != null && method.ClassId != Guid.Empty, "Class id is invalid!");
            Assert.AreEqual(method.ClassName, "ImageCapture", "Method class name differs!");
            Assert.AreEqual(method.DefinitionLineNumber, 83, "Method definition line number differs!");
            Assert.True(method.FullFilePath.EndsWith("\\TestFiles\\MethodElementTestFiles\\ImageCapture.cs".ToLowerInvariant()), "Method full file path is invalid!");
            Assert.AreEqual(method.Name, "FetchOutputStream", "Method name differs!");
            Assert.AreEqual(method.ProgramElementType, ProgramElementType.Method, "Program element type differs!");
            Assert.AreEqual(method.ReturnType, "void", "Method return type differs!");
            Assert.False(String.IsNullOrWhiteSpace(method.RawSource), "Method snippet is invalid!");
        }
Exemplo n.º 19
0
        private void SearchAsync(String text, SimpleSearchCriteria searchCriteria)
        {
            lock (this)
            {
                if (_isSearchingDisabled)
                {
                    return;
                }
                else
                {
                    _isSearchingDisabled = true;
                }

                var searchWorker = new BackgroundWorker();
                searchWorker.DoWork += SearchWorker_DoWork;
                searchWorker.RunWorkerCompleted += SearchWorker_Completed;
                var workerSearchParams = new WorkerSearchParameters { Query = text, Criteria = searchCriteria };
                searchWorker.RunWorkerAsync(workerSearchParams);
            }
        }
 public void MethodSearchRespectsFileExtensionsCriteria()
 {
     var codeSearcher = new CodeSearcher();
     var keywords = "main";
     var searchCriteria = new SimpleSearchCriteria()
         {
             SearchTerms = new SortedSet<string>(keywords.Split(' ')),
             SearchByFileExtension = true,
             FileExtensions = new SortedSet<string> {"cpp"}
         };
     var codeSearchResults = codeSearcher.Search(searchCriteria);
     Assert.AreEqual(2, codeSearchResults.Count, "Invalid results number");
     var methodSearchResult = codeSearchResults.Find(el => el.ProgramElement.ProgramElementType == ProgramElementType.Method && el.ProgramElement.Name == "main");
     if (methodSearchResult == null)
     {
         Assert.Fail("Failed to find relevant search result for search: " + keywords);
     }
     //var method = methodSearchResult.Element as MethodElement;
     //Assert.AreEqual(method.AccessLevel, AccessLevel.Public, "Method access level differs!");
     //Assert.AreEqual(method.Arguments, String.Empty, "Method arguments differs!");
     //Assert.NotNull(method.Body, "Method body is null!");
     //Assert.True(method.ClassId != null && method.ClassId != Guid.Empty, "Class id is invalid!");
     //Assert.AreEqual(method.ClassName, "SimpleSearchCriteria", "Method class name differs!");
     //Assert.AreEqual(method.DefinitionLineNumber, 31, "Method definition line number differs!");
     //Assert.True(method.FullFilePath.EndsWith("\\TestFiles\\MethodElementTestFiles\\Searcher.cs"), "Method full file path is invalid!");
     //Assert.AreEqual(method.Name, "ToQueryString", "Method name differs!");
     //Assert.AreEqual(method.ProgramElementType, ProgramElementType.Method, "Program element type differs!");
     //Assert.AreEqual(method.ReturnType, "void", "Method return type differs!");
     //Assert.False(String.IsNullOrWhiteSpace(method.RawSource), "Method snippet is invalid!");
 }
Exemplo n.º 21
0
        public void Search(String searchString, SimpleSearchCriteria searchCriteria, bool interactive = true)
        {
            if (!EnsureSolutionOpen())
                return;

            try
            {
                var codeSearcher = new CodeSearcher();
                if (String.IsNullOrEmpty(searchString))
                    return;

                var solutionKey = ServiceLocator.ResolveOptional<SolutionKey>(); //no opened solution
                if (solutionKey == null)
                {
                    this.UpdateMessage("Sando searches only the currently open Solution.  Please open a Solution and try again.");

                }
                else
                {

                    searchString = ExtensionPointsRepository.Instance.GetQueryRewriterImplementation().RewriteQuery(searchString);

                    //PreRetrievalMetrics preMetrics = new PreRetrievalMetrics(ServiceLocator.Resolve<DocumentIndexer>().Reader, ServiceLocator.Resolve<Analyzer>());
                    //LogEvents.PreSearch(this, preMetrics.MaxIdf(searchString), preMetrics.AvgIdf(searchString), preMetrics.AvgSqc(searchString), preMetrics.AvgVar(searchString));
                    LogEvents.PreSearchQueryAnalysis(this, QueryMetrics.ExamineQuery(searchString).ToString(), QueryMetrics.DiceCoefficient(QueryMetrics.SavedQuery, searchString));
                    QueryMetrics.SavedQuery = searchString;

                    var results = codeSearcher.Search(searchCriteria, true).AsQueryable();
                    var resultsReorderer = ExtensionPointsRepository.Instance.GetResultsReordererImplementation();
                    results = resultsReorderer.ReorderSearchResults(results);

                    var returnString = new StringBuilder();

                    if (searchCriteria.IsQueryReformed())
                    {
                        returnString.Append(searchCriteria.GetQueryReformExplanation());
                    }

                    if (!results.Any())
                    {
                        returnString.Append("No results found. ");
                    }
                    else
                    {
                        returnString.Append(results.Count() + " results returned. ");
                    }

                    if (null != this.SearchResultUpdated)
                    {
                        this.SearchResultUpdated(searchString, results);
                    }

                    this.UpdateMessage(returnString.ToString());

                    if (null != this.RecommendedQueriesUpdated)
                    {
                        this.RecommendedQueriesUpdated(searchCriteria.GetRecommendedQueries());
                    }

                    LogEvents.PostSearch(this, results.Count(), searchCriteria.NumberOfSearchResultsReturned,
                        PostRetrievalMetrics.AvgScore(results.ToList()), PostRetrievalMetrics.StdDevScore(results.ToList()));

                }

            }
            catch (Exception e)
            {
                this.UpdateMessage("Sando is experiencing difficulties. See log file for details.");
                LogEvents.UISandoSearchingError(this, e);
            }
        }