|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.japisoft.formula.Formula
Resolver of Mathematical Expression.
Example of expression :( 1 / 2 ) * ( m * g ) ^2 + eval( m, g ) - m and g are symbols or variables - eval is a function with two parameters
JFormula supports simplest format expression like : 2x+3x^2...
Usage sample :
Formula f = new Formula("2+cos(PI)");Variant res = f.evaluate(); double r = res.getDoubleValue();
Formula f = new Formula( "A=1\nB=A+1\nA+B" ); Variant res = f.evaluate(); double r = res.getDoubleValue(); // 3 f.getValueForSymbol( "A" ).getDoubleValue() // 1 f.getValueForSymbol( "B" ).getDoubleValue() // 2
Formula f = new Formula( "A=2\nif ( A > 1 ) then true else false" ); Variant res = f.evaluate(); res.getBooleanValue() // true
a variable value is retrieved by a symbol table or by a
SymbolResolver
delegate. The symbol table is updated while
evaluating (A=...) or by the user before evaluating calling
setSymbolValue( "A", ... )
addSymbolResolver
or/and
addFunctionResolver
with your delegate.
For overriding or create a new operator, call the getOperatorFactory. Note that
each time you call the method setHighPrecisionMode
the factory is updated and your
last change can be lost.
Field Summary | |
static int |
DEF_HP_SCALE
This is the default scale for the high precision mode |
static int |
DEF_ROUNDING_MODE
Default rounding mode for high precision usage |
static java.lang.String |
E_SYMBOL
Name of the symbol for the Exponential value |
static java.lang.String |
FALSE_SYMBOL
Name of the symbol for the 'false' value |
static java.lang.String |
PI_SYMBOL
Name of the symbol for the PI value |
static java.lang.String |
TRUE_SYMBOL
Name of the symbol for the 'true' value |
Constructor Summary | |
Formula()
Default constructor |
|
Formula(boolean highPrecisionMode)
Update the inner formula evaluator for working with java.lang.BigDecimal rather than with double value. |
|
Formula(Formula formulaContext)
This is a way to share symbol values / resolvers between different various formula context |
|
Formula(java.lang.String expression)
|
|
Formula(java.lang.String expression,
boolean highPrevisionMode)
|
Method Summary | |
void |
addFunctionResolver(FunctionResolver resolver)
add a function resolver. |
void |
addSymbolResolver(SymbolResolver resolver)
add a symbol resolver. |
Variant |
evaluate()
This method will evaluate the current expression. |
java.lang.String |
getExpression()
|
OperatorFactory |
getOperatorFactory()
|
java.util.Enumeration |
getSymbolNames()
|
java.util.Enumeration |
getSymbolsFromExpression()
|
Variant |
getValueForFunction(java.lang.String functionName,
ListOfArgument parameters)
|
Variant |
getValueForSymbol(java.lang.String symbolName)
|
boolean |
isHighPrecisionMode()
|
AbstractNode |
parse()
Parse the expression and return a node for evaluation. |
void |
removeFunctionResolver(FunctionResolver resolver)
Remove a known function resolver |
void |
removeSymbolResolver(SymbolResolver resolver)
Remove a known symbol resolver |
void |
removeSymbolValue(java.lang.String symbol)
Remove a symbol value |
void |
setExpression(java.lang.String expression)
|
void |
setFormulaTreeBuilder(FormulaTreeBuilder ftb)
Update the builder from the parsing processing. |
void |
setHighPrecisionMode(boolean mode)
Update the inner formula evaluator for working with java.lang.BigDecimal rather than with double value. |
void |
setOperatorFactory(OperatorFactory factory)
Update the current operator factory |
void |
setParent(Formula parent)
Deprecated. Call rather setShareFormulaContext |
void |
setShareFormulaContext(Formula context)
This is a way to share symbol values / resolvers between different various formula context |
void |
setSymbolValue(java.lang.String symbol,
java.math.BigDecimal value)
Assign a BigDecimal value to a symbol. |
void |
setSymbolValue(java.lang.String symbol,
boolean value)
Assign a boolean value to a symbol. |
void |
setSymbolValue(java.lang.String symbol,
double value)
Assign a double value to a symbol. |
void |
setSymbolValue(java.lang.String symbol,
java.lang.String value)
Assign a string value to a symbol. |
void |
setSymbolValue(java.lang.String symbol,
Variant value)
Set a symbol value, this value will be used while resolving the formula expression. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static java.lang.String PI_SYMBOL
public static java.lang.String E_SYMBOL
public static java.lang.String TRUE_SYMBOL
public static java.lang.String FALSE_SYMBOL
public static int DEF_ROUNDING_MODE
public static int DEF_HP_SCALE
Constructor Detail |
public Formula(Formula formulaContext)
public Formula()
public Formula(boolean highPrecisionMode)
java.lang.BigDecimal
rather than with double value.
highPrecisionMode
- enable BigDecimal rather than doublepublic Formula(java.lang.String expression)
expression
- Mathematical expressionpublic Formula(java.lang.String expression, boolean highPrevisionMode)
expression
- Mathematical expressionMethod Detail |
public void setParent(Formula parent)
parent
- a Formula parent using resolution information (resolvers,
symbol table) if a variable or function is unknown for this
formula. This is a way to share symbol values between
different various formula contextpublic void setShareFormulaContext(Formula context)
public void setOperatorFactory(OperatorFactory factory)
public OperatorFactory getOperatorFactory()
public void setSymbolValue(java.lang.String symbol, Variant value)
public void setSymbolValue(java.lang.String symbol, java.lang.String value)
public void setSymbolValue(java.lang.String symbol, boolean value)
public void setSymbolValue(java.lang.String symbol, double value)
public void setSymbolValue(java.lang.String symbol, java.math.BigDecimal value)
public void removeSymbolValue(java.lang.String symbol)
public java.util.Enumeration getSymbolNames()
public void addFunctionResolver(FunctionResolver resolver)
public void removeFunctionResolver(FunctionResolver resolver)
public void addSymbolResolver(SymbolResolver resolver)
public void removeSymbolResolver(SymbolResolver resolver)
public void setExpression(java.lang.String expression)
expression
- Mathematical expression
FormulaException
- for null
valuepublic java.lang.String getExpression()
public void setFormulaTreeBuilder(FormulaTreeBuilder ftb)
parse
method
public AbstractNode parse() throws EvaluateException
EvaluateException
public Variant evaluate() throws EvaluateException
EvaluateException
public Variant getValueForSymbol(java.lang.String symbolName) throws SymbolResolverException
SymbolResolver
delegate. If no value is found in
the current Formula
, the function tries to find a
value from a parent.
SymbolResolverException
- if it is unable to find a value or a valid symbol namepublic java.util.Enumeration getSymbolsFromExpression() throws EvaluateException
EvaluateException
- for a wrong syntax expressionpublic Variant getValueForFunction(java.lang.String functionName, ListOfArgument parameters) throws FunctionResolverException
FunctionResolver
delegate. If no value is found in the current
Formula
, the function tries to find a value from
a parent.
FunctionResolverException
- if it is unable to find a value or a valid functionpublic void setHighPrecisionMode(boolean mode)
java.lang.BigDecimal
rather than with double value. By
default false
. This method must be called before setting
an expression.
mode
- if true
, it works in high precisionpublic boolean isHighPrecisionMode()
true if high precision evaluation
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |