com.rubecula.jep
Class TermEvaluator

java.lang.Object
  extended bycom.rubecula.jep.TermEvaluator
All Implemented Interfaces:
org.nfunk.jep.ParserVisitor

public class TermEvaluator
extends java.lang.Object
implements org.nfunk.jep.ParserVisitor

This class is part of the link from JQuantity to JEP - TermEvaluator depends on JEP, but there is no dependence on the com.rubecula.jquantity package. Instead this class depends on the com.rubecula.util package.

Since:
V_0_1
Version:
$Revision: 1.11 $
Author:
Robin Hillyard

Field Summary
private  boolean _Debug
          Debug flag.
private  java.util.Hashtable _DeclaredVariables
          JEP's internal store of declared variables.
private  MillInterface _Mill
          Mill to evaluate expressions.
private  org.nfunk.jep.type.NumberFactory _NumberFactory
          Number Factory to create numbers from the doubles which are generated by JEP when a constant is encountered.
private  Lookupable _UndeclaredVariables
          Store in which to lookup the current values of variables that arise in the terms of expressions.
 
Constructor Summary
(package private) TermEvaluator(org.nfunk.jep.type.NumberFactory numberFactory, MillInterface mill, Lookupable variables)
          Constructor to create a new TermEvaluator for the purpose of evaluating expressions using JQuantity objects rather than Doubles.
 
Method Summary
 Presentable getValue()
          Mutating method to get the value of an expression (mutating because it pops the stack).
private  Presentable makePreciseNumber(java.lang.Object value)
           
private  void push(Presentable value)
           
 void setDebug(boolean debug)
           
 void setDeclaredVariables(java.util.Hashtable variables)
           
 java.lang.Object visit(org.nfunk.jep.ASTConstant node, java.lang.Object data)
          Method to visit a constant node.
 java.lang.Object visit(org.nfunk.jep.ASTFunNode node, java.lang.Object data)
          Method to visit a function node.
 java.lang.Object visit(org.nfunk.jep.ASTStart node, java.lang.Object data)
          Method to visit a start node.
 java.lang.Object visit(org.nfunk.jep.ASTVarNode node, java.lang.Object data)
          Method to visit a variable node.
 java.lang.Object visit(org.nfunk.jep.SimpleNode node, java.lang.Object data)
          Method to visit a simple node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_Debug

private boolean _Debug
Debug flag. If true then values for nodes will be printed.


_NumberFactory

private final org.nfunk.jep.type.NumberFactory _NumberFactory
Number Factory to create numbers from the doubles which are generated by JEP when a constant is encountered.


_Mill

private final MillInterface _Mill
Mill to evaluate expressions.


_UndeclaredVariables

private final Lookupable _UndeclaredVariables
Store in which to lookup the current values of variables that arise in the terms of expressions.


_DeclaredVariables

private java.util.Hashtable _DeclaredVariables
JEP's internal store of declared variables.

Constructor Detail

TermEvaluator

TermEvaluator(org.nfunk.jep.type.NumberFactory numberFactory,
              MillInterface mill,
              Lookupable variables)
        throws MillException
Constructor to create a new TermEvaluator for the purpose of evaluating expressions using JQuantity objects rather than Doubles.

Parameters:
numberFactory - a number factory which produces objects which are Presentable and which are grist to the mill provided (basically this means that they should also be BaseNumbers).
mill - a computation mill which can will operate on the kinds of objects produced by the numberFactory (may be null, in which case an exception will be thrown when the reference is dereferenced).
variables - a lookupable map of variables (may be null).
Throws:
MillException
Method Detail

setDebug

public void setDebug(boolean debug)

setDeclaredVariables

public void setDeclaredVariables(java.util.Hashtable variables)

getValue

public Presentable getValue()
Mutating method to get the value of an expression (mutating because it pops the stack).

Returns:
the top value from the mill stack.

visit

public java.lang.Object visit(org.nfunk.jep.SimpleNode node,
                              java.lang.Object data)
Method to visit a simple node.

Specified by:
visit in interface org.nfunk.jep.ParserVisitor
Parameters:
node - a simple node.
data - the (generic) data passed in.
Returns:
the (generic) data passed out (the result of calling the childrenAccept method on the node, passing in this as the first argument).

visit

public java.lang.Object visit(org.nfunk.jep.ASTStart node,
                              java.lang.Object data)
Method to visit a start node.

Specified by:
visit in interface org.nfunk.jep.ParserVisitor
Parameters:
node - a start node.
data - the (generic) data passed in.
Returns:
the (generic) data passed out (the result of calling the childrenAccept method on the node, passing in this as the first argument).

visit

public java.lang.Object visit(org.nfunk.jep.ASTFunNode node,
                              java.lang.Object data)
Method to visit a function node. This method has the side effect of operating on the mill stack, where the operator is derived from calling getName() on the node.

Specified by:
visit in interface org.nfunk.jep.ParserVisitor
Parameters:
node - a function node.
data - the (generic) data passed in.
Returns:
the (generic) data passed out (the result of calling the childrenAccept method on the node, passing in this as the first argument).

visit

public java.lang.Object visit(org.nfunk.jep.ASTVarNode node,
                              java.lang.Object data)
Method to visit a variable node. If the _DeclaredVariables instance field is set, this method has the side effect of pushing the variable's value onto the mill stack, where the key for the variable is the result of calling getName() on the node.

Specified by:
visit in interface org.nfunk.jep.ParserVisitor
Parameters:
node - a variable node.
data - the (generic) data passed in.
Returns:
the (generic) data passed out (the result of calling the childrenAccept method on the node, passing in this as the first argument). // TODO Something weird going on here if we try to look for "declared" variables first - the JEP expression parser seems to magically pick up some declared variables (with 0 values) thus throwing off the calculations. Maybe investigate eventually.

visit

public java.lang.Object visit(org.nfunk.jep.ASTConstant node,
                              java.lang.Object data)
Method to visit a constant node. If the _NumberFactory instance field is set, this method has the side effect of pushing the constant's value onto the mill stack, where the pushed value is a Presentable. If _NumberFactory is unset, an exception will result, thus requiring the JEP expression parser to evaluate the expression as it would without JQuantity involvement.

Specified by:
visit in interface org.nfunk.jep.ParserVisitor
Parameters:
node - a constant node.
data - the (generic) data passed in.
Returns:
the (generic) data passed out (the result of calling the childrenAccept method on the node, passing in this as the first argument).

makePreciseNumber

private Presentable makePreciseNumber(java.lang.Object value)

push

private void push(Presentable value)