private void WalkRule(KnowledgeBase kb, KnowledgeBaseRule rule) { foreach (Structure goal in rule.BodyGoals) { WalkGoal(kb, rule, goal); } }
public KnowledgeBase(string kbName, GameObject gameObject, KnowledgeBase parent, params KnowledgeBase[] otherImports) { if (kbName == null) { throw new ArgumentNullException("kbName"); } Name = kbName; this.Parent = parent; this.GameObject = gameObject; this.ELRoot = new ELNode(null, Symbol.Intern("root")); //if (parent == null) throw new ArgumentNullException("parent"); if (otherImports == null) { throw new ArgumentNullException("otherImports"); } foreach (var import in otherImports) { if (import == null) { throw new ArgumentNullException("otherImports"); } } if (parent != null) { imports.Add(parent); } imports.AddRange(otherImports); }
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 MakeKB() { var parent = transform.parent; KB parentKB = null; if (parent != null) { parentKB = parent.GetComponent <KB>(); } kb = IsGlobal? KnowledgeBase.Global : new KnowledgeBase( gameObject.name, gameObject, parentKB == null ? GameObject.Find(GlobalKBGameObjectName).KnowledgeBase() : parentKB.KnowledgeBase); // Add UID counter. ELNode.Store(kb.ELRoot / Symbol.Intern("next_uid") % 0); try { foreach (var file in SourceFiles) { kb.Consult(ExpandFileName(file)); } } catch (Exception) { Debug.Break(); // Pause the game throw; } }
/// <summary> /// Creates a blank DB entry for the specified functor /// </summary> public PredicateInfo(Symbol functorName, int arity, KnowledgeBase kb) { Name = functorName; Arity = arity; Entries = new List<KnowledgeBaseEntry>(); KnowledgeBase = kb; }
void KBChanged(KnowledgeBase newKB) { var eli = FindObjectOfType<ELInspector>(); if (eli != null) eli.SetKB(newKB); WindowTitle = "Prolog console: " + newKB.Name; }
/// <summary> /// Creates a blank DB entry for the specified functor /// </summary> public PredicateInfo(Symbol functorName, int arity, KnowledgeBase kb) { Name = functorName; Arity = arity; Entries = new List <KnowledgeBaseEntry>(); KnowledgeBase = kb; }
private void MakeKB() { var parent = transform.parent; KB parentKB = null; if (parent != null) { parentKB = parent.GetComponent<KB>(); } kb = IsGlobal? KnowledgeBase.Global : new KnowledgeBase( gameObject.name, gameObject, parentKB == null ? GameObject.Find(GlobalKBGameObjectName).KnowledgeBase() : parentKB.KnowledgeBase); // Add UID counter. ELNode.Store(kb.ELRoot/Symbol.Intern("next_uid")%0); try { foreach (var file in SourceFiles) kb.Consult(file); } catch (Exception) { Debug.Break(); // Pause the game throw; } }
/// <summary> /// Scans all rules for references to undefined predicates. /// </summary> public void WalkKB(KnowledgeBase kb) { foreach (var predicate in kb.Predicates) { if (predicate != null && predicate.Entries != null) { if (predicate.Public || predicate.Shadow) { MarkReferenced(predicate); } bool firstOne = true; foreach (var kbEntry in predicate.Entries) { var rule = kbEntry as KnowledgeBaseRule; if (rule != null) { if (firstOne) { MarkDefined(predicate, rule); firstOne = false; } WalkRule(kb, rule); if (predicate.HigherOrderArguments != null) { foreach (var arg in predicate.HigherOrderArguments) { WalkGoal(kb, rule, rule.HeadArgs[arg]); } } } } } } }
/// <summary> /// Scans all rules for references to undefined predicates. /// </summary> public void WalkKB(KnowledgeBase kb) { foreach (var predicate in kb.Predicates) if (predicate != null && predicate.Entries != null) { if (predicate.Public || predicate.Shadow) MarkReferenced(predicate); bool firstOne = true; foreach (var kbEntry in predicate.Entries) { var rule = kbEntry as KnowledgeBaseRule; if (rule != null) { if (firstOne) { MarkDefined(predicate, rule); firstOne = false; } WalkRule(kb, rule); if (predicate.HigherOrderArguments != null) foreach (var arg in predicate.HigherOrderArguments) WalkGoal(kb, rule, rule.HeadArgs[arg]); } } } }
public Compiler(Structure head, Structure[] body, KnowledgeBase kb) { this.head = head; bodyGoals = body; this.knowledgeBase = kb; env = new CompileTimeEnvironment(head.Arity); Compile(); }
void KBChanged(KnowledgeBase newKB) { var eli = FindObjectOfType <ELInspector>(); if (eli != null) { eli.SetKB(newKB); } WindowTitle = "Prolog console: " + newKB.Name; }
static PredicateInfo GetPredicateInfo(KnowledgeBase kb, PredicateIndicator p) { PredicateInfo result; if ((result = kb.CheckForPredicateInfoInThisKB(p)) != null) return result; foreach (KnowledgeBase import in kb.imports) if ((result = GetPredicateInfo(import, p)) != null) return result; return null; }
// // these write a single nodes, so they don't need to loop like queries do. // /// <summary> /// Write TERM to EL KB, creating any nodes that need to be cerated. /// </summary> /// <param name="term">Prolog-format term to store into KB</param> /// <param name="knowledgeBase">KB in which to assert the term.</param> /// <returns></returns> public static ELNode Update(object term, KnowledgeBase knowledgeBase) { term = Term.Deref(term); var s = term as Structure; if (s != null) return UpdateStructure(s, knowledgeBase); var n = term as ELNode; if (n != null) return n; throw new Exception("Malformed EL assertion: " + ISOPrologWriter.WriteToString(term)); }
/// <summary> /// Creates a PrologContext that can for at most the specified number of steps. /// </summary> public PrologContext(KnowledgeBase kb, int stepLimit) { StepsRemaining = stepLimit; goalStack = new List<GoalStackFrame>(); goalStackCurrentRules = new List<KnowledgeBaseEntry>(); GoalStackDepth = 0; KnowledgeBase = kb; traceVariables = new LogicVariable[256]; traceValues = new object[256]; tracePointer = 0; IndexicalBindingStack = new List<KeyValuePair<Symbol, object>>(); }
/// <summary> /// Creates a PrologContext that can for at most the specified number of steps. /// </summary> public PrologContext(KnowledgeBase kb, int stepLimit) { StepsRemaining = stepLimit; goalStack = new List <GoalStackFrame>(); goalStackCurrentRules = new List <KnowledgeBaseEntry>(); GoalStackDepth = 0; KnowledgeBase = kb; traceVariables = new LogicVariable[256]; traceValues = new object[256]; tracePointer = 0; IndexicalBindingStack = new List <KeyValuePair <Symbol, object> >(); isFree = true; }
public static ELNode UpdateStructure(Structure term, KnowledgeBase knowledgeBase) { if (term.Functor == Symbol.Slash) { if (term.Arity == 1) return knowledgeBase.ELRoot.StoreNonExclusive(Term.CopyInstantiation(term.Argument(0))); return Update(term.Argument(0), knowledgeBase).StoreNonExclusive(Term.CopyInstantiation(term.Argument(1))); } if (term.Functor == Symbol.Colon) { return Update(term.Argument(0), knowledgeBase).StoreExclusive(Term.CopyInstantiation(term.Argument(1)), true); } throw new Exception("Malformed EL assertion: "+ISOPrologWriter.WriteToString(term)); }
/// <summary> /// Gets a context that is currently free to use. Should be relased afterward using ReleaseContext(). /// </summary> /// <returns>A free PrologContext</returns> public static PrologContext Allocate(KnowledgeBase kb, object thisValue) { var c = Pool.Allocate(); if (!c.isFree) { throw new InvalidOperationException("Allocated PrologContext is still in use!"); } c.isFree = false; c.KnowledgeBase = kb; c.Reset(thisValue); c.Output = Console.Out; return(c); }
/// <summary> /// Resets the context (clears stack, etc.) and starts a proof of the specified goal. /// </summary> /// <param name="goal">Goal to attempt to prove</param> /// <returns>Enumerator for solutions</returns> public IEnumerable <bool> ResetStackAndProve(Structure goal) { Reset(this.This); foreach (var state in KnowledgeBase.Prove(goal.Functor, goal.Arguments, this, 0)) { if (state == CutState.ForceFail) { yield break; } else { yield return(false); } } }
public KnowledgeBase(string kbName, GameObject gameObject, KnowledgeBase parent, params KnowledgeBase[] otherImports) { if (kbName == null) throw new ArgumentNullException("kbName"); Name = kbName; this.Parent = parent; this.GameObject = gameObject; this.ELRoot = new ELNode(null, Symbol.Intern("root")); //if (parent == null) throw new ArgumentNullException("parent"); if (otherImports == null) throw new ArgumentNullException("otherImports"); foreach (var import in otherImports) if (import == null) throw new ArgumentNullException("otherImports"); if (parent != null) imports.Add(parent); imports.AddRange(otherImports); }
/// <summary> /// Resets the context (clears stack, etc.) and starts a proof of the specified goal. /// </summary> /// <param name="goal">Goal to attempt to prove</param> /// <returns>Enumerator for solutions</returns> public IEnumerable <bool> ResetStackAndProve(object goal) { Reset(this.This); Structure s = Term.Structurify(goal, "Invalid goal."); foreach (var state in KnowledgeBase.Prove(s.Functor, s.Arguments, this, 0)) { if (state == CutState.ForceFail) { yield break; } else { yield return(false); } } }
static PredicateInfo GetPredicateInfo(KnowledgeBase kb, PredicateIndicator p) { PredicateInfo result; if ((result = kb.CheckForPredicateInfoInThisKB(p)) != null) { return(result); } foreach (KnowledgeBase import in kb.imports) { if ((result = GetPredicateInfo(import, p)) != null) { return(result); } } return(null); }
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> /// Proves the specified goal, throwing an exception with badGoalErrorMessage if the goal is ill-formed. /// </summary> internal IEnumerable <CutState> Prove(object goal, string badGoalErrorMessage) { Structure s = Term.Structurify(goal, badGoalErrorMessage); return(KnowledgeBase.Prove(s.Functor, s.Arguments, this, CurrentFrame)); }
/// <summary> /// Proves the goal in the specified structure. /// </summary> internal IEnumerable <CutState> Prove(Structure goal) { return(KnowledgeBase.Prove(goal.Functor, goal.Arguments, this, CurrentFrame)); }
/// <summary> /// Creates a PrologContext with PrologContext.DefaultStepLimit /// </summary> private PrologContext(KnowledgeBase kb) : this(kb, DefaultStepLimit) { }
/// <summary> /// Creates a PrologContext with PrologContext.DefaultStepLimit /// </summary> private PrologContext(KnowledgeBase kb) : this(kb, DefaultStepLimit) { }
public void SetKB(KnowledgeBase kb) { root = kb.ELRoot; displayChildren.Add(root); WindowTitle = kb.Name + " KB"; }
static PredicateInfo GetPredicateInfo(KnowledgeBase kb, PredicateIndicator p) { PredicateInfo result; if ((result = kb.CheckForPredicateInfoInThisKB(p)) != null) return result; foreach (KnowledgeBase import in kb.imports) if ((result = GetPredicateInfo(import, p)) != null) return result; return null; }
static KnowledgeBase() { Global = new KnowledgeBase("global", null, null); PrologPrimitives.InstallPrimitives(); }
// // these write a single nodes, so they don't need to loop like queries do. // /// <summary> /// Write TERM to EL KB, creating any nodes that need to be cerated. /// </summary> /// <param name="term">Prolog-format term to store into KB</param> /// <param name="knowledgeBase">KB in which to assert the term.</param> /// <returns></returns> public static ELNode Update(object term, KnowledgeBase knowledgeBase) { term = Term.Deref(term); var s = term as Structure; if (s != null) return UpdateStructure(s, knowledgeBase); var n = term as ELNode; if (n != null) return n; throw new Exception("Malformed EL assertion: " + ISOPrologWriter.WriteToString(term)); }
/// <summary> /// Gets a context that is currently free to use. Should be relased afterward using ReleaseContext(). /// </summary> /// <returns>A free PrologContext</returns> public static PrologContext GetFreePrologContext(KnowledgeBase kb, object thisValue) { if (FreeContexts.Count > 0) { var c = FreeContexts.Pop(); if (!c.isFree) throw new InvalidOperationException("Allocated PrologContext is still in use!"); c.isFree = false; c.KnowledgeBase = kb; c.Reset(thisValue); c.Output = Console.Out; return c; } return new PrologContext(kb) { This = thisValue }; }
static KnowledgeBase() { Global = new KnowledgeBase("global", null, null); PrologPrimitives.InstallPrimitives(); }
/// <summary> /// Gets a context that is currently free to use. Should be relased afterward using ReleaseContext(). /// </summary> /// <returns>A free PrologContext</returns> public static PrologContext Allocate(KnowledgeBase kb, object thisValue) { var c = Pool.Allocate(); if (!c.isFree) throw new InvalidOperationException("Allocated PrologContext is still in use!"); c.isFree = false; c.KnowledgeBase = kb; c.Reset(thisValue); c.Output = Console.Out; return c; }
private static IEnumerable<CutState> RandomizeInternal(KnowledgeBase kb, PrologContext context, Structure t) { bool savedRandomize = context.Randomize; context.Randomize = true; foreach (var state in kb.Prove(t.Functor, t.Arguments, context, context.CurrentFrame)) if (state==CutState.ForceFail) yield break; else yield return state; context.Randomize = savedRandomize; }
public void SetKB(KnowledgeBase kb) { root = kb.ELRoot; displayChildren.Add(root); }
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; } }
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; } }
public static ELNode UpdateStructure(Structure term, KnowledgeBase knowledgeBase) { if (term.Functor == Symbol.Slash) { if (term.Arity == 1) return knowledgeBase.ELRoot.StoreNonExclusive(Term.CopyInstantiation(term.Argument(0))); return Update(term.Argument(0), knowledgeBase).StoreNonExclusive(Term.CopyInstantiation(term.Argument(1))); } if (term.Functor == Symbol.Colon) { return Update(term.Argument(0), knowledgeBase).StoreExclusive(Term.CopyInstantiation(term.Argument(1)), true); } throw new Exception("Malformed EL assertion: "+ISOPrologWriter.WriteToString(term)); }
private void WalkRule(KnowledgeBase kb, KnowledgeBaseRule rule) { foreach (Structure goal in rule.BodyGoals) WalkGoal(kb, rule, goal); }
public void SetKB(KnowledgeBase kb) { root = kb.ELRoot; displayChildren.Add(root); WindowTitle = kb.Name + " KB"; }
public void SetKB(KnowledgeBase kb) { root = kb.ELRoot; displayChildren.Add(root); }