Get all the conditions which resulted in rule execution

115 views Asked by At

I want to get all the individual conditions which resulted in the execution of a rule.

For example, if I have the following rule:

package app1;

rule 'rule1'
when
    MyObjectType1( booleanPredicate1() )
    or
    (
      MyObjectType2( booleanPredicate2() )
      and
      MyObjectType3( booleanPredicate3() )
    )
    or
    MyObjectType4( booleanPredicate4() )
then
    System.out.println("In rule - " + drools.getRule().getName());
end

and booleanPredicate1(), booleanPredicate2() and booleanPredicate4() are true, then I want to get the following output:

  • booleanPredicate1() resulted in rule execution.
  • booleanPredicate4() resulted in rule execution.

What I've tried so far is inside the implementation of all such predicate methods, I've added a logging statement which gets executed only when that method is going to return true:

boolean booleanPredicate1()
{
  boolean ret = false;
  ...
  ...
  if (<business-logic-defined-predicate>)
  {
    ret = true;
  }
  if(ret)
  {
    addToLog("booleanPredicate1 became true.");
  }
  return ret;
}

but with this solution, I'll also get the output booleanPredicate2() resulted in rule execution. which is wrong.

Is there any way with which I can get the correct logging results?

1

There are 1 answers

2
laune On

Consult my paper on rule design patterns it has a section answering your question.

To summarize it here: you need rules for the individual truth values to register what is true for some fact or some combination of facts. The rule as you have it now will then combine the boolean values from the registry, and registry contains the answer to your problem.