com.ff.JEvaluator
Class JEvaluator

java.lang.Object
  extended by com.ff.JEvaluator.JEvaluator

public class JEvaluator
extends java.lang.Object

Author:
fiorucci

Field Summary
private  java.util.HashMap<java.lang.String,JEvaluatorNodeBaseClass> evaluatorExprs
           
private  java.util.HashMap<java.lang.String,BaseFunctionInterface> evaluatorFuncs
           
private  java.util.HashMap<java.lang.String,java.lang.String> evaluatorNames
           
protected static org.apache.commons.logging.Log log
           
 
Constructor Summary
JEvaluator()
           
JEvaluator(java.lang.String varEqualsExpr)
          Creates an evaluator from an expression such as "varName=expr"
JEvaluator(java.lang.String varName, java.lang.String expr)
           
 
Method Summary
protected  java.util.List<java.lang.String> buildParamList(JEvaluatorNodeFunc f)
          Computes function parameter values and builds list of "double" values to be later passed to the compute() method of the selected function
protected  java.lang.String compute(JEvaluatorNodeBaseClass n)
          Recursively computes a subtree starting from the given rootNode
private  java.lang.String computeFunction(JEvaluatorNodeBaseClass n)
           
 boolean delVar(java.lang.String varName)
          Deletes a variable for the current evaluator instance
 void dump(java.lang.String varName)
           
private  void exprCharDump(JEvaluatorExprChar[] exprChars, int startPosition, int endPosition)
           
private  java.lang.String getExpressionRecursive(JEvaluatorNodeBaseClass n)
          Recursively traverses this evaluator instance's tree, rebuilding the mathematical expression string for later printout
 java.lang.String getVar(java.lang.String varName)
          Gets a variable's value for the current evaluator instance
 boolean init(java.lang.String configFile)
           
protected  BaseFunctionInterface initFunctionClass(java.lang.String fName)
          Instantiates a BaseFunctionInterface for later computation, based on the function's name
 boolean isFunctionRegistered(BaseFunctionInterface f)
          Checks to see whether a function was registered
 boolean isFunctionRegistered(java.lang.String functionName)
          Checks to see whether a function was registered
 boolean isVarDefined(java.lang.String varName)
          Checks to see whether varName is defined for the current evaluator instance
private  JEvaluatorNodeBaseClass parse(java.lang.String expression)
          Parses the specified mathematical expression
private  void recursiveDumper(JEvaluatorNodeBaseClass n)
           
private  JEvaluatorNodeBaseClass recursiveParser(JEvaluatorExprChar[] exprChars, int startPosition, int endPosition)
          Recursively parses the specified mathematical expression
 void registerFunction(BaseFunctionInterface f)
          Registers a customized function to JEvaluator
 boolean setExpr(java.lang.String expression)
          Sets a variable's value for the current evaluator instance, based on an expression in the format "varName=expression"
 boolean setVar(java.lang.String varName, java.lang.String expr)
          Sets a variable's value for the current evaluator instance
 void unregisterFunction(BaseFunctionInterface f)
          Unregisters a customized function to JEvaluator
 boolean unregisterFunction(java.lang.String functionName)
          Unregisters a customized function to JEvaluator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log

evaluatorNames

private java.util.HashMap<java.lang.String,java.lang.String> evaluatorNames

evaluatorExprs

private java.util.HashMap<java.lang.String,JEvaluatorNodeBaseClass> evaluatorExprs

evaluatorFuncs

private java.util.HashMap<java.lang.String,BaseFunctionInterface> evaluatorFuncs
Constructor Detail

JEvaluator

public JEvaluator()

JEvaluator

public JEvaluator(java.lang.String varName,
                  java.lang.String expr)

JEvaluator

public JEvaluator(java.lang.String varEqualsExpr)
Creates an evaluator from an expression such as "varName=expr"

Parameters:
varEqualsExpr -
Method Detail

init

public boolean init(java.lang.String configFile)

recursiveDumper

private void recursiveDumper(JEvaluatorNodeBaseClass n)

dump

public void dump(java.lang.String varName)

exprCharDump

private void exprCharDump(JEvaluatorExprChar[] exprChars,
                          int startPosition,
                          int endPosition)

recursiveParser

private JEvaluatorNodeBaseClass recursiveParser(JEvaluatorExprChar[] exprChars,
                                                int startPosition,
                                                int endPosition)
Recursively parses the specified mathematical expression

Parameters:
exprChars - An evaluatorExprChar array
startPosition - The leftmost evaluatorExprChar array element to consider
endPosition - The number of items in the array
Returns:
an evaluatorNode

parse

private JEvaluatorNodeBaseClass parse(java.lang.String expression)
Parses the specified mathematical expression

Parameters:
expression - The expression to be parsed
Returns:
an EvaluatorNode if parsing was successful, null if expression has unbalanced parenthesis

getExpressionRecursive

private java.lang.String getExpressionRecursive(JEvaluatorNodeBaseClass n)
Recursively traverses this evaluator instance's tree, rebuilding the mathematical expression string for later printout

Parameters:
n - the root node
Returns:
the expression string for this subtree

compute

protected java.lang.String compute(JEvaluatorNodeBaseClass n)
                            throws java.lang.Exception
Recursively computes a subtree starting from the given rootNode

Parameters:
n - the rootNode
Returns:
the subtree result
Throws:
java.lang.Exception

computeFunction

private java.lang.String computeFunction(JEvaluatorNodeBaseClass n)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

initFunctionClass

protected BaseFunctionInterface initFunctionClass(java.lang.String fName)
                                           throws JEvaluatorException
Instantiates a BaseFunctionInterface for later computation, based on the function's name

Parameters:
fName - the function's name (sum, avg, ...)
Returns:
a BaseFunctionInterface instance, for later bfi.compute() call
Throws:
JEvaluatorException

buildParamList

protected java.util.List<java.lang.String> buildParamList(JEvaluatorNodeFunc f)
                                                   throws java.lang.Exception
Computes function parameter values and builds list of "double" values to be later passed to the compute() method of the selected function

Parameters:
f - the evaluator node function
Returns:
the list of Double parameters to be passed to the function
Throws:
java.lang.Exception

setExpr

public boolean setExpr(java.lang.String expression)
Sets a variable's value for the current evaluator instance, based on an expression in the format "varName=expression"

Parameters:
expression - the expression in the format "varName=expression"
Returns:
false if varName was newly created, false in case varName's previous value was overridden

setVar

public boolean setVar(java.lang.String varName,
                      java.lang.String expr)
Sets a variable's value for the current evaluator instance

Parameters:
varName - the variable's name
expr - the value/expression to set
Returns:
false if varName was newly created, false in case varName's previous value was overridden

getVar

public java.lang.String getVar(java.lang.String varName)
                        throws java.lang.Exception
Gets a variable's value for the current evaluator instance

Parameters:
varName - the variable name
Returns:
the variable value
Throws:
java.lang.Exception

delVar

public boolean delVar(java.lang.String varName)
Deletes a variable for the current evaluator instance

Parameters:
varName - the variable name
Returns:
true if deletion was successful, false otherwise (variable not found)

isVarDefined

public boolean isVarDefined(java.lang.String varName)
Checks to see whether varName is defined for the current evaluator instance

Parameters:
varName - the variable's name
Returns:
true is varName is defined, false otherwise

registerFunction

public void registerFunction(BaseFunctionInterface f)
Registers a customized function to JEvaluator

Parameters:
f - the Function object to register

unregisterFunction

public void unregisterFunction(BaseFunctionInterface f)
Unregisters a customized function to JEvaluator

Parameters:
f - the Function object to unregister

unregisterFunction

public boolean unregisterFunction(java.lang.String functionName)
Unregisters a customized function to JEvaluator

Parameters:
functionName - the Function object to unregister
Returns:
true if unregistration was successful, false otherwise

isFunctionRegistered

public boolean isFunctionRegistered(BaseFunctionInterface f)
Checks to see whether a function was registered

Parameters:
f - the function object
Returns:
true if function was registered, false otherwise

isFunctionRegistered

public boolean isFunctionRegistered(java.lang.String functionName)
Checks to see whether a function was registered

Parameters:
functionName - the function name
Returns:
true if function was registered, false otherwise