0

I am using Drools 6.1.0 in my application.

We are planning to write a single huge .drl file which will contain all the rules.

There will be mainly 2 categories of the rules 1. Null checks 2. Business validation

Using ruleflow-group, activation-group and salience I am planning to manage which rules to execute / fire when a fact is added to the session.

Even if with this approach the solution is not working for me because.

Lets say I have below .drl file

rule "rule1"
ruleflow-group "primary"
activation-group "NullCheck"
salience 5
when 
    $m : Message(innerMsg.something == null)
then 
    // do something

rule "rule2"
ruleflow-group "primary"
activation-group "NullCheck"
salience 4
when 
    $m : Message(innerMsg.something.something == null)
then 
    // do something

rule "rule3"
ruleflow-group "primary"
activation-group "NullCheck"
salience 3
when 
    $m : Message(innerMsg.something.something.something == null)
then 
    // do something

Drools documentation says this "All constraints are evaluated when a fact is inserted. From the Drools manual: The condition evaluation is not tied to a specific evaluation sequence or point in time, but that it happens continually, at any time during the life time of the engine."

So what is happening is that the code which is executing this file is throwing Nullpointer Exception on rule2 because innerMsg.something.something is NULL

Note: I do not want to club all the null checks using || in a single when statement because I want to capture specific null condition and based on that create the error message.

My questions are as below.

  1. Is it a good idea to use drools rules to do a Nullchecks for chaining objects.
  2. Should I use something else like sequential rule execution (Not sure if that is available in Drools) which will allow me to execute the rules in specific order.
  3. Is there another way to achieve this
Shantanoo K
  • 765
  • 5
  • 15
  • 43

2 Answers2

0

I think http://drools-moved.46999.n3.nabble.com/rules-users-Drools-Activation-Group-td3546598.html might be relevant to what you're seeing. You could try something like the following.

rule "rule2" ruleflow-group "primary" activation-group "NullCheck" salience 4 when $m : Message(innerMsg != null) Message(this == $m, innerMsg.something != null) Message(this == $m, innerMsg.something.something == null) then

This works against your desire to avoid a bunch of || to a certain extent, but it is a different syntax.

dew_the_fifth
  • 131
  • 1
  • 9
  • There is absolutely no reason why one shouldn't put this all into a single pattern, using `&&` to combine all the constraints. My answer to that Nabble question is outdated since null-safety has been added to Drools in the meantime. – laune May 22 '15 at 04:19
0

What you cannot avoid is the use of making your code null-safe.

Either you prefix each and every condition accessing a field with a subtype of Object with a null test:

Fact( field != null && field.subfield == whatsoever )

or you use the null-safe dereferencing operator:

Fact( field!.subfield == whatsoever )

Note that == and != are implicitly null-safe so that you may write

Fact( field == whatever )

without a problem if field == null.

Drop the idea of using activation-group and salience to achieve flow control as quickly as you can. It'll lead to a very bad rule design, unmaintainable code and general unhappiness. If you can't, write it all in Java or some other language.

laune
  • 31,114
  • 3
  • 29
  • 42
  • So is it advisable as not to use agenda-group/ runflow-group etc.? – Shantanoo K May 22 '15 at 18:44
  • There are situations where agenda-groups and salience are indicated but AFAIK I don't think these are to be found in your application. – laune May 23 '15 at 06:51