http://www.eteks.com

com.eteks.parser
Class CompiledFunction

java.lang.Object
  |
  +--com.eteks.parser.CompiledFunction
All Implemented Interfaces:
Function, java.io.Serializable

public class CompiledFunction
extends java.lang.Object
implements Function

Compiled functions with a fixed number of parameters and able to compute its value with an interpreter. Instances of this class are returned by the compileFunction () method of the FunctionParser class, and can be computed with the value of its parameters. This method parses the definition of a function and builds a tree of type ExpressionNode matching the expression of the function.
The two computeFunction () methods of this class allow to compute the value of a function according to the value of the parameters passed to the method :

For example, the 2 following calls to computeFunction () returns the same result :
 FunctionParser parser = new FunctionParser ();
 CompiledFunction function = parser.compileFunction ("f(x) = x * ln (x)");

 double doubleResult1 = function.computeFunction (new double [] {2});

 Interpreter interpreter = new DoubleInterpreter ();
 Object doubleResult2 = function.computeFunction (interpreter, new Double [] {new Double (2)});
The other methods of this class returns the different properties of a function (name, parameters, definition,...).

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

Constructor Summary
CompiledFunction(java.lang.String definition, java.lang.String name, java.lang.String[] parameters, ExpressionNode expressionTree)
          Creates a compiled function named name with its parameters parameters, its definition definition and the tree expressionTree matching the expression of the function.
 
Method Summary
 double computeFunction(double[] parametersValue)
          Returns the result of the function computed with the value of its parameters.
 java.lang.Object computeFunction(Interpreter interpreter, java.lang.Object[] parametersValue)
          Returns the result of the function computed with the value of its parameters parametersValue.
 java.lang.String getDefinition()
          Returns the definition of this function.
 ExpressionNode getExpressionTree()
          Returns the tree of the expression of this function.
 java.lang.String getName()
          Returns the name of this function.
 int getParameterCount()
          Returns the number of parameters this function is requiring at runtime.
 java.lang.String[] getParameters()
          Returns the array of the name of the parameters this function is requiring at runtime.
 boolean isValidParameterCount(int count)
          Returns true if count is equal to the required number of parameters.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompiledFunction

public CompiledFunction(java.lang.String definition,
                        java.lang.String name,
                        java.lang.String[] parameters,
                        ExpressionNode expressionTree)
Creates a compiled function named name with its parameters parameters, its definition definition and the tree expressionTree matching the expression of the function.
Parameters:
definition - the definition of this function. This is the parsed string from which was created this function.
name - the name of this function (extracted from the definition)
parameters - the name of its parameters (extracted from the definition)
expressionTree - the tree built by the parser matching the expression of the function.
Method Detail

getDefinition

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

getName

public java.lang.String getName()
Returns the name of this function.
Specified by:
getName in interface Function
Returns:
a string which is the public name that may be supported by a syntax to call this function in expressions parsed with instances of FunctionParser or ExpressionParser.

getParameters

public java.lang.String[] getParameters()
Returns the array of the name of the parameters this function is requiring at runtime.
Returns:
an array containing the parameters name.

getParameterCount

public int getParameterCount()
Returns the number of parameters this function is requiring at runtime.
Returns:
the number of parameters (may be equal to 0).

isValidParameterCount

public boolean isValidParameterCount(int count)
Returns true if count is equal to the required number of parameters.
Specified by:
isValidParameterCount in interface Function
Following copied from interface: com.eteks.parser.Function
Parameters:
count - Number of parameters (may be equal to 0).

getExpressionTree

public ExpressionNode getExpressionTree()
Returns the tree of the expression of this function. The returned node is the root of the tree matching the expression defined in the function. For example, the function f(x) = x * ln (x) has the following tree :
Returns:
the tree of this function.

computeFunction

public double computeFunction(double[] parametersValue)
Returns the result of the function computed with the value of its parameters. This method simply calls computeExpression (parametersValue) 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 computeFunction (Interpreter, Object []) method of this class, but of course it can compute only double numbers.
Parameters:
parametersValue - the value of parameters required to compute this function.
Returns:
the result of the function.
See Also:
ExpressionNode

computeFunction

public java.lang.Object computeFunction(Interpreter interpreter,
                                        java.lang.Object[] parametersValue)
Returns the result of the function computed with the value of its parameters parametersValue. This method simply calls computeExpression (interpreter, parametersValue) on the root of its tree.
This method is thread safe.
Specified by:
computeFunction in interface Function
Parameters:
interpreter - the runtime interpreter used to compute the operations of Syntax.
parametersValue - the value of parameters required to compute this function (already evaluated if they are expressions). parametersValue may be null if this function doesn't require parameters.
Returns:
the result of the function. The returned object may be of any class (Double, String, ...) according to interpreter implementation.
Throws:
java.lang.IllegalArgumentException - if the parametersValue size is different of the required one.
See Also:
ExpressionNode

&cp; 1998-2003 eTeks - All rights reserved