Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Making formula configurable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possible solution is to use a scripting language for the formula, for example Groovy.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a tool called formula4j that will do the job. (It's a commercial tool.)
 
expectation is the root of all heartache - shakespeare. tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic