/* * this method is to set a rule as a fact in the workingMemory * before this method is called, nodeName should be given and look up nodeMap in NodeSet to find variableName of the node * then the variableName of the node should be passed to this method. */ public void SetFact(string nodeVariableName, FactValue value) { if (workingMemory.ContainsKey(nodeVariableName)) { FactValue tempFv = workingMemory[nodeVariableName]; if (tempFv.GetFactValueType().Equals(FactValueType.LIST)) { ((FactListValue)tempFv).AddFactValueToListValue(tempFv); } else { FactListValue flv = FactValue.Parse(new List <FactValue>()); flv.AddFactValueToListValue(tempFv); } } else { workingMemory.Add(nodeVariableName, value); } }
///* // * this method is used when a givenList does NOT exist // */ public void IterateFeedAnswers(Node targetNode, string questionName, FactValue nodeValue, NodeSet parentNodeSet, AssessmentState parentAst, Assessment ass) { if (this.iterateNodeSet == null) { Node firstIterateQuestionNode = parentNodeSet.GetNodeByNodeId(parentNodeSet.GetDependencyMatrix().GetToChildDependencyList(this.GetNodeId()).Min()); if (questionName.Equals(firstIterateQuestionNode.GetNodeName())) { this.givenListSize = Int32.Parse(FactValue.GetValueInString(nodeValue.GetFactValueType(), nodeValue)); } this.iterateNodeSet = CreateIterateNodeSet(parentNodeSet); this.iterateIE = new InferenceEngine(this.iterateNodeSet); if (this.iterateIE.GetAssessment() == null) { this.iterateIE.SetAssessment(new Assessment(this.iterateNodeSet, this.GetNodeName())); } } this.iterateIE.GetAssessment().SetNodeToBeAsked(targetNode); this.iterateIE.FeedAnswerToNode(targetNode, questionName, nodeValue, this.iterateIE.GetAssessment()); Dictionary <string, FactValue> iterateWorkingMemory = this.iterateIE.GetAssessmentState().GetWorkingMemory(); Dictionary <string, FactValue> parentWorkingMemory = parentAst.GetWorkingMemory(); TranseferFactValue(iterateWorkingMemory, parentWorkingMemory); }
public bool CanBeSelfEvaluated(Dictionary <string, FactValue> workingMemory) { bool canBeSelfEvaluated = false; if (this.iterateIE != null) { FactValue outFactValue = null; List <int> numberOfDeterminedSecondLevelNode = this.iterateIE.GetNodeSet().GetDependencyMatrix().GetToChildDependencyList(this.nodeId) .Where((i) => i != this.nodeId + 1) .Where((id) => workingMemory.TryGetValue(this.iterateIE.GetNodeSet().GetNodeIdMap()[id], out outFactValue) && outFactValue != null && FactValue.GetValueInString(outFactValue.GetFactValueType(), outFactValue) != null) .ToList(); if (this.givenListSize == numberOfDeterminedSecondLevelNode.Count && this.iterateIE.HasAllMandatoryChildAnswered(this.nodeId)) { canBeSelfEvaluated = true; } } return(canBeSelfEvaluated); }
public NodeSet CreateIterateNodeSet(NodeSet parentNodeSet) { DependencyMatrix parentDM = parentNodeSet.GetDependencyMatrix(); Dictionary <string, Node> parentNodeMap = parentNodeSet.GetNodeMap(); Dictionary <int?, string> parentNodeIdMap = parentNodeSet.GetNodeIdMap(); Dictionary <string, Node> thisNodeMap = new Dictionary <string, Node>(); Dictionary <int?, string> thisNodeIdMap = new Dictionary <int?, string>(); List <Dependency> tempDependencyList = new List <Dependency>(); NodeSet newNodeSet = new NodeSet(); thisNodeMap.Add(this.nodeName, this); thisNodeIdMap.Add(this.nodeId, this.nodeName); Enumerable.Range(1, this.givenListSize).ToList().ForEach((nth) => { parentDM.GetToChildDependencyList(this.nodeId).ForEach((item) => { if (this.GetNodeId() + 1 != item) // not first question id { Node tempChildNode = parentNodeMap[parentNodeIdMap[item]]; LineType lineType = tempChildNode.GetLineType(); Node tempNode = null; string nextNThInString = Oridinal(nth); if (lineType.Equals(LineType.VALUE_CONCLUSION)) { tempNode = new ValueConclusionLine(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), tempChildNode.GetTokens()); } else if (lineType.Equals(LineType.COMPARISON)) { tempNode = new ComparisonLine(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), tempChildNode.GetTokens()); FactValue tempNodeFv = ((ComparisonLine)tempNode).GetRHS(); if (tempNodeFv.GetFactValueType().Equals(FactValueType.STRING)) { FactValue tempFv = FactValue.Parse(nextNThInString + " " + this.GetVariableName() + " " + FactValue.GetValueInString(FactValueType.STRING, tempNodeFv)); tempNode.SetValue(tempFv); } } else if (lineType.Equals(LineType.EXPR_CONCLUSION)) { tempNode = new ExprConclusionLine(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), tempChildNode.GetTokens()); } thisNodeMap.Add(tempNode.GetNodeName(), tempNode); thisNodeIdMap.Add(tempNode.GetNodeId(), tempNode.GetNodeName()); tempDependencyList.Add(new Dependency(this, tempNode, parentDM.GetDependencyType(this.nodeId, item))); CreateIterateNodeSetAux(parentDM, parentNodeMap, parentNodeIdMap, thisNodeMap, thisNodeIdMap, tempDependencyList, item, tempNode.GetNodeId(), nextNThInString); } else // first question id { Node firstIterateQuestionNode = parentNodeSet.GetNodeByNodeId(parentNodeSet.GetDependencyMatrix().GetToChildDependencyList(this.GetNodeId()).Min()); if (!thisNodeMap.ContainsKey(firstIterateQuestionNode.GetNodeName())) { thisNodeMap.Add(firstIterateQuestionNode.GetNodeName(), firstIterateQuestionNode); thisNodeIdMap.Add(item, firstIterateQuestionNode.GetNodeName()); tempDependencyList.Add(new Dependency(this, firstIterateQuestionNode, parentDM.GetDependencyType(this.nodeId, item))); } } }); }); int numberOfRules = Node.GetStaticNodeId(); int[,] dependencyMatrix = new int[numberOfRules, numberOfRules]; tempDependencyList.ForEach(dp => { int parentId = dp.GetParentNode().GetNodeId(); int childId = dp.GetChildNode().GetNodeId(); int dpType = dp.GetDependencyType(); dependencyMatrix[parentId, childId] = dpType; }); newNodeSet.SetNodeIdMap(thisNodeIdMap); newNodeSet.SetNodeMap(thisNodeMap); newNodeSet.SetDependencyMatrix(new DependencyMatrix(dependencyMatrix)); newNodeSet.SetFactMap(parentNodeSet.GetFactMap()); newNodeSet.SetNodeSortedList(TopoSort.DfsTopoSort(thisNodeMap, thisNodeIdMap, dependencyMatrix)); // newNodeSet.getNodeSortedList().stream().forEachOrdered(item->System.out.println(item.getNodeId()+"="+item.getNodeName())); return(newNodeSet); }
public void CreateIterateNodeSetAux(DependencyMatrix parentDM, Dictionary <string, Node> parentNodeMap, Dictionary <int?, string> parentNodeIdMap, Dictionary <string, Node> thisNodeMap, Dictionary <int?, string> thisNodeIdMap, List <Dependency> tempDependencyList, int originalParentId, int modifiedParentId, string nextNThInString) { List <int> childDependencyList = parentDM.GetToChildDependencyList(originalParentId); if (childDependencyList.Count > 0) { childDependencyList.ForEach((item) => { Node tempChildNode = parentNodeMap[parentNodeIdMap[item]]; LineType lt = tempChildNode.GetLineType(); Node tempNode; thisNodeMap.TryGetValue(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), out tempNode); if (tempNode == null) { if (lt.Equals(LineType.VALUE_CONCLUSION)) { tempNode = new ValueConclusionLine(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), tempChildNode.GetTokens()); if (parentNodeMap[parentNodeIdMap[originalParentId]].GetLineType().Equals(LineType.EXPR_CONCLUSION)) { ExprConclusionLine exprTempNode = thisNodeMap[thisNodeIdMap[modifiedParentId]] as ExprConclusionLine; string replacedString = FactValue.GetValueInString(exprTempNode.GetEquation().GetFactValueType(), exprTempNode.GetEquation()).Replace(tempChildNode.GetNodeName(), nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName()); exprTempNode.SetValue(FactValue.Parse(replacedString)); exprTempNode.SetEquation(FactValue.Parse(replacedString)); } } else if (lt.Equals(LineType.COMPARISON)) { tempNode = new ComparisonLine(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), tempChildNode.GetTokens()); FactValue tempNodeFv = ((ComparisonLine)tempNode).GetRHS(); if (tempNodeFv.GetFactValueType().Equals(FactValueType.STRING)) { FactValue tempFv = FactValue.Parse(nextNThInString + " " + this.GetVariableName() + " " + tempNodeFv); tempNode.SetValue(tempFv); } } else if (lt.Equals(LineType.EXPR_CONCLUSION)) { tempNode = new ExprConclusionLine(nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName(), tempChildNode.GetTokens()); } } else { if (lt.Equals(LineType.VALUE_CONCLUSION) && parentNodeMap[parentNodeIdMap[originalParentId]].GetLineType().Equals(LineType.EXPR_CONCLUSION)) { ExprConclusionLine exprTempNode = ((ExprConclusionLine)thisNodeMap[thisNodeIdMap[modifiedParentId]]); string replacedString = FactValue.GetValueInString(exprTempNode.GetEquation().GetFactValueType(), exprTempNode.GetEquation()).Replace(tempChildNode.GetNodeName(), nextNThInString + " " + this.GetVariableName() + " " + tempChildNode.GetNodeName()); exprTempNode.SetValue(FactValue.Parse(replacedString)); exprTempNode.SetEquation(FactValue.Parse(replacedString)); } } if (!thisNodeMap.ContainsKey(tempNode.GetNodeName())) { thisNodeMap.Add(tempNode.GetNodeName(), tempNode); thisNodeIdMap.Add(tempNode.GetNodeId(), tempNode.GetNodeName()); tempDependencyList.Add(new Dependency(thisNodeMap[thisNodeIdMap[modifiedParentId]], tempNode, parentDM.GetDependencyType(originalParentId, item))); CreateIterateNodeSetAux(parentDM, parentNodeMap, parentNodeIdMap, thisNodeMap, thisNodeIdMap, tempDependencyList, item, tempNode.GetNodeId(), nextNThInString); } }); } }
public override FactValue SelfEvaluate(Dictionary <string, FactValue> workingMemory, Jint.Engine jint) { /* * Negation type can only be used for this line type * */ FactValue workingMemoryLhsValue = workingMemory.ContainsKey(this.variableName) ? workingMemory[this.variableName] : null; string rhsValueInString = FactValue.GetValueInString(this.GetRHS().GetFactValueType(), this.GetRHS()); FactValue workingMemoryRhsValue = this.GetRHS().GetFactValueType().Equals(FactValueType.STRING) ? workingMemory[rhsValueInString] : this.GetRHS(); String script = ""; /* * There will NOT be the case of that workingMemoryRhsValue is null because the node must be in following format; * - A = 12231 (int or double) * - A = Adam sandler (String) * - A = 11/11/1977 (Date) * - A = 123123dfae1421412aer(Hash) * - A = 1241414-12421312-142421312(UUID) * - A = true(Boolean) * - A = www.aiBrain.com(URL) * - A = B(another variable) */ /* * if it is about date comparison then string of 'script' needs rewriting */ if ((workingMemoryLhsValue != null && workingMemoryLhsValue.GetFactValueType().Equals(FactValueType.DATE)) || (workingMemoryRhsValue != null && workingMemoryRhsValue.GetFactValueType().Equals(FactValueType.DATE))) { Boolean returnValue; switch (this.operatorString) { case ">": returnValue = (((FactDateValue)workingMemoryLhsValue).GetValue()).CompareTo((((FactDateValue)workingMemoryRhsValue).GetValue())) > 0 ? true : false; return(FactValue.Parse(returnValue)); case ">=": returnValue = (((FactDateValue)workingMemoryLhsValue).GetValue()).CompareTo((((FactDateValue)workingMemoryRhsValue).GetValue())) >= 0 ? true : false; return(FactValue.Parse(returnValue)); case "<": returnValue = (((FactDateValue)workingMemoryLhsValue).GetValue()).CompareTo((((FactDateValue)workingMemoryRhsValue).GetValue())) < 0 ? true : false; return(FactValue.Parse(returnValue)); case "<=": returnValue = (((FactDateValue)workingMemoryLhsValue).GetValue()).CompareTo((((FactDateValue)workingMemoryRhsValue).GetValue())) <= 0 ? true : false; return(FactValue.Parse(returnValue)); } // script = "new Date("+((FactDateValue)workingMemoryLhsValue).getValue().getYear()+"/"+((FactDateValue)workingMemoryLhsValue).getValue().getMonthValue()+"/"+((FactDateValue)workingMemoryLhsValue).getValue().getDayOfMonth()+")"+operator+"new Date("+((FactDateValue)workingMemoryRhsValue).getValue().getYear()+"/"+((FactDateValue)workingMemoryRhsValue).getValue().getMonthValue()+"/"+((FactDateValue)workingMemoryRhsValue).getValue().getDayOfMonth()+");" ; } else if (workingMemoryLhsValue != null && (workingMemoryLhsValue.GetFactValueType().Equals(FactValueType.DECIMAL) || workingMemoryLhsValue.GetFactValueType().Equals(FactValueType.DOUBLE) || workingMemoryLhsValue.GetFactValueType().Equals(FactValueType.INTEGER) || workingMemoryLhsValue.GetFactValueType().Equals(FactValueType.NUMBER))) { script = FactValue.GetValueInString(workingMemoryLhsValue.GetFactValueType(), workingMemoryLhsValue) + operatorString + FactValue.GetValueInString(workingMemoryRhsValue.GetFactValueType(), workingMemoryRhsValue); } else { if (workingMemoryRhsValue != null && workingMemoryLhsValue != null) { script = "'" + FactValue.GetValueInString(workingMemoryLhsValue.GetFactValueType(), workingMemoryLhsValue) + "' " + operatorString + " '" + FactValue.GetValueInString(workingMemoryRhsValue.GetFactValueType(), workingMemoryRhsValue) + "'"; } } Boolean result; FactValue fv = null; if (workingMemoryRhsValue != null && workingMemoryLhsValue != null) { result = Convert.ToBoolean(jint.Execute(script).GetCompletionValue().ToString()); fv = FactValue.Parse(result); } return(fv); }
public override FactValue SelfEvaluate(Dictionary <string, FactValue> workingMemory, Jint.Engine nashorn) { FactValue fv = null; /* * Negation and Known type are a part of dependency * hence, only checking its variableName value against the workingMemory is necessary. * type is as follows; * 1. the rule is a plain statement * 2. the rule is a statement of 'A IS B' * 3. the rule is a statement of 'A IS IN LIST: B' * 4. the rule is a statement of 'needs(wants) A'. this is from a child node of ExprConclusionLine type */ if (!this.isPlainStatementFormat) { if (tokens.tokensList.Any(s => s.Equals("IS"))) { fv = this.value; } else if (tokens.tokensList.Any((s) => s.Equals("IS IN LIST:"))) { bool lineValue = false; string listName = FactValue.GetValueInString(this.GetFactValue().GetFactValueType(), this.GetFactValue()); if (workingMemory[listName] != null) { FactValue variableValueFromWorkingMemory = workingMemory[this.variableName]; lineValue = variableValueFromWorkingMemory != null ? ((FactListValue)workingMemory[listName]).GetValue().Any((item) => (item as FactStringValue).GetValue().Equals(FactValue.GetValueInString(variableValueFromWorkingMemory.GetFactValueType(), variableValueFromWorkingMemory))) : ((FactListValue)workingMemory[listName]).GetValue().Any((item) => (item as FactStringValue).GetValue().Equals(this.variableName)); } fv = FactValue.Parse(lineValue); } } return(fv); }
public override FactValue SelfEvaluate(Dictionary <string, FactValue> workingMemory, Jint.Engine jint) { /* * calculation can only handle int, double(long) and difference in years between two dates at the moment. * if difference in days or months is required then new 'keyword' must be introduced such as 'Diff Years', 'Diff Days', or 'Diff Months' */ string equationInString = FactValue.GetValueInString(this.equation.GetFactValueType(), this.equation); string pattern = @"[-+/*()?:;,.""](\s*)"; string datePattern = @"([0-2]?[0-9]|3[0-1])/(0?[0-9]|1[0-2])/([0-9][0-9])?[0-9][0-9]|([0-9][0-9])?[0-9][0-9]/(0?[0-9]|1[0-2])/([0-2]?[0-9]|3[0-1])"; /* * logic for this is as follows; * 1. replace all variables with actual values from 'workingMemory' * 2. find out if equation is about date (difference in years) calculation or not * 3. if it is about date then call 'java.time.LocalDate'and 'java.time.temporal.ChronoUnit' package then do the calculation * 3-1. if it is about int or double(long) then use plain Javascript * */ string script = equationInString; string tempScript = script; string result; if (Regex.IsMatch(equationInString, pattern)) { string[] tempArray = Regex.Split(equationInString, pattern); int tempArrayLength = tempArray.Length; string tempItem; for (int i = 0; i < tempArrayLength; i++) { tempItem = tempArray[i]; if (!string.IsNullOrEmpty(tempItem.Trim()) && (workingMemory.ContainsKey(tempItem.Trim()) && workingMemory[tempItem.Trim()] != null)) { FactValue tempFv = workingMemory[tempItem.Trim()]; if (tempFv.GetFactValueType().Equals(FactValueType.DATE)) { /* * below line is temporary solution. * Within next iteration it needs to be that this node should take dateFormatter for its constructor to determine which date format it needs */ string tempStr = DateTime.ParseExact(FactValue.GetValueInString(tempFv.GetFactValueType(), tempFv), dateFormatter, CultureInfo.InvariantCulture).ToString().Split(' ')[0].Trim(); tempScript = tempScript.Replace(tempItem.Trim(), tempStr); } else { tempScript = tempScript.Replace(tempItem.Trim(), FactValue.GetValueInString(workingMemory[tempItem.Trim()].GetFactValueType(), workingMemory[tempItem.Trim()]).Trim()); } } } } MatchCollection dateMatcher = Regex.Matches(tempScript, datePattern); List <DateTime> dateTimeList = new List <DateTime>(); if (dateMatcher.Count > 0) { foreach (Match match in dateMatcher) { string[] datetimeString = Regex.Split(match.Groups[0].ToString(), @"/"); int year = 0; int month = 0; int day = 0; if (Int32.TryParse(datetimeString[2], out year) && Int32.TryParse(datetimeString[1], out month) && Int32.TryParse(datetimeString[0], out day)) { dateTimeList.Add(new DateTime(year, month, day)); } } result = (dateTimeList[0].Subtract(dateTimeList[1]).TotalDays / 365.25).ToString(); } else { result = jint.Execute(tempScript).GetCompletionValue().ToString(); } FactValue returnValue = null; switch (Tokenizer.GetTokens(result).tokensString) { case "No": int intResult = 0; Int32.TryParse(result, out intResult); returnValue = FactValue.Parse(intResult); break; case "De": returnValue = FactValue.Parse(Convert.ToDouble(result)); break; //there is no function for outcome to be a date at the moment E.g.The determination IS CALC(enrollment date + 5 days) //case "Da": default: returnValue = FactValue.Parse(result); break; } return(returnValue); }