private GraphStructure BuildCurrentGraphStructure(FactUnit currentFactUnit) { var tripleList = currentFactUnit.GetFactUnit; int edgeCounter = Variable.VariableZero; HashSet <string> nodeNameSet = new HashSet <string>(); List <GraphNode> graphNodeList = new List <GraphNode>(); foreach (var triple in tripleList) { var currentSubject = triple.SubjectValue; var currentPredicate = triple.PredicateValue; var currentObject = triple.ObjectValue; int PredicateID = ++edgeCounter; #region single single if (!currentSubject.Contains(Variable.VariableAnd) && !currentObject.Contains(Variable.VariableOr))//single variable { GraphEdge outLinkEdge = new GraphEdge(currentPredicate, PredicateID, currentObject); GraphEdge inLinkEdge = new GraphEdge(currentPredicate, PredicateID, currentSubject); graphNodeList = UpdateSingleSubject(ref nodeNameSet, currentSubject, graphNodeList, outLinkEdge); graphNodeList = UpdateSingleObject(ref nodeNameSet, currentObject, graphNodeList, inLinkEdge); } #endregion else if (!currentSubject.Contains(Variable.VariableAnd) && currentObject.Contains(Variable.VariableOr)) { if (currentObject.Contains(Variable.VariableAnd)) { throw new Exception(); } if (!currentObject.Contains(Variable.VariableOr)) { throw new Exception(); } var tempObject = currentObject.Replace(Variable.VariableOr + Variable.SpaceString, Variable.NullString); string[] objectArray = tempObject.Split(new char[] { Variable.SpaceChar }, StringSplitOptions.RemoveEmptyEntries); foreach (var ele in objectArray) { var outLinkEdge = new GraphEdge(currentPredicate, PredicateID, ele.Trim()); graphNodeList = UpdateSingleSubject(ref nodeNameSet, currentSubject, graphNodeList, outLinkEdge); } var inLinkEdge = new GraphEdge(currentPredicate, PredicateID, currentSubject); foreach (var ele in objectArray) { graphNodeList = UpdateSingleObject(ref nodeNameSet, ele.Trim(), graphNodeList, inLinkEdge); } } else if (currentSubject.Contains(Variable.VariableAnd) && !currentObject.Contains(Variable.VariableOr)) { if (currentSubject.Contains(Variable.VariableOr)) { throw new Exception(); } if (!currentSubject.Contains(Variable.VariableAnd)) { throw new Exception(); } var tempSubject = currentSubject.Replace(Variable.VariableAnd + Variable.SpaceString, Variable.NullString); string[] subjectArray = tempSubject.Split(new char[] { Variable.SpaceChar }, StringSplitOptions.RemoveEmptyEntries); var outLinkEdge = new GraphEdge(currentPredicate, PredicateID, currentObject); foreach (var ele in subjectArray) { graphNodeList = UpdateSingleSubject(ref nodeNameSet, ele.Trim(), graphNodeList, outLinkEdge); } foreach (var ele in subjectArray) { var inLinkEdge = new GraphEdge(currentPredicate, PredicateID, ele.Trim()); graphNodeList = UpdateSingleObject(ref nodeNameSet, currentObject, graphNodeList, inLinkEdge); } } else if (currentSubject.Contains(Variable.VariableAnd) && currentObject.Contains(Variable.VariableOr)) { throw new Exception(); } } return(new GraphStructure(nodeNameSet, edgeCounter, graphNodeList)); }
private List <GraphNode> UpdateSingleSubject(ref HashSet <string> nodeNameSet, string currentSubject, List <GraphNode> graphNodeList, GraphEdge outLinkEdge) { if (nodeNameSet.Contains(currentSubject))//update node { var index = GetGraphNodeIndex(currentSubject, graphNodeList); var edgeList = graphNodeList[index].OutLinks; var outLinkEdgeList = new List <GraphEdge>(edgeList); outLinkEdgeList.Add(outLinkEdge); graphNodeList[index] = new GraphNode(currentSubject, outLinkEdgeList, graphNodeList[index].InLinks); return(graphNodeList); } else//add node { graphNodeList.Add(new GraphNode(currentSubject, new List <GraphEdge> { outLinkEdge }, new List <GraphEdge>())); nodeNameSet.Add(currentSubject); return(graphNodeList); } }
private List <GraphNode> UpdateSingleObject(ref HashSet <string> nodeNameSet, string currentObject, List <GraphNode> graphNodeList, GraphEdge inLinkEdge) { if (nodeNameSet.Contains(currentObject))//update { var index = GetGraphNodeIndex(currentObject, graphNodeList); var edgeList = graphNodeList[index].InLinks; var inLinkEdgeList = new List <GraphEdge>(edgeList); inLinkEdgeList.Add(inLinkEdge); graphNodeList[index] = new GraphNode(currentObject, graphNodeList[index].OutLinks, inLinkEdgeList); return(graphNodeList); } else//add { graphNodeList.Add(new GraphNode(currentObject, new List <GraphEdge>(), new List <GraphEdge> { inLinkEdge })); nodeNameSet.Add(currentObject); return(graphNodeList); } }