Hmmm, an absolutely naive attempt:
public interface Condition<Type T> { public boolean process(T object); } ArrayList row = new ArrayList<Condition>(10); row.add( new Condition<YourObject>() { public boolean process(YourObject obj) { if ( obj.property > 0 ) return true; else return false; }); row.add( new Condition<YourObject>() { public boolean process(YourObject obj) { if ( obj.property2 == 100 ) return true; else return false; });
Then you will iterate:
for ( Condition<YourObject> cond : row ) { if ( ! cond.process(yourobj) ) break; }
A slightly more complex example: you can write your decision table in javascript much more succinctly and possibly use Beanshell to execute the logic. I would have to hit the shell and stumble upon this a bit before I can post you an example.
Or perhaps if you posted an example, someone might come up with some simple Scala routines to do what you want.
EDIT:
So, I did a little work and thought, and for Beanshell you can use something like this:
import bsh.Interpreter; Interpreter i = new Interpreter(); // Construct an interpreter YourObject yourObject = new YourObject(); i.set("myObject", yourObject ); // Source an external script file i.source("somefile.bsh");
And somefile.bsh might look like this:
var rules = new Array(); rules.push( function(var) { if ( var.getProperty() == 0 ) return true; else return false; }); rules.push( function(var) { if ( var.getProperty() < 1000 ) return true; else return false; }); ... more rules ... for ( var func in rules ) { if ( !func( myObject ) ) break; }
This will give you more flexibility to change the rules than recompiling the Java source.
You will need to add another array to any of these solutions in order to get 100 source βstringsβ
source share