FormulaFactory
mFac = FormulaFactory.getInstance();
Formula
mForm = mFac.getFormula( YOUR_EXPRESSION );
Variant
v = mForm.evaluate();
FormulaFactory.getInstance( true ). Note that some operators
cannot works for this mode.| Type |
Operator |
Example |
| Numerical operators |
+ - * / : Basic operators % : Modulo operators (not in high precision) ^ : Power operators (not in high precision) |
(-1 + 50*2 ) / ( 2^4 ) |
| Boolean
operators |
~, xor : operators &&, and : And operators ||, or : Or operators !, not : Not operators < : less operator > : great operator <= : less or equal operator >= : great or equal operator ==, equals : equal operators !=, <> : not equal operators |
!(A && (B <
10)) | NOT ( A XOR ( B equals C ) ) A != 2 || B > 2 "string1" == "string2" A or B A or ( B <> C ) |
| String
operators |
== : 2 strings equals != : 2 strings not equals <> : 2 strings not equals < : The first string less lexically than the second one > : The first string great lexically than the second one <= :The first string less or equals lexically than the second one >= : The first string great or equals lexically than the second one + : Concat string |
"string1" == "string2" :
false "string1" + "a" : "string1a" "abc" > "aaa" : true "zyx" < "bcd" : false |
| List operators |
+ : Concat two lists - : Substract a list to another one in : Test if an element is inside a list |
(1,2)+(3,4) = (1,2,3,4) (1,2) + 3 = (1,2,3) 3+(1,2)=(1,2,3) (1,2,3,4)-(3,4)=(1,2) (1,2,3,4)-3=(1,2,4) 2 in (1,2,3)=true 4 in (1,2,3)=false |
| Other
operators |
= : set a variable operator [] : absolute value ² : power 2 operator % : Percent operators |
A = [ 2 - A ] * 2 2² 10%=0.1 |
| Conditional
operators |
if then if then else |
if ( A > 2 ) then "Ok" if ( A <=2 ) THEN B=3 else B=4 |
parse
method
which returns an AbstractNode
root.getValueForSymbol
which
returns a Variant.
If the symbol nameIF condition THEN expression1 / ELSE
expression2" expression the evaluate method
returns the last evaluated expression expression1 or expression2
depending on the value of condition. If you use an "IF condition
THEN expression" and if the condition is false,
the evaluate method will return a special Variant having
the hasNoResult method to true.evaluate method
returns the last expression value.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
setSymbolValue( "A", new Variant( "1" ) )
setSymbolValue( "B", new Variant( "2" ) )
Formula f = new Formula( "A=1;B=A+1;A+B" );
Variant res = f.evaluate();
double r = res.getDoubleValue(); // 3
f.getValueForSymbol( "A" ).getDoubleValue() // 1
f.getValueForSymbol( "B" ).getDoubleValue() // 2
public class CustomResolver implements SymbolResolver,FunctionResolver
{
/** Resolver for a symbol value */
public Variant getValue( String symbol ) {
if ( "PI".equals( symbol ) )
return new Variant( Math.PI );
else {
throw new SymbolResolverException(
"Unknown " + symbol );
}
}
/** Resolver for a "sumi function" */
public Variant getValue( String function, ListOfArgument
args ) {
if ( "sumi".equals( function ) ) {
for ( int i = 0; i <
args.getArgumentCount(); i++ ) {
Variant mV =
args.getArgumentAt( i );
if (
mV.isDouble() ) {
int a =
(int)mV.getDoubleValue();
return new Variant(
(double)( ( a * ( a + 1 ) ) / 2 ) );
}
}
}
throw new FunctionResolverException(
"Unknown " + function );
}
} FormulaFactory
mFac = FormulaFactory.getInstance();
Formula
mForm = mFac.getFormula( "2 + cos( 2 * PI ) + sumi( 3 )" );
CustomResolver mResolver = new CustomResolver();
mForm.addSymbolResolver( mResolver );
mForm.addFunctionResolver( mResolver );
mForm.evaluate();
FormulaFactory
mFac = FormulaFactory.getInstance();
Formula
mForm = mFac.getFormula( "a + b + c" );
mForm.setSymbolValue( "a", 10 );
mForm.setSymbolValue( "b", 20 );
mForm.setSymbolValue( "c", 30 );
mForm.evaluate();
| Function |
Role |
| acos |
Arc cosine with a radian argument |
| asin |
Arc sine with a radian argument |
| atan |
Arc tan with a radian argument |
| avg |
The average of the arguments |
| ceil | the smallest (closest to negative infinity) double value that is greater than or equal to the argument |
| cos |
Cosine with a radian argument |
| exp |
Compute the euler's number e raised to the power
of the argument |
| floor | the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer |
| int |
Convert the double argument to integer |
| logn |
Natural logarithm in n base :
logn( BASE, VAL) |
| log 10 |
Natural logarithm in 10 base |
| log |
Natural logarithm in e base |
| max |
The maximal value of the arguments |
| min |
The minimal value of the arguments |
| pow |
The first argument power the second one |
| prod |
The product of the arguments |
| random |
A random value from 0 to 1 |
| round | The closest long to the argument |
| sin |
Sine with a radian argument |
| sqrt |
Square root |
| sum |
Sum the arguments |
| tan |
tan with a radian argument |
| degTorad |
Convert angle from degrees to
radians |
| radTodeg |
Convert angle from radians to
degrees |
| strlen |
Compute the length of a string |
| strcontains |
return true if the second string
is includes in the first one |
| strget |
extract the string starting from
1 to a position starting from 1 : ex strget( "abc", 1, 2 ) == "ab" |
import com.japisoft.formula.*;
import com.japisoft.formula.lib.*;
import com.japisoft.formula.lib.standard.*;
/** Sample for showing the content of the default mathematical library.
This
* library is increased by a new function that compute the
opposite of its first
* argument */
public class Demo {
static class CustomFunction extends AbstractFunction
{
public CustomFunction() {
super( "opp", 1 );
}
public Variant evaluate( ListOfArgument
args ) {
return new Variant( -(
getFirstArgument( args ) ) );
}
}
public static void main( String[] _args) {
Lib mLib = LibManager.getLib();
System.out.println( "Current mathematical library :"
+ mLib );
// Show all functions for the current library
Function[]
mFunctions = mLib.getFunctions();
for ( int i = 0; i < mFunctions.length; i++ ) {
System.out.println( "- " +
mFunctions[ i ] );
}
// Add a new function
((AbstractLib)mLib).install(
new CustomFunction() );
// Evaluate it
ListOfArgument
args = new ListOfArgument();
args.addElement( new Variant( 10.4
) );
System.out.println( "Evaluate new function : " +
mLib.evaluate( "opp", args ) );
}
} import com.japisoft.formula.ListOfArgument;
import com.japisoft.formula.Variant;
/**
* Compute the max values
* @author (c) 2002 JAPISoft
*/
public class MaxFunction extends AbstractFunction{
public MaxFunction() {
super( "max", ANY );
}
public Variant evaluate( ListOfArgument
args ) {
double max = Double.MIN_VALUE;
for ( int i = 0; i < args.getArgumentCount(); i++
) {
if ( args.getArgumentAt( i
).isDouble() ) {
max = Math.max( max,
args.getArgumentAt( i ).getDoubleValue() );
}
}
return new Variant( max );
}
// true if there's at least one double argument
public boolean matchArgument( ListOfArgument args ) {
return hasDoubleArgument( args );
}
} /** Here a new MINUS operator */
class NewMinusBinaryOperators implements BinaryOperator
{
public Object eval(OperatorContext
context) throws EvaluateException {
Object o =
context.getValue1();
if ( o
instanceof String ) {
String s1 = (String)o;
String s2 = (String)context.getValue2();
int i = 0;
for ( i = 0; i < Math.min( s1.length(),
s2.length() ) && s1.charAt( i ) == s2.charAt( i ); i++ ) {}
if ( i > 0 )
return s1.substring( i );
}
// Here a trivial
and not optimized way to return the default operator value
MINUSOperator
delegate = new MINUSOperator();
return
delegate.eval( context );
}
}Formula f...
f.getOperatorFactory().setBinaryOperator( MINUSOperator.NAME, new
NewMinusBinaryOperators() );
class ZZZBinaryOperator implements BinaryOperator
{
public Object eval(OperatorContext
context) throws EvaluateException {
Double d1 =
(Double)context.getValue1();
Double d2 =
(Double)context.getValue2();
return new
Double( ( d1.doubleValue() + d2.doubleValue() ) / 2.0 );
}
}Formula f...
f.getOperatorFactory().setBinaryOperator( "zzz", new
ZZZBinaryOperator() );