private void WalkGoal(KnowledgeBase kb, KnowledgeBaseRule rule, object goal) { goal = Term.Deref(goal); var atom = goal as Symbol; if (atom != null) { var p = new PredicateIndicator(atom, 0); if (PrologPrimitives.IsDefined(p)) { return; } var predicate = kb.CheckForPredicateInfo(p); if (predicate == null) { rule.PrintWarning("undefined predicate {0}", p); } else { MarkReferenced(predicate); } } else { var s = goal as Structure; if (s != null) { WalkGoal(kb, rule, s); } else if (!(goal is LogicVariable) && !(goal is bool)) { rule.PrintWarning("malformed goal: {0}", goal); } } }
private void WalkRule(KnowledgeBase kb, KnowledgeBaseRule rule) { foreach (Structure goal in rule.BodyGoals) { WalkGoal(kb, rule, goal); } }
private void MarkDefined(PredicateInfo predicate, KnowledgeBaseRule rule) { CheckerInfo predicateCheckerInfo = PredicateCheckerInfo(predicate); if (predicateCheckerInfo.DefiningRule == null) { predicateCheckerInfo.DefiningRule = rule; } }
/// <summary> /// Adds a KnowledgeBaseRule to the predicate. /// NOT THREADSAFE! /// </summary> /// <param name="assertion">The rule to add</param> /// <param name="atEnd">If true, adds to be beginning, else the end.</param> public void Assert(KnowledgeBaseRule assertion, bool atEnd) { var entries = GetEntriesListForUpdate(); if (atEnd) { entries.Add(assertion); } else { entries.Insert(0, assertion); } }
void ReallyCheck() { WalkKB(KnowledgeBase.Global); foreach (var component in UnityEngine.Object.FindObjectsOfType <KB>()) { WalkKB(component.KnowledgeBase); } foreach (var pair in checkerInfoTable) { var checkerInfo = pair.Value; if (!checkerInfo.Referenced) { KnowledgeBaseRule rule = checkerInfo.DefiningRule; PredicateInfo global = KnowledgeBase.Global.CheckForPredicateInfo(new PredicateIndicator(rule.HeadFunctor, rule.HeadArity)); if (global == null || !global.External) { rule.PrintWarning("{0}/{1} is never used.", rule.HeadFunctor, rule.HeadArity); } } } }
private void WalkGoal(KnowledgeBase kb, KnowledgeBaseRule rule, object goal) { goal = Term.Deref(goal); var atom = goal as Symbol; if (atom != null) { var p = new PredicateIndicator(atom, 0); if (PrologPrimitives.IsDefined(p)) return; var predicate = kb.CheckForPredicateInfo(p); if (predicate == null) rule.PrintWarning("undefined predicate {0}", p); else MarkReferenced(predicate); } else { var s = goal as Structure; if (s != null) WalkGoal(kb, rule, s); else if (!(goal is LogicVariable) && !(goal is bool)) rule.PrintWarning("malformed goal: {0}", goal); } }
/// <summary> /// Adds a KnowledgeBaseRule to the predicate. /// NOT THREADSAFE! /// </summary> /// <param name="assertion">The rule to add</param> /// <param name="atEnd">If true, adds to be beginning, else the end.</param> public void Assert(KnowledgeBaseRule assertion, bool atEnd) { var entries = GetEntriesListForUpdate(); if (atEnd) entries.Add(assertion); else entries.Insert(0, assertion); }
private void MarkDefined(PredicateInfo predicate, KnowledgeBaseRule rule) { CheckerInfo predicateCheckerInfo = PredicateCheckerInfo(predicate); if (predicateCheckerInfo.DefiningRule == null) predicateCheckerInfo.DefiningRule = rule; }
private void WarnUndefined(KnowledgeBaseRule rule,Symbol functor,int arity) { rule.PrintWarning("{0}/{1} undefined", functor, arity); }
private void WalkRule(KnowledgeBase kb, KnowledgeBaseRule rule) { foreach (Structure goal in rule.BodyGoals) WalkGoal(kb, rule, goal); }
private void WalkGoal(KnowledgeBase kb, KnowledgeBaseRule rule, Structure goal) { var predicateIndicator = goal.PredicateIndicator; Symbol functor = goal.Functor; int arity = goal.Arity; switch (functor.Name) { case "once": case "check": case "randomize": case "not": case "\\+": if (arity == 1) { WalkGoal(kb, rule, goal.Argument(0)); } else WarnUndefined(rule, functor, arity); break; case ",": case ";": case "->": if (arity == 2) { WalkGoal(kb, rule, goal.Argument(0)); WalkGoal(kb, rule, goal.Argument(1)); } else WarnUndefined(rule, functor, arity); break; case "call": case "maplist": if (arity < 1) WarnUndefined(rule, functor, arity); else { object goalToCall = goal.Argument(0); var goalToCallAsStructure = goalToCall as Structure; if (goalToCallAsStructure != null) { var newArgs = new object[arity - 1 + goalToCallAsStructure.Arity]; goalToCallAsStructure.Arguments.CopyTo(newArgs, 0); WalkGoal(kb, rule, new Structure(goalToCallAsStructure.Functor, newArgs)); } else { var call = goalToCall as Symbol; if (call != null) { this.WalkGoal(kb, rule, new Structure(call, new object[arity - 1])); } } } break; case "arg_min": case "arg_max": if (arity == 3) { WalkGoal(kb, rule, goal.Argument(2)); } else WarnUndefined(rule, functor, arity); break; case "find_all": if (arity == 3) { WalkGoal(kb, rule, goal.Argument(1)); } else WarnUndefined(rule, functor, arity); break; default: if (PrologPrimitives.IsDefined(predicateIndicator)) { var arglist = PrologPrimitives.Arglist(predicateIndicator.Functor); for (int i = 0; i < Math.Min(predicateIndicator.Arity,arglist.Count); i++) { var argSym = arglist[i] as Symbol; if (argSym != null) { var arg = argSym.Name; if (arg[0] == ':') WalkGoal(kb, rule, goal.Argument(i)); else if (arg == "..." && arglist[i - 1] is string && ((string)arglist[i - 1])[0] == ':') { // Predicate accepts a rest arg of goals for (int j = i; j < predicateIndicator.Arity; j++) WalkGoal(kb, rule, goal.Argument(j)); } } } } else { var predicate = kb.CheckForPredicateInfo(predicateIndicator); if (predicate == null) WarnUndefined(rule, functor, arity); else { MarkReferenced(predicate); if (predicate.HigherOrderArguments != null) foreach (int argIndex in predicate.HigherOrderArguments) WalkGoal(kb, rule, goal.Argument(argIndex)); } } break; } }
/// <summary> /// Add a term (fact or rule) to the KB. /// </summary> public void Assert(Structure structure, bool atEnd, bool checkSingletons) { if (structure == null) { throw new ArgumentNullException("structure", "Term to add to KB may not be null."); } //structure = structure.Expand(); if (structure == null) { throw new ArgumentNullException("structure"); } Structure head = structure.IsFunctor(Symbol.Implication, 2) ? Term.Structurify(structure.Argument(0), "Head of :- must be a valid proposition or predicate.") : structure; if (head.IsFunctor(Symbol.ColonColon, 2)) { var argument = head.Argument(0); var kb = argument as KnowledgeBase; if (kb == null) { var o = argument as GameObject; if (o != null) { kb = o.KnowledgeBase(); } else { var c = argument as Component; if (c != null) { kb = c.KnowledgeBase(); } else { throw new ArgumentTypeException( "assert", "knowledgebase", argument, typeof(KnowledgeBase)); } } } if (structure.IsFunctor(Symbol.Implication, 2)) { kb.Assert( new Structure(Symbol.Implication, head.Argument(1), structure.Argument(1)), atEnd, checkSingletons); } else { kb.Assert(structure.Argument(1), atEnd, checkSingletons); } } else { if (PrologPrimitives.Implementations.ContainsKey(head.Functor)) { throw new PrologException( new Structure( "error", new Structure( "permission_error", Symbol.Intern("modify"), Symbol.Intern("static_procedure"), Term.PredicateIndicatorExpression(head)))); } KnowledgeBaseRule assertion = KnowledgeBaseRule.FromTerm( structure, checkSingletons, Prolog.CurrentSourceFile, Prolog.CurrentSourceLineNumber); PredicateInfo info = EntryForStoring(head.PredicateIndicator); PredicateInfo parentInfo; if (!info.Shadow && this.Parent != null && (parentInfo = this.Parent.CheckForPredicateInfoInThisKB(head.PredicateIndicator)) != null && !parentInfo.External) { throw new PrologException( new Structure( "error", new Structure( "permission_error", Symbol.Intern("shadow"), Term.PredicateIndicatorExpression(head)))); } info.Assert(assertion, atEnd); } }
private void WarnUndefined(KnowledgeBaseRule rule, Symbol functor, int arity) { rule.PrintWarning("{0}/{1} undefined", functor, arity); }
private void WalkGoal(KnowledgeBase kb, KnowledgeBaseRule rule, Structure goal) { var predicateIndicator = goal.PredicateIndicator; Symbol functor = goal.Functor; int arity = goal.Arity; switch (functor.Name) { case "begin": foreach (var arg in goal.Arguments) { WalkGoal(kb, rule, arg); } break; case "once": case "check": case "randomize": case "not": case "\\+": if (arity == 1) { WalkGoal(kb, rule, goal.Argument(0)); } else { WarnUndefined(rule, functor, arity); } break; case ",": case ";": case "->": if (arity == 2) { WalkGoal(kb, rule, goal.Argument(0)); WalkGoal(kb, rule, goal.Argument(1)); } else { WarnUndefined(rule, functor, arity); } break; case "call": case "maplist": if (arity < 1) { WarnUndefined(rule, functor, arity); } else { object goalToCall = goal.Argument(0); var goalToCallAsStructure = goalToCall as Structure; if (goalToCallAsStructure != null) { var newArgs = new object[arity - 1 + goalToCallAsStructure.Arity]; goalToCallAsStructure.Arguments.CopyTo(newArgs, 0); WalkGoal(kb, rule, new Structure(goalToCallAsStructure.Functor, newArgs)); } else { var call = goalToCall as Symbol; if (call != null) { this.WalkGoal(kb, rule, new Structure(call, new object[arity - 1])); } } } break; case "arg_min": case "arg_max": if (arity == 3) { WalkGoal(kb, rule, goal.Argument(2)); } else { WarnUndefined(rule, functor, arity); } break; case "find_all": if (arity == 3) { WalkGoal(kb, rule, goal.Argument(1)); } else { WarnUndefined(rule, functor, arity); } break; default: if (PrologPrimitives.IsDefined(predicateIndicator)) { var arglist = PrologPrimitives.Arglist(predicateIndicator.Functor); for (int i = 0; i < Math.Min(predicateIndicator.Arity, arglist.Count); i++) { var argSym = arglist[i] as Symbol; if (argSym != null) { var arg = argSym.Name; if (arg[0] == ':') { WalkGoal(kb, rule, goal.Argument(i)); } else if (arg == "..." && arglist[i - 1] is string && ((string)arglist[i - 1])[0] == ':') { // Predicate accepts a rest arg of goals for (int j = i; j < predicateIndicator.Arity; j++) { WalkGoal(kb, rule, goal.Argument(j)); } } } } } else { var predicate = kb.CheckForPredicateInfo(predicateIndicator); if (predicate == null) { WarnUndefined(rule, functor, arity); } else { MarkReferenced(predicate); if (predicate.HigherOrderArguments != null) { foreach (int argIndex in predicate.HigherOrderArguments) { WalkGoal(kb, rule, goal.Argument(argIndex)); } } } } break; } }