edu.sdsc.inca.util
Class ExprEvaluator

java.lang.Object
  extended byedu.sdsc.inca.util.ExprEvaluator

public class ExprEvaluator
extends java.lang.Object

A simple top-down boolean expression evaluator. Supports the boolean binary operators <, <=, >, >=, ==, !=, <>, &&, and ||, as well as perl's pattern match (=~) and mismatch (!~) operators. Supports parenthesized sub-expressions. Operands may be any of: a quoted string literal; a numeric literal; a pattern literal enclosed in slashes; an identifier defined in the report body by an ID tag; a version literal composed of digits, letters, underscores and dots.

Author:
jhayes

Field Summary
static java.lang.String FAIL_EXPR_FAILED
          Failure result values from eval.
static java.lang.String FAIL_INVALID_OPERATOR
           
static java.lang.String FAIL_MISSING_OPERAND
           
static java.lang.String FAIL_MISSING_OPERATOR
           
static java.lang.String FAIL_MISSING_RPAREN
           
static java.lang.String FAIL_UNDEFINED_SYMBOL
           
protected static java.util.regex.Pattern OPERAND_PATTERN
          Operands: a quoted or slash-surrounded literal or a symbol.
 
Constructor Summary
ExprEvaluator()
           
 
Method Summary
protected static java.lang.String binary(java.lang.StringBuffer expr, java.util.Properties symbols, java.lang.String valueIfUndef)
          Part of a simple top-down expression evaluator.
protected static java.lang.String conjunction(java.lang.StringBuffer expr, java.util.Properties symbols, java.lang.String valueIfUndef)
          Part of a simple top-down expression evaluator.
static java.lang.String eval(java.lang.String expr, java.util.Properties symbols, java.lang.String valueIfUndef)
          Evaluate a boolean expression given a set of symbols.
protected static java.lang.String getOperand(java.lang.StringBuffer expr)
          Part of a simple top-down expression evaluator.
protected static java.lang.String parenthesized(java.lang.StringBuffer expr, java.util.Properties symbols, java.lang.String valueIfUndef)
          Part of a simple top-down expression evaluator.
protected static void skipSpaces(java.lang.StringBuffer sb)
          Removes leading spaces from a StringBuffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FAIL_EXPR_FAILED

public static java.lang.String FAIL_EXPR_FAILED
Failure result values from eval.


FAIL_INVALID_OPERATOR

public static java.lang.String FAIL_INVALID_OPERATOR

FAIL_MISSING_OPERAND

public static java.lang.String FAIL_MISSING_OPERAND

FAIL_MISSING_OPERATOR

public static java.lang.String FAIL_MISSING_OPERATOR

FAIL_MISSING_RPAREN

public static java.lang.String FAIL_MISSING_RPAREN

FAIL_UNDEFINED_SYMBOL

public static java.lang.String FAIL_UNDEFINED_SYMBOL

OPERAND_PATTERN

protected static java.util.regex.Pattern OPERAND_PATTERN
Operands: a quoted or slash-surrounded literal or a symbol.

Constructor Detail

ExprEvaluator

public ExprEvaluator()
Method Detail

eval

public static java.lang.String eval(java.lang.String expr,
                                    java.util.Properties symbols,
                                    java.lang.String valueIfUndef)
Evaluate a boolean expression given a set of symbols.

Parameters:
expr - the expression to evaluate
symbols - a table that associates symbol names with values
valueIfUndef - the value to use for identifiers encountered in the expression that have no value in the symbol table. If null, the method throws an exception when it encounters an undefined symbol.
Returns:
null if the expression is true; otherwise, a failure message that consists of one of the result values above optionally followed by a colon and a space-delimited set of symbols participating in the failure

conjunction

protected static java.lang.String conjunction(java.lang.StringBuffer expr,
                                              java.util.Properties symbols,
                                              java.lang.String valueIfUndef)
Part of a simple top-down expression evaluator. This precedence level strips and evaluates the left-associative && and || operators.

Parameters:
expr - the expression to evaluate
symbols - a table that associates symbol names with values
valueIfUndef - the value to use for identifiers encountered in the expression that have no value in the symbol table. If null, the method throws an exception when it encounters an undefined symbol.
Returns:
null if the expression is true; otherwise, a failure message that consists of one of the result values above optionally followed by a colon and a space-delimited set of symbols participating in the failure

parenthesized

protected static java.lang.String parenthesized(java.lang.StringBuffer expr,
                                                java.util.Properties symbols,
                                                java.lang.String valueIfUndef)
Part of a simple top-down expression evaluator. This precedence level strips and evaluates parenthesized sub-expressions.

Parameters:
expr - the expression to evaluate
symbols - a table that associates symbol names with values
valueIfUndef - the value to use for identifiers encountered in the expression that have no value in the symbol table. If null, the method throws an exception when it encounters an undefined symbol.
Returns:
null if the expression is true; otherwise, a failure message that consists of one of the result values above optionally followed by a colon and a space-delimited set of symbols participating in the failure

binary

protected static java.lang.String binary(java.lang.StringBuffer expr,
                                         java.util.Properties symbols,
                                         java.lang.String valueIfUndef)
Part of a simple top-down expression evaluator. This precedence level strips and evaluates the binary operations <, <=, >, >=, ==, !=, <>, =~, and !~.

Parameters:
expr - the expression to evaluate
symbols - a table that associates symbol names with values
valueIfUndef - the value to use for identifiers encountered in the expression that have no value in the symbol table. If null, the method throws an exception when it encounters an undefined symbol.
Returns:
null if the expression is true; otherwise, a failure message that consists of one of the result values above optionally followed by a colon and a space-delimited set of symbols participating in the failure

getOperand

protected static java.lang.String getOperand(java.lang.StringBuffer expr)
Part of a simple top-down expression evaluator. This precedence level strips and returns operands.

Parameters:
expr - the expression to evaluate
Returns:
the operand at the beginning of the expression; null if none

skipSpaces

protected static void skipSpaces(java.lang.StringBuffer sb)
Removes leading spaces from a StringBuffer.

Parameters:
sb - the string buffer to trim