Пример #1
0
        private List <GraphNode> MergeSurplusEdge(List <GraphNode> subGraph, IntermediateRuleStructure graph)
        {
            List <GraphNode> result = new List <GraphNode>();

            foreach (var graphNode in subGraph)
            {
                List <GraphEdge> outlink = graphNode.OutLinks;
                List <GraphEdge> inlink  = graphNode.InLinks;
                Dictionary <string, List <int> > outlinkDic = new Dictionary <string, List <int> >();
                Dictionary <string, List <int> > inlinkDic  = new Dictionary <string, List <int> >();
                for (int i = Variable.VariableZero; i < outlink.Count; i++)
                {
                    string targetname = outlink[i].NodeName;
                    if (outlinkDic.ContainsKey(targetname))
                    {
                        outlinkDic[targetname].Add(i);
                    }
                    else
                    {
                        outlinkDic[targetname] = new List <int> {
                            i
                        }
                    };
                }
                for (int i = Variable.VariableZero; i < inlink.Count; i++)
                {
                    string sourcename = inlink[i].NodeName;
                    if (inlinkDic.ContainsKey(sourcename))
                    {
                        inlinkDic[sourcename].Add(i);
                    }
                    else
                    {
                        inlinkDic[sourcename] = new List <int> {
                            i
                        }
                    };
                }
                List <int> outlinkindexList = new List <int>();
                List <int> inlinkIndexlist  = new List <int>();
                foreach (var ele in outlinkDic)
                {
                    if (ele.Value.Count > Variable.VariableSingle)
                    {
                        Dictionary <string, List <int> > className2index = new Dictionary <string, List <int> >();

                        foreach (var index in ele.Value)
                        {
                            var name = GetEquClassName(outlink[index].Predicate);
                            if (className2index.ContainsKey(name))
                            {
                                className2index[name].Add(index);
                            }
                            else
                            {
                                className2index[name] = new List <int> {
                                    index
                                }
                            };
                        }
                        foreach (var e in className2index)
                        {
                            if (e.Value.Count == Variable.VariableSingle)
                            {
                                outlinkindexList.Add(e.Value[Variable.VariableZero]);
                            }
                            else
                            {
                                var flag = DetectIsMerge(outlink, e.Value, inlink);
                                if (flag)
                                {
                                    outlinkindexList.Add(e.Value[e.Value.Count - Variable.VariableSingle]);
                                }
                                else
                                {
                                    outlinkindexList.AddRange(e.Value);
                                }
                            }
                        }
                    }
                    else
                    {
                        outlinkindexList.Add(ele.Value[Variable.VariableZero]);
                    }
                }
                foreach (var ele in inlinkDic)
                {
                    if (ele.Value.Count > Variable.VariableSingle)
                    {
                        Dictionary <string, List <int> > className2index = new Dictionary <string, List <int> >();
                        foreach (var index in ele.Value)
                        {
                            var name = GetEquClassName(inlink[index].Predicate);
                            if (className2index.ContainsKey(name))
                            {
                                className2index[name].Add(index);
                            }
                            else
                            {
                                className2index[name] = new List <int> {
                                    index
                                }
                            };
                        }
                        foreach (var e in className2index)
                        {
                            if (e.Value.Count == Variable.VariableSingle)
                            {
                                inlinkIndexlist.Add(e.Value[Variable.VariableZero]);
                            }
                            else
                            {
                                var flag = DetectIsMerge(inlink, e.Value, outlink);
                                if (flag)
                                {
                                    inlinkIndexlist.Add(e.Value[e.Value.Count - Variable.VariableSingle]);
                                }
                                else
                                {
                                    inlinkIndexlist.AddRange(e.Value);
                                }
                            }
                        }
                    }
                    else
                    {
                        inlinkIndexlist.Add(ele.Value[Variable.VariableZero]);
                    }
                }
                List <GraphEdge> newOutLink = new List <GraphEdge>();
                List <GraphEdge> newInLink  = new List <GraphEdge>();
                foreach (var ele in outlinkindexList)
                {
                    newOutLink.Add(outlink[ele]);
                }
                foreach (var ele in inlinkIndexlist)
                {
                    newInLink.Add(inlink[ele]);
                }
                result.Add(new GraphNode(graphNode.Name, newOutLink, newInLink));
            }
            return(result);
        }
Пример #2
0
 public RuleStructureCombination(IntermediateRuleStructure source, TargetRuleStructure target)
 {
     _source = source;
     _target = target;
 }
Пример #3
0
        private List <GraphNode> ShrinkGraphNodeCollection(List <GraphNode> nodeList, List <KeyValuePair <string, VariableType> > qaStructure, IntermediateRuleStructure graph)
        {
            List <GraphNode> result = new List <GraphNode>();

            HashSet <string> added       = new HashSet <string>();
            HashSet <string> variableSet = new HashSet <string>();

            foreach (var ele in qaStructure)
            {
                if (ele.Value != VariableType.Unknown)
                {
                    variableSet.Add(ele.Key);
                }
            }

            List <string> keyNode = new List <string>(variableSet);
            Dictionary <string, HashSet <string> > variableRelatedList = new Dictionary <string, HashSet <string> >();
            int initCount    = keyNode.Count;
            var variableList = variableSet.ToList();

            if (variableList.Count == Variable.VariableSingle)
            {
                result.Add(GetGraphNode(nodeList, variableList[Variable.VariableZero]));
                return(MergeSurplusEdge(result, graph));
            }
            if (variableList.Count < Variable.NodeThreshold)
            {
                for (int i = Variable.VariableZero; i < variableList.Count; i++)
                {
                    for (int j = i + Variable.VariableSingle; j < variableList.Count; j++)
                    {
                        var pathSet = IsConnection(variableList[i], variableList[j], nodeList);
                        if (pathSet.Count != Variable.VariableZero)
                        {
                            foreach (var path in pathSet)
                            {
                                foreach (var ele in path)
                                {
                                    if (!keyNode.Contains(ele))
                                    {
                                        keyNode.Add(ele);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (!keyNode.Contains(variableList[i]))
                            {
                                keyNode.Add(variableList[i]);
                            }
                            if (!keyNode.Contains(variableList[j]))
                            {
                                keyNode.Add(variableList[j]);
                            }
                        }
                    }
                }
            }

            if (keyNode.Count > Variable.NodeThreshold)
            {
                var initkeyNodeCopy = new List <string>(keyNode);
                initkeyNodeCopy.RemoveRange(Variable.VariableDouble, keyNode.Count - Variable.VariableDouble);
                var tempCopy = new List <string>(keyNode);
                tempCopy.RemoveRange(Variable.VariableDouble, Variable.VariableDouble);
                string toKeep    = Variable.NullString;
                int    timeStamp = Variable.VariableZero;
                foreach (var ele in tempCopy)
                {
                    var timeSize = CalcTimeStamp(nodeList, ele, initkeyNodeCopy);
                    if (timeSize > timeStamp)
                    {
                        toKeep    = ele;
                        timeStamp = timeSize;
                    }
                }
                initkeyNodeCopy.Add(toKeep);
                keyNode = new List <string>(initkeyNodeCopy);
            }

            int           oldCount   = keyNode.Count;
            List <string> newKeyNode = new List <string>(keyNode);

            if (keyNode.Count < Variable.NodeThreshold)
            {
                newKeyNode = UpdateKeyNode(keyNode.ToList(), nodeList);
            }
            int newCount = newKeyNode.Count;

            foreach (var ele in newKeyNode)//get a subgraph only contain keynode
            {
                try
                {
                    result.Add(GetGraphNode(nodeList, ele));
                }
                catch { }
            }


            var subGraph      = DelEdge(new HashSet <string>(newKeyNode), result);
            var mergedOutlink = MergeOutlinks(subGraph, new HashSet <string>(newKeyNode));
            var tt            = MergeSurplusEdge(mergedOutlink, graph);

            if (newKeyNode.Count < Variable.NodeThreshold)
            {
                return(tt);
            }
            else
            {
                return(MSTGeneration(tt, newKeyNode, oldCount, newCount));
            }
        }