Editing rules for employee rostering example

593 views Asked by At

I am currently implementing this for my project.

I need to add a rule for "at most four shift assignments per week per employee." I am new to java and drools. Is there an easy way to edit the rule below to match the constraint I am looking for?

rule "At most one shift assignment per day per employee"
when
    $s : Shift(
            employee != null,
            $e : employee,
            $leftDay : startDateTime.toLocalDate())
    Shift(
            employee == $e,
            startDateTime.toLocalDate() == $leftDay,
            this != $s)
then
    scoreHolder.addHardConstraintMatch(kcontext, -10);
end
1

There are 1 answers

5
n1ck On

You could try to use accumulate

Your rule could look like this (I haven't tested it, but it should point you in the right direction):

rule "At most four shift assignment per week per employee"
when
    $shiftWeek: ShiftWeek() //assuming this is some kind of problemfact you have in your session
    $employee: Employee()
    $count: Number(intValue() > 4) //conditional, only fire rule when $count > 4
        from accumulate(
            $shift: Shift(
                $employee == employee,
                $shiftWeek == shiftWeek
            ),
            count($shift)
        )
then
    scoreHolder.addHardConstraintMatch(kcontext, 4 - $count.intValue()); //You could also just do "- $count.intValue()", but I like the constraint match to start at -1
end