http://www.eteks.com

com.eteks.parser
Class CompiledExpression

java.lang.Object
  |
  +--com.eteks.parser.CompiledExpression
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
JeksExpression

public class CompiledExpression
extends java.lang.Object
implements java.io.Serializable

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 :

For example, the 2 following calls to computeExpression () returns the same result :
 // 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);
The other methods of this class returns the different properties of an expression (parameters, definition,...).

Since:
Jeks 1.0
Version:
1.0
Author:
Emmanuel Puybaret
See Also:
ExpressionParser, Interpreter, Serialized Form

Constructor 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

CompiledExpression

public 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. Expressions are not named.
Parameters:
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

getDefinition

public java.lang.String getDefinition()
Returns the definition of this expression. This is the string parsed by the instance of ExpressionParser that created this expression.
Returns:
the definition of this expression.

getParameters

public java.util.Hashtable getParameters()
Returns a hashtable that contains all the parameters found during the parsing of the definition of this expression. This hashtable stores a pair of (key,value) for each parameter required by this expression with its name as the key and a key matching the parameter as the value. A parameter key is returned by the 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.
Returns:
a hashtable containing the parameters name and their matching key.
See Also:
ExpressionParameter

getParameterCount

public int getParameterCount()
Returns the number of parameters this expression is requiring at runtime.
Returns:
the number of parameters (may be equal to 0) : it's equal to getParameters ().size ().

getExpressionParameter

public ExpressionParameter getExpressionParameter()
Returns the instance of 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.
Returns:
the instance of ExpressionParameter of this expression.
See Also:
ExpressionParser

getExpressionTree

public ExpressionNode getExpressionTree()
Returns the tree of this expression, instance of 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 :
Returns:
the tree of this expression.

computeExpression

public 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. This method simply calls computeExpression (null) on the root of its tree.
As this method avoid method calls to an interpreter and instantiation of objects for parameters and return values, it runs much faster than the 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.
Returns:
the result of the function.
See Also:
ExpressionNode

computeExpression

public 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. This method simply calls computeExpression (interpreter, null) on the root of its tree.
This method is thread safe.
Parameters:
interpreter - the runtime interpreter used to compute the operations of Syntax.
Returns:
the result of the function. The returned object may be of any class (Double, String, ...) according to interpreter implementation.
See Also:
ExpressionNode

&cp; 1998-2003 eTeks - All rights reserved