Optaplanner:试图了解护士排班示例中不需要的模式规则

Optaplanner: trying to understand unwanted pattern rule in nurse rostering example

我是 drools 和 java 的新手,我正在尝试了解护士轮班示例中的这条规则是如何工作的,尤其是关于 $pattern 的第一部分。

rule "unwantedPatternShiftType3DaysPattern"
    when
        $pattern : ShiftType3DaysPattern(
            $dayIndex0ShiftType : dayIndex0ShiftType,
            $dayIndex1ShiftType : dayIndex1ShiftType,
            $dayIndex2ShiftType : dayIndex2ShiftType
        )
        PatternContractLine(
            pattern == $pattern, $contract : contract
        )

        ShiftAssignment(
            shiftType == $dayIndex0ShiftType,
            contract == $contract,
            $employee : employee, $firstDayIndex : shiftDateDayIndex
        )
        ShiftAssignment(
            shiftType == $dayIndex1ShiftType,
            employee == $employee,
            shiftDateDayIndex == ($firstDayIndex + 1)
        )
        ShiftAssignment(
            shiftType == $dayIndex2ShiftType,
            employee == $employee,
            shiftDateDayIndex == ($firstDayIndex + 2)
        )
    then
        scoreHolder.addSoftConstraintMatch(kcontext, - $pattern.getWeight());
end

具体来说,drools 如何知道以下值是什么:dayIndex0ShiftType、dayIndex1ShiftType、dayIndex2ShiftType?它使用这些值调用 ShiftType3DaysPattern class,但这些值是如何确定的?

此外,当它进行此调用时:

ShiftType3DaysPattern (dayIndex0ShiftType, dayIndex1ShiftType, dayIndex2ShiftType)

指的是:

@XStreamAlias("ShiftType3DaysPattern")
public class ShiftType3DaysPattern extends Pattern {

    private ShiftType dayIndex0ShiftType;
    private ShiftType dayIndex1ShiftType;
    private ShiftType dayIndex2ShiftType;

    public ShiftType getDayIndex0ShiftType() {
        return dayIndex0ShiftType;
    }

    public void setDayIndex0ShiftType(ShiftType dayIndex0ShiftType) {
        this.dayIndex0ShiftType = dayIndex0ShiftType;
    }

    public ShiftType getDayIndex1ShiftType() {
        return dayIndex1ShiftType;
    }

    public void setDayIndex1ShiftType(ShiftType dayIndex1ShiftType) {
        this.dayIndex1ShiftType = dayIndex1ShiftType;
    }

    public ShiftType getDayIndex2ShiftType() {
        return dayIndex2ShiftType;
    }

    public void setDayIndex2ShiftType(ShiftType dayIndex2ShiftType) {
        this.dayIndex2ShiftType = dayIndex2ShiftType;
    }

    @Override
    public String toString() {
        return "Work pattern: " + dayIndex0ShiftType + ", " + dayIndex1ShiftType + ", " + dayIndex2ShiftType;
    }

}

shorthand 是 ShiftType3DaysPattern.getDayIndex0ShiftTypeShiftType3DaysPattern.getDayIndex1ShiftTypeShiftType3DaysPattern.getDayIndex2ShiftType 的吗?

如果是这种情况,如果 xml 源文件中有多个“3 天模式”,ShiftType3DaysPattern 如何知道 return 哪个模式?我错过了什么?

此外,如果有多个“3 天模式”,那么 drool 如何自动将此规则应用于所有这些“3 天模式”?

Specifically, how does drools know what value is in: dayIndex0ShiftType, dayIndex1ShiftType, dayIndex2ShiftType? It calls the ShiftType3DaysPattern class with these values, but how are these values determined?

您的问题事实集合中的每个 ShiftType3DaysPattern 都将结合 when 子句中的其他条件根据此规则进行评估。因此,将评估 Kie 会话中作为问题事实可用的 ShiftType3DaysPattern、PatternContractLine 和三个 ShiftAssignments 的每个组合。如果所有条件都匹配(因此 PatternContractLine 匹配 ShiftType3DayPattern 并且所有三个 ShiftAssignment 类型都按照不需要的 ShiftType3DayPattern 的顺序分配,则将触发规则并且分数将受到负面影响。因此 dayIndex0ShiftType、dayIndex1ShiftType 和 dayIndex2ShiftType 的值将可以是问题事实集合中 ShiftType3DaysPattern 中设置的任何内容。

Is this shorthand for ShiftType3DaysPattern.getDayIndex0ShiftType, ShiftType3DaysPattern.getDayIndex1ShiftType, and ShiftType3DaysPattern.getDayIndex2ShiftType?

我不确定你指的到底是什么,但如果你指的是这个:

$pattern : ShiftType3DaysPattern(
    $dayIndex0ShiftType : dayIndex0ShiftType,
    $dayIndex1ShiftType : dayIndex1ShiftType,
    $dayIndex2ShiftType : dayIndex2ShiftType
)

这只是将 dayIndex0ShiftType 分配给变量 $dayIndex0ShiftType 的语法,以便稍后在规则中引用它。模式本身也被分配给一个变量$pattern。美元符号本身只是一种约定。

And if this is the case, how does ShiftType3DaysPattern know which pattern to return if there are more than one "3 day patterns" in the xml source files? What am I missing?

Furthermore, if there are more than one "3 day pattern" then, how does drool automatically apply this rule to all of these "3 day patterns"?

正如我之前所说,规则将针对每个可用的问题事实组合进行评估,因此每个 ShiftType3DaysPattern 将根据规则的 when 子句中陈述的其他事实进行评估。

我建议您阅读 Drools 文档,它将帮助您提高对 Drools 的基本了解。这是一个很长的阅读,但将是值得的。至少阅读 User guide 章节。