提问者:小点点

在drools中使用枚举


我正在解决一个员工名册问题。其中一个限制是来自每个“类型”的员工应该每天都出现。类型被定义为枚举。

我现在已将此规则配置如下:

rule "All employee types must be covered"
when
    not Shift(employeeId != null, $employee: getEmployee(), $employee.getType() == "Developer")
then
    scoreHolder.addHardConstraintMatch(kcontext, -100);
end

这很好。但是,我必须为所有可能的员工类型配置一个类似的规则。

为了概括它,我尝试了这个:

rule "All employee types must be covered"
when
    $type: Constants.EmployeeType()
    not Shift(employeeId != null, $employee: getEmployee(), $employee.getType() == $type.getValue())
then
    scoreHolder.addHardConstraintMatch(kcontext, -100);
end

但是,此规则不会执行。下面是我在常量文件中定义的枚举

public enum EmployeeType {
    Developer("Developer"),
    Manager("Manager");

    private String value;

    Cuisine(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

我做错了什么?


共1个答案

匿名用户

我想问题是您永远不会在会话中插入枚举(它们不是事实)。解决问题的一种方法是手动插入它们:

for(EmployeeType type : Constants.EmployeeType.values()){
  ksession.insert(type);
}

另一种方法是让您的规则从枚举中获取所有可能的值:

rule "All employee types must be covered"
when
  $type: Constants.EmployeeType() from Constants.EmployeeType.values()       
  not Shift(employeeId != null, $employee: getEmployee(), $employee.getType() == $type.getValue())

then
  scoreHolder.addHardConstraintMatch(kcontext, -100);
end

希望有帮助,