fr.expression4j.core.impl
Class ExpressionImpl

java.lang.Object
  extended by fr.expression4j.core.impl.ExpressionImpl
All Implemented Interfaces:
Expression
Direct Known Subclasses:
OptimizedExpression

public class ExpressionImpl
extends java.lang.Object
implements Expression

 Construct expression relative to the BNF:
 
 expression        ::= term additive-op expression | term
 additive-op       ::= "+" | "-"
 term              ::= factor multiplicatice-op term | factor
 multiplicative-op ::= "*" | "-"
 factor            ::= (signed-expression "^" factor) | signed-expression
 signed-expression ::= additive-op simple-expression | simple-expression
 simple-expression ::= complex | real | constant | variable | function | "(" expression ")"
 
 complex           ::= real "i"
 real              ::= real-simple | real-simple "e" signed-integer
 real-simple       ::= integer | integer "." integer 
 signed-integer    ::= additive-op integer | integer
 integer           ::= number integer | number
 number            ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
 
 constant                        ::= variable
 variable          ::= letters integer | letters
 letters           ::= letter letters | letter
 letter            ::= a - Z
 
 function          ::= function-name "(" parameters ")"
 function-name     ::= variable
 parameters        ::= expression "," parameters | expression | ""
 
 function-def      ::= function-name "(" parameters-def ")=" expression
 parameters-def    ::= variable "," parameters-def | variable | ""
 
 expr              ::= function-def = expression
 
 Valid expression is like:
                - f()=2*5+1
                - g(x)=2.5*x
                - h(x)=-2e-3*(x+1)
                - i(x,y)=h(y)*2+sqrt(g(x))
                - j(z)=h(2*z+4)-g(h(z))
                - ...

 How does it work

 The parser parse expression and create an expression tree modeling the expression.
 Expression is not parsed each time you evaluate it. When evaluate expression
 only the tree is used.


                        g(x)=4*2/2+f(x)
 
                          +
                         / \
                        /    f()
                           / \    |
                          *   2   x
                         / \
                        4   2
 
 

Author:
SGINER

Field Summary
protected  Catalog catalog
           
protected  ExpressionModel expressionModel
           
protected  java.util.List functionParameters
           
protected  java.lang.String name
           
protected  TreeElement rootElement
           
 
Constructor Summary
ExpressionImpl()
          Constructor.
 
Method Summary
 MathematicalElement evaluate(OperatorManager operatorManager, Parameters parameters)
          Evaluate the value of the expression.
 MathematicalElement evaluate(Parameters parameters)
          Evaluate the value of the expression with the default operator manager.
 Catalog getCatalog()
          return the catalog of the expression.
 ExpressionModel getExpressionModel()
          Return the expression model associated to the expression.
 java.lang.String getName()
          Get the name of the function.
 java.util.List getParameters()
          Get the parameter list associated to the function.
 TreeElement getRootElement()
           
static void main(java.lang.String[] args)
           
 void parse(java.lang.String expression, Catalog catalog, ExpressionModel model)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

rootElement

protected TreeElement rootElement

name

protected java.lang.String name

functionParameters

protected java.util.List functionParameters

catalog

protected Catalog catalog

expressionModel

protected ExpressionModel expressionModel
Constructor Detail

ExpressionImpl

public ExpressionImpl()
Constructor.

Method Detail

parse

public void parse(java.lang.String expression,
                  Catalog catalog,
                  ExpressionModel model)
           throws ParsingException
Throws:
ParsingException

getCatalog

public Catalog getCatalog()
Description copied from interface: Expression
return the catalog of the expression.

Specified by:
getCatalog in interface Expression
Returns:
catalog of current expression, null if no catalog is needed of define.

evaluate

public MathematicalElement evaluate(OperatorManager operatorManager,
                                    Parameters parameters)
                             throws EvalException
Description copied from interface: Expression
Evaluate the value of the expression.

Specified by:
evaluate in interface Expression
Parameters:
operatorManager - operator manager to use to evaluate expression.
parameters - parameters values for evaluating expression.
Returns:
value of the evaluated expression as a double.
Throws:
EvalException - when error occurd (parameter not found ...)

evaluate

public MathematicalElement evaluate(Parameters parameters)
                             throws EvalException
Description copied from interface: Expression
Evaluate the value of the expression with the default operator manager.

Specified by:
evaluate in interface Expression
Parameters:
parameters - parameters values for evaluating expression.
Returns:
value of the evaluated expression as a double.
Throws:
EvalException - when error occurd (parameter not found ...)

getName

public java.lang.String getName()
Description copied from interface: Expression
Get the name of the function. If function is f(x,y,z), name if "f".

Specified by:
getName in interface Expression
Returns:
name of the function.

getParameters

public java.util.List getParameters()
Description copied from interface: Expression
Get the parameter list associated to the function. If function is f(x,y,z), parameter list is an ordered list of String with "x", "y" and "z".

Specified by:
getParameters in interface Expression
Returns:
parameter list associated to the expression.

getRootElement

public TreeElement getRootElement()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)

getExpressionModel

public ExpressionModel getExpressionModel()
Description copied from interface: Expression
Return the expression model associated to the expression.

Specified by:
getExpressionModel in interface Expression
Returns:
expression model associated to the expression.