首页 > 解决方案 > 员工排班示例的编辑规则

问题描述

我目前正在为我的项目实施这个

我需要为“每位员工每周最多四次轮班分配”添加一条规则。我是 Java 和流口水的新手。有没有一种简单的方法来编辑下面的规则以匹配我正在寻找的约束?

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

标签: optaplannerrosteringoptaweb-employee-rostering

解决方案


你可以尝试使用累积

您的规则可能如下所示(我尚未对其进行测试,但它应该为您指明正确的方向):

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

推荐阅读