Inheritance: AbstractProgram
示例#1
0
        public void AddLabel_4()
        {
            ArrayList p = new ArrayList();
            HaltInstruction hi = new HaltInstruction();
            p.Add(hi);

            AMProgram program = new AMProgram();
            program.Initialize(p);

            program.AddLabel("male/1", new ProgramClause("male", 1));
            program.AddLabel("male/1", new ProgramClause("male", 1));
            program.AddLabel("male/1", new ProgramClause("male", 1));
            program.AddLabel("male/1", new ProgramClause("male", 1));

            ProgramClause male1 = program["male/1"];
            ProgramClause male2 = male1.NextPredicate;
            ProgramClause male3 = male2.NextPredicate;
            ProgramClause male4 = male3.NextPredicate;

            Assert.AreEqual(male1.Name, "male");
            Assert.AreEqual(male1.Arity, 1);
            Assert.AreEqual(male1.Instruction.Name(), "try_me_else");

            Assert.AreEqual(male2.Name, "male%1/1");
            Assert.AreEqual(male2.Arity, 1);
            Assert.AreEqual(male2.Instruction.Name(), "retry_me_else");

            Assert.AreEqual(male3.Name, "male%2/1");
            Assert.AreEqual(male3.Arity, 1);
            Assert.AreEqual(male3.Instruction.Name(), "retry_me_else");

            Assert.AreEqual(male4.Name, "male%3/1");
            Assert.AreEqual(male4.Arity, 1);
            Assert.AreEqual(male4.Instruction.Name(), "trust_me");
        }
        public void Backtrack()
        {
            AMProgram program = (AMProgram)_program;

            if (_b == null)
            {
                program.P = new ProgramNode(new HaltInstruction());
            }
            else
            {
                _fail     = false;
                program.P = _b.NextClause;
            }
        }
        /// <summary>
        /// Call a predicate that takes no arguments
        /// </summary>
        /// <param name="predicateName">predicate name only.</param>
        /// <returns>success or failure</returns>
        public bool Call(string predicateName)
        {
            AMProgram program = (AMProgram)_program;

            AMInstructionSet iset = new AMInstructionSet();

            if (!program.IsDefined(predicateName + "/0"))
            {
                return(false);
            }

            // Add the call instruction
            program.P = new ProgramNode(iset.CreateInstruction("call", predicateName, "0"));

            program.AddProgramNode(program.P);

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

            // Execute the program
            Transition();

            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);
        }
        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);
        }
示例#6
0
        public void Stop()
        {
            ArrayList p = new ArrayList();
            p.Add(new HaltInstruction());

            AMProgram program = new AMProgram();
            program.Initialize(p);

            Assert.IsTrue(program.Stop());
        }
示例#7
0
        public void Program()
        {
            ArrayList p = new ArrayList();
            HaltInstruction hi = new HaltInstruction();
            p.Add(hi);

            AMProgram program = new AMProgram();
            program.Initialize(p);

            Assert.AreSame(program.P.Instruction, program.Program.Instruction);
        }
示例#8
0
        public void Initialize()
        {
            ArrayList program = new ArrayList();

            NopInstruction inst = new NopInstruction();
            program.Add(inst);

            AMProgram p = new AMProgram();
            p.Initialize(program);

            Assert.AreSame(p.Program.Instruction, inst);
        }
示例#9
0
        public void AssertLast()
        {
            ArrayList p = new ArrayList();
            HaltInstruction hi = new HaltInstruction();
            p.Add(hi);

            AMProgram program = new AMProgram();
            program.Initialize(p);

            program.AssertLast("male", 1, p);
            program.AssertLast("male", 1, p);

            ProgramClause male1 = program["male/1"];
            ProgramClause male2 = male1.NextPredicate;

            Assert.AreEqual(male1.Name, "male");
            Assert.AreEqual(male1.Arity, 1);
            Assert.AreEqual(male1.Instruction.Name(), "try_me_else");

            Assert.AreEqual(male2.Name, "male%1/1");
            Assert.AreEqual(male2.Arity, 1);
            Assert.AreEqual(male2.Instruction.Name(), "trust_me");
        }
示例#10
0
        public void AssertFirst_2()
        {
            ArrayList p = new ArrayList();
            HaltInstruction hi = new HaltInstruction();
            p.Add(hi);

            AMProgram program = new AMProgram();
            program.Initialize(p);

            program.AssertFirst("male", 1, p);

            ProgramClause oldFirst = program["male/1"];

            program.AssertFirst("male", 1, p);

            ProgramClause newFirst = program["male/1"];

            Assert.AreEqual(newFirst.Name, "male");
            Assert.AreEqual(newFirst.Arity, 1);
            Assert.AreEqual(newFirst.Instruction.Name(), "try_me_else");

            Assert.AreEqual(oldFirst.Name, "male%1/1");
            Assert.AreEqual(oldFirst.Arity, 1);
            Assert.AreEqual(oldFirst.Instruction.Name(), "trust_me");
        }
示例#11
0
        public void AssertFirst_1()
        {
            ArrayList p = new ArrayList();
            HaltInstruction hi = new HaltInstruction();
            p.Add(hi);

            AMProgram program = new AMProgram();
            program.Initialize(p);

            program.AssertFirst("male", 1, p);

            ProgramClause male1 = program["male/1"];

            Assert.AreEqual(male1.Name, "male");
            Assert.AreEqual(male1.Arity, 1);
            Assert.AreEqual(male1.Instruction.Name(), "nop");
        }