public PrologObject AdjustVariables(PrologObject po, ArrayList vars, int offset) { if (po is StructureObject) { po = ((StructureObject)po).Normalize(); } if (po is StringObject && ((StringObject)po).IsVariableName()) { int off; off = vars.IndexOf(po); if (off >= 0) { return(GetVariable(off + offset)); } vars.Add(po); po = GetVariable(offset + vars.Count - 1); } else if (po is StructureObject) { po = new StructureObject((StructureObject)po, this, vars, offset); } return(po); }
public void PushPending(PrologObject po) { if (!(po == null)) { pendings.Add(po.MakeNode(this)); } }
public void Add(PrologObject fact) { if (!(facts.Contains(fact))) { facts.Add(fact); } }
public static bool IsAtom(PrologObject po) { if (po is StringObject) { return(true); } return(false); }
public static bool IsVariable(PrologObject po) { if (po is Variable) { return(true); } return(false); }
public static bool IsAtomic(PrologObject po) { if (IsAtom(po) || IsInteger(po)) { return(true); } return(false); }
public static bool IsInteger(PrologObject po) { if (po is IntegerObject) { return(true); } return(false); }
private void Add(PrologMachine pm, PrologObject po) { if (po is StructureObject && ((StructureObject)(po)).Functor == this) { StructureObject ast = ((StructureObject)(po)); Add(pm, ast.Parameters[1]); Add(pm, ast.Parameters[0]); } else { pm.PushPending(po); } }
public void Bind(PrologObject obj) { if (obj is Variable) { Variable v = ((Variable)(obj)); if (v.Id > id) { v.Bind(this); return; } } this.value = obj; this.machine.Bindings.Add(this); }
public virtual bool Unify(PrologObject po) { PrologObject po0; po0 = this.Dereference(); if (!(po0 == this)) { return(po0.Unify(po)); } po = po.Dereference(); if (po is Variable) { return(po.Unify(this)); } return(Equals(po)); }
private bool IsPredicate(PrologObject fact, StringObject atom) { if (fact.Equals(atom)) { return(true); } if (!(fact is StructureObject)) { return(false); } StructureObject st = ((StructureObject)(fact)); if (st.Functor.Equals(atom)) { return(true); } if (!(st.Functor == IfPrimitive.GetInstance())) { return(false); } fact = st.Parameters[0]; if (fact.Equals(atom)) { return(true); } if (!(fact is StructureObject)) { return(false); } st = (StructureObject)fact; if (st.Functor.Equals(atom)) { return(true); } return(false); }
public StructureObject(StructureObject st, PrologMachine pm, ArrayList vars, int offset) { this.functor = pm.AdjustVariables(st.Functor, vars, offset); if (st.Arity == 0) { this.parameters = null; } else { this.parameters = new PrologObject[st.Arity]; } for (int np = 0; np <= st.Arity - 1; np++) { this.parameters[np] = pm.AdjustVariables(st.Parameters[np], vars, offset); } }
public bool Unify(PrologObject po1, PrologObject po2) { po1 = po1.Dereference(); po2 = po2.Dereference(); if (po1.Equals(po2)) { return(true); } if (po1 is Variable) { ((Variable)(po1)).Bind(po2); return(true); } if (po2 is Variable) { ((Variable)(po2)).Bind(po1); return(true); } if (po1 is StructureObject && po2 is StructureObject) { StructureObject st1 = ((StructureObject)(po1)); StructureObject st2 = ((StructureObject)(po2)); if (!(Unify(st1.Functor, st2.Functor))) { return(false); } if (!(st1.Arity == st2.Arity)) { return(false); } for (int k = 0; k <= st1.Arity - 1; k++) { if (!(Unify(st1.Parameters[k], st2.Parameters[k]))) { return(false); } } return(true); } return(false); }
bool NextFact() { if (mFactsEnum == null) { return(false); } while (mFactsEnum.MoveNext()) { PrologObject fact = (PrologObject)mFactsEnum.Current; SaveStatus(); fact = Machine.AdjustVariables(fact); if (fact is StructureObject && ((StructureObject)(fact)).Functor.Equals(IfPrimitive.GetInstance())) { StructureObject ifpo; ifpo = ((StructureObject)(fact)); PrologObject thenpo = ifpo.Parameters[1]; fact = ifpo.Parameters[0]; if (Machine.Unify(this.Object, fact)) { Machine.Level += 1; Machine.PushPending(thenpo); Machine.PushNode(this); return(true); } } else { if (Machine.Unify(this.Object, fact)) { Machine.PushNode(this); return(true); } } RestoreStatus(); } Machine.PushPending(this); return(false); }
public static PrologObject[] Evaluate(PrologObject[] pars) { if (pars == null) { return(null); } if (pars.Length == 0) { return(null); } PrologObject[] result; result = new PrologObject[pars.Length]; for (int np = 0; np <= pars.Length - 1; np++) { result[np] = pars[np].Dereference(); } return(result); }
public StructureObject(PrologObject functor, params object[] parameters) { if (functor == null) { throw new ArgumentNullException("functor"); } this.functor = functor; if (parameters == null || parameters.Length == 0) { this.parameters = null; return; } else { this.parameters = new PrologObject[parameters.Length]; } for (int np = 0; np <= parameters.Length - 1; np++) { this.parameters[np] = Utilities.ToPrologObject(parameters[np]); } }
public List <PrologObject> GetFacts(PrologObject po) { return(this.facts); }
public FactNode(PrologObject obj, PrologMachine mach) : base(mach, obj) { }
public PrologObject AdjustVariables(PrologObject po) { return(AdjustVariables(po, new ArrayList(), variables.Count)); }
public bool Resolve(PrologObject po) { variables.Clear(); bindings.Clear(); pendings.Clear(); nodes.Clear(); queryVariables.Clear(); level = 0; ArrayList vars = new ArrayList(); po = AdjustVariables(po, vars, variables.Count); if (Interactive) { queryVariables = vars; } PushPending(po); Node node; bool result; result = true; while (true) { if (Trace) { ExecuteTrace(); } if (result) { node = PopPending(); if (node == null) { if (Interactive) { if (DoSolution()) { return(true); } else { result = false; } } else { return(true); } } else { result = node.ExecuteCall(); } } else { node = PopNode(); if (node == null) { if (Interactive) { DoNoSolution(); } return(false); } result = node.ExecuteRedo(); } } }
public IList GetFacts(PrologObject po) { return(facts.GetFacts(po)); }
public void Assertz(PrologObject po) { facts.Add(po); }
public void Unbind() { value = null; }
public Node(PrologMachine mach, PrologObject obj) { mMachine = mach; mObject = obj; }