Search code examples
drools

Drools Rule - writing rule against boolean field, name starting with "is"


In a drools rule, wanted to test for the value of a boolean field isValid (note: field name starts with "is"). Getting the below error:

Unable to create Field Extractor for 'isValid' of '[ClassObjectType class=domain.SpecialObject]' in rule 'Test boolean stuff' : [Rule name='Test boolean stuff'] Exception in thread "main" java.lang.IllegalArgumentException: Could not parse drl files.

However, another boolean field "solid" within the rule works fine.

Environment: Drools version - 5.1.1, dialect=mvel

<Drl file>
import deshaw.compliance.regsys.dep.domain.SpecialObject;
dialect "mvel"
rule "Test boolean stuff"
no-loop
  when
    $obj: SpecialObject(isValid == true)  // -->Problematic guy
    //$obj: SpecialObject(solid == true)  // -->This works fine
then
   System.out.println("[SplObject]:Class=" + $obj.class + ";;;obj=" + $obj);
end

<domain object>
public class SpecialObject {

private boolean isValid;
private boolean solid;

public boolean isValid() {
    return isValid;
}

public void setValid(boolean isValid) {
    this.isValid = isValid;
}

public boolean isSolid() {
    return solid;
}

public void setSolid(boolean solid) {
    this.solid = solid;
}

}

Note: The class belongs to a third party lib and hence I cannot change the name of "isValid" field


Solution

  • Try

    when
      $obj : SpecialObject( valid == true )
    then
       ...
    

    Since the accessor of the attribute is isValid you should refer to it as valid from mvel. Another alternative is using full method name with brackets.

    Name of the field itself is irrelevant, although it would be better if the author had adhered to naming standards.