File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Making formula configurable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Making formula configurable" Watch "Making formula configurable" New topic

Making formula configurable

Amruta S Deo

Joined: Mar 23, 2007
Posts: 2
I am having a formula which I want to make configurable. As in, this formula will be kept in the properties file. The formula is something like -
value = (class1.getField1()+class2.getField2())*class3.getField3()

Now if I keep the RHS of the formula in the properties file and get it in my code at runtime, it will just be a normal string and JVM won't understand that it has to process it and get the value. How can I make JVM to process it at runtime? Any pointers in this regard will be of great help.

Thanks in advance.

Regards,<br />Amruta Deo
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42946
Welcome to JavaRanch.

The JEP library can interpret formulas with respect to parameter passed in from the outside. But if your code is actual Java code, i.e. you need to access object and methods, then you'll need to create actual class files. The Javassist library makes it easy to create and use classes at runtime. (Never mind the references to JBoss - Javassist is independent of it.)
[ March 24, 2007: Message edited by: Ulf Dittmer ]
Amruta S Deo

Joined: Mar 23, 2007
Posts: 2
Thanks for your reply.

But if this needs to create classes, is there any use if suppose tomorrow the formula is going to change and it still has impact on code level? That's all is the purpose of making it configurable, right? It shouldn't have any impact on code even if the formula is changed. I should just modify the properties (or whatever configurable file is) and that's it... code is ready to run with the new formula...
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42946
Note that I said "at runtime". The class that will do the calculation never exists as a physical class file - it is created from the formula while the code is running. And, being a regular class, it is subject to being compiled by the JIT, so it's fast too - very cool stuff. If during the runtime of the program you need to evaluate several different formulas, no problem - just create several classes (which need to have different class names, of course, or the class loader won't recognize them as being different).
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Another possible solution is to use a scripting language for the formula, for example Groovy.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12835
The advantage of Groovy being that it is well integrated with standard Java.

Another possibility for interpreting user written scripts is the Java version of Python (Jython).

In any case, be sure you are not creating a gaping security hole.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435

One of the new features of Java 6 is support for scripting; see the API documentation of the package javax.script. Using the Java 6 scripting API, you can plug in any scripting language you like. Included with Java 6 is an engine for JavaScript, but there are currently engines available for about 25 scripting languages. See:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
John O'Hanley

Joined: Oct 13, 2002
Posts: 24
There's a tool called formula4j that will do the job. (It's a commercial tool.)
Consider Paul's rocket mass heater.
subject: Making formula configurable