public List <RuleSetUnit> StoreRuleSet(List <List <RuleStructure> > ruleSet, List <RuleSetUnit> ruleCollection) { List <RuleSetUnit> result = new List <RuleSetUnit>(ruleCollection); foreach (var currentSession in ruleSet) { foreach (var currentRule in currentSession) { var currentQAStructure = currentRule.QAStructure; var tripleList = currentRule.Triple; FactUnit currentFact = new FactUnit(tripleList); var ruleNameIndex = RuleNameMatching(currentQAStructure, result); if (ruleNameIndex == Variable.NegInit)//the rule name appear first time { RuleStorage currentRuleStorage = new RuleStorage(tripleList.Count, new List <FactUnit> { currentFact }); RuleSetUnit resultObj = new RuleSetUnit(currentQAStructure, new List <RuleStorage> { currentRuleStorage }, currentRule.QuestionLength); result.Add(resultObj); } else { var factIndex = CurrentFactsMatching(currentFact, result, ruleNameIndex); var currentRuleSetUnit = result[ruleNameIndex]; if (factIndex == Variable.NegInit) { RuleStorage currentRuleStorage = new RuleStorage(tripleList.Count, new List <FactUnit> { currentFact }); var factExpressionSet = currentRuleSetUnit.FactExpressionSet; factExpressionSet.AddRange(new List <RuleStorage> { currentRuleStorage }); result[ruleNameIndex] = new RuleSetUnit(currentQAStructure, factExpressionSet, currentRule.QuestionLength); } else { var currentRuleStorage = currentRuleSetUnit.FactExpressionSet[factIndex]; var currentFactUnitList = currentRuleStorage.FactsCount2Facts; var flag = FactUnitMatching(currentFact, currentFactUnitList); if (!flag) { List <FactUnit> temp = new List <FactUnit>(currentFactUnitList); temp.Add(currentFact); RuleStorage tempRule = new RuleStorage(currentRuleStorage.FactCount, temp); var tt = result[ruleNameIndex].FactExpressionSet; List <RuleStorage> rr = new List <RuleStorage>(tt); rr[factIndex] = tempRule; result[ruleNameIndex] = new RuleSetUnit(currentQAStructure, rr, currentRule.QuestionLength); } } } } } return(result); }
private bool FactUnitMatching(FactUnit currentFactUnit, List <FactUnit> factUnitList) { foreach (var unit in factUnitList) { if (unit.IsEqual(currentFactUnit)) { return(true); } } return(false); }
private int CurrentFactsMatching(FactUnit currentFact, List <RuleSetUnit> result, int nameIndex) { var currentUnit = result[nameIndex]; var ruleStorageList = currentUnit.FactExpressionSet; int factIndex = Variable.NegInit; for (int i = Variable.VariableZero; i < ruleStorageList.Count; i++) { if (ruleStorageList[i].FactCount == currentFact.GetFactUnit.Count) { factIndex = i; break; } } return(factIndex); }
public bool IsEqual(FactUnit factUnitObj) { var tripleTermList = factUnitObj.GetFactUnit; if (_factUnit.Count != tripleTermList.Count) { return(false); } for (int i = Variable.VariableZero; i < _factUnit.Count; i++) { if (tripleTermList[i].SubjectValue != _factUnit[i].SubjectValue || tripleTermList[i].PredicateValue != _factUnit[i].PredicateValue || tripleTermList[i].ObjectValue != _factUnit[i].ObjectValue) { return(false); } } return(true); }
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)); }