Exemplo n.º 1
0
        public void Clear()
        {
            SearchDictionary <string> dictionary = new SearchDictionary <string>();
            const string Key = "Test";

            dictionary.Add(Key, "data");
            dictionary.Clear();
            dictionary.Add(Key, "data");
            dictionary.Clear();

            // Test that the dictionary count is 0.
            Assert.AreEqual(0, dictionary.Count);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     If Revit API elements are shown, hides them.  Otherwise,
        ///     shows them.  Update search when done with either.
        /// </summary>
        public void ToggleIncludingRevitAPIElements()
        {
            if (!IncludeRevitAPIElements)
            {
                this.RemoveCategory(BuiltinNodeCategories.REVIT_API);

                foreach (var ele in RevitApiSearchElements)
                {
                    SearchDictionary.Remove(ele, ele.Name);
                    if (!(ele is CategorySearchElement))
                    {
                        SearchDictionary.Remove(ele, BuiltinNodeCategories.REVIT_API + "." + ele.Name);
                    }
                }
            }
            else
            {
                var  revitCat = this.AddCategory(BuiltinNodeCategories.REVIT_API);
                bool addToCat = !revitCat.Items.Any();

                // add elements to search
                foreach (var ele in RevitApiSearchElements)
                {
                    if (addToCat)
                    {
                        revitCat.Items.Add(ele);
                    }
                    SearchDictionary.Add(ele, ele.Name);
                    if (!(ele is CategorySearchElement))
                    {
                        SearchDictionary.Add(ele, BuiltinNodeCategories.REVIT_API + "." + ele.Name);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void Adding10000RandomKeysAndRemovingHalfWorks()
        {
            var randomKeys = Keygenerator.GenerateKeys(49, 20, '0', 'z', 100000);

            var searchDictionary = new SearchDictionary <int>();

            foreach (var randomKey in randomKeys)
            {
                searchDictionary.Add(randomKey, 0);
            }

            var i = 0;

            foreach (var randomKey in randomKeys)
            {
                if (i % 2 == 0)
                {
                    Assert.IsTrue(searchDictionary.Remove(randomKey), $"RemoveKey(\"{randomKey}\"");
                }

                i++;
            }

            i = 0;
            foreach (var randomKey in randomKeys)
            {
                Assert.AreEqual(i % 2 != 0, searchDictionary.ContainsKey(randomKey), $"ContainsKey(\"{randomKey}\"");
                i++;
            }

            Assert.AreEqual(randomKeys.Count / 2, searchDictionary.Count);
        }
Exemplo n.º 4
0
        public void GetThis()
        {
            SearchDictionary <object> dictionary = new SearchDictionary <object>();
            const string Key     = "Test";
            object       object1 = new object();
            object       object2 = new object();

            dictionary.Add(Key + 1, object1);
            dictionary.Add(Key + 2, object2);

            // Test that key1 maps to object1 and key2 maps to object2.
            Assert.AreEqual(object1, dictionary[Key + 1]);
            Assert.AreEqual(object2, dictionary[Key + 2]);

            // Test that KeyNotFoundException is thrown.
            var obj = dictionary[Key + 3];
        }
Exemplo n.º 5
0
        /// <summary>
        /// Attempts to obtain the list of search results.  If it fails, it does nothing
        /// </summary>
        public void Refresh()
        {
            var pkgs = PackageManagerClientViewModel.ListAll();

            pkgs.Sort((e1, e2) => e1.Name.ToLower().CompareTo(e2.Name.ToLower()));
            LastSync = pkgs;

            SearchDictionary = new SearchDictionary <PackageManagerSearchElement>();

            foreach (var pkg in pkgs)
            {
                SearchDictionary.Add(pkg, pkg.Name);
                SearchDictionary.Add(pkg, pkg.Description);
                SearchDictionary.Add(pkg, pkg.Maintainers);
                SearchDictionary.Add(pkg, pkg.Keywords);
            }
        }
Exemplo n.º 6
0
        public void Add()
        {
            var dictionary = new SearchDictionary <string>
            {
                { "Test", "Data" }
            };

            // Test that ArgumentException is thrown.
            dictionary.Add("Test", "Data");
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Adds a PackageHeader, recently downloaded from the Package Manager, to Search
        /// </summary>
        /// <param name="packageHeader">A PackageHeader object</param>
        public void Add(PackageHeader packageHeader)
        {
            var searchEle = new PackageManagerSearchElement(packageHeader);

            SearchDictionary.Add(searchEle, searchEle.Name);
            if (packageHeader.keywords != null && packageHeader.keywords.Count > 0)
            {
                SearchDictionary.Add(searchEle, packageHeader.keywords);
            }
            SearchDictionary.Add(searchEle, searchEle.Description);
        }
Exemplo n.º 8
0
        public void TesGetItems()
        {
            // Arrange
            var searchDictionary = new SearchDictionary <string>();
            var tag1             = Guid.NewGuid().ToString();
            var tag2             = Guid.NewGuid().ToString();

            //It will generate the numbers from 1 to 10, starting from 1
            IEnumerable <string> ranges1 = from value in Enumerable.Range(1, 10)
                                           select value.ToString();

            //It will generate the numbers from 1 to 10, starting from 1
            IEnumerable <string> ranges2 = from value in Enumerable.Range(1, 10)
                                           select value.ToString();

            //Act
            searchDictionary.Add(ranges1, tag1); //Add 10 strings using the tag1
            searchDictionary.Add(ranges2, tag2); //Add 10 strings using the tag2

            //This will execute the method internal IEnumerable<V> ByTag(string tag)
            var ienumTags = searchDictionary.ByTag(tag1);

            var tags = searchDictionary.GetTags("5");       //This will return 2 tags

            var weights = searchDictionary.GetWeights("5"); //This will return 2 weights

            //Assert
            //This will validate that the method ByTag returns the 10 strings inserted
            Assert.AreEqual(ienumTags.Count(), 10);

            //This will validate that the dictionary contains the value "6"
            Assert.IsTrue(searchDictionary.Contains("6"));//This will execute the method bool Contains(V a)

            //Check that the two different tags are returned for the "5" value
            Assert.AreEqual(tags.Count(), 2);

            //Check that the two different weights are returned for the "5" value
            Assert.AreEqual(weights.Count(), 2);
        }
Exemplo n.º 9
0
        public void TestAddItems()
        {
            //Arrange
            var searchDictionary = new SearchDictionary <string>();
            var tag1             = Guid.NewGuid().ToString();

            //It will generate the numbers from 1 to 10, starting from 1
            IEnumerable <string> ranges1 = from value in Enumerable.Range(1, 10)
                                           select value.ToString();

            //This linq expression will generate 1 tags
            IEnumerable <string> tags = from value in Enumerable.Range(1, 1)
                                        select Guid.NewGuid().ToString();

            //This linq expression will generate 20 weights
            IEnumerable <double> wrongWeights = from value in Enumerable.Range(1, 20)
                                                select double.Parse(value.ToString());

            //This linq expression will generate 1 value starting from 21
            IEnumerable <string> values = from value in Enumerable.Range(21, 1)
                                          select value.ToString();

            //Act
            //This will execute the method void Add(IEnumerable<V> values, string tag, double weight = 1)
            searchDictionary.Add(ranges1, tag1);//Add 10 strings

            //This will execute the method internal void Add(IEnumerable<V> values, IEnumerable<string> tags, double weight = 1)
            searchDictionary.Add(values, tags);//Add 1 string

            //Assert
            //This will execute the method void Add(V value, IEnumerable<string> tags, IEnumerable<double> weights)
            //It will fail due that number of tags is different than number of weights
            Assert.Throws <ArgumentException>(() => searchDictionary.Add("Test", tags, wrongWeights));


            //Check that we have 11 items in the dictionary
            Assert.AreEqual(searchDictionary.NumTags, 11);
        }
Exemplo n.º 10
0
        public void ContainsKey()
        {
            SearchDictionary <string> dictionary = new SearchDictionary <string>();
            const string Key = "Test";

            // Test that the key does exist.
            Assert.IsFalse(dictionary.ContainsKey(Key));

            dictionary.Add(Key, "Data");

            // Test that the key exist.
            Assert.IsTrue(dictionary.ContainsKey(Key));
            Assert.IsFalse(dictionary.ContainsKey(Key.Substring(0, 3)));
            Assert.IsFalse(dictionary.ContainsKey(Key + "t"));
        }
Exemplo n.º 11
0
        public bool Add(CustomNodeInfo nodeInfo)
        {
            var nodeEle = new CustomNodeSearchElement(nodeInfo);

            if (SearchDictionary.Contains(nodeEle))
            {
                return(this.Refactor(nodeInfo));
            }

            SearchDictionary.Add(nodeEle, nodeEle.Name);
            SearchDictionary.Add(nodeEle, nodeInfo.Category + "." + nodeEle.Name);

            TryAddCategoryAndItem(nodeInfo.Category, nodeEle);

            return(true);
        }
Exemplo n.º 12
0
        public void OptimizeDoesNotDestroyTheTree()
        {
            var randomKeys = Keygenerator.GenerateKeys(57, 20, '0', 'z', 10000);

            var searchDictionary = new SearchDictionary <int>();

            foreach (var ranomKey in randomKeys)
            {
                searchDictionary.Add(ranomKey, 0);
            }

            foreach (var randomKey in randomKeys)
            {
                Assert.IsTrue(searchDictionary.ContainsKey(randomKey), $"ContainsKey(\"{randomKey}\"");
            }
        }
Exemplo n.º 13
0
        public void GetCount()
        {
            SearchDictionary <string> dictionary = new SearchDictionary <string>();
            const string Key   = "Test";
            const int    Count = 34;

            // Test that the dictionary count is 0.
            Assert.AreEqual(dictionary.Count, 0);

            for (int i = 1; i <= Count; i++)
            {
                dictionary.Add(Key + i, null);

                // Test that the count is i.
                Assert.AreEqual(i, dictionary.Count);
            }
        }
Exemplo n.º 14
0
        public void Adding10000RandomKeysWork()
        {
            var randomKeys = Keygenerator.GenerateKeys(42, 20, '0', 'z', 10000);

            var searchDictionary = new SearchDictionary <int>();

            foreach (var randomKey in randomKeys)
            {
                searchDictionary.Add(randomKey, 0);
            }

            Assert.AreEqual(randomKeys.Count, searchDictionary.Count);
            foreach (var randomKey in randomKeys)
            {
                Assert.IsTrue(searchDictionary.ContainsKey(randomKey));
            }
        }
Exemplo n.º 15
0
        public void TestRemoveItems()
        {
            // Arrange
            var searchDictionary = new SearchDictionary <string>();
            var tag1             = Guid.NewGuid().ToString();

            //It will generate the numbers from 1 to 10, starting from 1
            IEnumerable <string> ranges1 = from value in Enumerable.Range(1, 10)
                                           select value.ToString();

            //It will generate the numbers from 1 to 10, starting from 1
            IEnumerable <string> tags = from value in Enumerable.Range(1, 2)
                                        select tag1;

            //Delegate that uses a lamda expression to delete the tag "9" inside the searchDictionary
            Func <string, bool> selector = str => str.Equals("9");

            //Delegate that uses a lamda expression to delete the tag "8" inside the searchDictionary
            Func <string, bool> selectorValue = str => str.Equals("8");

            //Delegate that uses a lamda expression to delete the tag "8" inside the searchDictionary
            Func <string, bool> selectorTag = str => str.Equals(tag1);

            //Act
            searchDictionary.Add(ranges1, tag1);//Add 10 strings

            //This will execute the method  internal bool Remove(V value, string tag)
            searchDictionary.Remove("10", tag1);//Remove the value "10"

            //This will execute the method int Remove(Func<V, bool> removeCondition)
            searchDictionary.Remove(selector);//Remove the value "9"

            //This will execute the method  internal int Remove(Func<V, bool> valueCondition, Func<string, bool> removeTagCondition)
            searchDictionary.Remove(selectorValue, selectorTag);//Remove the value 8

            //This will execute the method internal int Remove(V value, IEnumerable<string> tags)
            searchDictionary.Remove("7", tags);                      //Remove the value 7
            int valuesRemoved = searchDictionary.Remove("16", tags); //Value won't be found so it will retung 0

            //Assert
            //Check that we have only 7 strings in the dictionary, at the beginning we had 10 but 3 were removed
            Assert.AreEqual(searchDictionary.NumTags, 6);
            Assert.AreEqual(valuesRemoved, 0);//Means that value was't found, the Remove(V value, IEnumerable<string> tags) method returned 0
        }
Exemplo n.º 16
0
        public void AllKeyValuePairsCanBeEnumerated()
        {
            var allItems = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("a", "a"),
                new KeyValuePair <string, string>("aa", "aa"),
                new KeyValuePair <string, string>("c", "c"),
                new KeyValuePair <string, string>("b", "b"),
            };

            var dictionary = new SearchDictionary <string>();

            foreach (var item in allItems)
            {
                dictionary.Add(item);
            }

            CollectionAssert.AreEqual(allItems.OrderBy(item => item.Key).ToArray(), dictionary.OrderBy(item => item.Key).ToArray());
        }
Exemplo n.º 17
0
        public void TestSearchDictionaryPerformance()
        {
            //Arrange
            var searchDictionary = new SearchDictionary <string>();

            var tagsPath = System.IO.Path.Combine(TestDirectory, "performance", "search_tags", "searchtags.log");
            var tags     = System.IO.File.ReadAllLines(tagsPath);
            int value    = 0;

            foreach (var tag in tags)
            {
                searchDictionary.Add($"Value:{value++}", tag);
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var query1 = "all";

            searchDictionary.Search(query1);
            var query2 = "all elements";

            searchDictionary.Search(query2);
            var query3 = "all elements of";

            searchDictionary.Search(query3);
            var query4 = "all elements of category";

            searchDictionary.Search(query4);
            var query   = "az";
            var results = searchDictionary.Search(query);

            stopwatch.Stop();

            Assert.AreEqual(results.Count(), 20);

            double timeLimit = 300;//ms
            double range     = 70;

            Assert.IsTrue(Math.Abs(stopwatch.ElapsedMilliseconds - timeLimit) < range / 100 * timeLimit, $"Search time should be within a range of +/- {range}% of {timeLimit}ms but we got {stopwatch.ElapsedMilliseconds}ms");
        }
Exemplo n.º 18
0
        private static void TestPostionalPostingList()
        {
            //Build dictionary
            String           fileName = "PositionalIndex.txt";
            int              counter  = 0;
            string           line;
            SearchDictionary sd = new SearchDictionary();

            // Read the file and display it line by line.
            System.IO.StreamReader file =
                new System.IO.StreamReader(fileName);
            while ((line = file.ReadLine()) != null)
            {
                Console.WriteLine(line);
                sd.Add(line);
                counter++;
            }
            file.Close();
            String word    = "angels fear to tread";//"fools rush in";
            PList  docList = PositionalQuery.FindDocsWithPhrase(sd, word);
        }
Exemplo n.º 19
0
        /// <summary>
        ///     Add a custom node to search.
        /// </summary>
        /// <param name="workspace">A dynWorkspace to add</param>
        /// <param name="name">The name to use</param>
        public void Add(string name, string category, string description, Guid functionId)
        {
            if (name == "Home")
            {
                return;
            }

            // create the node in search
            var nodeEle = new NodeSearchElement(name, description, functionId);

            if (SearchDictionary.Contains(nodeEle))
            {
                return;
            }

            SearchDictionary.Add(nodeEle, nodeEle.Name);
            SearchDictionary.Add(nodeEle, category + "." + nodeEle.Name);

            TryAddCategoryAndItem(category, nodeEle);

            NodeCategories[category].NumElements++;
        }
Exemplo n.º 20
0
        public void Adding10000RandomKeysAndStartsWithFindsRandomKeysWork()
        {
            var randomKeys = Keygenerator.GenerateKeys(57, 20, '0', 'z', 10000);
            var random     = new Random(57);

            var searchDictionary = new SearchDictionary <string>();

            foreach (var randomKey in randomKeys)
            {
                searchDictionary.Add(randomKey, randomKey);
            }

            foreach (var randomKey in randomKeys)
            {
                var length     = random.Next(1, randomKey.Length + 1);
                var startOfKey = randomKey.Substring(0, length);

                var foundValues  = searchDictionary.StartsWith(startOfKey).OrderBy(value => value).ToArray();
                var actualValues = randomKeys.Where(key => key.StartsWith(startOfKey)).OrderBy(value => value).ToArray();

                CollectionAssert.AreEqual(foundValues, actualValues);
            }
        }
Exemplo n.º 21
0
        /// <summary>
        ///     Adds a local DynNode to search
        /// </summary>
        /// <param name="dynNode">A Dynamo node object</param>
        public void Add(Type t)
        {
            // get name, category, attributes (this is terribly ugly...)
            var attribs = t.GetCustomAttributes(typeof(NodeNameAttribute), false);
            var name    = "";

            if (attribs.Length > 0)
            {
                name = (attribs[0] as NodeNameAttribute).Name;
            }

            attribs = t.GetCustomAttributes(typeof(NodeCategoryAttribute), false);
            var cat = "";

            if (attribs.Length > 0)
            {
                cat = (attribs[0] as NodeCategoryAttribute).ElementCategory;
            }

            attribs = t.GetCustomAttributes(typeof(NodeSearchTagsAttribute), false);
            var tags = new List <string>();

            if (attribs.Length > 0)
            {
                tags = (attribs[0] as NodeSearchTagsAttribute).Tags;
            }

            attribs = t.GetCustomAttributes(typeof(NodeDescriptionAttribute), false);
            var description = "";

            if (attribs.Length > 0)
            {
                description = (attribs[0] as NodeDescriptionAttribute).ElementDescription;
            }

            var searchEle = new NodeSearchElement(name, description, tags);

            attribs = t.GetCustomAttributes(typeof(NodeSearchableAttribute), false);
            bool searchable = true;

            if (attribs.Length > 0)
            {
                searchable = (attribs[0] as NodeSearchableAttribute).IsSearchable;
            }

            searchEle.SetSearchable(searchable);

            // if it's a revit search element, keep track of it
            if (cat.Equals(BuiltinNodeCategories.REVIT_API))
            {
                this.RevitApiSearchElements.Add(searchEle);
                if (!IncludeRevitAPIElements)
                {
                    return;
                }
            }

            if (!string.IsNullOrEmpty(cat))
            {
                SearchDictionary.Add(searchEle, cat + "." + searchEle.Name);
            }

            TryAddCategoryAndItem(cat, searchEle);

            SearchDictionary.Add(searchEle, searchEle.Name);
            if (tags.Count > 0)
            {
                SearchDictionary.Add(searchEle, tags);
            }
            SearchDictionary.Add(searchEle, description);
        }
Exemplo n.º 22
0
 /// <summary>
 ///     Adds the Home Workspace to search.
 /// </summary>
 private void AddHomeToSearch()
 {
     SearchDictionary.Add(new WorkspaceSearchElement("Home", "Navigate to Home Workspace"), "Home");
 }
Exemplo n.º 23
0
        /// <summary>
        ///     Adds DesignScript function groups
        /// </summary>
        /// <param name="func"></param>
        public void Add(IEnumerable <FunctionGroup> functionGroups)
        {
            if (null == functionGroups)
            {
                return;
            }

            foreach (var functionGroup in functionGroups)
            {
                var functions = functionGroup.Functions.ToList();
                if (!functions.Any())
                {
                    continue;
                }

                bool isOverloaded = functions.Count > 1;

                foreach (var function in functions)
                {
                    //Don't add the functions that are not visible in library.
                    if (!function.IsVisibleInLibrary)
                    {
                        continue;
                    }

                    // For overloaded functions, only parameters are displayed
                    // for this item. E.g, for Count(), on UI it is:
                    //
                    // -> Abs
                    //      +----------------+
                    //      | dValue: double |
                    //      +----------------+
                    //      | nValue: int    |
                    //      +----------------+
                    var displayString = function.UserFriendlyName;
                    var category      = function.Category;

                    // do not add GetType method names to search
                    if (displayString.Contains("GetType"))
                    {
                        continue;
                    }

                    if (isOverloaded)
                    {
                        var args = string.Join(", ", function.Parameters.Select(p => p.ToString()));

                        if (!string.IsNullOrEmpty(args))
                        {
                            displayString = displayString + "(" + args + ")";
                        }
                    }

                    var searchElement = new DSFunctionNodeSearchElement(displayString, function);
                    searchElement.SetSearchable(true);
                    searchElement.FullCategoryName = category;

                    // Add this search eleemnt to the search view
                    TryAddCategoryAndItem(category, searchElement);

                    // function.QualifiedName is the search string for this
                    // element
                    SearchDictionary.Add(searchElement, function.QualifiedName);

                    // add all search tags
                    function.GetSearchTags().ToList().ForEach(x => SearchDictionary.Add(searchElement, x));
                }
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// Attempts to obtain the list of search results.  If it fails, it does nothing
        /// </summary>
        public void Refresh()
        {
            var pkgs = PackageManagerClientViewModel.ListAll();

            pkgs.Sort((e1, e2) => e1.Name.ToLower().CompareTo(e2.Name.ToLower()));
            LastSync = pkgs;

            SearchDictionary = new SearchDictionary<PackageManagerSearchElement>();

            foreach (var pkg in pkgs)
            {
                SearchDictionary.Add(pkg, pkg.Name);
                SearchDictionary.Add(pkg, pkg.Description);
                SearchDictionary.Add(pkg, pkg.Maintainers);
                SearchDictionary.Add(pkg, pkg.Keywords);
            }
        }