views:

191

answers:

4

Are there any good open source expression engines for java? I am not looking for a rules engine (which implement conditionals and logic).

I want to be able to define a number of "facts" and dynamically generate expressions based on these facts.

For example:

Say I have facts such as:

Lemons=$5
IceCubes=$1
Cups=$2
Water=$1.50

I want to be able to dynamically define an expression:

COSTPERPITCHER=3 * LEMONS + ICECUBES + WATER
and
COSTPERCUP = COSTPERPITCHER / 25 + CUPS

I'd like to be able to store these expressions in a database or created dynamically.

+2  A: 

If you just need expression evaluation, take a look at JUEL. Spring 3.0 has Spring expression language - might be worth looking at that too.

If you need actual variables, perhaps Rhino would help.

ChssPly76
JUEL looks similar to what I need. How widely accepted is it? How stable is it? Is there a Java standards option?
Tazzy531
It _is_ java standard, or, rather an implementation of one - JSR-245 to be specific. I can't testify to its popularity but we've been using it in production for about 2 years now with no problems. What you want seems rather straightforward, so I doubt you'll hit some strange issues noone encountered before. Spring 3.0 expressions are presumably an enhanced version of this, so if you don't mind using RC code it might be worth looking into (if you use Spring already, that is - otherwise it's overkill).
ChssPly76
Great. Thanks.
Tazzy531
+2  A: 

How about Apache Commons EL?

It was originally geared towards JSP expressions, but now it can be used as a standalone expression evaluator. I've never used it, but it's worth a look.

skaffman
Commons EL is tied to JSP which in this case is most likely unnecessary. JUEL is not.
ChssPly76
Well, it's tied to the JSP API, which isn't the same thing, but I take your point.
skaffman
+1  A: 

http://mvel.codehaus.org/ - works pretty good

Darren Gilroy
A: 

Lately, I have been using Groovy to serve as an all purpose expression language. The advantage to groovy is that you don't need to learn a brand new syntax. You essentially write code to evaluate, bind in the variables required as a context and execute your script.

Groovy's String capabilities make this very easy to do.

For more information on using groovy inside Java as a scripting language, you can see this page: http://groovy.codehaus.org/Scripts+and+Classes

Chris Dail