jwo.utils.expression
Class MathEvaluator

java.lang.Object
  extended by jwo.utils.expression.MathEvaluator

public final class MathEvaluator
extends Object

Evaluates a mathematical expression stored in an postfix stack (created using MathParser). Can handle basic arithmetic (e.g. +,-,*), trig mathematical operators (e.g. cos, sin, ln), constants (e.g. pi, e), random number generators (e.g. rand, gauss), statistical operators (e.g. min, max, percentile), colour operators (e.g. red, rgb), focal operators (e.g. row, col, easting, northing) and string operators (e.g. &, compare, substring). For a full list of operators and functions that are handled see MathOperator.

Can also substitute variables z1,z2,x and y with numbers defined by the constructor or the relevant evaluate() method. Alternatively, can handle 'evaluate-time' variable and function substitutions by passing a reference to the class handling the substitution (which must implement the Substitutable interface).

Modified from original code described at www.devx.com/premier/mgznarch/javapro/2001/05may01/ps0105/ps0105.asp.

Version:
2.3, 15th February, 2001, modified 6th November, 2008.
Author:
Original code Daniel Savarese, modified by Jo Wood.
See Also:
MathOperator

Field Summary
static int EXPR_NUMERIC
          Indicates expression returns a numeric value.
static int EXPR_OTHER
          Indicates expression an undefined type.
static int EXPR_SPATIAL
          Indicates expression returns a spatial object.
static int EXPR_STRING
          Indicates expression returns a text string.
static int EXPR_VARIABLE
          Indicates expression returns variable name.
static String HELP_ID
          Indicates that a help message has been requested.
 
Constructor Summary
MathEvaluator()
          Creates an evaluator with no shortcut variables.
MathEvaluator(double[] z)
          Deprecated. This constructor is maintained for backward compatibility only and should not be used with the preferred more general variable substitution mechanism via the Substitutable interface.
 
Method Summary
 Object evaluate(Stack operations)
          Evaluates the given expression stack using (if required), the stored variable substitutions.
 double evaluate(Stack operations, double z1, double z2, double x, double y)
          Deprecated. This method is maintained for backward compatibility only and should not be used with the preferred more general variable substitution mechanism via the Substitutable interface and calling the addVariable() method of MathParser().
 int getExpressionType()
          Reports the type of value returned by the last evaluation.
static String getExpressionTypeAsText(int expressionType)
          Reports the given expression type as text.
 boolean isHelpRequested()
          Reports whether the last evaluation was a request for a help message.
 boolean isInvalid()
          Reports whether the last evaluation produced an invalid result.
 void setSubstitutor(Substitutable substitutor)
          Sets the variable substitutor to considered by the evaluator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HELP_ID

public static final String HELP_ID
Indicates that a help message has been requested.

See Also:
Constant Field Values

EXPR_OTHER

public static final int EXPR_OTHER
Indicates expression an undefined type.

See Also:
Constant Field Values

EXPR_STRING

public static final int EXPR_STRING
Indicates expression returns a text string.

See Also:
Constant Field Values

EXPR_VARIABLE

public static final int EXPR_VARIABLE
Indicates expression returns variable name.

See Also:
Constant Field Values

EXPR_NUMERIC

public static final int EXPR_NUMERIC
Indicates expression returns a numeric value.

See Also:
Constant Field Values

EXPR_SPATIAL

public static final int EXPR_SPATIAL
Indicates expression returns a spatial object.

See Also:
Constant Field Values
Constructor Detail

MathEvaluator

public MathEvaluator()
Creates an evaluator with no shortcut variables. For variable substitution, use the Substitutable interface.


MathEvaluator

public MathEvaluator(double[] z)
Deprecated. This constructor is maintained for backward compatibility only and should not be used with the preferred more general variable substitution mechanism via the Substitutable interface.

Creates an evaluator with variables z1,z2,x,y defined in the given array of length 4.

Parameters:
z - List of variables for substitution. Expressions that use variables should name them z1,z2, x and y.
Method Detail

setSubstitutor

public void setSubstitutor(Substitutable substitutor)
Sets the variable substitutor to considered by the evaluator. This can be used to substitute the contents of a named variable at evaluation time. Note that only variable names that were added to the MathParser class via addVariable() will be evaluated.

Parameters:
substitutor - Class handling the substitution of variables.

evaluate

public Object evaluate(Stack operations)
Evaluates the given expression stack using (if required), the stored variable substitutions. If the result of the evaluation is a numerical value it will be stored in a Double object. If it is a text it will be stored in a String object). To find For other return types, call the returned object's getClass() method.

Parameters:
operations - Expression stack containing postfixed expression.
Returns:
Evaluated expression. Either a String (isString()>/code> is true) or a Double (isString()>/code> is false).

evaluate

public double evaluate(Stack operations,
                       double z1,
                       double z2,
                       double x,
                       double y)
Deprecated. This method is maintained for backward compatibility only and should not be used with the preferred more general variable substitution mechanism via the Substitutable interface and calling the addVariable() method of MathParser().

Evaluates the given expression stack using (if required), the stored variable substitutions. Variables z1, z2, x and y are replaced with the given values. Note that this version does not handle strings, which in all cases are evaluated as 0.

Parameters:
operations - Expression stack containing postfixed expression.
z1 - Value of z1 to substitute.
z2 - Value of z2 to substitute.
x - Value of x to substitute.
y - Value of y to substitute.
Returns:
Evaluated expression.

isInvalid

public boolean isInvalid()
Reports whether the last evaluation produced an invalid result.

Returns:
True if expression produces an invalid result (e.g. infinity, out of bounds NaN etc.).

isHelpRequested

public boolean isHelpRequested()
Reports whether the last evaluation was a request for a help message.

Returns:
True if expression contained request for help message.

getExpressionType

public int getExpressionType()
Reports the type of value returned by the last evaluation.

Returns:
Type of value returned by expression. Should be one of EXPR_NUMERIC, EXPR_STRING, EXPR_SPATIAL or EXPR_OTHER.

getExpressionTypeAsText

public static String getExpressionTypeAsText(int expressionType)
Reports the given expression type as text. Can be used for debugging output from getExpressionType().

Parameters:
expressionType - Expression type to evaluate.
Returns:
Text representing the given expression type.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009