public static DirectedGraph ConstructDPBGraph(this LeafNodeDictionaryAnalysis input, DataPointMapperResult output)
        {
            Func <graphWrapNode <LeafNodeDictionaryEntry>, Double> nodeW = x =>
            {
                foreach (var b in output.MapBlocks)
                {
                    if (x.path.StartsWith(b.BlockXPathRoot))
                    {
                        return(1);
                    }
                }
                return(0.5);
            };

            StructureGraphConverter converter = new StructureGraphConverter()
            {
                CategoryID = x => "Default",
                TypeID     = x => 1,
                NodeWeight = nodeW,
                LinkWeight = (x, y) => 1
            };


            var c_dmgl = converter.Convert(input.CompleteGraph, 500);

            return(c_dmgl);
        }
        public static DirectedGraph ConstructGraph(this LeafNodeDictionaryAnalysis analysis, DataPointMapperResult output)
        {
            //graphWrapNode<LeafNodeDictionaryEntry> dynamicContent = graphTools.BuildGraphFromItems<LeafNodeDictionaryEntry, graphWrapNode<LeafNodeDictionaryEntry>>(analysis.DynamicContent.items, new Func<LeafNodeDictionaryEntry, string>(x => x.XPath), "/");

            //graphWrapNode<LeafNodeDictionaryEntry> staticContent = graphTools.BuildGraphFromItems<LeafNodeDictionaryEntry, graphWrapNode<LeafNodeDictionaryEntry>>(analysis.StaticContent.items, new Func<LeafNodeDictionaryEntry, string>(x => x.XPath), "/");



            Dictionary <IGraphNode, Double> Weight = new Dictionary <IGraphNode, double>();


            foreach (graphWrapNode <LeafNodeDictionaryEntry> leaf in analysis.CompleteGraph.getAllLeafs())
            {
                if (leaf.item != null)
                {
                    if (leaf.item.Category.HasFlag(NodeInTemplateRole.Dynamic))
                    {
                        var nd = leaf.Add("$DYNAMIC$");
                        Weight[leaf] = 1;
                    }
                    else if (leaf.item.Category.HasFlag(NodeInTemplateRole.Static))
                    {
                        Weight[leaf] = 0.8;
                        if (!leaf.item.Content.isNullOrEmpty())
                        {
                            var nd = leaf.Add(leaf.item.Content.trimToLimit(50, true));
                        }
                    }
                }
            }


            Func <graphWrapNode <LeafNodeDictionaryEntry>, Int32> typeID = x =>
            {
                if (x.item == null)
                {
                    return(0);
                }
                return(1);
            };

            Func <graphWrapNode <LeafNodeDictionaryEntry>, Double> nodeW = x =>
            {
                if (x.item == null)
                {
                    return(0.2);
                }
                if (Weight.ContainsKey(x))
                {
                    return(Weight[x]);
                }
                return(0.3);
                //return (1 - (((Double)0.8 / ((Double)x.Count() + 1))));
            };

            Func <graphWrapNode <LeafNodeDictionaryEntry>, String> categoryID = x =>
            {
                foreach (var b in output.MapBlocks)
                {
                    if (x == null)
                    {
                        return("Null");
                    }
                    if (x.item == null)
                    {
                        return("Null");
                    }
                    if (x.item.XPath.StartsWith(b.BlockXPathRoot))
                    {
                        String dpr = x.item.XPath.removeStartsWith(b.BlockXPathRoot);

                        foreach (var dp in b.DataPoints)
                        {
                            if (dpr == dp.DataPointXPathRoot + dp.LabelXPathRelative)
                            {
                                return("Label");
                            }

                            if (dpr == dp.DataPointXPathRoot + dp.DataXPathRelative)
                            {
                                return("Data");
                            }
                        }

                        return(b.name);
                    }
                }

                if (x.item != null)
                {
                    return(x.item.Category.ToString());
                }
                return("Structure");
            };

            List <String> Categories = new List <string>();

            foreach (graphWrapNode <LeafNodeDictionaryEntry> g in analysis.CompleteGraph.getAllChildren())
            {
                Categories.AddUnique(categoryID(g));
            }


            StructureGraphConverter converter = new StructureGraphConverter()
            {
                CategoryID = categoryID,
                TypeID     = typeID,
                NodeWeight = nodeW,
                LinkWeight = (x, y) => 1
            };



            var c_dmgl = converter.Convert(analysis.CompleteGraph, 500);



            return(c_dmgl);
        }
        /// <summary>
        /// Gets the data point pairs: Junction method
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="ChildrenCountTrigger">The children count trigger.</param>
        /// <returns></returns>
        public DataPointMapperResult GetDataPointPairs(LeafNodeDictionaryAnalysis input)
        {
            List <DataPointMapEntry> dpList = new List <DataPointMapEntry>();

            var allLeafs = input.CompleteGraph.getAllLeafs();


            List <String> dpRoots = new List <string>();

            foreach (graphWrapNode <LeafNodeDictionaryEntry> g in allLeafs)
            {
                var inputLeaf = input.Nodes.GetEntry(g.path);

                if (g.parent != null)
                {
                    if (inputLeaf.Category.HasFlag(NodeInTemplateRole.Dynamic))
                    {
                        graphWrapNode <LeafNodeDictionaryEntry> head = g;

                        Int32 pC = head.Count();

                        while (pC < ChildrenCountTrigger)
                        {
                            if (head.parent != null)
                            {
                                head = head.parent as graphWrapNode <LeafNodeDictionaryEntry>;
                                if (head == null)
                                {
                                    break;
                                }
                                else
                                {
                                    pC = head.parent.Count();
                                }
                            }
                        }
                        if (head != null)
                        {
                            if (head.path.isNullOrEmpty())
                            {
                                if (!dpRoots.Contains(head.path))
                                {
                                    dpRoots.Add(head.path);
                                }
                            }
                        }
                    }
                    else
                    {
                    }
                }
            }



            foreach (String root in dpRoots)
            {
                var dpItems = input.Nodes.items.Where(x => x.XPath.StartsWith(root)).ToList();

                List <DataPointMapEntry> dp_tmp = MergeEntriesToDataPoints(dpItems, input);

                if (dp_tmp.Count > 1 && flags.HasFlag(DataPointMapBlockDetectionFlags.AllowMultiColumnDataPoints))
                {
                    DataPointMapEntry parent_dp = new DataPointMapEntry()
                    {
                        DataPointXPathRoot = root,
                        LabelXPathRelative = "",
                        DataXPathRelative  = "",
                        Properties         = dp_tmp
                    };
                    foreach (var d in dp_tmp)
                    {
                        d.DataPointXPathRoot = "";
                    }

                    dpList.Add(parent_dp);
                }
                else
                {
                    dpList.AddRange(dp_tmp);
                }
            }



            List <graphWrapNode <LeafNodeDictionaryEntry> > dpRootGraphNodes = new List <graphWrapNode <LeafNodeDictionaryEntry> >();

            foreach (String root in dpRoots)
            {
                var cp = input.CompleteGraph.GetChildAtPath(root);
                if (cp != null)
                {
                    dpRootGraphNodes.AddUnique(cp);
                }
            }

            DataPointMapperResult output = new DataPointMapperResult();


            List <String> BlockRoots = new List <string>();

            foreach (graphWrapNode <LeafNodeDictionaryEntry> g in dpRootGraphNodes)
            {
                if (g.parent != null)
                {
                    graphWrapNode <LeafNodeDictionaryEntry> head = g;

                    Int32 pC = head.Count();

                    while (pC < JunctionSizeMin)
                    {
                        if (head.level > 1)
                        {
                            head = head.parent as graphWrapNode <LeafNodeDictionaryEntry>;
                            if (head.parent == null)
                            {
                                break;
                            }
                            pC = head.parent.Count();
                        }
                    }

                    if (head != null)
                    {
                        if (!head.path.isNullOrEmpty())
                        {
                            if (!BlockRoots.Contains(head.path))
                            {
                                BlockRoots.Add(head.path);
                            }
                        }
                    }
                }
            }


            if (!BlockRoots.Any())
            {
                return(output);
            }

            if (flags.HasFlag(DataPointMapBlockDetectionFlags.maximizeBlockSize))
            {
                BlockRoots = BlockRoots.OrderBy(x => x.Length).ToList();
            }
            else if (flags.HasFlag(DataPointMapBlockDetectionFlags.maximizeDataRelatness))
            {
                BlockRoots = BlockRoots.OrderByDescending(x => x.Length).ToList();
            }



            foreach (String blockRoot in BlockRoots)
            {
                var b = new DataPointMapBlock(blockRoot);

                var dpl = dpList.ToList().Where(x => x.DataPointXPathRoot.StartsWith(blockRoot));

                foreach (DataPointMapEntry e in dpl)
                {
                    dpList.Remove(e);
                    b.DataPoints.Add(e);
                }
                if (b.DataPoints.Count > 0)
                {
                    output.MapBlocks.Add(b);
                }
            }

            //if (flags.HasFlag(DataPointMapBlockDetectionFlags.BreakByDimensions))
            //{
            //    BreakBlocksByRecordDimensions(output.MapBlocks);

            //}

            return(output);
        }
        /// <summary>
        /// Merges the entries to data points.
        /// </summary>
        /// <param name="entries">The entries.</param>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        internal List <DataPointMapEntry> MergeEntriesToDataPoints(List <LeafNodeDictionaryEntry> entries, LeafNodeDictionaryAnalysis input)
        {
            List <DataPointMapEntry> dpList = new List <DataPointMapEntry>();

            List <DataPointMapEntry> dp_tmp = new List <DataPointMapEntry>();
            DataPointMapEntry        dp     = new DataPointMapEntry()
            {
            };

            dp_tmp.Add(dp);



            foreach (LeafNodeDictionaryEntry leaf in entries)
            {
                Boolean wasSet = false;

                var inputLeaf = input.Nodes.GetEntry(leaf.XPath);

                for (int i = 0; i < dp_tmp.Count; i++)
                {
                    if (dp_tmp[i].SetLeaf(leaf, inputLeaf.Category.HasFlag(NodeInTemplateRole.Dynamic)))
                    {
                        ;
                    }
                    {
                        wasSet = true;
                        break;
                    }
                }
                if (!wasSet)
                {
                    var dpn = new DataPointMapEntry()
                    {
                    };
                    dpn.SetLeaf(leaf, inputLeaf.Category.HasFlag(NodeInTemplateRole.Dynamic));
                    dp_tmp.Add(dpn);
                }
            }
            List <DataPointMapEntry> post_deploy = new List <DataPointMapEntry>();
            String root = "";

            foreach (var dpn in dp_tmp)
            {
                if (dpn.IsSet)
                {
                    dpList.Add(dpn);
                    root = dpn.DataPointXPathRoot;
                }
                else
                {
                    if (dp_tmp.Count > 1)
                    {
                        if (flags.HasFlag(DataPointMapBlockDetectionFlags.AllowAsimetricMultiColumnDataPoints))
                        {
                            post_deploy.Add(dpn);
                        }
                    }
                }
            }

            foreach (var p in post_deploy)
            {
                if (p.DataPointXPathRoot.isNullOrEmpty())
                {
                    p.DataPointXPathRoot = root;
                }
            }

            return(dpList);
        }