FormulaFactory
mFac = FormulaFactory.getInstance();
Formula
mForm = mFac.getFormula( YOUR_EXPRESSION );
Variant
v = mForm.evaluate();
Numerical operators |
+, -, *, /, %, ^ | (-1 + 50*2 ) / ( 2^4 ) |
Boolean
operators |
~, &&, ||, !, <, >, <=, >=, ==, or, and, not | !(A && (B < 10)) | NOT ( A XOR ( B equals C ) ) |
Other
operators |
=, [] ² |
A = [ 2 - A ] * 2 2² |
Conditional
operators |
if then if then else |
if ( A > 2 ) then "Ok" if ( A <=2 ) THEN B=3 else B=4 |
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" ) )User can access to any symbol value by calling getValueForSymbol which returns a Variant. If the symbol name
setSymbolValue( "B", new Variant( "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 |
cos |
Cosine with a radian argument |
exp |
Compute the euler's number e raised to the power
of the argument |
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 |
sin |
Sine with a radian argument |
sqrt |
Square root |
sum |
Sum the arguments |
tan |
tan with a radian argument |
deg2rad |
Convert angle from degrees to
radians |
rad2deg |
Convert angle from radians to
degrees |
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 );
}
}