Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java formula evaluation library with out-of-order variables feature

I'm currently looking for a java library (or native library with a java API) for formula parsing and evaluation.

Using recommandations from here, I took a look on many libraries :

  • JFormula
  • JEval
  • Symja
  • JEP

But none of them fulfil my needs, that are :

  • Multiple formula evaluation with dependency between them (a formula is always an affectation to a variable using other variables or numerical values)
  • Possibility to change only one formula out of maybe 50, with good performances if only one formule changes
  • no need to handle by hand variables dependancies
  • Automatically update other dependant variables if a formula changes
  • Possibility to listen which variable changed
  • no need to have a specific format for the variables (the user will directly enter a name and doesn't want to have a complexe notation)

Maybe an exemple will be better. Let's say we have, entered in the system in this order :

  • a = b + c
  • c = 2 * d
  • b = 3
  • d = 2

I would like to be able to enter those 4 lines in this order, and ask for the result of "a" (or "b", whatever). Then if in the user interface (basically a table variable <> formula) "b" is changed to "2 * d", the library will automatically change the value of "b" and "a", and return me (or lunch an event, or call a function) a list of changes

The best library would be one just like JEP, but with the out-of-order variables capability and the possibility to auto-evaluate dependant variables

I know that compilers and spreadsheet softwares uses such mechanisms, but I didn't found any java or java compatible libraries directly usable

Does someone know one?

EDIT : Precision : the question is really about a library, or eventually a set of libraries to link together. The question is for a project in a company and the idea is to spend the minimum amount of time. The "do it yourself" solution has already been estimated and is not in the scope of the question

like image 799
cporte Avatar asked Jun 20 '12 10:06

cporte


1 Answers

For a project that I also needed a simple formula parser I used the code of the article Lexical analysis, Part 2: Build an application in javaworld.com. It's simple and small (7 classes), and you can adapt it to your needs.

You can downdoad the source form here (search for 'Lexical Analysis Part II' entry).

like image 189
polypiel Avatar answered Dec 28 '22 23:12

polypiel