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);
            }
        }
Exemple #2
0
        /// <summary>
        /// Updates the cache by getting all relations and all taxa from Taxon service
        /// and create a TaxonRelationTree.
        /// This method is thread safe.
        /// </summary>
        /// <param name="userContext">The user context.</param>
        public static void UpdateCache(IUserContext userContext)
        {
            lock (LockingTarget)
            {
                Debug.WriteLine("Start update Cache {0}", DateTime.Now);
                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                var allTaxa = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                TaxonRelationList           allRelations   = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);
                TaxonRelationList       = allRelations;
                CachedTaxonRelationTree = TaxonRelationsTreeManager.CreateTaxonRelationsTree(
                    userContext,
                    allRelations,
                    allTaxa);
                CacheLastUpdatedTime = DateTime.Now;
                Debug.WriteLine("End update Cache {0}", DateTime.Now);
                DyntaxaLogger.WriteMessage("DyntaxaTree updated {0}", DateTime.Now);
            }
        }
        public void CreateCompleteTaxonRelationsTree()
        {
            using (ShimsContext.Create())
            {
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext = ApplicationUserContextSV;

                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                var allTaxa = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                var allRelations = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);

                var tree = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, allRelations, allTaxa, false);

                //1012265
                //1011617
                //6001191 ensam
                var    edges    = tree.GetAllChildAndParentEdges(266838);
                string graphviz = GraphvizManager.CreateGraphvizFormatRepresentation(edges);

                // find suspicious hybrids
                List <ITaxonRelationsTreeNode> invalidHybrids;


                // find root nodes
                tree.RootNodes      = new HashSet <ITaxonRelationsTreeNode>();
                tree.ValidRootNodes = new HashSet <ITaxonRelationsTreeNode>();
                foreach (var node in tree.AllTreeNodes)
                {
                    tree.RootNodes.Add(node.RootNode);

                    if (node.RootNode.Taxon.IsValid)
                    {
                        tree.ValidRootNodes.Add(node.RootNode);
                    }
                }
            }
        }
        public void SplitGraphTest()
        {
            using (ShimsContext.Create())
            {
                //Arrange
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext      = ApplicationUserContextSV;
                const int    lumpSplitTaxonId = 2755;
                //TaxonIdBefore: 233285
                //TaxonIdAfter: 2755

                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                TaxonList            allTaxa             = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                TaxonRelationList           allRelations   = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);
                TaxonRelationsTree          tree           = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, allRelations,
                                                                                                                allTaxa, false);

                var edges  = tree.GetAllChildAndParentEdges(lumpSplitTaxonId);
                var edges2 = tree.GetAllValidChildAndParentEdges(lumpSplitTaxonId);
                List <ITaxonRelationsTreeNode> sourceNodes = new List <ITaxonRelationsTreeNode> {
                    tree.GetTreeNode(lumpSplitTaxonId)
                };
                GraphVizFormat graphVizFormat = new GraphVizFormat()
                {
                    ShowLumpsAndSplits = true,
                    ShowRelationId     = false
                };

                string graphRepresentation2 = GraphvizManager.CreateGraphvizFormatRepresentation(
                    userContext,
                    tree,
                    edges2,
                    sourceNodes,
                    graphVizFormat);
                int x = 8;
            }
        }
        public void ValidateTaxonRelationsTree()
        {
            using (ShimsContext.Create())
            {
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext = ApplicationUserContextSV;

                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                var allTaxa = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                var allRelations = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);
                var tree         = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, allRelations, allTaxa, false);

                List <ITaxonRelationsTreeNode> collectiveTaxa = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> hybridTaxa     = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> artkomplexTaxa = new List <ITaxonRelationsTreeNode>();

                List <ITaxonRelationsTreeNode> validCollectiveTaxa = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> validHybridTaxa     = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> validArtkomplexTaxa = new List <ITaxonRelationsTreeNode>();

                List <ITaxonRelationsTreeNode> possiblyInvalidCollectiveTaxa = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> possiblyInvalidHybridTaxa     = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> possiblyInvalidArtkomplexTaxa = new List <ITaxonRelationsTreeNode>();

                List <ITaxonRelationsTreeNode> notYetHandledHybridTaxa = new List <ITaxonRelationsTreeNode>();


                foreach (var node in tree.AsDepthFirstNodeIterator())
                {
                    if (node.Taxon.Category.Id == 27)
                    {
                        collectiveTaxa.Add(node);

                        // Kollektivtaxon ska ha 0 eller flera barn?
                        // MainChildren = 0 && SecondaryChildren >= 0
                        if (node.ValidMainChildren == null)
                        {
                            validCollectiveTaxa.Add(node);
                        }
                        else
                        {
                            possiblyInvalidCollectiveTaxa.Add(node);
                        }
                    }
                    if (node.Taxon.Category.Id == 21)
                    {
                        hybridTaxa.Add(node);

                        // Hybrider ska ha exakt 1 primär förälder precis som alla andra taxon.
                        // När alla Hybrider har hanterats i revisioner (Mora är på gång att ta tag i det så ska den ha
                        // 1 primär relation oftast under ett släkte (minsta gemensamma nämnaren för föräldrarna)
                        // men ibland även högre upp. Tyvärr verkar det ligga någon regel i Dyntaxa som
                        // förhindrar vissa av dessa saker. För släktet Anodonta kan jag t ex inte lägga till
                        // ett artkomplex (möjligen samma för hybrider) som sekundär förälder.
                        // 2 eller flera sekundära föräldrar. Oftast till arter.

                        // Hybrider ska ha exakt 2 sekundära föräldrar.
                        // och exakt 1 primär förälder.
                        if (node.ValidMainParents != null && node.ValidMainParents.Count == 1 &&
                            node.ValidSecondaryParents != null && node.ValidSecondaryParents.Count >= 2)
                        {
                            validHybridTaxa.Add(node);
                        }
                        else
                        {
                            notYetHandledHybridTaxa.Add(node);
                            //possiblyInvalidHybridTaxa.Add(node);
                        }
                    }
                    if (node.Taxon.Category.Id == 28)
                    {
                        artkomplexTaxa.Add(node);

                        // Artkomplex ska ha 0 eller fler barn?
                        // MainChildren = 0 && SecondaryChildren >= 0
                        if (node.ValidMainChildren == null)
                        {
                            validArtkomplexTaxa.Add(node);
                        }
                        else
                        {
                            possiblyInvalidArtkomplexTaxa.Add(node);
                        }

                        if (node.ValidSecondaryChildren != null && node.ValidSecondaryChildren.Count == 1)
                        {
                            int z = 8;
                        }
                    }
                }
                int t = 8;
            }
        }