0
votes

I have a decision table with multiple imports of the same type: be.example.Person . One person is the offender, the other person is the legal guardian.

This is the start of the decision table: enter image description here

The compilation of the decision table is succesfull, however at runtime, it does not give back the expected result. The way I insert my facts for the decision table is as follows:

StatefulKnowledgeSession ksession = null;
    try {
        ksession = ruleEngine.createStatelessKnowledgeSession();
        ksession.setGlobal("result", result);
        ksession.insert(offender);
        ksession.insert(legalGuardian);
        ksession.fireAllRules(1);
    } finally {
        if(Controles.isRenseignee(ksession)) {
            ksession.dispose();
        }
    }

It seems that when I fire the rules, drools seems to insert the objects 2 times as p:Person and not as: the first one is p:Person and the second one is q:Person.

Or to put it otherwise, the execution seems to be like:

  1. Execute rule1 with offender as p:Person
  2. Execute rule1 with legalGuardian as p:Person
  3. Execute rule2 with offender as p:Person
  4. Execute rule2 with legalGuardian as p:Person

Whereas, I would have expected it to behave like:

  1. Execute rule1 with offender as p:Person and legalGuardian as q:Person.
  2. Execute rule2 with legalGuardian as p:Person and legalGuardian as q:Person.

Does drools not support this kind of functionality? In the past we used similar decision tables where the type of objects where different and that seemed to work fine. But in this case, with the objects being of the same type it doesn,'t.

Anyone has an idea if it's not possible or if I'm injecting the objects in an incorrect way?

The version of drools being used is 5.5.0.Final.

1
If you call a variable in your program offender or legalGuardian, Drools is not aware of the meaning of these names. Also, apart from the semantic conclusion that you wouldn't test alreadyConvicted for a potential guardian there is nothing in the rule which would make Drools separate the bad from the good.laune

1 Answers

0
votes

Using the very same class for two rather different entities isn't good design. The good and the bad do have several properties in common: name, age and so on. But the flag alreadyConvicted should be reserved for offenders only.

For best results, consider subclassing Person:

class Offender extends Person {
    private boolean alreadyConvicted;
    //...
}
class Guardian extends Person {
}

and create and insert them according to some info you should have.

The decision table should have p:Offender and q:Guardian.

If you are fishing in a sea of People for possible pairs of Offender and Guardian, you'll have to make sure that p and q are different by using the mantra this != p along with the second Person pattern.