Search code examples
droolsoptaplannerdrools-planner

Using enum in drools


I am solving an employee rostering problem. One of the constraints there is that Employee from each "type" should be present on every day. Type is defined as an enum.

I have right now configured this rule as follows:

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

This works fine. However I would have to configure a similar rule for all possible employee types.

To generalize it, I tried this:

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

However, this rule doesn't get executed. Below is my enum defined in Constants file

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

    private String value;

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

    public String getValue() {
        return value;
    }
}

What am I doing wrong?


Solution

  • I guess the problem is that you are never inserting the enums in your session (they are not facts). One way to solve it is to manually insert them:

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

    Another way is to make your rule fetch all the possible values from the enum:

    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
    

    Hope it helps,