public void Call() { ArrayList prog = new ArrayList(); prog.Add(new NopInstruction()); prog.Add(new HaltInstruction()); AbstractMachineState state = new AbstractMachineState(new AMFactory()); state.Initialize(prog); AMProgram program = (AMProgram)state.Program; ArrayList predicateCode = new ArrayList(); AMInstructionSet iset = new AMInstructionSet(); // say_hello(X) :- write(X). predicateCode.Add(iset.CreateInstruction("bcall", "write/1")); predicateCode.Add(iset.CreateInstruction("proceed")); AbstractTerm X0 = (AbstractTerm)state["X0"]; X0.Assign(new ConstantTerm("Hello, World!")); program.AssertFirst("say_hello", 1, predicateCode); Assert.IsTrue(state.Call("say_hello", 1, new object[] { "Hello man" })); }
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 CreateInstruction() { AMInstructionSet am = new AMInstructionSet(); foreach (string s in instructions) { AbstractInstruction i = null; if (s == "set_void" || s == "unify_void") { i = am.CreateInstruction(s, "1"); Assert.AreEqual(s, i.Name()); } else { i = am.CreateInstruction(s, "f/1", "1", "X2", "X3"); Assert.AreEqual(s, i.Name()); } } }
/// <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); }
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 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); }
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 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; }
/// <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) { 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; }