public override bool Unify(AbstractTerm term) { if (term.IsReference) { term.Assign(this); return(true); } if (term.IsStructure) { if (term.Name != _name || term.Arity != _arity) { return(false); } for (int i = 0; i < _arity; i++) { if (!this[i].Unify(term[i])) { return(false); } } return(true); } return(false); }
public void UnsaveRegisters(AbstractMachineState state, int count) { for (int i = 0; i < count; i++) { AbstractTerm Xi = (AbstractTerm)state["X" + i.ToString()]; Xi.Assign((AbstractTerm)_savedVariables[i]); } }
private bool CreateObject(string classType, AbstractTerm netObject, AbstractMachineState state) { Assembly asm = GetRequiredAssembly(classType, state); Type[] types = asm.GetTypes(); Type type = null; type = asm.GetType(fullTypeName); if (type == null) { return false; } netObject.Assign(new ObjectTerm(Activator.CreateInstance(type))); return true; }
public override bool Unify(AbstractTerm term) { if (term.IsReference) { term.Assign(this); return(true); } if (term.IsList) { return(Head.Unify(term.Head) && Tail.Unify(term.Tail)); } return(false); }
public override bool Unify(AbstractTerm term) { if (term.IsReference) { term.Assign(this); return(true); } if (term.IsConstant) { bool res = term.Data().Equals(this.Data()); return(res); } return(false); }
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); }
/// <summary> /// Binds this object to another one. /// </summary> /// <param name="term"></param> public virtual void Bind(AbstractTerm term) { AbstractTerm t1 = this; AbstractTerm t2 = term; AMTrail trail = AMTrail.Instance; // if t1 is a reference // and // (t2 is not a reference) or address2 < address1 if (t1.IsReference && !t2.IsReference) { Assign(t2); trail.Trail(t1); } else { t2.Assign(t1); trail.Trail(t2); } }
public override bool Unify(AbstractTerm term) { if (term.IsReference) { term.Assign(this); return true; } if (term.IsStructure) { if (term.Name != _name || term.Arity != _arity) { return false; } for (int i = 0; i < _arity; i++) { if (!this[i].Unify(term[i])) { return false; } } return true; } return false; }
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)); }
public void Unbind() { AbstractTerm term = new AbstractTerm(); ConstantTerm con = new ConstantTerm("ali"); term.Assign(con); term.Unbind(); Assert.AreNotSame(term.Reference(), con.Reference()); }
public override bool Unify(AbstractTerm term) { if (term.IsReference) { term.Assign(this); return true; } if (term.IsConstant) { bool res = term.Data().Equals(this.Data()); return res; } return false; }
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 override bool Unify(AbstractTerm term) { if (term.IsReference) { term.Assign(this); return true; } if (term.IsList) { return Head.Unify(term.Head) && Tail.Unify(term.Tail); } return false; }
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; }