All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class gnu.jel.Evaluator

java.lang.Object
   |
   +----gnu.jel.Evaluator

public class Evaluator
extends Object
This class is intended for evaluation of the algebraic expressions.

There is not much sense to use this class when expression has to be evaluated only once. The overhead of compilation makes usage of interpreter much more suitable in this case. If, however, there is a need to evaluate some function repeatedly (functions plotting, numerical methods,...), this class should be very well suitable.

The expression language apart of usage of the arifmetic expressions allows to call functions. Functions are divided then into two groups. "Stateless" functions, whose result depends on their argument only (no state) and "virtual" functions, whose result explicitly depends on some externally given state (Object pointer). Functions should be defined prior to the expression and passed in the gnu.jel.Library object.

For example, if You a library based on java.lang.Math as the static lirary the following expressions will be supported : "sin(0.1)", "sin(0.1)*cos(0.01)+abs(-1)".

If class is nominated as static library all its public static methods are exported for use in expressions. If it is nominated as dynamic library all of it's virtual methods are exported. The basic difference is that the dynamic libraries require class instance variable of corresponding class to be supplied to the compiled code, the static methods do not.

Syntax supports variables, which are, in fact, functions with no argiments. If You define such function in the static library -- it is a constant. If you define the method , such as "double x() {};" in dynamic library class the expression "sin(x)" will call the method "x()" ( and function Math.sin() ) each time it is evaluated.

All constants in the expressions are assumed to be either long and double java types. It is possible to have any type of intermediate object throughout the calculation as long as types of the function return values and parameters stay compatible.

Result is always returned as an object, even if it was calculated as a primitive type. Precisely if the result of the expression is primitive integral type ( byte, char, short, int, boolean) it's converted to long and the new java.lang.Long object is returned. Floating point types ( float, double) are returned as java.lang.Double.

Care should be taken during the assembly of static and dynamic libraries to avoid conflicts and unsupported return types.

(c) 1998, by Konstantin Metlov
Prague, CZ


Constructor Index

 o Evaluator()

Method Index

 o compile(String, Library)
Compiles expression, resolving the function names in the library.

Constructors

 o Evaluator
 public Evaluator()

Methods

 o compile
 public static CompiledExpression compile(String expression,
                                          Library lib) throws CompilationException
Compiles expression, resolving the function names in the library.

Parameters:
expression - is the expression to compile. i.e. "sin(666)" .
Returns:
Instance of the CompiledExpression subclass, implementing the specified expression evaluation.
Throws: CompilationException
if the expression is not syntactically or semantically correct.
See Also:
evaluate

All Packages  Class Hierarchy  This Package  Previous  Next  Index