|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.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.
More information at : http://www.japisoft.com
Field Summary | |
static java.lang.String |
E_SYMBOL
|
static java.lang.String |
FALSE_SYMBOL
|
static java.lang.String |
PI_SYMBOL
|
static java.lang.String |
TRUE_SYMBOL
|
Constructor Summary | |
Formula()
Default constructor |
|
Formula(Formula formulaContext)
This is a way to share symbol values / resolvers between different various formula context |
|
Formula(java.lang.String expression)
|
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()
|
java.util.Enumeration |
getSymbolNames()
|
Variant |
getValueForFunction(java.lang.String functionName,
ListOfArgument parameters)
|
Variant |
getValueForSymbol(java.lang.String symbolName)
|
static void |
main(java.lang.String[] args)
|
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 |
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,
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
Constructor Detail |
public Formula(Formula formulaContext)
public Formula()
public Formula(java.lang.String expression)
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 setSymbolValue(java.lang.String symbol, Variant 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 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 static void main(java.lang.String[] args) throws java.lang.Throwable
java.lang.Throwable
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |