1

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?

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31
Pravesh Jain
  • 4,128
  • 6
  • 28
  • 47

1 Answers1

1

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,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31
  • Yes that was the issue. Thanks a lot! – Pravesh Jain Jan 04 '19 at 14:04
  • The rule I've written missed checking the "date" part of it. Every shift has a string date assigned to it. And I have a List of date strings for which I am trying to come up with the roster assignment. How can I iterate over the date string in the rule? – Pravesh Jain Jan 04 '19 at 16:53
  • One idea would be to put the list of dates in a class, insert an object of that class into the session and use it in your rules. I would suggest you to create a new question in SO. – Esteban Aliverti Jan 07 '19 at 07:26
  • I posted it here https://stackoverflow.com/questions/54044004/optaplanner-iterate-over-list-variable-in-drools-rule-file No answers yet. – Pravesh Jain Jan 07 '19 at 10:37