3

It seems that Java is not set up to do what I have previously done in C++ (no big surprise there). I've got a set of rules that are generated from another application (a series of if-then checks). These rules change from time to time, so in C++ I would do this:

double variableForRules=1;
bool condition=false;
#include "rules.out";
if(condition) //do something

Essentially the if-then checks in rules.out would use the "variableForRules" (and several other variables) to decide whether condition should be set to true. If it gets set to true after the eval of the rules, the program does something.

Is there a similar way to do this in Java? Or is my only option to have rules.out actually be an entire class that needs to be instantiated, etc.?

Thanks!

3
  • So rules.out is C++ code then, right? Is it hand generated or auto-generated? Commented Feb 1, 2010 at 23:18
  • It's auto-generated, although I can have it auto-generate anything. I was hoping there was an easier way to do it, similar to C++, but it sounds like the only way is to auto-generate the full class. Commented Feb 2, 2010 at 14:05
  • Or spawn it directly with something like Beanshell or Groovy which can interpret Java code without a class around it. Commented Feb 2, 2010 at 20:04

7 Answers 7

7

Since you're autogenerating that rules.out, you could autogenerate your Java function as well. Hopefully it's not too painful to add that functionality.

Sign up to request clarification or add additional context in comments.

1 Comment

The only reason I can think that it would be difficult would be if the "rules.out" file is C++ code! Apart from that, slapping some boilerplate Java code at the start and end should be trivial.
3

Since there is no preprocessor in Java, you can't do this. Like you said, you have to implement your logic inside a class.

2 Comments

There's no preprocessor that comes as standard, but there are preprocessors that work with Java source.
Yea ... but for something as simple as this, you'd probably do better avoiding the dependency on a third party preprocessor.
1

Maybe you could use scripting for that. You could take a look at the Java Scripting Programmer's guide.

Comments

1

In Java, it would be common for the other application to save the rules into an .xml or .properties file, and then have Java read in that file.

Comments

1

rules.out actually has to be an entire class that needs to be instantiated for code to be executed.

Since rules.out is generated by third-party application, best thing would be to write your own CppToJavaTransformer that reads file rules.out as input and generates Rules.java. This assumes rules.out is available before compile time and Rules.java will be used at compile time. Drawback of this is that there is an extra transformation required.

Alternately you can write code that interprets rules.out and execute required instructions using introspection. This is hard way but rules.out can be changed at runtime as well.

1 Comment

If the rules are already valid Java (or close) then the "alternatively" could just be a matter of setting up Groovy or Beanshell to execute the rules file directly. I've done similar things in beanshell ages ago.
0

Even if could include the rules file it would be of no help. Your rule are dynamic as you said. Looks like you java code needs to change for a different scenario.

Comments

0

You could try using reflection (See: Creating New Objects and Invoking Methods by Name)

First generate a Rules.java in the manner in which you currently build rules.out and compile it.

Then load the class file into your app at runtime in the manner in which JDBC drivers were traditionally loaded.

Class clazz = Class.forName("com.mydomain.Rules");

If your app runs for long periods of time (longer then the lifetime of a single Rules.class file) then you would have to create your own ClassLoader in order to swap out the underlying class during a single runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.