public void GetAllGuldmosslavParentsHierarchicalTest() { using (ShimsContext.Create()) { //Arrange const int GuldmosslavTaxonId = 228321; LoginApplicationUserAndSetSessionVariables(); SetSwedishLanguage(); IUserContext userContext = ApplicationUserContextSV; TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria(); searchCriteria.Scope = TaxonRelationSearchScope.AllParentRelations; searchCriteria.Taxa = new TaxonList { CoreData.TaxonManager.GetTaxon(userContext, GuldmosslavTaxonId) }; TaxonRelationList guldmosslavAllParentRelations = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria); TaxonRelationsTree tree = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, guldmosslavAllParentRelations, searchCriteria.Taxa); ITaxonRelationsTreeNode node = tree.GetTreeNode(GuldmosslavTaxonId); //Act List <ITaxonRelationsTreeEdge> parentEdges = node.GetAllValidParentEdgesTopToBottom(true); //Assert Assert.AreEqual(0, parentEdges.First().Parent.Taxon.Category.Id); // Assert first item is Biota Assert.AreEqual(GuldmosslavTaxonId, parentEdges.Last().Child.Taxon.Id); // Assert last item is guldmosslav // When getting guldmosslav taxonrelations a non valid parent relation is included. // which should be removed when getting all parents hierarchical using the tree. Assert.AreNotEqual(guldmosslavAllParentRelations.Count, parentEdges.Count); } }
/// <summary> /// Gets all valid child and parent edges. /// Includes both main and secondary parent/children. /// </summary> /// <param name="treeNode">The tree node.</param> /// <returns>A set with all child and parent edges.</returns> public HashSet <ITaxonRelationsTreeEdge> GetAllValidChildAndParentEdges(ITaxonRelationsTreeNode treeNode) { return(GetAllValidChildAndParentEdges(new List <ITaxonRelationsTreeNode>() { treeNode })); }
/// <summary> /// Returns a reversed breadth first parent edge iterator. /// </summary> /// <param name="treeNode">The tree node.</param> /// <param name="treeIterationMode">The tree iteration mode.</param> /// <returns>A reversed breadth first parent edge iterator.</returns> public static IEnumerable <ITaxonRelationsTreeEdge> AsTopDownBreadthFirstParentEdgeIterator( this ITaxonRelationsTreeNode treeNode, TaxonRelationsTreeParentsIterationMode treeIterationMode) { return(AsTopDownBreadthFirstParentEdgeIterator(new List <ITaxonRelationsTreeNode> { treeNode }, treeIterationMode)); }
/// <summary> /// Returns a breadth first child edge iterator. /// </summary> /// <param name="treeNode">The tree node.</param> /// <param name="treeIterationMode">The tree iteration mode.</param> /// <returns>A breadth first child edge iterator.</returns> public static IEnumerable <ITaxonRelationsTreeEdge> AsBreadthFirstChildEdgeIterator( this ITaxonRelationsTreeNode treeNode, TaxonRelationsTreeChildrenIterationMode treeIterationMode) { return(AsBreadthFirstChildEdgeIterator(new List <ITaxonRelationsTreeNode> { treeNode }, treeIterationMode)); }
/// <summary> /// Creates an iterator that traverses a tree nodes parents bottom first breadth first. /// </summary> public static IEnumerable <ITaxonRelationsTreeNode> AsBottomFirstBreadthFirstParentNodeIterator( this ITaxonRelationsTreeNode treeNode, TaxonRelationsTreeParentsIterationMode treeIterationMode, bool returnStartNode = true) { return(AsBottomFirstBreadthFirstParentNodeIterator(new List <ITaxonRelationsTreeNode> { treeNode }, treeIterationMode, returnStartNode)); }
/// <summary> /// Creates an iterator that traverses a tree nodes parents bottom first breadth first. /// </summary> public static IEnumerable <ITaxonRelationsTreeNode> AsBottomFirstBreadthFirstParentNodeIterator( this ICollection <ITaxonRelationsTreeNode> treeNodes, TaxonRelationsTreeParentsIterationMode treeIterationMode, bool returnStartNodes = true) { HashSet <ITaxonRelationsTreeNode> visitedNodes = new HashSet <ITaxonRelationsTreeNode>(); Queue <ITaxonRelationsTreeNode> queue = new Queue <ITaxonRelationsTreeNode>(); foreach (var treeNode in treeNodes) { queue.Enqueue(treeNode); } while (queue.Any()) { ITaxonRelationsTreeNode current = queue.Dequeue(); if (current != null) { if (current.ValidMainParents != null) { for (int i = current.ValidMainParents.Count - 1; i >= 0; i--) { queue.Enqueue(current.ValidMainParents[i].Parent); } } if (treeIterationMode == TaxonRelationsTreeParentsIterationMode.BothValidMainAndSecondaryParents) { if (current.ValidSecondaryParents != null) { for (int i = current.ValidSecondaryParents.Count - 1; i >= 0; i--) { queue.Enqueue(current.ValidSecondaryParents[i].Parent); } } } // Avoid cycles. Just return not visited nodes. if (!visitedNodes.Contains(current)) { visitedNodes.Add(current); if (returnStartNodes || !treeNodes.Any(x => Equals(x, current))) { yield return(current); } } } } }
/// <summary> /// Gets all valid child and parent edges. /// Includes both main and secondary parent/children. /// </summary> /// <param name="treeNode">The tree node.</param> /// <param name="treeIterationMode">The tree iteration mode.</param> /// <param name="onlyValid">if set to <c>true</c> only valid relations are included.</param> /// <param name="includeChildrenSecondaryParents">if set to <c>true</c> secondary parents to children are included.</param> /// <returns> /// A set with all child and parent edges. /// </returns> public HashSet <ITaxonRelationsTreeEdge> GetAllEdges( ITaxonRelationsTreeNode treeNode, TaxonRelationsTreeIterationMode treeIterationMode, bool onlyValid, bool includeChildrenSecondaryParents = true) { return(GetAllEdges( new List <ITaxonRelationsTreeNode>() { treeNode }, treeIterationMode, onlyValid, includeChildrenSecondaryParents)); }
/// <summary> /// Gets the node label. /// </summary> /// <param name="node">The node.</param> /// <returns>A string representation for the node.</returns> private static string GetNodeLabel(ITaxonRelationsTreeNode node) { return(GetNodeLabel(node.Taxon)); //string label = string.Format( // "{0}\\n{1}\\n{2}", // node.Taxon.ScientificName.Replace("/", ""), // node.Taxon.Id, // node.Taxon.Category.Name); //if (!node.Taxon.Category.IsTaxonomic) //{ // label += "\\nTaxonomic: False"; //} //if (!node.Taxon.IsValid) //{ // label += "\\nValid: False"; //} //return label; }
public void CreateBiotaTaxonRelationsParentsTreeTest() { using (ShimsContext.Create()) { //Arrange const int BiotaTaxonId = 0; LoginApplicationUserAndSetSessionVariables(); SetSwedishLanguage(); IUserContext userContext = ApplicationUserContextSV; ITaxon taxon = CoreData.TaxonManager.GetTaxon(userContext, BiotaTaxonId); TaxonRelationsTree tree = TaxonRelationsTreeManager.CreateTaxonRelationsParentsTree(userContext, taxon); ITaxonRelationsTreeNode node = tree.GetTreeNode(BiotaTaxonId); // Act List <ITaxonRelationsTreeEdge> allValidParentEdgesTopToBottom = node.GetAllValidParentEdgesTopToBottom(true); // Assert Assert.IsNotNull(allValidParentEdgesTopToBottom); Assert.AreEqual(0, allValidParentEdgesTopToBottom.Count); } }
/// <summary> /// Creates an iterator that traverses a list of tree nodes depth first. /// </summary> public static IEnumerable <ITaxonRelationsTreeNode> AsDepthFirstNodeIterator(this ICollection <ITaxonRelationsTreeNode> treeNodes) { HashSet <ITaxonRelationsTreeNode> visitedNodes = new HashSet <ITaxonRelationsTreeNode>(); Stack <ITaxonRelationsTreeNode> stack = new Stack <ITaxonRelationsTreeNode>(); foreach (var treeNode in treeNodes) { stack.Push(treeNode); } while (stack.Any()) { ITaxonRelationsTreeNode current = stack.Pop(); if (current != null) { if (current.ValidMainChildren != null) { for (int i = current.ValidMainChildren.Count - 1; i >= 0; i--) { stack.Push(current.ValidMainChildren[i].Child); } } if (current.ValidSecondaryChildren != null) { for (int i = current.ValidSecondaryChildren.Count - 1; i >= 0; i--) { stack.Push(current.ValidSecondaryChildren[i].Child); } } // Avoid cycles. Just return not visited nodes. if (!visitedNodes.Contains(current)) { visitedNodes.Add(current); yield return(current); } } } }
/// <summary> /// Creates a new taxon relation tree edge. /// </summary> /// <param name="parent">The parent node.</param> /// <param name="child">The child node.</param> /// <param name="taxonRelation">The taxon relation.</param> public TaxonRelationsTreeEdge(ITaxonRelationsTreeNode parent, ITaxonRelationsTreeNode child, ITaxonRelation taxonRelation) { Parent = parent; Child = child; TaxonRelation = taxonRelation; }
/// <summary> /// Determines whether the specified <see cref="TaxonRelationsTreeNode" />, is equal to this instance. /// </summary> /// <param name="other">The <see cref="TaxonRelationsTreeNode" /> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="TaxonRelationsTreeNode" /> is equal to this instance; otherwise, <c>false</c>. /// </returns> protected bool Equals(ITaxonRelationsTreeNode other) { return(Equals(Taxon.Id, other.Taxon.Id)); }
/// <summary> /// Tries to get tree node. /// </summary> /// <param name="taxonId">The taxon identifier.</param> /// <param name="treeNode">The tree node.</param> /// <returns>True if the node was found; otherwise false.</returns> public bool TryGetTreeNode(int taxonId, out ITaxonRelationsTreeNode treeNode) { return(TreeNodeDictionary.TryGetValue(taxonId, out treeNode)); }
/// <summary> /// Creates an iterator that traverses a tree nodes children depth first. /// </summary> public static IEnumerable <ITaxonRelationsTreeNode> AsDepthFirstNodeIterator(this ITaxonRelationsTreeNode treeNode) { return(AsDepthFirstNodeIterator(new List <ITaxonRelationsTreeNode> { treeNode })); }