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

别等时光非礼了梦想. 提交于 2019-12-10 15:20:04

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/18598431/drools-rule-writing-rule-against-boolean-field-name-starting-with-is

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!