Provides a container for an abstract term.
Inheritance: HeapNode
        public override bool Unify(AbstractTerm term)
        {
            if (term.IsReference)
            {
                term.Assign(this);
                return(true);
            }
            if (term.IsStructure)
            {
                if (term.Name != _name || term.Arity != _arity)
                {
                    return(false);
                }

                for (int i = 0; i < _arity; i++)
                {
                    if (!this[i].Unify(term[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(false);
        }
 public void UnsaveRegisters(AbstractMachineState state, int count)
 {
     for (int i = 0; i < count; i++)
     {
         AbstractTerm Xi = (AbstractTerm)state["X" + i.ToString()];
         Xi.Assign((AbstractTerm)_savedVariables[i]);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Assigns this object to another one.
 /// </summary>
 /// <param name="term"></param>
 public void Assign(AbstractTerm term)
 {
     //if (term._containee == this)
     //{
     //    return;
     //}
     //_containee = term;
     _containee = term.Dereference();
 }
 public void SaveRegisters(AbstractMachineState state, int count)
 {
     for (int i = 0; i < count; i++)
     {
         AbstractTerm toSave = new AbstractTerm();
         toSave.Copy((AbstractTerm)state["X" + i.ToString()]);
         _savedVariables.Add(toSave);
     }
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            var peg = new PrologPeg();
            var result = new AbstractTerm();
            peg.solve(result);

            System.Console.WriteLine(result);
            System.Console.ReadKey();
        }
Beispiel #6
0
        public void Bind()
        {
            AbstractTerm term = new AbstractTerm();
            AbstractTerm term2 = new AbstractTerm();

            term.Bind(term2);

            Assert.AreSame(term2, term.Reference());
        }
Beispiel #7
0
        public void Push_AbstractTerm()
        {
            AMHeap heap = new AMHeap();

            AbstractTerm term = new AbstractTerm();
            heap.Push(term);

            Assert.AreSame(heap.Top(), term);
        }
Beispiel #8
0
        public void Trail()
        {
            AMTrail trail = AMTrail.Instance;

            AbstractTerm term = new AbstractTerm();
            trail.Initialize();
            trail.Trail(term);

            Assert.AreEqual(1, trail.TR);
        }
Beispiel #9
0
        public void Dereference()
        {
            AbstractTerm term1 = new AbstractTerm();
            AbstractTerm term2 = new AbstractTerm();
            AbstractTerm term3 = new AbstractTerm();

            term2.Bind(term3);
            term1.Bind(term2);

            Assert.AreSame(term3, term1.Dereference());
        }
Beispiel #10
0
 public void Unwind(int count)
 {
     if (count == 0)
     {
         return;
     }
     for (int i = count; i < _tr; i++)
     {
         AbstractTerm term = (AbstractTerm)_trail[i];
         term.Unbind();
     }
 }
Beispiel #11
0
        static void Main(string[] args)
        {
            var list = new PrologList();

            var a = new AbstractTerm();
            var b = new AbstractTerm();
            var c = new AbstractTerm();
            list.runme(a, b, c);

            System.Console.WriteLine(a);
            System.Console.WriteLine(b);
            System.Console.WriteLine(c);
            System.Console.ReadKey();
        }
Beispiel #12
0
        private bool CreateObject(string classType, AbstractTerm netObject, AbstractMachineState state)
        {
            Assembly asm = GetRequiredAssembly(classType, state);
            Type[] types = asm.GetTypes();
            Type type = null;

            type = asm.GetType(fullTypeName);
            if (type == null)
            {
                return false;
            }
            netObject.Assign(new ObjectTerm(Activator.CreateInstance(type)));
            return true;
        }
Beispiel #13
0
        public override bool Unify(AbstractTerm term)
        {
            if (term.IsReference)
            {
                term.Assign(this);
                return(true);
            }
            if (term.IsList)
            {
                return(Head.Unify(term.Head) && Tail.Unify(term.Tail));
            }

            return(false);
        }
 public void AddVariable()
 {
     if (_permanentVariablesTop == null)
     {
         _permanentVariablesTop = new AbstractTerm();
         _permanentVariables    = _permanentVariablesTop;
     }
     else
     {
         _permanentVariablesTop.Next = new AbstractTerm();
         _permanentVariablesTop      = (AbstractTerm)_permanentVariablesTop.Next;
     }
     _variableCount++;
 }
Beispiel #15
0
        static void Main(string[] args)
        {
            Family familyDB = new Family();
            
            if(familyDB.son("jack", "tom"))
                System.Console.WriteLine("Yeah!");

            System.Console.WriteLine("Who is the father of john? ");

            AbstractTerm f = new AbstractTerm();
            familyDB.father(f, "john");

            System.Console.WriteLine(f);
        }
Beispiel #16
0
        public override bool Unify(AbstractTerm term)
        {
            if (term.IsReference)
            {
                term.Assign(this);
                return(true);
            }
            if (term.IsConstant)
            {
                bool res = term.Data().Equals(this.Data());
                return(res);
            }

            return(false);
        }
        private AbstractTerm GetArgumentIndex(int index)
        {
            AbstractTerm term = this;

            if (index == 0)
            {
                return((AbstractTerm)_next);
            }

            for (int i = 0; i <= index; i++)
            {
                term = (AbstractTerm)term.Next;
            }

            return(term);
        }
Beispiel #18
0
        public void Assign()
        {
            AbstractTerm term = new AbstractTerm();
            ConstantTerm con = new ConstantTerm("ali");

            term.Assign(con);

            Assert.AreSame(term.Dereference(), con);
            Assert.AreSame(term.Dereference(), con.Dereference());
            Assert.AreEqual(term.Data(), con.Data());
            Assert.AreSame(term.Reference(), con.Reference());
            Assert.IsFalse(term.IsList);
            Assert.IsFalse(term.IsObject);
            Assert.IsFalse(term.IsReference);
            Assert.IsFalse(term.IsStructure);
            Assert.IsTrue(term.IsConstant);
        }
        public override string ToString()
        {
            string str = _name + "(";

            for (int i = 0; i < _arity; i++)
            {
                AbstractTerm term = (AbstractTerm)this[i];
                str += term.ToString();
                if (i == _arity - 1)
                {
                    str += ")";
                }
                else
                {
                    str += ",";
                }
            }
            return(str);
        }
Beispiel #20
0
        public void Push(HeapNode newItem)
        {
            if (newItem is AbstractTerm)
            {
                AbstractTerm at = newItem as AbstractTerm;
                if (at.IsReference)
                {
                    if (at.Name == null || at.Name == "")
                    {
                        at.Name = "__" + _variableIndex.ToString();
                        _variableIndex++;
                    }
                }
            }
            bool changeS = (_state.S == _extraItem);

            if (_h == null)
            {
                _heap               = newItem;
                _extraItem          = new HeapNode();
                newItem.Next        = _extraItem;
                _extraItem.Previous = newItem;
                _h = newItem;
                return;
            }


            _extraItem          = newItem;
            _extraItem.Previous = _h;
            _h.Next             = _extraItem;
            _h = _h.Next;
            if (changeS)
            {
                _state.S = _h;
            }

            HeapNode e = new HeapNode();

            _extraItem.Next = e;
            e.Previous      = _extraItem;
            _extraItem      = _extraItem.Next;
        }
Beispiel #21
0
        /// <summary>
        /// Binds this object to another one.
        /// </summary>
        /// <param name="term"></param>
        public virtual void Bind(AbstractTerm term)
        {
            AbstractTerm t1 = this;
            AbstractTerm t2 = term;

            AMTrail trail = AMTrail.Instance;

            // if t1 is a reference
            //   and
            //  (t2 is not a reference) or address2 < address1
            if (t1.IsReference && !t2.IsReference)
            {
                Assign(t2);
                trail.Trail(t1);
            }
            else
            {
                t2.Assign(t1);
                trail.Trail(t2);
            }
        }
Beispiel #22
0
        static void Main(string[] args)
        {
            MyMath math = new MyMath();


            // Check if 2 is the minimum of 2 or 3
            if (math.min(2, 3, 2))
            {
                Console.WriteLine("Yes");
            }

            // Calculate the factorial 
            AbstractTerm a = new AbstractTerm();
            math.factorial(3, a);
            Console.WriteLine("Factorial of 3 is: " + a);

            // calculate the fifth fibonacci (should be 8)
            AbstractTerm fibValue = new AbstractTerm();
            math.fib(5, fibValue);
            Console.WriteLine("Fibonacci of 3 is: " + fibValue);
        }
 private AbstractTerm GetPermanentRegister(int i)
 {
     if (i == 0)
     {
         return(_permanentVariables);
     }
     else
     {
         if (_variableCount < i + 1)
         {
             for (int n = 0; n < (i - _variableCount + 10); n++)
             {
                 AddVariable();
             }
         }
         AbstractTerm vPtr = _permanentVariables;
         for (int j = 0; j < i; j++)
         {
             vPtr = (AbstractTerm)vPtr.Next;
         }
         return(vPtr);
     }
 }
Beispiel #24
0
        public virtual AbstractTerm this[int index]
        {
            get
            {
                if (IsAssigned())
                {
                    return(_containee[index]);
                }

                if (index == 0)
                {
                    return((AbstractTerm)_next);
                }
                else
                {
                    AbstractTerm vPtr = (AbstractTerm)_next;
                    for (int j = 0; j < index; j++)
                    {
                        vPtr = (AbstractTerm)vPtr.Next;
                    }
                    return(vPtr);
                }
            }
        }
Beispiel #25
0
        public virtual bool Unify(AbstractTerm term)
        {
            if (IsAssigned())
            {
                return(_containee.Unify(term));
            }

            // perform unification here
            if (this == term)
            {
                return(true);
            }

            if (!term.IsReference)
            {
                this.Assign(term);
                return(true);
            }
            else
            {
                this.Bind(term);
                return(true);
            }
        }
Beispiel #26
0
        public void Unify_ref_lis()
        {
            AbstractTerm term = new AbstractTerm();
            ListTerm con = new ListTerm();

            Assert.IsTrue(term.Unify(con));

            Assert.AreSame(term.Reference(), con.Reference());
            Assert.IsTrue(term.IsList);
            Assert.IsFalse(term.IsReference);
        }
Beispiel #27
0
 public void Copy(AbstractTerm term)
 {
     this._containee = term._containee;
     this._reference = term._reference;
 }
Beispiel #28
0
 public void IsStructure()
 {
     AbstractTerm t = new AbstractTerm();
     Assert.IsFalse(t.IsStructure);
 }
Beispiel #29
0
 public void IsObject()
 {
     AbstractTerm t = new AbstractTerm();
     Assert.IsFalse(t.IsObject);
 }
Beispiel #30
0
 public void IsConstant()
 {
     AbstractTerm t = new AbstractTerm();
     Assert.IsFalse(t.IsConstant);
 }
Beispiel #31
0
 public void Data()
 {
     AbstractTerm t = new AbstractTerm();
     Assert.IsNull(t.Data());
 }
Beispiel #32
0
        public void Unify_ref_str()
        {
            AbstractTerm term = new AbstractTerm();
            StructureTerm con = new StructureTerm();

            Assert.IsTrue(term.Unify(con));

            Assert.AreSame(term.Reference(), con.Reference());
            Assert.IsTrue(term.IsStructure);
            Assert.IsFalse(term.IsReference);
        }
Beispiel #33
0
        public override bool Unify(AbstractTerm term)
        {
            if (term.IsReference)
            {
                term.Assign(this);
                return true;
            }
            if (term.IsConstant)
            {
                bool res = term.Data().Equals(this.Data());
                return res;
            }

            return false;
        }
Beispiel #34
0
 public void Unbind()
 {
     _containee = null;
     _reference = this;
 }
Beispiel #35
0
        public override bool Unify(AbstractTerm term)
        {
            if (term.IsReference)
            {
                term.Assign(this);
                return true;
            }
            if (term.IsStructure)
            {
                if (term.Name != _name || term.Arity != _arity)
                {
                    return false;
                }

                for (int i = 0; i < _arity; i++)
                {
                    if (!this[i].Unify(term[i]))
                    {
                        return false;
                    }
                }
                return true;
            }

            return false;
        }
        public void UnifyVariable()
        {
            AbstractMachineState state = SetupMachine();

            state.IsReadMode = true;

            AbstractTerm sVar = new AbstractTerm();
            state.S = sVar;

            UnifyVariableInstruction i = new UnifyVariableInstruction();

            object[] args = { "X0" };

            i.Process(args);
            i.Execute(state);

            Assert.AreEqual("unify_variable", i.Name());
            Assert.AreEqual(1, i.NumberOfArguments());
            AbstractTerm X0 = ((AbstractTerm)state["X0"]).Dereference();
            Assert.AreSame(X0.Dereference(), sVar);
            Assert.IsNull(state.S);
        }
Beispiel #37
0
 /// <summary>
 /// default constructor.
 /// </summary>
 public AbstractTerm()
 {
     _variableName += "_" + _variableIndex;
     _reference     = this;
     _variableIndex++;
 }
 abstract public void Trail(AbstractTerm term);
Beispiel #39
0
        public void Unify_ref_ref()
        {
            AbstractTerm term = new AbstractTerm();
            AbstractTerm another = new AbstractTerm();

            bool result = term.Unify(another);

            Assert.AreSame(term.Dereference(), another);
            Assert.IsTrue(result);
        }
Beispiel #40
0
 public override void Bind(AbstractTerm term)
 {
 }
        public override bool Unify(AbstractTerm term)
        {
            // Unify with a .NET object
            if (term.IsObject)
            {
                if (_data.Equals(term.Data()))
                {
                    return(true);
                }
                else if (_data == term.Data())
                {
                    return(true);
                }
                return(false);
            }

            // Unify with a constant
            if (term.IsConstant)
            {
                // unify with an int
                int   v;
                float x;
                if (Int32.TryParse((string)term.Data(), out v))
                {
                    if (_data.GetType().ToString() == "System.Int32")
                    {
                        if (((int)_data) == v)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    return(false);
                }
                // Unify with a float
                else if (float.TryParse((string)term.Data(), out x))
                {
                    if (_data.GetType().ToString() == "System.Float")
                    {
                        if (((float)_data) == x)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    return(false);
                }
                // Unify with a constant atom/string
                else
                {
                    if (_data.GetType().ToString() == "System.String")
                    {
                        if (((string)_data) == ((string)term.Data()))
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    return(false);
                }
            }

            // Unify with a list (need to unify it with an ArrayList)
            else
            {
                return(false);
            }

            // Unify with a structure

            // Unify with a reference
        }
Beispiel #42
0
        public override bool Unify(AbstractTerm term)
        {
            // Unify with a .NET object
            if (term.IsObject)
            {
                if (_data.Equals(term.Data()))
                {
                    return true;
                }
                else if (_data == term.Data())
                {
                    return true;
                }
                return false;
            }

            // Unify with a constant
            if (term.IsConstant)
            {
                // unify with an int
                int v;
                float x;
                if (Int32.TryParse((string)term.Data(), out v))
                {

                    if (_data.GetType().ToString() == "System.Int32")
                    {
                        if (((int)_data) == v)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return false;
                }
                // Unify with a float
                else if (float.TryParse((string)term.Data(), out x))
                {
                    if (_data.GetType().ToString() == "System.Float")
                    {
                        if (((float)_data) == x)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return false;
                }
                // Unify with a constant atom/string
                else
                {
                    if (_data.GetType().ToString() == "System.String")
                    {
                        if (((string)_data) == ((string)term.Data()))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return false;
                }

            }

            // Unify with a list (need to unify it with an ArrayList)
            else
            {
                return false;
            }

            // Unify with a structure

            // Unify with a reference
        }
Beispiel #43
0
 public override void Trail(AbstractTerm term)
 {
     _trail.Add(term);
     _tr++;
 }
Beispiel #44
0
        public void Unbind()
        {
            AbstractTerm term = new AbstractTerm();
            ConstantTerm con = new ConstantTerm("ali");

            term.Assign(con);

            term.Unbind();

            Assert.AreNotSame(term.Reference(), con.Reference());
        }
Beispiel #45
0
 public void IsList()
 {
     AbstractTerm t = new AbstractTerm();
     Assert.IsFalse(t.IsList);
 }
 public override void Bind(AbstractTerm term)
 {
     // do nothing
 }
Beispiel #47
0
 public void IsReference()
 {
     AbstractTerm t = new AbstractTerm();
     Assert.IsTrue(t.IsReference);
 }
Beispiel #48
0
        public void Unify_ref_con()
        {
            AbstractTerm term = new AbstractTerm();
            ConstantTerm con = new ConstantTerm();

            Assert.IsTrue(term.Unify(con));

            Assert.AreEqual(term.Data(), con.Data());
            Assert.AreSame(term.Reference(), con.Reference());
            Assert.IsTrue(term.IsConstant);
            Assert.IsFalse(term.IsReference);
        }
Beispiel #49
0
        public void Unify_after_assignment()
        {
            AbstractTerm term = new AbstractTerm();
            ConstantTerm con = new ConstantTerm("test");

            term.Assign(con);

            Assert.AreEqual(term.Unify(con), con.Unify(term));
        }
Beispiel #50
0
 public void SaveVariable(AbstractTerm register)
 {
 }
        public bool Call(string predicateName, int arity, object[] args)
        {
            AMProgram        program = (AMProgram)_program;
            AMHeap           heap    = (AMHeap)_dataArea;
            AMInstructionSet iset    = new AMInstructionSet();

            if (!program.IsDefined(predicateName + "/" + arity))
            {
                return(false);
            }
            ArrayList a = new ArrayList();

            ProgramNode entryPoint = null;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i].GetType().ToString())
                {
                case "System.String":
                case "System.Int32":
                case "System.Boolean":
                    if (entryPoint == null)
                    {
                        entryPoint = new ProgramNode(iset.CreateInstruction("put_constant", args[i].ToString(), "X" + i.ToString()));
                        program.AddProgramNode(entryPoint);
                    }
                    else
                    {
                        program.AddInstruction(iset.CreateInstruction("put_constant", args[i].ToString(), "X" + i.ToString()));
                    }
                    break;

                case "Axiom.Runtime.AbstractTerm":
                    a.Add(i);
                    if (entryPoint == null)
                    {
                        entryPoint = new ProgramNode(iset.CreateInstruction("put_variable", "X" + args[i].ToString(), "X" + i.ToString()));
                        program.AddProgramNode(entryPoint);
                    }
                    else
                    {
                        program.AddInstruction(iset.CreateInstruction("put_variable", "X" + args[i].ToString(), "X" + args[i].ToString()));
                    }
                    break;
                }
            }

            // Add the call instruction
            program.AddInstruction(iset.CreateInstruction("call", predicateName, arity.ToString()));

            // Add the halt insturction
            program.AddInstruction(iset.CreateInstruction("halt"));

            program.P = entryPoint;

            // Execute the program
            Transition();

            foreach (int argumentNumber in a)
            {
                AbstractTerm var = (AbstractTerm)args[argumentNumber];
                var.Assign((AbstractTerm)this["X" + argumentNumber.ToString()]);
            }

            return(!_fail);
        }
        public bool Call(string predicatename, int arity, object[] args, bool more)
        {
            AMProgram        program = (AMProgram)_program;
            AMHeap           heap    = (AMHeap)_dataArea;
            AMInstructionSet iset    = new AMInstructionSet();

            // argument indexes
            ArrayList argumentIndexes = new ArrayList();

            if (!more)
            {
                _passedVariables = new ArrayList();
            }


            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] != null)
                {
                    switch (args[i].GetType().ToString())
                    {
                    case "System.String":
                    case "System.Int32":
                    case "System.Boolean":
                        if (!more)
                        {
                            AbstractTerm var = new ConstantTerm(args[i].ToString());
                            heap.Push(var);
                            _passedVariables.Add(-1);
                            AbstractTerm Xn = (AbstractTerm)this["X" + i];
                            Xn.Assign(var);
                        }
                        break;

                    case "Axiom.Runtime.AbstractTerm":
                        if (!more)
                        {
                            AbstractTerm heapVariable = new AbstractTerm();
                            heap.Push(heapVariable);
                            _passedVariables.Add(heapVariable);
                            AbstractTerm Xn = (AbstractTerm)this["X" + i];
                            Xn.Assign(heapVariable);
                        }
                        break;
                    }
                }
            }

            if (!more)
            {
                program.P = new ProgramNode(iset.CreateInstruction("call", predicatename, arity.ToString()));
                program.AddProgramNode(program.P);
                program.AddInstruction(iset.CreateInstruction("halt"));
            }

            // Execute the program
            Transition();

            for (int i = 0; i < _passedVariables.Count; i++)
            {
                if (!(_passedVariables[i] is int))
                {
                    AbstractTerm v = (AbstractTerm)_passedVariables[i];
                    AbstractTerm argumentVariable = (AbstractTerm)args[i];
                    argumentVariable.Assign(v.Dereference());
                }
            }

            return(!_fail);
        }
Beispiel #53
0
        public bool Getproperty(AbstractTerm obj, AbstractTerm method, AbstractTerm returnObject)
        {
            if (obj.IsConstant)
            {
                // invoke a static get property
                Assembly asm = GetRequiredAssembly(obj.Data() as string, runtime); ;
                Type type = asm.GetType(obj.Data() as string);
                if (type == null)
                {
                    return false;
                }
                if (type.GetProperty(method.Data() as string) == null)
                {
                    return false;
                }
                object res = type.InvokeMember(method.Data() as string, BindingFlags.GetProperty | BindingFlags.Static | BindingFlags.Public, null, null, null);
                switch (res.GetType().ToString())
                {
                    case "System.String":
                        AbstractTerm rC = new ConstantTerm(res.ToString());
                        if (!returnObject.Unify(rC))
                        {
                            return false;
                        }
                        break;
                    case "System.Char":
                    case "System.Int32":
                    case "System.Boolean":
                        AbstractTerm returnConstant = new ConstantTerm(res.ToString());
                        if (!returnObject.Unify(returnConstant))
                        {
                            return false;
                        }
                        break;
                    default:
                        returnObject.Unify(new ObjectTerm(res));
                        break;
                }
            }
            else
            {
                if (obj.Data().GetType().GetProperty(method.Data() as string) == null)
                {
                    return false;
                }

                // invoke an instance get property
                object res = obj.Data().GetType().InvokeMember(method.Data() as string, BindingFlags.Default | BindingFlags.ExactBinding | BindingFlags.GetProperty, null, obj.Data(), null);
                switch (res.GetType().ToString())
                {
                    case "System.Char":
                    case "System.String":
                    case "System.Int32":
                    case "System.Boolean":
                        returnObject.Unify(new ConstantTerm(res.ToString()));
                        break;
                    default:
                        returnObject.Unify(new ObjectTerm(res));
                        break;
                }
            }
            return true;
        }
Beispiel #54
0
 public override void Bind(AbstractTerm term)
 {
 }
Beispiel #55
0
 public override void Bind(AbstractTerm term)
 {
     // do nothing
 }
 public void SaveVariable(AbstractTerm register)
 {
 }