Inheritance: AbstractTerm
示例#1
0
 public void IsConstant()
 {
     ConstantTerm t = new ConstantTerm();
     AbstractTerm a = new ConstantTerm();
     Assert.IsTrue(t.IsConstant);
     Assert.IsTrue(a.IsConstant);
 }
示例#2
0
 public void IsObject()
 {
     ConstantTerm t = new ConstantTerm();
     AbstractTerm a = new ConstantTerm();
     Assert.IsFalse(t.IsObject);
     Assert.IsFalse(a.IsObject);
 }
示例#3
0
 public void IsReference()
 {
     ConstantTerm t = new ConstantTerm();
     AbstractTerm a = new ConstantTerm();
     Assert.IsFalse(t.IsReference);
     Assert.IsFalse(a.IsReference);
 }
示例#4
0
        public void Bind()
        {
            ConstantTerm term = new ConstantTerm();
            ConstantTerm term2 = new ConstantTerm();

            term.Bind(term2);

            Assert.AreSame(term, term.Reference());
        }
示例#5
0
        public void Push_ConstantTerm()
        {
            AMHeap heap = new AMHeap();

            ConstantTerm con = new ConstantTerm("Hello, World!");

            heap.Push(con);

            Assert.AreSame(con, heap.Top());
        }
示例#6
0
        public void Dereference()
        {
            ConstantTerm term1 = new ConstantTerm();
            ConstantTerm term2 = new ConstantTerm();
            ConstantTerm term3 = new ConstantTerm();

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

            Assert.AreSame(term1, term1.Dereference());
        }
示例#7
0
        public void Pop_one_item()
        {
            AMHeap heap = new AMHeap();

            ConstantTerm con = new ConstantTerm("ali");

            heap.Push(con);

            heap.Pop();

            Assert.IsNull(heap.Top());
        }
示例#8
0
        public void Pop_two_items()
        {
            AMHeap heap = new AMHeap();

            ConstantTerm con = new ConstantTerm("ali");
            ConstantTerm first = new ConstantTerm("foo");

            heap.Push(first);
            heap.Push(con);

            heap.Pop();

            Assert.AreSame(first, heap.Top());
        }
示例#9
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);
        }
示例#10
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));
        }
示例#11
0
        public void Unbind()
        {
            AbstractTerm term = new AbstractTerm();
            ConstantTerm con = new ConstantTerm("ali");

            term.Assign(con);

            term.Unbind();

            Assert.AreNotSame(term.Reference(), con.Reference());
        }
        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);
        }
示例#13
0
 public void IsStructure()
 {
     ConstantTerm t = new ConstantTerm();
     AbstractTerm a = new ConstantTerm();
     Assert.IsFalse(t.IsStructure);
     Assert.IsFalse(a.IsStructure);
 }
        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;
        }
示例#15
0
        public void Unify_lis_con()
        {
            ListTerm list = new ListTerm();
            ConstantTerm con = new ConstantTerm();

            Assert.IsFalse(list.Unify(con));
        }
示例#16
0
        public void Unify_con_lis()
        {
            ConstantTerm con = new ConstantTerm("ali");
            ListTerm term = new ListTerm();

            Assert.IsFalse(con.Unify(term));
        }
示例#17
0
        private bool InvokeMethod(AbstractTerm obj, AbstractTerm method, AbstractTerm returnObject, AbstractMachineState state)
        {
            if (obj.IsConstant)
            {
                // invoke a static method
                Assembly asm = GetRequiredAssembly(obj.Data() as string, state);
                Type type = asm.GetType(obj.Data() as string);
                if (type == null)
                {
                    return false;
                }
                ArrayList paramArray = new ArrayList();
                GetTypes(type.GetMethod(method.Data() as string), method, ref paramArray, obj);

                object[] arguments = paramArray.ToArray();
                object res = type.InvokeMember(method.Data() as string, BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, arguments);
                switch (res.GetType().ToString())
                {
                    case "System.Char":
                    case "System.String":
                    case "System.Int32":
                    case "System.Boolean":
                        returnObject.Assign(new ConstantTerm(res.ToString()));
                        break;
                    default:
                        returnObject.Assign(new ObjectTerm(res));
                        break;
                }
                SetTypes(method, arguments);
            }
            else
            {
                // invoke an instance method
                ArrayList paramArray = new ArrayList();

                GetTypes(obj.Data().GetType().GetMethod(method.Data() as string), method, ref paramArray, obj);
                object classObject = obj.Data();
                MethodInfo omi = classObject.GetType().GetMethod(method.Data() as string);

                object[] arguments = paramArray.ToArray();
                object res = obj.Data().GetType().InvokeMember(method.Data() as string, BindingFlags.Default | BindingFlags.ExactBinding | BindingFlags.InvokeMethod, null, obj.Data(), arguments);
                if (res != null)
                {
                    switch (res.GetType().ToString())
                    {

                        case "System.String":
                            ConstantTerm rC = new ConstantTerm(res.ToString());
                            if (!returnObject.Unify(rC))
                            {
                                return false;
                            }
                            break;
                        case "System.Char":
                        case "System.Int32":
                        case "System.Boolean":
                            ConstantTerm returnConstant = new ConstantTerm(res.ToString());
                             if (!returnObject.Unify(returnConstant))
                            {
                                return false;
                            }
                            break;
                        default:
                            returnObject.Assign(new ObjectTerm(res));
                            break;
                    }
                }
                SetTypes(method, arguments);
            }
            return true;
        }
示例#18
0
 public void Data()
 {
     ConstantTerm t = new ConstantTerm();
     Assert.IsNull(t.Data());
 }
        public void UnifyLocalValue()
        {
            AbstractMachineState state = SetupMachine();

            UnifyLocalValueInstruction i = new UnifyLocalValueInstruction();

            ConstantTerm con = new ConstantTerm("ali");
            state.S = con;

            object[] args = { "X0" };

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

            Assert.AreEqual("unify_local_value", i.Name());
            Assert.AreEqual(1, i.NumberOfArguments());
            AbstractTerm  X0 = (AbstractTerm)state["X0"];
            Assert.AreEqual("ali", X0.Dereference().Data());
        }
示例#20
0
        public void Unify_con_str()
        {
            ConstantTerm con = new ConstantTerm("ali");
            StructureTerm term = new StructureTerm("samir", 2);

            Assert.IsFalse(con.Unify(term));
        }
示例#21
0
        public void Unify_con_ref()
        {
            AbstractTerm term = new AbstractTerm();

            ConstantTerm con = new ConstantTerm("ali");

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

            Assert.AreSame(term.Data(), con.Data());
        }
示例#22
0
        public void Unify_con_ne_con()
        {
            ConstantTerm con = new ConstantTerm("ali");
            ConstantTerm term = new ConstantTerm("samir");

            Assert.IsFalse(con.Unify(term));
        }
示例#23
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);
        }
示例#24
0
        public void Unify_str_con()
        {
            ConstantTerm term = new ConstantTerm("ali");
            StructureTerm s = new StructureTerm("s", 2);

            Assert.IsFalse(s.Unify(term));
        }
示例#25
0
        public void Unify_con_eq_con()
        {
            ConstantTerm con = new ConstantTerm("ali");
            ConstantTerm term = new ConstantTerm("ali");

            Assert.IsTrue(con.Unify(term));
        }
示例#26
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;
        }
示例#27
0
        public void ConstantTerm()
        {
            ConstantTerm c = new ConstantTerm("Ali");

            Assert.AreEqual("Ali", c.Data());
        }