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
Something like this?
<http://pscode.org/jse.html?eval=Math.pow(7.27%2C0.5)>
--
Andrew Thompson
http://pscode.org/
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
No, no particular need for it to be Java expression syntax. I'll take a
look at OGNL.
Patricia
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
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
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
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?
--
Steve Wampler -- swam...@noao.edu
The gods that smiled on your birth are now laughing out loud.
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/
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
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
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
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.
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
It was just a suggestion; depending on performance considerations, it may
make no sense at all.