简体   繁体   中英

Editing rules for employee rostering example

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM