Search code examples
droolsrulesbusiness-rulesmvel

How to do null or empty check in drools - mvel dialect?


When the configList is null, the AND logic should not proceed further, but I am getting this error - "array index out of bounds".

Below is the rule :

rule "testRule"
   when
       config : Config( configList != null && !configList.empty && configList[0].attribute != null )
   then
       // logic
end

Solution

  • Because of the way Drools executes the conditions of the rules, short-circuit logical operators are not guaranteed. In some cases they work, but in some other cases they don't.

    As a workaround, you might split the single pattern you have into two:

    rule "testRule"
    when
      config : Config( $configList: configList != null, configList.empty == false)
      Attribute() from $configList.get(0)
    then
      // logic
    end
    

    I'm assuming that $configList is a list of Attribute objects.

    Hope it helps,