Axiom.Runtime.AMProgram.AssertFirst C# (CSharp) Method

AssertFirst() public method

public AssertFirst ( string predicateName, int arity, ArrayList code ) : void
predicateName string
arity int
code System.Collections.ArrayList
return void
        public void AssertFirst(string predicateName, int arity, ArrayList code)
        {
            AMInstructionSet iset = new AMInstructionSet();

            if (!_labels.ContainsKey(predicateName + "/" + arity))
            {
                AddLabel(predicateName + "/" + arity, new ProgramClause(predicateName, arity));
                _labelOccurrence[predicateName + "/" + arity] = 1;
                // add instructions

                return;
            }

            string pLabel = predicateName + "/" + arity;

            int occurrence = (int)_labelOccurrence[predicateName + "/" + arity];

            if (occurrence == 1)
            {
                ProgramClause oldPredicate = (ProgramClause)_labels[pLabel];
                oldPredicate.Instruction = iset.CreateInstruction("trust_me");

                string nextLabel = predicateName + "%" + occurrence + "/" + arity;

                oldPredicate.Name = nextLabel;
                _labels[nextLabel] = oldPredicate;

                ProgramClause newFirst = new ProgramClause(predicateName, arity, iset.CreateInstruction("try_me_else", nextLabel));
                newFirst.NextPredicate = oldPredicate;

                _labels[pLabel] = newFirst;

                occurrence++;

                _labelOccurrence[pLabel] = occurrence;

                AddProgramNode(newFirst);

                foreach (AbstractInstruction inst in code)
                {
                    AddInstruction(inst);
                }
            }
            else
            {
                ProgramClause oldFirst = (ProgramClause)_labels[pLabel];

                ProgramClause newFirst = new ProgramClause(predicateName, arity, iset.CreateInstruction("try_me_else", predicateName + "%1/" + arity));
                newFirst.NextPredicate = oldFirst;

                oldFirst.Name = predicateName + "%1/" + arity;
                oldFirst.Instruction = iset.CreateInstruction("retry_me_else", predicateName + "%2/" + arity);

                _labels[pLabel] = newFirst;

                AddProgramNode(newFirst);

                foreach (AbstractInstruction inst in code)
                {
                    AddInstruction(inst);
                }
                occurrence++;

                _labelOccurrence[pLabel] = occurrence;

                PatchPredicates(predicateName, arity, oldFirst);

            }
        }

Usage Example

Ejemplo n.º 1
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");
        }
All Usage Examples Of Axiom.Runtime.AMProgram::AssertFirst