public void Call() { AbstractMachineState state = new AbstractMachineState(new AMFactory()); ArrayList prog = new ArrayList(); prog.Add(new PutConstantInstruction()); prog.Add(new HaltInstruction()); state.Initialize(prog); AMProgram program = (AMProgram)state.Program; ProgramClause clause = new ProgramClause("male", 2); program.AddLabel("male/2", clause); CallInstruction i = new CallInstruction(); object[] args = { "male", "2" }; i.Process(args); i.Execute(state); Assert.AreEqual("call", i.Name()); Assert.AreEqual(2, i.NumberOfArguments()); Assert.AreSame(clause, program.P); Assert.AreEqual("halt", program.CP.Instruction.Name()); }
public Choicepoint(int arity, EnvironmentFrame ce, ProgramNode cp, Choicepoint b, ProgramClause nextClause, int tr, HeapNode h) { _arity = arity; _ce = ce; _cp = cp; _b = b; _nextClause = nextClause; _tr = tr; _h = h; }
public void AddLabel(string label, ProgramClause procedure) { if (!_labels.ContainsKey(label)) { _labels.Add(label, procedure); _labelOccurrence[label] = 1; AddProgramNode(procedure); } else { AddLabelAndPatchPredicates(label, procedure); int i = (int)_labelOccurrence[label]; i++; _labelOccurrence[label] = i; } }
public void Backtrack() { AbstractMachineState state = new AbstractMachineState(new AMFactory()); AMProgram program = (AMProgram)state.Program; state.Backtrack(); Assert.IsNotNull(program.P); ProgramClause nextClause = new ProgramClause(); state.B = new Choicepoint(2, null, null, null, nextClause, 3, null); state.Backtrack(); Assert.AreSame(program.P, nextClause); }
public override void Initialize(ArrayList program) { foreach (AbstractInstruction i in program) { if (i.Name() == "procedure") { ProcedureInstruction p = (ProcedureInstruction)i; ProgramClause pClause = new ProgramClause(p.ProcedureName, p.Arity); AddLabel(pClause.Name + "/" + pClause.Arity, pClause); } else { AddInstruction(i); } } _p = _program; }
public void Custom() { HeapNode h = new HeapNode(); EnvironmentFrame ce = new EnvironmentFrame(); ProgramClause clause = new ProgramClause(); Choicepoint b = new Choicepoint(); ProgramNode cp = new ProgramNode(); Choicepoint c = new Choicepoint(2, ce, cp, b, clause, 3, h); Assert.AreSame(h, c.H); Assert.AreEqual(2, c.Arity); Assert.AreSame(ce, c.CE); Assert.AreSame(cp, c.CP); Assert.AreSame(b, c.B); Assert.AreSame(clause, c.NextClause); Assert.AreEqual(3, c.TR); }
private void PatchPredicates(string predicateName, int arity, ProgramClause oldFirst) { AMInstructionSet iset = new AMInstructionSet(); int i = 1; ProgramClause clause = null; for (clause = oldFirst; clause.Instruction.Name() != "trust_me"; clause = clause.NextPredicate) { clause.Name = predicateName + "%" + i + "/" + arity; if (clause.Instruction.Name() != "try_me_else") { clause.Instruction = iset.CreateInstruction("retry_me_else", predicateName + "%" + (i + 1) + "/" + arity); } _labels[predicateName + "%" + i + "/" + arity] = clause; i++; } // patch the last predicate also clause.Name = predicateName + "%" + i + "/" + arity; _labels[predicateName + "%" + i + "/" + arity] = clause; }
public void atom_1_int() { AbstractMachineState state = SetupMachine(); AMProgram program = (AMProgram)state.Program; _p = new AtomPredicate(); AbstractTerm X0 = (AbstractTerm)state["X0"]; // integer X0.Assign(new ConstantTerm("10")); Choicepoint b = new Choicepoint(); ProgramClause nextClause = new ProgramClause(); state.B = new Choicepoint(0, null, null, b, nextClause, 2, null); Verify("atom", 1); _p.Execute(state); Assert.AreSame(nextClause, program.P); }
private void AddLabelAndPatchPredicates(string label, ProgramClause procedure) { AMInstructionSet instructionSet = new AMInstructionSet(); int occurrence = (int)_labelOccurrence[label]; if (occurrence == 1) { ProgramClause p = (ProgramClause)_labels[label]; string nextLabelName = p.Name + "%" + occurrence + "/" + p.Arity; p.Instruction = instructionSet.CreateInstruction("try_me_else", nextLabelName); ProgramClause newClause = new ProgramClause(nextLabelName, p.Arity, instructionSet.CreateInstruction("trust_me")); p.NextPredicate = newClause; AddProgramNode(newClause); _labels[nextLabelName] = newClause; } else { ProgramClause pc = (ProgramClause)_labels[label]; string nextLabelName = pc.Name + "%" + occurrence + "/" + pc.Arity; ProgramClause lastLabel = this[pc.Name + "%" + (occurrence - 1) + "/" + pc.Arity]; lastLabel.Instruction = instructionSet.CreateInstruction("retry_me_else", nextLabelName); ProgramClause newClause = new ProgramClause(nextLabelName, pc.Arity, instructionSet.CreateInstruction("trust_me")); lastLabel.NextPredicate = newClause; AddProgramNode(newClause); _labels[nextLabelName] = newClause; } }
public void ExecuteVar() { AbstractMachineState state = SetupMachine(); ExecuteVariableInstruction i = new ExecuteVariableInstruction(); i.Process(new string[] { "X0" }); AMProgram program = (AMProgram)state.Program; ProgramClause x = new ProgramClause("male", 1); program.AddLabel("male/1", x); AbstractTerm X0 = (AbstractTerm)state["X0"]; StructureTerm s = new StructureTerm("male", 1); s.Next = new ConstantTerm("ali"); X0.Assign(s); i.Execute(state); Assert.AreSame(program.P, x); Assert.AreEqual("executevar", i.Name()); Assert.AreEqual(1, i.NumberOfArguments()); }
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); } }
public void string_1_string() { AbstractMachineState state = SetupMachine(); AMProgram program = (AMProgram)state.Program; _p = new CharPredicate(); AbstractTerm X0 = (AbstractTerm)state["X0"]; X0.Assign(new ConstantTerm("a")); Choicepoint b = new Choicepoint(); ProgramClause nextClause = new ProgramClause(); state.B = new Choicepoint(0, null, null, b, nextClause, 2, null); Verify("char", 1); _p.Execute(state); Assert.AreNotSame(nextClause, program.P); }
public void var_1_unbound() { AbstractMachineState state = SetupMachine(); AMProgram program = (AMProgram)state.Program; _p = new VarPredicate(); AbstractTerm X0 = (AbstractTerm)state["X0"]; Choicepoint b = new Choicepoint(); ProgramClause nextClause = new ProgramClause(); state.B = new Choicepoint(0, null, null, b, nextClause, 2, null); Verify("var", 1); _p.Execute(state); Assert.AreNotSame(nextClause, program.P); }
public void notunifiable_2() { AbstractMachineState state = SetupMachine(); AMProgram program = (AMProgram)state.Program; _p = new NotUnifiablePredicate(); AbstractTerm X0 = (AbstractTerm)state["X0"]; AbstractTerm X1 = (AbstractTerm)state["X1"]; X0.Assign(new ConstantTerm("ali")); X1.Assign(new ConstantTerm("ali")); Choicepoint b = new Choicepoint(); ProgramClause nextClause = new ProgramClause(); state.B = new Choicepoint(0, null, null, b, nextClause, 2, null); Verify("\\=", 2); _p.Execute(state); Assert.AreSame(nextClause, program.P); }
public void lessthan_2() { AbstractMachineState state = SetupMachine(); AMProgram program = (AMProgram)state.Program; _p = new LessThanPredicate(); Verify("<", 2); AbstractTerm X0 = (AbstractTerm)state["X0"]; AbstractTerm X1 = (AbstractTerm)state["X1"]; Choicepoint b = new Choicepoint(); ProgramClause nextClause = new ProgramClause(); state.B = new Choicepoint(0, null, null, b, nextClause, 2, null); X0.Assign(new ConstantTerm("5")); X1.Assign(new ConstantTerm("1")); // X0 < X1 _p.Execute(state); Assert.AreSame(nextClause, program.P); }
public void NextClause() { Choicepoint c = new Choicepoint(); ProgramClause clause = new ProgramClause(); c.NextClause = clause; Assert.AreSame(clause, c.NextClause); }