private static Rule TryRuleBase(ExpressionBase RuleBase, Fact Goal) { return((Rule)RuleBase.FindExpression((rule) => TryThenExpression((Fact)((Rule)rule).ThenExpression, Goal))); }
private static Fact TryFactBase(ExpressionBase FactBase, Fact Goal) { return((Fact)FactBase.FindExpression((fact) => fact.IsEqualTo(Goal))); }
private static bool TryThenExpression(Fact ThenExpression, Fact Goal) { return(ThenExpression.IsEqualTo(Goal)); }
private static MyTreeNode SearchIntoDepth(KnowledgeBase KB, Fact Goal) { if (++StepCount > MaxStepCount) { StepCount--; return(null); } MyTreeNode Res = new MyTreeNode(Goal); if (Goal.LeftArg.Type == ExpressionType.Fact) { if (Goal.RightArg.Type == ExpressionType.Fact) // Если искомое выражение составное { if (Goal.Name == Library.StandartOperations[0].ToString()) { MyTreeNode TR = SearchIntoDepth(KB, (Fact)Goal.RightArg); MyTreeNode TL = TR != null?SearchIntoDepth(KB, (Fact)Goal.LeftArg) : null; StepCount--; return(TL != null ? new MyTreeNode(Goal, TR, TL) : null); } if (Goal.Name == Library.StandartOperations[1].ToString()) { MyTreeNode TR = SearchIntoDepth(KB, (Fact)Goal.RightArg); MyTreeNode TL = SearchIntoDepth(KB, (Fact)Goal.LeftArg); StepCount--; return(((TL != null) || (TR != null)) ? new MyTreeNode(Goal, TR, TL) : null); } Res.Left = SearchIntoDepth(KB, (Fact)Goal.LeftArg); Res.Right = SearchIntoDepth(KB, (Fact)Goal.RightArg); } else { throw new Exception("Невозможно осуществить операцию \"" + Goal.Name + "\" между фактом и не-фактом"); } } else if (Goal.RightArg.Type == ExpressionType.Fact) { throw new Exception("Невозможно осуществить операцию \"" + Goal.Name + "\" между фактом и не-фактом"); } else { // Если искомое выражение - простой факт Res.SelfWay = new MyTreeNode(TryFactBase(KB.BaseOfFacts, Goal)); if (Res.SelfWay.Value == null) { foreach (EBCell cell in KB.BaseOfRules) { Rule rul = (Rule)cell.Value; if (rul.ThenExpression.IsEqualTo(Goal)) { Res.SelfWay = SearchIntoDepth(KB, (Fact)rul.IfExpression); if (Res.SelfWay == null) { continue; } } } } } StepCount--; if (Res.SelfWay == null) { if ((Res.Left == null) && (Res.Right == null)) { return(null); } else { ; } } else if (Res.SelfWay.Value == null) { return(null); } return(Res); }