views:

315

answers:

6

Even something very rudimentary is fine. Like entering and evaluating something like x+1, where x is a variable I get to specify.

I was thinking of writing my own using Strings for each formula, regex for parsing them, etc., but let's assume my goal is to be as lazy as I can possibly get away with, and that if there's another option (especially a formal one), I'd rather try to use that instead, first.

Is there anything out there that does something like this?

+4  A: 

You can think about using scripting from Java 6. The reference JDK comes with a JavaScript implementation, and you can plug in other languages too.

Chris Jester-Young
That could possibly work, but that might be too much power for what I need.Anything simpler than that?
Daddy Warbox
Well, where are you drawing the line at what level of power you need? :-)
Chris Jester-Young
Well I'm not really drawing a line unless it gets performance prohibitive down the road. I guess I'll just do it this way, then.
Daddy Warbox
If you care about performance, then you should download the Rhino engine from https://scripting.dev.java.net/ (since Rhino has a bytecode compiler---but the version bundled with JDK has the compiler stripped out).
Chris Jester-Young
BTW it's easy to write your own engine, just use the code from that site as a template (go to the CVS section, and check out the code using user "guest"---no password needed).
Chris Jester-Young
Neat. Thanks for the info.
Daddy Warbox
Scripting is great, but doesn't it have security issues?
Jason S
I'll be careful. I'm not that dumb. ;)
Daddy Warbox
A: 

Maybe MathML?

I'll definitely look into this later on.
Daddy Warbox
I thought MathML is just for marking up mathematical expressions, not evaluating them.
Chris Jester-Young
Yeah. XML. Not what I need unless I start going in a new direction with my project. Still, it's nice to know about.
Daddy Warbox
MathML is way to verbose for human entry.
starblue
+1  A: 

I've used JEP with success a couple of years ago and it still seems to be actively developed. I believe they had a more permissive license back in those days, but it's probably cheaper than coding it yourself (depending on your exact needs and skills).

wwwclaes
Interesting. If this fits better, this is what I might use. Looking into it now.
Daddy Warbox
Oh jeez! That's a hell of a license! I think I'll just stick with scripting, then, since what I need done is pretty rudimentary (for the time being).
Daddy Warbox
JEP used to be GPL. You can still download the GPL version here:http://sourceforge.net/projects/jep/
Ross
+1  A: 

I've built a java library to do exactly this as a learning example for Lex/YACC, for myself. It was incorporated into a larger project, where it seems to work well. Were I to start the larger project again, I'd follow the advice above and use the Java scripting engine.

Thomas Jones-Low
Yeah I agree. And especially since a license for JEP would run at around 500$. :P
Daddy Warbox
+3  A: 

I know the following libraries:

axelclk
+1  A: 

You could try LibFormula, a formula eavluation library that's based on OpenDocument's OpenFormula standard.

OpenFormula was written for spreadsheet applications and uses a syntax close to Excel-formulas. More info can be found in the Pentaho-Wiki

Sample:

=[x]+1
Thomas Morgner