public class Expression
extends java.lang.Object
(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)
Modifier | Constructor and Description |
---|---|
|
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.
|
protected |
Expression(java.util.Vector v)
This constructor isn't for public consumption.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
convertOperand(java.lang.Object o,
java.util.Hashtable h)
Converts an object to its Object type for evaluation
|
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
|
protected java.lang.String |
nextToken(java.util.Vector v) |
protected void |
parseExpression(java.util.Vector v)
Parses the vector of tokens into a hierarchy of expressions.
|
void |
replaceComparisons(java.util.Hashtable h)
Recursively replaces comparison subexpressions with arbitrary
variables ment to represent their actual outcome.
|
protected java.lang.Object |
simplifyOperand(java.lang.Object o)
Converts an object to either a Double (string or numeric
constants) or a String (variable reference).
|
java.lang.String |
toString()
Stringifys it
|
public Expression(java.lang.String s) throws java.text.ParseException
s
- The string version of the expressionjava.text.ParseException
protected Expression(java.util.Vector v) throws java.text.ParseException
java.text.ParseException
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 referencesprotected java.lang.Object convertOperand(java.lang.Object o, java.util.Hashtable h)
o
- The operand to converth
- The hashtable of data to referenceprotected void parseExpression(java.util.Vector v) throws java.text.ParseException
v
- A vector of tokensjava.text.ParseException
protected java.lang.String nextToken(java.util.Vector v)
public 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 converted 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.
protected java.lang.Object simplifyOperand(java.lang.Object o)
o
- The operand to convertpublic 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)