gnu.jel
Class Evaluator

java.lang.Object
  extended by gnu.jel.Evaluator

public class Evaluator
extends Object

This is the main frontend to JEL.

It is intended for compilation of algebraic expressions involving functions.

Syntax allows variables, which can be either a public fields of certain objects or functions with no arguments. If a method like "double x() {};" is defined in the dynamic library class (see gnu.jel.Library documentation on how to do this), an expression "sin(x)" will call the method "x()" ( and function Math.sin() ) each time it is evaluated. Static methods in namespace are assumed to be stateless (unless this default behaviour is explicitly overridden, as is necessary for Math.random()) and will be called at compile time if their arguments are known.

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. The compiler can do all the type conversions permissible in Java language and more (e.g. between reflection wrappers java.lang.Integer,... and primitives). Widening type conversions (not leading to precision loss) are applied by JEL automatically, narrowing should be explicitly requested.

There is variant of the "compile" function with three arguments, which allows to fix the type of the expression result. For example:

 CompiledExpression expression=compile("2*6+6",lib,Double.TYPE); 
 
will produce a compiled expression, whose return type is always double. For additional information on how to use this feature to eliminate object allocation overhead see gnu.jel.CompiledExpression documentation.

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

(c) 1998-2003, by Konstantin Metlov
Prague, CZ

See Also:
CompiledExpression, Library

Field Summary
protected static ClassFile cf_orig
           
protected static LocalMethod[] eval_methods
           
protected static int retID_patchback
           
protected static int retIDC_patchback
           
 
Constructor Summary
Evaluator()
           
 
Method Summary
static CompiledExpression compile(String expression, Library lib)
          Compiles expression, resolving the function names in the library.
static CompiledExpression compile(String expression, Library lib, Class resultType)
          Compiles expression, resolving the function names in the library.
static byte[] compileBits(String expression, Library lib)
          Compiles expression, resolving the function names in the library.
static byte[] compileBits(String expression, Library lib, Class resultType)
          Compiles expression, resolving the function names in the library.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cf_orig

protected static ClassFile cf_orig

retID_patchback

protected static int retID_patchback

retIDC_patchback

protected static int retIDC_patchback

eval_methods

protected static LocalMethod[] eval_methods
Constructor Detail

Evaluator

public Evaluator()
Method Detail

compile

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

Parameters:
expression - is the expression to compile. i.e. "sin(666)" .
lib - Library of functions exported for use in expression.
resultType - identifies the type result should be converted to. Can be null, in this case the result type is not fixed.
Returns:
Instance of the CompiledExpression subclass, implementing the specified expression evaluation.
Throws:
CompilationException - if the expression is not syntactically or semantically correct.
See Also:
CompiledExpression

compileBits

public static byte[] compileBits(String expression,
                                 Library lib,
                                 Class resultType)
                          throws CompilationException
Compiles expression, resolving the function names in the library.

This variant of compile allows to store expressions in a java.io.OutputStream using Java serialization mechanism.

Parameters:
expression - is the expression to compile. i.e. "sin(666)" .
lib - Library of functions exported for use in expression.
resultType - identifies the type result should be converted to. Can be null, in this case the result type is not fixed.
Returns:
Byte array, representing the expression in a standard Java classfile format. It can be conveniently loaded (with renaming, if necessary) into Java VM using gnu.jel.ImageLoader.
Throws:
CompilationException - if the expression is not syntactically or semantically correct.
See Also:
CompiledExpression, ImageLoader

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)" .
lib - Library of functions exported for use in expression.
Returns:
Instance of the CompiledExpression subclass, implementing the specified expression evaluation.
Throws:
CompilationException - if the expression is not syntactically or semantically correct.
See Also:
CompiledExpression

compileBits

public static byte[] compileBits(String expression,
                                 Library lib)
                          throws CompilationException
Compiles expression, resolving the function names in the library.

This variant of compile allows to store expressions in a java.io.OutputStream using Java serialization mechanism.

Parameters:
expression - is the expression to compile. i.e. "sin(666)" .
lib - Library of functions exported for use in expression.
Returns:
Byte array, representing the expression in a standard Java classfile format. It can be conveniently loaded (with renaming, if necessary) into Java VM using gnu.jel.ImageLoader.
Throws:
CompilationException - if the expression is not syntactically or semantically correct.
See Also:
CompiledExpression, ImageLoader


Copyright © 1998-2009 Konstantin L. Metlov All Rights Reserved.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the JEL manual.