chemaxon.jep
Class ChemJEP

java.lang.Object
  extended by chemaxon.nfunk.jep.JEP
      extended by chemaxon.jep.ChemJEP
All Implemented Interfaces:
chemaxon.marvin.util.CallbackIface, java.io.Serializable

public class ChemJEP
extends chemaxon.nfunk.jep.JEP
implements chemaxon.marvin.util.CallbackIface

Expression parser and evaluator for chemical expressions.

Since:
JChem 2.2, Marvin 5.1
Author:
Nora Mate
See Also:
Serialized Form

Field Summary
static int FALSE
          Logical result value: FALSE.
static int TRUE
          Logical result value: TRUE.
static int UNDECIDED
          Logical result value: UNDECIDED.
 
Fields inherited from class chemaxon.nfunk.jep.JEP
allowUndeclared, COMPILE_ERROR, errorList, EVALUATION_ERROR, funTab, implicitMul, symTab
 
Constructor Summary
ChemJEP()
          Constructor.
ChemJEP(java.lang.Class contextClass)
          Constructor.
 
Method Summary
 void addFunction(java.lang.String name, chemaxon.nfunk.jep.function.PostfixMathCommandI function)
          Adds a new function to the parser.
 java.lang.Object callback(java.lang.String method, java.lang.Object arg)
          Delegates CallbackIface.callback(String, Object) to the context.
 void compile(java.lang.String expression)
          Compiles the expression string to an inner structure.
 boolean evaluate_boolean(ChemContext context)
          Evaluates the expression and converts the result boolean.
 boolean[] evaluate_booleans(ChemContext context)
          Evaluates the expression and converts the result to boolean[].
 double evaluate_double(ChemContext context)
          Evaluates the expression and converts the result to double.
 double[] evaluate_doubles(ChemContext context)
          Evaluates the expression and converts the result to double[].
 java.lang.Object evaluate(ChemContext context)
          Evaluates the expression.
static int getLogicalValue(java.lang.Object result)
          Returns the logical correspondent of the specified result object: TRUE, FALSE or UNDECIDED.
static boolean isUndecided(java.lang.Object result)
          Returns whether the result object is undecided.
protected  void setSymbolTable(chemaxon.nfunk.jep.SymbolTable symTab)
          Sets the SymbolTable object.
 
Methods inherited from class chemaxon.nfunk.jep.JEP
addComplex, addComplexVariable, addError, addStandardConstants, addStandardFunctions, addVariable, addVariableAsObject, getComplexValue, getError, getErrorCount, getErrorInfo, getErrors, getFunction, getNumberFactory, getSymbolTable, getTopNode, getValue, getValueAsObject, hasError, initFunTab, initSymTab, parseExpression, removeFunction, removeVariable, setAllowUndeclared, setImplicitMul, setTraverse
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FALSE

public static final int FALSE
Logical result value: FALSE.

See Also:
Constant Field Values

TRUE

public static final int TRUE
Logical result value: TRUE.

See Also:
Constant Field Values

UNDECIDED

public static final int UNDECIDED
Logical result value: UNDECIDED.

See Also:
Constant Field Values
Constructor Detail

ChemJEP

public ChemJEP()
        throws chemaxon.nfunk.jep.ParseException
Constructor. Initiates the this JEP with no context.

Throws:
chemaxon.nfunk.jep.ParseException - on error

ChemJEP

public ChemJEP(java.lang.Class contextClass)
        throws chemaxon.nfunk.jep.ParseException
Constructor. Sets the context class, adds context functions.

Parameters:
contextClass - is the context class
Throws:
chemaxon.nfunk.jep.ParseException - on error
Method Detail

addFunction

public void addFunction(java.lang.String name,
                        chemaxon.nfunk.jep.function.PostfixMathCommandI function)
                 throws chemaxon.nfunk.jep.ParseException
Adds a new function to the parser. This must be done before parsing an expression so the parser is aware that the new function may be contained in the expression.

Overrides:
addFunction in class chemaxon.nfunk.jep.JEP
Parameters:
name - is the name of the function
function - is the function object that is used for evaluating the function
Throws:
chemaxon.nfunk.jep.ParseException - if there exists a function with the same name

setSymbolTable

protected void setSymbolTable(chemaxon.nfunk.jep.SymbolTable symTab)
Sets the SymbolTable object.

Parameters:
symTab - is the SymbolTable object

compile

public void compile(java.lang.String expression)
             throws chemaxon.nfunk.jep.ParseException
Compiles the expression string to an inner structure. This speeds-up evaluation.

Parameters:
expression - is the expression string
Throws:
chemaxon.nfunk.jep.ParseException - on compile error

callback

public java.lang.Object callback(java.lang.String method,
                                 java.lang.Object arg)
Delegates CallbackIface.callback(String, Object) to the context.

Specified by:
callback in interface chemaxon.marvin.util.CallbackIface
Parameters:
method - is the method name
arg - is the method argument

evaluate

public java.lang.Object evaluate(ChemContext context)
                          throws chemaxon.nfunk.jep.ParseException
Evaluates the expression. The expression string must have been given and compiled beforehand by compile(String expression).

Parameters:
context - is the evaluation context
Returns:
the evaluation result
Throws:
chemaxon.nfunk.jep.ParseException - on evaluation error

evaluate_double

public double evaluate_double(ChemContext context)
                       throws chemaxon.nfunk.jep.ParseException
Evaluates the expression and converts the result to double. The expression string must have been given and compiled beforehand by compile(String expression).

Parameters:
context - is the evaluation context
Returns:
the evaluation result
Throws:
EvaluationException - on evaluation error
chemaxon.nfunk.jep.ParseException

evaluate_doubles

public double[] evaluate_doubles(ChemContext context)
                          throws chemaxon.nfunk.jep.ParseException
Evaluates the expression and converts the result to double[]. This is useful if the result is an object array in which case each element is converted to double. The expression string must have been given and compiled beforehand by compile(String expression).

Parameters:
context - is the evaluation context
Returns:
the evaluation result
Throws:
EvaluationException - on evaluation error
chemaxon.nfunk.jep.ParseException

evaluate_boolean

public boolean evaluate_boolean(ChemContext context)
                         throws chemaxon.nfunk.jep.ParseException
Evaluates the expression and converts the result boolean. Boolean return values are returned as double: 1.0 for true and 0.0 for false. This function returns true for a value greater than 0.5, false otherwise. This means that for boolean expressions the return value will be OK (and we also eliminated floating-point comparison inaccuracy :-). The expression string must have been given and compiled beforehand by compile(String expression).

Parameters:
context - is the evaluation context
Returns:
the evaluation result
Throws:
EvaluationException - on evaluation error
chemaxon.nfunk.jep.ParseException

evaluate_booleans

public boolean[] evaluate_booleans(ChemContext context)
                            throws chemaxon.nfunk.jep.ParseException
Evaluates the expression and converts the result to boolean[]. This is useful if the result is an object array in which case each element is converted to boolean. The expression string must have been given and compiled beforehand by compile(String expression).

Parameters:
context - is the evaluation context
Returns:
the evaluation result
Throws:
EvaluationException - on evaluation error
chemaxon.nfunk.jep.ParseException

getLogicalValue

public static int getLogicalValue(java.lang.Object result)
Returns the logical correspondent of the specified result object: TRUE, FALSE or UNDECIDED.

Parameters:
result - is the result object
Returns:
the corresponding logical value
Since:
JChem 3.2

isUndecided

public static boolean isUndecided(java.lang.Object result)
Returns whether the result object is undecided.

Returns:
true if the result is undecided