Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

formula

Formula is a parser and evaluator for mathematical and logical formula language.

Example

To parse a formula, instantiate the Parser and call the method parser(..).

Expression expression = new Parser().parse("=2*2^2");

To evaluate the expression, instantiate an Evaluator and call evaluate(..).

Evaluator evaluator = new Evaluator();
int result = evaluator.evaluate(expression);

Here is a bit more complicated example with user defined variables, functions und other formulas wich are used inside target one.

// Define variables
Map<String, Object> variables = new HashMap<>();
variables.put("a", 1);
variables.put("text1", "TEXT_1");
variables.put("text2", "TEXT_2");

// Define functions
Map<String, IFunction> functions = new HashMap<>();
functions.put("sqrt", value -> Math.sqrt((Double)value[0]));

// Define formulas
Map<String, Expression> formulas = new HashMap<>();
formulas.put("f1", new Parser().parse("=a*2"));
formulas.put("f2", new Parser().parse("=a*3"));

// Use it all
Evaluator evaluator = new Evaluator(functions, variables, formulas);

Expression expression = new Parser().parse("=5 * when(text1=text2;=f1;=f2)");
evaluator.evaluate(expression);

// The result is 15.0
double result = evaluator.evaluate(expression);

Description

The formula language supports a number of built-in mathematical and comparison operators and user defined variables, functions and references to other formulas.

  • A formula should be started with = character. Although it is optional.

  • Mathematical operators: +, -, *, /, ^

  • Comparison operators: <, <=, =, >=, > and <>

  • A variable is a string which consist letter and number characters and must begin with a letter.

  • A function is a string (name of the function) with subsequent ( and ) characters. The function may have parametrs. They must be placed between ( and ). The multiple parameters must be delimited by the ; character.

  • A formula reference begins with = character folowing by a string (name of the formula).

License

Apache License Version 2.0 (see https://raw.githubusercontent.com/eduardsdv/formula/master/LICENSE file for the full text)

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages