예제 #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;
 }