Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Expression evaluator

12 views
Skip to first unread message

Patricia Shanahan

unread,
Dec 7, 2008, 7:40:35 PM12/7/08
to
I have a need for a Java expression evaluator, something that can
evaluate a single variable expression, given a string representation of
the expression and the value of the variable.

It must support exponentiation.

I did a Google search for "Java expression evaluator" and got several
results, which I am examining. However, I hope there is a particularly
standard and convenient way of doing this. Recommendations?

Thanks,

Patricia

Andrew Thompson

unread,
Dec 7, 2008, 7:53:58 PM12/7/08
to
On Dec 8, 11:40 am, Patricia Shanahan <p...@acm.org> wrote:
> I have a need for a Java expression evaluator, something that can
> evaluate a single variable expression, given a string representation of
> the expression and the value of the variable.
>
> It must support exponentiation.

Something like this?
<http://pscode.org/jse.html?eval=Math.pow(7.27%2C0.5)>

--
Andrew Thompson
http://pscode.org/

Daniel Pitts

unread,
Dec 7, 2008, 8:25:59 PM12/7/08
to
Need it be Java expressions? I find that OGNL is a very suitable
language for embedded expressions. You might also experiment with the
JavaScript engine that comes with JDK1.6

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Patricia Shanahan

unread,
Dec 7, 2008, 8:36:13 PM12/7/08
to

No, no particular need for it to be Java expression syntax. I'll take a
look at OGNL.

Patricia

Patricia Shanahan

unread,
Dec 7, 2008, 8:37:16 PM12/7/08
to

Yes, thanks, something just like that. The only downside is that it
requires 1.6. Not a problem on the machines I control, but one of the
systems I would like to run on has 1.4 and 1.5, but not 1.6 :-(

Patricia

Joshua Cranmer

unread,
Dec 7, 2008, 8:51:18 PM12/7/08
to

There's Beanshell, which can do full-fledged Java (although not 1.5 AFAIK).

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Arne Vajhøj

unread,
Dec 7, 2008, 8:35:35 PM12/7/08
to

I think you should go with Andrews recommendation. The JavaScript
engine and JavaScript eval function seems to be the easiest
solution.

Alternatively go with Java and compile a String to a class
and load it all dynamically.

Arne

Steve Wampler

unread,
Dec 7, 2008, 8:51:08 PM12/7/08
to
Patricia Shanahan wrote:
> Yes, thanks, something just like that. The only downside is that it
> requires 1.6. Not a problem on the machines I control, but one of the
> systems I would like to run on has 1.4 and 1.5, but not 1.6 :-(

This may be overkill, since it can do a lot more than simple expressions,
(and hence is fairly substantial) but have you looked into the BeanShell
package?

http://www.beanshell.org/

--
Steve Wampler -- swam...@noao.edu
The gods that smiled on your birth are now laughing out loud.

John B. Matthews

unread,
Dec 7, 2008, 9:18:22 PM12/7/08
to
In article <e5idnRk60-fQ56HU...@earthlink.com>,
Patricia Shanahan <pa...@acm.org> wrote:

I know this recursive descent parser runs on 1.5; but it uses enum,
which might be awkward on 1.4:

<http://sites.google.com/site/drjohnbmatthews/enumerated-functions>

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

Patricia Shanahan

unread,
Dec 8, 2008, 12:45:37 AM12/8/08
to

I've implemented it using javax.script. This solution seemed too simple
to ignore. I am going to start pushing for 1.6 on the system I don't
control. If that does not work out, I may have to revisit the topic.

Patricia

Eric Sosman

unread,
Dec 8, 2008, 8:47:47 AM12/8/08
to
Patricia Shanahan wrote:
> I have a need for a Java expression evaluator, something that can
> evaluate a single variable expression, given a string representation of
> the expression and the value of the variable.
>
> It must support exponentiation.

I've written one that may be overkill, used thusly:

Expression e = Expression.compile(
"cos(theta*PI)^2 / ratio", // expression
new String[] { "theta", "ratio" } // variables
);
double v1 = e.value(new double[] {
0.42, // theta
9.7 // ratio
});
double v2 = e.value(new double[] { -0.3, 14.6 });

The object `e' is a subclass of Expression, compiled to
byte code (no JDK required) and loaded.

You mention elsethread that you may need to support Java 1.4,
and my code uses some 1.5 features: generics, "for each," and
possibly a few others I didn't notice. I imagine these could
be stripped out easily enough. Let me know if you'd like it:
it's free, and worth every penny.

--
Eric Sosman
eso...@ieee-dot-org.invalid

Patricia Shanahan

unread,
Dec 8, 2008, 11:00:45 AM12/8/08
to

I do use 1.5, because the worst case machine has both it and 1.4. I
don't understand why anyone would keep 1.4 around but not have 1.6.

The code using 1.6 was so simple that I implemented and tested it last
night. I'm going to stick with that, and start work trying to get 1.6
onto the machine that does not have it. It will be a race to see which
happens first - I need to use it for a test, or it gets 1.6.

If I have to back off from 1.6, then I'll revisit all the suggestions,
yours included.

Patricia

Mike Schilling

unread,
Dec 8, 2008, 11:20:37 AM12/8/08
to

Implement it as a web service on the 1.6 machine, and generate a
client (using, say, Axis) on the 1.4 and 1.5 machines.


Arne Vajhøj

unread,
Dec 8, 2008, 8:25:28 PM12/8/08
to

At least that is common practice in a typical business
server side apps world, but if Patricia is doing scientific
desktop apps, then it may be less natural.

Arne

Mike Schilling

unread,
Dec 8, 2008, 10:36:07 PM12/8/08
to

It was just a suggestion; depending on performance considerations, it may
make no sense at all.


0 new messages