10

In the application that I'm working on right now, I need to periodically check eligibility of tens of thousands of objects for some kind of a service. The decision diagram itself is in the following form, just way larger: Decision diagram

In each of the end nodes (circles), I need to run an action (change an object's field, log information etc). I tried using Drool Expert framework, but in that case I'd need to write a long rule for every path in the diagram leading to an end node. Drools Flow doesn't seem to be built for such a use case either - I take an object and then, depending on the decisions along the way, I end up in one of the end nodes; and then again for another object. Or is it? Could you give me some examples/links to such solutions?

UPDATE:

Drools Flow calls might look like this:

// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Map<String, Object> params = new HashMap<String, Object>();

for(int i = 0; i < 10000; i++) {

    Application app = somehowGetAppById(i);

    // insert app into working memory
    FactHandle appHandle = ksession.insert(app);

    // app variable for action nodes
    params.put("app", app);

    // start a new process instance
    ProcessInstance instance = ksession.startProcess("com.sample.ruleflow", params);
    while(true) {
        if(instance.getState() == instance.STATE_COMPLETED) {
            break;
        }
    }

  // remove object from working memory
    ksession.retract(appHandle);
}

That is: I'd take an Application object, start a new process for it, when the process is finished (the final, action node would modify the application somehow), I'd remove the object from working memory and repeat the process for a new App object. What do you think about this solution?

SOLUTION:
I've ended up using Drools Flow and it has been working quite fine. My decision process isn't as straightforward as Drools Expert asks for and depending on where in the decision tree the process is it needs to load lists of objects from the database, transform them, make decisions, log everything etc. I use a Process object that is passed to the process as a parameter and stores all my global variables (for the process) and some convenience methods that are repeated at different points in the tree (as writing Java code in the Script Task nodes isn't very convenient itself). I also ended up using Java to make decisions (and not mvel or rules) - it's faster and I'd say easier to control. All objects that I work with are passed as parameters and used as normal Java variables in the code.

John Manak
  • 13,328
  • 29
  • 78
  • 119

3 Answers3

12

Drools expert is definitely the way to go.

If you want to avoid repeating yourself for the higher nodes, then the trick is to use insertLogical (or just insert if you're in a stateless session) and to understand that rules can trigger rules (It's not your father's SQL query). For example:

// we just insert Customer objects in the WM

rule "evaluateRetired"
when
    $c : Customer(age > 65)
then
    insertLogical(new Retiree($c));
end

rule "evaluteRetireeIsFemale"
when
    $r : Retiree(customer.gender == Gender.FEMALE, $c : customer)
then
    ...
end

If the decision diagram frequently changes (and you want non-programmers to edit it), take a look at the documentation on decision tables (and DSL). In that case you'll probably repeat the entire path for each rule, but that's actually ok in most cases.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • Also, as your decision tree grows, you might find out that some end nodes share actions (for example all Retiree's need to get a pension, irrelevant of their gender) and it's inefficient to re-declare that action per end-node. – Geoffrey De Smet Feb 03 '11 at 15:38
  • What about Drools Flow? I can model the decision tree using that and then I could put one object to the Working Memory, start the process and let it decide which end node to take, then take the object out, put another one in, start it again and so on? Isn't that a clearer solution? – John Manak Feb 04 '11 at 09:24
  • Drools Flow doesn't make sense. Not saying it couldn't work, but since you're making a decision, a decision table implemented with a rule engine feels much more logical/natural. Trying to fit it into a workflow is wierd: A workflow is long-lived, each node is a state. – Geoffrey De Smet Feb 07 '11 at 08:02
  • Take a look at "decision tables" in the drools reference manual: http://downloads.jboss.com/drools/docs/5.1.1.34858.FINAL/drools-expert/html_single/index.html#d0e5683 You probably don't want to start from XLS, but use DRL directly, but the design of your rules is the same: every rule is a decision line. – Geoffrey De Smet Feb 07 '11 at 08:05
  • Decision tables are a nice feature. However, in my decision tree, there are sometimes more than just one ways to reach a specific end node and if I were to rewrite it in rules, their number would grow very quickly. Also, to update the rules in case of a change in the diagram would be quite a tedious task. So, in my opinion, a tree/diagram (even in Drools Flow) is a more natural way to represent the decision making. – John Manak Feb 10 '11 at 09:11
  • Drools Flow is now called jBPM 5 and it's the final 5 version is just released. It's very good, but I still doubt if it fits your use case, since you're making a decision, not a workflow, but it's interesting to see if it would work :) The decision table option doesn't seem optimal indeed. So I'd still recommend the rules trigger other rules trigger other rules (and so forth) option. – Geoffrey De Smet Feb 10 '11 at 16:24
0

I had a similiar problem and used Neo4J node database as a simple and very flexible rules engine. You can use is it with a REST service interface so it is independent from the main application. You can also have a separate application to configure the rules (even by end users).

supercoco
  • 512
  • 2
  • 7
  • 25
0

You can try the iLog framework cum rules engine.

Sid
  • 4,893
  • 14
  • 55
  • 110
  • 1
    Thanks, but iLog seems unnecessarily complicated. I'm looking for a leaner solution. – John Manak Feb 03 '11 at 15:20
  • How about Java Rules Engine API based on JSR 94? There's also one called Jess. You can find a list at this link: http://java-source.net/open-source/rule-engines – Sid Feb 03 '11 at 15:31
  • 2
    You 'll have the same design problems there. Switching the rule engine implementation won't help. Drools implements JSR-94, but the JSR-94 API is too confined for most real use cases. It hasn't evolved in years. – Geoffrey De Smet Feb 07 '11 at 10:11