wood burning stoves
The moose likes Beginning Java and the fly likes Runtime Java Expression Evaluator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime Java Expression Evaluator" Watch "Runtime Java Expression Evaluator" New topic

Runtime Java Expression Evaluator

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426

Good Day!

I am facing this requirements wherein I need to evaluate math expressions at runtime.

Say for example, I have this:

But I think I cannot do these in pure J2SE. Do you know any free expression evaluator or library that I can use to accomplish this?
Or should I fall back to Scripting.

While googling I found some post that is related to scripting but I am not sure yet If that is the right tool for the job at hand. Thanks

Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44016
Try using a regular expression to match the operator. You can also use the String#split (and possibly trim) methods to divide the String into operands around the operator. You can try multiple if-elses, or create an Operator enum, then you can have pairs in a Map, eg ">=" --- Operator.GREATER_OR_EQUAL, and use the enum member as the key in a switch-case.

That will work if you only ever have one operator in each String, and you will have to change it if you use uinary operators.
Greg Charles

Joined: Oct 01, 2001
Posts: 2931

I'm concerned about the "Get from DB" comment. That would mean you have to get involved with JDBC and in that case you have some configuration to do first.

The regular expressions Campbell mentions would be useful if you need to parse a string of the form "operand1 operator operand2", but the way you've laid it out, you just have to figure out which which operator you have, and then apply it. I don't see any way to do that except with a bunch of if-else if tests. One nice thing is that all your possible operators result in a boolean value (i.e., true or false). You're assigning that result to a String (called expression), but then immediately treating expression like it's a boolean value anyway. You need to change that.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Greg And Campbell,

Thanks for your valuable inputs.

But while coding it, it leads me to lots of If and Else statements and logic handling for two or more operands.

Fortunately while googling, I found post related to Java SE 6 support for scripting.

I modified a little code to fit my needs. Here's what I am using now.

Thanks though for your inputs. They are very higly regarded.
Henry Wong

Joined: Sep 28, 2004
Posts: 20057

I am actually not a fan of this... the expression is really simple, and hence, the parser should be really simple.

While the parser code will likely be longer than the code presented, I can't imagine it being less efficient than bringing in a scripting engine, and a different language, to process such a simple expression.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Henry,

I think I got your point regarding creating a parser for this.

But I think the expression could get a lot complex especially If I am given expression such as this

The possibility of combination could be a lot bigger as this is driven by table enrollment that I am retrieving thru JDBC.

I will try to develop my own parser for these but as of the moment I need to meet this requirement first.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
If the expressions are purely arithmetical and/or logical like the one you posted, then the JEP library might work well. For a full-fledged Java approach, check out this article by yours truly.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Ulf,

This looks good. I will experiment with this you bet.

I agree. Here's the link: http://aspose.com/file-tools
subject: Runtime Java Expression Evaluator
It's not a secret anymore!