0

In a nutshell, I'm looking to know what the standard methodology is for coding user defined criteria (such as Excel's conditional formatting)

For example, let's say I have a commission system with the following code:

if (sales < 100 && empCategory = "junior") {
   commRate = 0.50;
} else if (sales < 100 && empCategory = "senior") { 
   commRate = 0.60;
} else if (sales >= 100 && sales <1000) { 
   commRate = 0.70;
} else {
   commRate = 0.80;
}

So, in this example the logic that determines the commission rate is hardcoded. However, I want to take this logic out of the hardcoded code and allow the user to add, modify and delete these criteria using boolean and comparison operators against fields in the db (again, just like Excel conditional formatting)

Note I'm using the setting of a variable as an example. Instead, I could be calling another procedure based on the criteria?

Also, please note that the above example is very simplistic. The calculations in real life will involve a much more complex formula that uses most of the boolean/comparison operators with nested statements. There will almost always be an "else" case if no condition is met. Finally, this will be applied to each row is a fairly large recordset.

Designing the GUI for this isn't a problem, but I'm unsure:

  1. Where to store the user's conditions (in a db table, in an XML, etc...)?

  2. How to set the commRate variable if the boolean/comparison operators and indeed the very structure of the boolean statement (i.e nested statement) isn't hardcoded

I'm stumped how to achieve this if I'm not actually using the boolean/comparison operators in my actual code, but I know it is possible. I know you can use a binary tree data structure to work with math formulae but I'm unsure if I'm on the right track.

I'd appreciate any advice.

Thanks

John Steed
  • 599
  • 1
  • 12
  • 31
  • 1
    This is handled in a [business rules engine](http://en.wikipedia.org/wiki/Business_rules_engine). A Java solution is explained [here](http://stackoverflow.com/q/776901/1065197) with JBoss drools. – Luiggi Mendoza Feb 21 '13 at 21:15

2 Answers2

0

Assuming you have the rules in the following form, taken from the GUI or your DB or a file (etc):

condition1 = sales < 100 && empCategory == "Junior"
outcome1 = 0.50

You can then evaluate them at runtime with a ScriptEngine:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Test {

    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");

        //actual value of the parameter
        double sales = 90;
        String empCategory = "Junior";

        //pass the actual values to the engine
        engine.eval("sales = " + sales);
        engine.eval("empCategory = " + empCategory);

        //get the rules from the DB / GUI
        String condition1Str = "sales < 100 && empCategory == \"Junior\"";
        double outcome1 = 0.50;

        //check the actual value vs. the rules
        Boolean condition1 = (Boolean) engine.eval(condition1Str);

        //rest of your logic here
        if (condition1) commRate = outcome1;
    }
}

This is a contrived example but gives you the idea of how it works. If you have many different rules, an ad-hoc library might be better suited.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Since the OP says the actual problem is more complex than his post, it's hard to tell if this solution would be cumbersome for his actual application. Perhaps, the rules in their entirety change each time. – kasavbere Feb 21 '13 at 21:21
  • @kasavbere Agreed - it depends on his use case. But considering the good performance and simplicity of installation, it is probably worth having a look at. Plus good OO principles can make it an elegant solution. – assylias Feb 21 '13 at 21:23
  • @kasavbere the only problem in both your answer and this one is OP's requirement: *allow the user to add, modify and delete these criteria using boolean and comparison operators against fields in the db (again, just like Excel conditional formatting)*. This is handled by BRMS and Drools is a Java open-source tool that handles this kind of job (more info in link in my comment on OP's question). – Luiggi Mendoza Feb 21 '13 at 21:26
  • @LuiggiMendoza I agree that if the OP needs advanced features this is going to be cumbersome. – assylias Feb 21 '13 at 21:30
  • @LuiggiMendoza, kasavbere Thank you both for your suggestions. Drools looks interesting so I'll investigate that. Thanks. – John Steed Feb 22 '13 at 07:58
0

Technically java does not have methods that take functions or partial functions as parameters. If you know how to use scala you can combine scala into java. It is probably a lot cheaper than buying some rules engine, etc. BTW, scala was built by the same folks who built java; so you should really look into that.

kasavbere
  • 5,873
  • 14
  • 49
  • 72