utilities
Class Expression

java.lang.Object
  |
  +--utilities.Expression

public class Expression
extends java.lang.Object

This class allows you to evaluate simple, fully parenthesized expressions, using references from a given hashtable. Here's some examples:

   (a == b)
   ((a != b) || (b == c))
   ((a >= 0) && (a < 10))
   ((a != null) && (a == "some string"))
   (((!(a == b)) && (c < 10)) || (d == "cheese"))
   ((a + 5) == b)
   ((3 / 2) == 1)
   (((a + b) < c) && ((c / 2) > 5))
   ((a % 2) == 0)
   ((a % 2) == 1)
 
In the above, numbers and quoted strings represent their given values. Bare words (e.g. a, b, c, and d above) represent references to variables, which will be dereferenced from a hashtable when the expression is evaluated. For instance, in the third example above, when the expression is evaluated the key "a" will be used to query a hashtable (passed into the evaluation function). Let's say in the hashtable there is a field "5" stored under the key "a". So this expression will then actually evaluate to be ((5 >= 0) && (5 < 10)), which will be true. Data in the hashtable, and in quoted strings, will be converted to a number if possible, permitting typical numeric comparisons. In general the expressions parsed by this class should be pretty intuitive.

The keyword "null" can be used to check for the absence of a key in the hashtable, as in the fourth example above.

Operators permitted: ==, !=, <, >, <=, >=, !, &&, ||, +, -, *, /, %.

See Also:
isTrue(java.util.Hashtable)

Constructor Summary
Expression(java.lang.String s)
          Pass your string representation of the expression into this function and it will parse it into a hierarchy of Expression objects which can be evaluated.
 
Method Summary
 java.lang.Object evaluate(java.util.Hashtable h)
          This method performs the evaluation of the expression, implementing binary and unary operator functions, and recursively calling itself for sub-expressions.
 java.util.Enumeration findVariables()
          Returns a list of the variables present in the expression.
 java.util.Hashtable generateLinearForm()
          Converts the expression to a more normal polynomial form, consisting of a sum of a number of variable/coefficient pairs.
 java.lang.Object getOperand1()
          Accessors
 java.lang.Object getOperand2()
           
 java.lang.Object getOperator()
           
 boolean isBooleanExpression()
          Determines if the expression is a boolean one (i.e.
 boolean isComparisonExpression()
          Determines if the expression is a comparison one (i.e.
 boolean isFalse(java.util.Hashtable h)
          Just like isTrue, except not.
 boolean isMathematicalExpression()
          Determines if the expression is a mathematics one (i.e.
 boolean isSatisfiable()
          Determines if the expression is satisfiable - that there is some possible assignment of variables such that the expression will as a whole be true.
 boolean isTrue(java.util.Hashtable h)
          Use this function to check if the expression evaluates to be true or not.
static void main(java.lang.String[] argv)
          This is just to test the Expression
 void replaceComparisons(java.util.Hashtable h)
          Recursively replaces comparison subexpressions with arbitrary variables ment to represent their actual outcome.
 java.lang.String toString()
          Stringifys it
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Expression

public Expression(java.lang.String s)
           throws java.text.ParseException
Pass your string representation of the expression into this function and it will parse it into a hierarchy of Expression objects which can be evaluated.
Parameters:
s - The string version of the expression
Method Detail

getOperand1

public java.lang.Object getOperand1()
Accessors

getOperand2

public java.lang.Object getOperand2()

getOperator

public java.lang.Object getOperator()

isTrue

public boolean isTrue(java.util.Hashtable h)
Use this function to check if the expression evaluates to be true or not. The hashtable is a set of key/value pairs where any bare word in the expression is used as a key. When the expression is evaluated, it uses these keys to find the appropriate value (if any) in the hashtable and then perform the check with that value.

This function uses the evaluate method to calculate the value of the expression.

Parameters:
h - A hashtable of references
Returns:
True if the expression evaluates to true

isFalse

public boolean isFalse(java.util.Hashtable h)
Just like isTrue, except not.
Parameters:
h - A hashtable of references
Returns:
true, if the expression is false
See Also:
isTrue(java.util.Hashtable)

evaluate

public java.lang.Object evaluate(java.util.Hashtable h)
This method performs the evaluation of the expression, implementing binary and unary operator functions, and recursively calling itself for sub-expressions.

This short ciruits ||s and &&s.

Parameters:
h - A hashtable of references
Returns:
True if the expression evaluates to true

generateLinearForm

public java.util.Hashtable generateLinearForm()
Converts the expression to a more normal polynomial form, consisting of a sum of a number of variable/coefficient pairs. This data is returned in a hashtable, where each key is a variable name, and the data stored in that key will be a Double representing the coefficient. The special key CONSTANT will contain the constant values, and the key OPERATOR will contain the operator string.

All the items in the expression will be moved over to the LHS, so the RHS is effectively zero.

In this function, strings are considered constants, and will be comverted to some Double form with reasonable uniqueness within the space.

If the expression cannot be reduced to this form, because of embedded boolean expressions or nonlinear expressions null will be returned. It will also return null for linear multiplications, i.e. (a * b) cannot be represented this way.

Returns:
A hashtable as described above.

isSatisfiable

public boolean isSatisfiable()
Determines if the expression is satisfiable - that there is some possible assignment of variables such that the expression will as a whole be true.
Returns:
true if the expression is satisfiable

replaceComparisons

public void replaceComparisons(java.util.Hashtable h)
Recursively replaces comparison subexpressions with arbitrary variables ment to represent their actual outcome. In a nutshell, this will convert all comparison subexpressions to simple boolean variables, so that the expression as a whole will then be just a simple boolean expression. The replacement mapping is stored in the provided hashtable.
Parameters:
h - Send in an empty hashtable, it will contain the boolean variable to replaced expression mapping on completion.

findVariables

public java.util.Enumeration findVariables()
Returns a list of the variables present in the expression. Each variable is a String.
Returns:
An enumeration of Strings

isBooleanExpression

public boolean isBooleanExpression()
Determines if the expression is a boolean one (i.e. &&, ||, or !, or just a simple Boolean object).
Returns:
true if the expression is a boolean one

isComparisonExpression

public boolean isComparisonExpression()
Determines if the expression is a comparison one (i.e. <, >, ==, !=, >=, <=).
Returns:
true if the expression is a comparison one

isMathematicalExpression

public boolean isMathematicalExpression()
Determines if the expression is a mathematics one (i.e. +, -, /, %, *)
Returns:
true if the expression is a mathematical one

toString

public java.lang.String toString()
Stringifys it
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] argv)
This is just to test the Expression