private List <string> GetPathFromDepth(List <string> p, Depth d) { List <string> newP = new List <string>(); int i = 0; switch (d) { case Depth.CategoryType: i = 1; break; case Depth.Category: i = 2; break; default: i = 1; break; } for (int k = 0; k < i; k++) { newP.Add(p[k]); } return(newP); }
/// <summary> /// Takes the given set of blacklisted parameters and uses it to update all applicable filters /// </summary> /// <param name="set"></param> /// <param name="depth"></param> public void SetBlackList(HashSet <string> set, Depth depth) { HashSet <string> oldSet; if (!this.fieldBlackList.ContainsKey(depth)) { // If the fieldblacklist doesn't have an entry to {depth}, get an empty hashset as a substitute oldSet = new HashSet <string>(); } else { // If the fieldblacklist has an entry to {depth}, get its hashset from the dictionary oldSet = this.fieldBlackList[depth]; } // addSet = set - oldSet HashSet <string> addSet = new HashSet <string>(set.Except(oldSet)); // remSet = oldSet - set HashSet <string> remSet = new HashSet <string>(oldSet.Except(set)); // Adds and removes the parameter names from the blacklist AddToFieldBlackList(addSet, depth); RemoveFromFieldBlackList(remSet, depth); // TODO: Must develop more efficient removal of ElementIds from the cache if (addSet.Count != 0) { this.cachedWhiteList.Clear(); } if (remSet.Count != 0) { this.cachedBlackList.Clear(); } }
/// <summary> /// Adds new leaves into the treeView and contructs the branches to get there by recursion /// </summary> /// <param name="elementIds"></param> /// <param name="treeNodes"></param> /// <param name="tree"></param> /// <param name="depth"></param> /// <param name="lowestDepth"></param> private void AddNodesToTree( List <ElementId> elementIds, TreeNodeCollection treeNodes, TreeStructure tree, Depth depth, Depth lowestDepth = Depth.Instance) { // If the method currently is at the lowestDepth... if (depth == lowestDepth) { // For every elementId... TreeNode node; foreach (ElementId id in elementIds) { // Get a clone of the tree's cached node node = CloneTreeNode(tree.ElementIdNodes[id]); // Add the node into the treeNode treeNodes.Add(node); // Record the elementId and node to keep track of it and for easy access this.curElementIds.Add(id); this.leafNodes.Add(id, node); } // Exit out immediately return; } Depth nextDepth; SortedDictionary <string, List <ElementId> > grouping; // Get the next depth nextDepth = GetNextDepth(depth, lowestDepth); // Get the next grouping grouping = GetNextGrouping(elementIds, tree.ElementIdNodes, depth.ToString()); // Sort the dictionary in alphebetical order grouping.OrderBy(key => key.Key); List <TreeNode> branches; foreach (KeyValuePair <string, List <ElementId> > kvp in grouping) { branches = treeNodes.Find(kvp.Key, false).ToList(); if (branches.Count == 0) { TreeNode brnch = new TreeNode(); brnch.Name = kvp.Key; brnch.Text = kvp.Key; // Put it into treeView treeNodes.Add(brnch); // Add it into branches branches.Add(brnch); } foreach (TreeNode brnch in branches) { AddNodesToTree(kvp.Value, brnch.Nodes, tree, nextDepth); } } }
/// <summary> /// Add the set of string to fieldBlackList in the corresponding entry to depth /// </summary> /// <param name="addset"></param> /// <param name="depth"></param> public void AddToFieldBlackList(HashSet <string> addset, Depth depth) { // If the set of parameter names to remove is empty, simply return if (addset.Count == 0) { return; } // If this.fieldBlackList doesn't already contain an entry of the blacklist, // add an entry with addset as its value if (!this.fieldBlackList.ContainsKey(depth)) { this.fieldBlackList.Add(depth, addset); return; } // Add the elements from addset into fieldBlackList[depth] this.fieldBlackList[depth].UnionWith(addset); }
/// <summary> /// Gets the next lowest depth of the given depth and returns Depth.Invalid if depth == lowest /// </summary> /// <param name="depth"></param> /// <param name="lowest"></param> /// <returns></returns> private Depth GetNextDepth(Depth depth, Depth lowest) { Depth next; if (depth == Depth.Invalid) { throw new ArgumentException(); } else if (depth == lowest) { next = Depth.Invalid; } else { next = (Depth)((int)depth + 1); } return(next); }
/// <summary> /// Gets the next lowest depth of the given depth and returns Depth.Invalid if depth == lowest /// </summary> /// <param name="depth"></param> /// <param name="lowest"></param> /// <returns></returns> private Depth GetNextDepth(Depth depth, Depth lowest) { Depth next; if (depth == Depth.Invalid) { // If depth is Invalid, then something is wrong, throw exception throw new ArgumentException(); } else if (depth == lowest) { // If depth is at its lowest, then set the next depth to Invalid next = Depth.Invalid; } else { // Get the depth corresponding to the number that is one greater than the current one next = (Depth)((int)depth + 1); } return(next); }
public HashSet <TreeNode> GetBranchNodes(IEnumerable <ElementId> elementIds, Depth depth) { IEnumerable <TreeNode> nodes = from ElementId id in elementIds where this.leafNodes.ContainsKey(id) select this.leafNodes[id]; IEnumerable <List <string> > paths = from TreeNode n in nodes select GetNamePath(n, new List <string>()); IEnumerable <List <string> > stubs = new HashSet <List <string> >( from List <string> subPaths in paths select GetPathFromDepth(subPaths, depth)); IEnumerable <TreeNode> branches = new List <TreeNode>( from List <string> stub in stubs select GetTreeNodeFromStub(stub)); return(new HashSet <TreeNode>(branches)); }
/// <summary> /// Remove the set of strings from fieldBlackList in the corresponding entry to depth /// </summary> /// <param name="remset"></param> /// <param name="depth"></param> public void RemoveFromFieldBlackList(HashSet <string> remset, Depth depth) { // If the set of parameter names to remove is empty, simply return if (remset.Count == 0) { return; } // If this.fieldBlackList doesn't already contain an entry of the blacklist, then return if (!this.fieldBlackList.ContainsKey(depth)) { return; } // Remove the elements from this.fieldBlackList[depth] that remset has this.fieldBlackList[depth].ExceptWith(remset); // this.fieldBlackList[depth].UnionWith(this.persistentBlacklist[depth]); // If the entry corresponding to depth is empty, then remove it completely if (this.fieldBlackList[depth].Count == 0) { this.fieldBlackList.Remove(depth); } }
private void RemoveFromTree(List <NodeData> nodes, ElementSet set, Depth depth) { Depth newDepth; Dictionary <string, List <NodeData> > grouping; // Retrieve the next depth if (depth == Depth.Invalid) { // If current depth is invalid, then throw an exception, the invalid depth indicates that something has gone wrong. throw new ArgumentException(); } else if (depth == Depth.Instance) { // current depth is the last depth, set nextDepth to depth.Invalid newDepth = Depth.Invalid; foreach (NodeData data in nodes) { set.Set.Remove(data.Id); } return; } else { // Get next depth down newDepth = (Depth)((int)depth + 1); } // Get the grouping on a paramName for all the nodes (CategoryType, Category, Family, ElementType) grouping = GetGroupingByNodeData(nodes, depth.ToString()); // For each KeyValuePair in grouping... ElementSet newSet; foreach (KeyValuePair <string, List <NodeData> > kvp in grouping) { // Get a new ElementSet corresponding to kvp.Key newSet = set.GetElementSet(kvp.Key); // If newSet doesn't exist, then add a branch and set newSet to the newly created branch if (newSet == null) { throw new NullReferenceException(); } //----- Debug ------ /* * StringBuilder sb = new StringBuilder(); * sb.AppendLine(depth.ToString()); * sb.AppendLine(kvp.Key); * foreach (NodeData d in kvp.Value) * { * sb.AppendLine(d.Id.ToString()); * } * MessageBox.Show(sb.ToString()); */ //----- Debug ------ // Recurse down RemoveFromTree(kvp.Value, newSet, newDepth); if (newSet.Set.Count == 0) { // When returning from the recursion, if the branch's contents are empty, // then remove the branch from the current ElementSet set.RemoveBranch(kvp.Key); } // When returning from the recursion, // remove the elementIds found within kvp.Value foreach (NodeData data in kvp.Value) { set.Set.Remove(data.Id); } } }
private void AddToTree(List <NodeData> nodes, ElementSet set, Depth depth) { Depth newDepth; Dictionary <string, List <NodeData> > grouping; // Retrieve the next depth if (depth == Depth.Invalid) { // If current depth is invalid, then throw an exception, the invalid depth indicates that something has gone wrong. throw new ArgumentException(); } else if (depth == Depth.Instance) { // current depth is the last depth, set nextDepth to depth.Invalid newDepth = Depth.Invalid; foreach (NodeData data in nodes) { set.Set.Add(data.Id); } return; } else { // Get next depth down newDepth = (Depth)((int)depth + 1); } // Get the grouping on a paramName for all the nodes (CategoryType, Category, Family, ElementType) grouping = GetGroupingByNodeData(nodes, depth.ToString()); // For each KeyValuePair in grouping... ElementSet newSet; foreach (KeyValuePair <string, List <NodeData> > kvp in grouping) { // Get a new ElementSet corresponding to kvp.Key newSet = set.GetElementSet(kvp.Key); // If newSet doesn't exist, then add a branch and set newSet to the newly created branch if (newSet == null) { newSet = set.AddBranch(kvp.Key); set.Name = depth.ToString(); } //----- Debug ------ /* * StringBuilder sb = new StringBuilder(); * sb.AppendLine(depth.ToString()); * sb.AppendLine(kvp.Key); * foreach (NodeData d in kvp.Value) * { * sb.AppendLine(d.Id.ToString()); * } * MessageBox.Show(sb.ToString()); */ //----- Debug ------ // Recurse down AddToTree(kvp.Value, newSet, newDepth); // When returning from the recursion, add the newSet's set // onto set.Set, as it should be recently updated set.Set.UnionWith(newSet.Set); } }
/// <summary> /// Deletes existing leaves in the treeView and removes the branches if not needed by recursion /// </summary> /// <param name="elementIds"></param> /// <param name="treeNodes"></param> /// <param name="nodeDict"></param> /// <param name="depth"></param> /// <param name="lowestDepth"></param> private void DelNodesInTree( List <ElementId> elementIds, TreeNodeCollection treeNodes, Dictionary <ElementId, TreeNode> nodeDict, Depth depth, Depth lowestDepth = Depth.Instance) { // If the method currently is at the lowestDepth... if (depth == lowestDepth) { // For every elementId... TreeNode node; foreach (ElementId id in elementIds) { // Get the node from the node mapping node = nodeDict[id]; // Remove the node from treeView treeNodes.Remove(node); // Remove the node and elementId from the records this.curElementIds.Remove(id); this.leafNodes.Remove(id); } // Exit out immediately return; } Depth nextDepth; SortedDictionary <string, List <ElementId> > grouping; // Get next depth nextDepth = GetNextDepth(depth, lowestDepth); // Get next grouping grouping = GetNextGrouping(elementIds, nodeDict, depth.ToString()); // Sort the elements in alphebetical order grouping.OrderBy(key => key.Key); List <TreeNode> branches; foreach (KeyValuePair <string, List <ElementId> > kvp in grouping) { branches = treeNodes.Find(kvp.Key, false).ToList(); if (branches.Count == 0) { foreach (ElementId id in kvp.Value) { this.curElementIds.Remove(id); this.leafNodes.Remove(id); } continue; } foreach (TreeNode brnch in branches) { DelNodesInTree(kvp.Value, brnch.Nodes, nodeDict, nextDepth); if (brnch.Nodes.Count == 0) { brnch.Remove(); } } } }