This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making formula configurable

 
Amruta S Deo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possible solution is to use a scripting language for the formula, for example Groovy.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: https://scripting.dev.java.net/
 
John O'Hanley
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a tool called formula4j that will do the job. (It's a commercial tool.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic