示例#1
0
        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);
        }
示例#2
0
 public void PushPending(PrologObject po)
 {
     if (!(po == null))
     {
         pendings.Add(po.MakeNode(this));
     }
 }
示例#3
0
 public void Add(PrologObject fact)
 {
     if (!(facts.Contains(fact)))
     {
         facts.Add(fact);
     }
 }
示例#4
0
        public static bool IsAtom(PrologObject po)
        {
            if (po is StringObject)
            {
                return(true);
            }

            return(false);
        }
示例#5
0
        public static bool IsVariable(PrologObject po)
        {
            if (po is Variable)
            {
                return(true);
            }

            return(false);
        }
示例#6
0
        public static bool IsAtomic(PrologObject po)
        {
            if (IsAtom(po) || IsInteger(po))
            {
                return(true);
            }

            return(false);
        }
示例#7
0
        public static bool IsInteger(PrologObject po)
        {
            if (po is IntegerObject)
            {
                return(true);
            }

            return(false);
        }
示例#8
0
 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);
     }
 }
示例#9
0
        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);
        }
示例#10
0
        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));
        }
示例#11
0
        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);
        }
示例#12
0
        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);
            }
        }
示例#13
0
 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);
 }
示例#14
0
        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);
        }
示例#15
0
        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);
        }
示例#16
0
        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]);
            }
        }
示例#17
0
 public List <PrologObject> GetFacts(PrologObject po)
 {
     return(this.facts);
 }
示例#18
0
 public FactNode(PrologObject obj, PrologMachine mach)
     : base(mach, obj)
 {
 }
示例#19
0
 public PrologObject AdjustVariables(PrologObject po)
 {
     return(AdjustVariables(po, new ArrayList(), variables.Count));
 }
示例#20
0
        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();
                }
            }
        }
示例#21
0
 public IList GetFacts(PrologObject po)
 {
     return(facts.GetFacts(po));
 }
示例#22
0
 public void Assertz(PrologObject po)
 {
     facts.Add(po);
 }
示例#23
0
 public void Unbind()
 {
     value = null;
 }
示例#24
0
 public Node(PrologMachine mach, PrologObject obj)
 {
     mMachine = mach;
     mObject  = obj;
 }