|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--utilities.Expression
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: ==, !=, <, >, <=, >=, !, &&, ||, +, -, *, /, %.
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 |
public Expression(java.lang.String s) throws java.text.ParseException
s
- The string version of the expressionMethod Detail |
public java.lang.Object getOperand1()
public java.lang.Object getOperand2()
public java.lang.Object getOperator()
public boolean isTrue(java.util.Hashtable h)
This function uses the evaluate method to calculate the value of the expression.
h
- A hashtable of referencespublic boolean isFalse(java.util.Hashtable h)
h
- A hashtable of referencesisTrue(java.util.Hashtable)
public java.lang.Object evaluate(java.util.Hashtable h)
This short ciruits ||s and &&s.
h
- A hashtable of referencespublic java.util.Hashtable generateLinearForm()
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.
public boolean isSatisfiable()
public void replaceComparisons(java.util.Hashtable h)
h
- Send in an empty hashtable, it will contain the boolean
variable to replaced expression mapping on completion.public java.util.Enumeration findVariables()
public boolean isBooleanExpression()
public boolean isComparisonExpression()
public boolean isMathematicalExpression()
public java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] argv)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |