|
http://www.eteks.com | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.eteks.parser.CompiledExpression
Compiled expression using parameters and able to compute its value
with an interpreter. Instances of this class are returned by the
compileExpression ()
method of the ExpressionParser
class,
and can be computed with parameters value. This method parses the definition of an expression
and builds a tree of type ExpressionNode
matching the definition.
The two computeExpression ()
methods of this class allow to compute
the value of an expression according to the value of the parameters returned by
the getParameterValue ()
method of its instance of ExpressionParameter
:
double computeExpression ()
is the easiest
method to use if your expression computes numbers.Object computeExpression (Interpreter interpreter)
allows to choose the implementation of the interpreter that computes
the constants, operators and functions of Syntax
with the
value of parameters.computeExpression ()
returns the same
result :
The other methods of this class returns the different properties of an expression (parameters, definition,...).// A dummy class implementing ExpressionParameter supporting identifiers x and y class XYParameter implements ExpressionParameter { public Object getParameterKey (String parameter) { if ( "x".equalsIgnoreCase (parameter) || "y".equalsIgnoreCase (parameter)) return parameter; else return null; } public Object getParameterValue (Object parameterKey) { if ("x".equalsIgnoreCase ((String)parameterKey)) return new Double (1); else // identifierKey equal to y return new Double (2); } } ExpressionParser parser = new ExpressionParser (new XYParameter ()); CompiledExpression expression = parser.compileExpression ("=x * ln (x)"); double doubleResult1 = expression.computeExpression (); Interpreter interpreter = new DoubleInterpreter (); Object doubleResult2 = expression.computeExpression (interpreter);
ExpressionParser
,
Interpreter
, Serialized FormConstructor Summary | |
CompiledExpression(java.lang.String definition,
java.util.Hashtable parameters,
ExpressionParameter expressionParameter,
ExpressionNode expressionTree)
Creates a compiled expression with its parameters parameters ,
its definition definition , its instance of ExpressionParameter
expressionParameter and the tree expressionTree matching the definition.
|
Method Summary | |
double |
computeExpression()
Returns the result of the expression computed with the value of its parameters returned by the getParameterValue () method of the instance of ExpressionParameter
of this expression.
|
java.lang.Object |
computeExpression(Interpreter interpreter)
Returns the result of the expression computed with the value of its parameters returned by the getParameterValue () method of the instance of ExpressionParameter
of this expression.
|
java.lang.String |
getDefinition()
Returns the definition of this expression. |
ExpressionParameter |
getExpressionParameter()
Returns the instance of ExpressionParameter of this expression used to get
the value of each parameter with its key. |
ExpressionNode |
getExpressionTree()
Returns the tree of this expression, instance of ExpressionNode .
|
int |
getParameterCount()
Returns the number of parameters this expression is requiring at runtime. |
java.util.Hashtable |
getParameters()
Returns a hashtable that contains all the parameters found during the parsing of the definition of this expression. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public CompiledExpression(java.lang.String definition, java.util.Hashtable parameters, ExpressionParameter expressionParameter, ExpressionNode expressionTree)
parameters
,
its definition definition
, its instance of ExpressionParameter
expressionParameter
and the tree expressionTree
matching the definition.
Expressions are not named.definition
- the definition of this expression. This is
the parsed string from which was created this expression.parameters
- a hashtable containing all the pairs (parameter name, parameter key)
of the expression (extracted from the definition).expressionParameter
- the instance of ExpressionParameter
used to retrieve
the value of each parameter with its key.expressionTree
- the tree built by the parser matching the expression.Method Detail |
public java.lang.String getDefinition()
ExpressionParser
that created this expression.public java.util.Hashtable getParameters()
getParameterKey ()
method of the
instance of ExpressionParameter
of this expression, and is used
by the interpreter to get the value of each parameter using the
getParameterValue ()
method of the instance of ExpressionParameter
of this expression.ExpressionParameter
public int getParameterCount()
getParameters ().size ()
.public ExpressionParameter getExpressionParameter()
ExpressionParameter
of this expression used to get
the value of each parameter with its key. This is the same object
as the one stored by the intance of ExpressionParser
used
to compile this expression.ExpressionParameter
of this expression.ExpressionParser
public ExpressionNode getExpressionTree()
ExpressionNode
.
The returned node is the root of the tree matching the definition of this expression.
For example, the expression =x * ln (x)
has the following tree :
BinaryOperatorNode
with OPERATOR_MULTIPLY
keyExpressionParameterNode
for parameter x
keyCommonFunctionNode
with FUNCTION_LN
keyExpressionParameterNode
for parameter x
keypublic double computeExpression()
getParameterValue ()
method of the instance of ExpressionParameter
of this expression.
This method simply calls computeExpression (null)
on the root of its tree.computeExpression (Interpreter)
method of this class.
It can compute only double numbers and all the values returned by
the getParameterValue ()
method of the instance of ExpressionParameter
must be instances of Number
.ExpressionNode
public java.lang.Object computeExpression(Interpreter interpreter)
getParameterValue ()
method of the instance of ExpressionParameter
of this expression.
This method simply calls computeExpression (interpreter, null)
on the root of its tree.interpreter
- the runtime interpreter used to compute the operations of Syntax
.Double
, String
, ...) according to
interpreter
implementation.ExpressionNode
|
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |