Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Catching in the LHS of Drools

Tags:

I have a problem where the when clause of a drools rule throws a MethodNotFoundException. I'm looking for a way to figure out which rule it is during runtime to be able to remove it from the list of rules to use.

Rule Example

Rule "FooBar"
when
 $V1 : Foo (    )  AND
 $V2 : FooBar(    ) from $V1.getGoodMethod()  AND
 $V3 : FooBarBar( status == "FooBar" ) from $V2.getBadMethod()  
reply : FooFooBar()
then
reply.getList().add("FooBar");
end

So, the getBadMethod on FooBar doesn't exist. I would like a way of telling what rule it is, and removing it from the list of rules to use.

Tried and Failed Solutions:

I've tried extending the DefaultAgendaEventListener and overriding the beforeActivationFired method to add the rule being fired to a list. I was hoping the last one in the list would be the one that threw the error, but sadly it didn't work out that way.

I've now tried adding "always true" rules before all my rules. They log the name of the rule that comes after it. The problem being when there is an exception in the "WHEN" clause nothing gets logged. It's as if no rules get fired when an Exception such as the one above occurs.

The problem all lies with the dynamic drools generation code. I would like to take a two pronged approach of fixing the generation code, and catching exceptions like the one listed in this post.

Side note: I do check for errors in the builder. I receive no errors from the below code.

KnowledgeBuilderErrors errors = builder.getErrors();

if (!errors.isEmpty()) {
    for (KnowledgeBuilderError error : errors) {
        ...
    }
}