Dereference() public method

Dereferences this object to the object it is assigned to.
public Dereference ( ) : AbstractTerm
return AbstractTerm
Example #1
0
        /// <summary>
        /// Dereferences this object to the object it is assigned to.
        /// </summary>
        /// <returns></returns>
        public virtual AbstractTerm Dereference()
        {
            // in case we are wrapping a variable

            if (IsAssigned())
            {
                return(_containee.Dereference());
            }

            return(this);

            //if (_containee == null || _containee == this)
            //{
            //    return this;
            //}
            //else
            //{
            //    AbstractTerm reference = _containee;
            //    while (reference._containee != null)
            //    {
            //        _reference = _reference._containee;
            //    }
            //    return _reference;
            //}
            //return null;
        }
Example #2
0
        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);
        }
Example #3
0
 /// <summary>
 /// Assigns this object to another one.
 /// </summary>
 /// <param name="term"></param>
 public void Assign(AbstractTerm term)
 {
     //if (term._containee == this)
     //{
     //    return;
     //}
     //_containee = term;
     _containee = term.Dereference();
 }
Example #4
0
        public void Dereference()
        {
            AbstractTerm term1 = new AbstractTerm();
            AbstractTerm term2 = new AbstractTerm();
            AbstractTerm term3 = new AbstractTerm();

            term2.Bind(term3);
            term1.Bind(term2);

            Assert.AreSame(term3, term1.Dereference());
        }
Example #5
0
        public void Unify_ref_ref()
        {
            AbstractTerm term = new AbstractTerm();
            AbstractTerm another = new AbstractTerm();

            bool result = term.Unify(another);

            Assert.AreSame(term.Dereference(), another);
            Assert.IsTrue(result);
        }
        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);
        }
Example #7
0
        public static double Evaluate(AbstractTerm t)
        {
            double result = 0;

            AbstractTerm term = t.Dereference();

            if (term.IsConstant)
            {
                int r;
                double d;
                if (Int32.TryParse((string)term.Data(), out r))
                {
                    return r;
                }
                else if (Double.TryParse((string)term.Data(), out d))
                {
                    return d;
                }
                else
                {
                    switch ((string)term.Data())
                    {
                        case "pi":
                            result = Math.PI;
                            break;
                        case "e":
                            result = Math.E;
                            break;
                        case "cputime":
                            Process p = Process.GetCurrentProcess();
                            result = (p.TotalProcessorTime.TotalMilliseconds/1000);
                            break;
                        default:
                            throw new Exception("TermEvaluator: cannot evaluate string " + term.Data());
                    }
                }
            }
            else if (term.IsReference)
            {
                throw new Exception("TermEvaluator: cannot evaluate a reference term.");
            }
            else if (term.IsStructure)
            {
                AbstractTerm op1 = (AbstractTerm)term.Next;
                AbstractTerm op2 = (AbstractTerm)term.Next.Next;

                switch (term.Name)
                {
                    case "+":
                        result = Evaluate(op1) + Evaluate(op2);
                        break;
                    case "-":
                        result = Evaluate(op1) - Evaluate(op2);
                        break;
                    case "*":
                        result = Evaluate(op1) * Evaluate(op2);
                        break;
                    case "/":
                        if (Evaluate(op2) == 0)
                        {
                            throw new Exception("TermEvaluator: Division by zero error.");
                        }
                        result = Evaluate(op1) / Evaluate(op2);
                        break;
                    case "^":
                        result = Math.Pow(Evaluate(op1), Evaluate(op2));
                        break;
                    case "cos":
                        result = Math.Cos(Evaluate(op1));
                        break;
                    case "sin":
                        result = Math.Sin(Evaluate(op1));
                        break;
                    case "tan":
                        result = Math.Tan(Evaluate(op1));
                        break;
                    case "log":
                        result = Math.Log(Evaluate(op1));
                        break;
                }
            }
            return result;
        }
Example #8
0
 /// <summary>
 /// Assigns this object to another one.
 /// </summary>
 /// <param name="term"></param>
 public void Assign(AbstractTerm term)
 {
     //if (term._containee == this)
     //{
     //    return;
     //}
     //_containee = term;
     _containee = term.Dereference();
 }