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> /// True if the specified functor/arity is undefined. /// </summary> public bool Undefined(PredicateIndicator p) { if (PrologPrimitives.IsDefined(p)) return false; if (CheckForPredicateInfoInThisKB(p) != null) return false; foreach (KnowledgeBase import in imports) if (!import.Undefined(p)) return false; return true; }
/// <summary> /// True if the specified functor/arity is undefined. /// </summary> public bool Undefined(PredicateIndicator p) { if (PrologPrimitives.IsDefined(p)) { return(false); } if (CheckForPredicateInfoInThisKB(p) != null) { return(false); } foreach (KnowledgeBase import in imports) { if (!import.Undefined(p)) { return(false); } } return(true); }
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; } }