com.japisoft.formula
Class Formula

java.lang.Object
  extended bycom.japisoft.formula.Formula

public class Formula
extends java.lang.Object

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", ... )

a function value is retrieved by the function library or by a function delegate FunctionResolver
For the previous cases, just call addSymbolResolver or/and addFunctionResolver with your delegate.

More information at : http://www.japisoft.com

Version:
2.6
Author:
(c) 2002 - 2004 JAPISoft /

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

PI_SYMBOL

public static java.lang.String PI_SYMBOL

E_SYMBOL

public static java.lang.String E_SYMBOL

TRUE_SYMBOL

public static java.lang.String TRUE_SYMBOL

FALSE_SYMBOL

public static java.lang.String FALSE_SYMBOL
Constructor Detail

Formula

public Formula(Formula formulaContext)
This is a way to share symbol values / resolvers between different various formula context


Formula

public Formula()
Default constructor


Formula

public Formula(java.lang.String expression)
Parameters:
expression - Mathematical expression
Method Detail

setParent

public void setParent(Formula parent)
Deprecated. Call rather setShareFormulaContext

Parameters:
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 context

setShareFormulaContext

public void setShareFormulaContext(Formula context)
This is a way to share symbol values / resolvers between different various formula context


setSymbolValue

public void setSymbolValue(java.lang.String symbol,
                           Variant value)
Set a symbol value, this value will be used while resolving the formula expression. Note that the SymbolResolver delegate will not be invoked if a value exists for the symbol while resolving


removeSymbolValue

public void removeSymbolValue(java.lang.String symbol)
Remove a symbol value


getSymbolNames

public java.util.Enumeration getSymbolNames()
Returns:
current known symbol name

addFunctionResolver

public void addFunctionResolver(FunctionResolver resolver)
add a function resolver. This is useful for resolving function while evaluating the current expression


removeFunctionResolver

public void removeFunctionResolver(FunctionResolver resolver)
Remove a known function resolver


addSymbolResolver

public void addSymbolResolver(SymbolResolver resolver)
add a symbol resolver. This is useful for resolving variable while evaluating the current expression


removeSymbolResolver

public void removeSymbolResolver(SymbolResolver resolver)
Remove a known symbol resolver


setExpression

public void setExpression(java.lang.String expression)
Parameters:
expression - Mathematical expression
Throws:
FormulaException - for null value

getExpression

public java.lang.String getExpression()
Returns:
the current expression

setFormulaTreeBuilder

public void setFormulaTreeBuilder(FormulaTreeBuilder ftb)
Update the builder from the parsing processing. This builder made a tree with node that can be evaluated. It is used when calling the parse method


parse

public AbstractNode parse()
                   throws EvaluateException
Parse the expression and return a node for evaluation. This operation shouldn't be recall for the same expression. Each time you want to evaluate a result you call the evaluate method

Throws:
EvaluateException

evaluate

public Variant evaluate()
                 throws EvaluateException
This method will evaluate the current expression. It will parse the expression the first one and evaluate only an the parsing tree result for each invokation until the expression will change

Throws:
EvaluateException

getValueForSymbol

public Variant getValueForSymbol(java.lang.String symbolName)
                          throws SymbolResolverException
Returns:
a symbol value using the symbol table and in a second time the SymbolResolver delegate. If no value is found in the current Formula, the function tries to find a value from a parent.
Throws:
SymbolResolverException - if it is unable to find a value or a valid symbol name

getValueForFunction

public Variant getValueForFunction(java.lang.String functionName,
                                   ListOfArgument parameters)
                            throws FunctionResolverException
Returns:
a function value using the FunctionResolver delegate. If no value is found in the current Formula, the function tries to find a value from a parent.
Throws:
FunctionResolverException - if it is unable to find a value or a valid function

main

public static void main(java.lang.String[] args)
                 throws java.lang.Throwable
Throws:
java.lang.Throwable