在Drools中,比较ID意味着什么

时间:2017-07-05 05:17:20

标签: drools optaplanner drools-planner

我理解现在编写drools规则的基础知识,但我似乎无法理解我所见过的例子(optaplanner),有ID的比较。这有必要吗?它为什么存在?

// RoomOccupancy: Two lectures in the same room at the same period.
// Any extra lecture in the same period and room counts as one more violation.
rule "roomOccupancy"
    when
        Lecture($leftId : id, period != null, $period : period, room != null, $room : room)
        // $leftLecture has lowest id of the period+room combo
        not Lecture(period == $period, room == $room, id < $leftId)
        // rightLecture has the same period
        Lecture(period == $period, room == $room, id > $leftId, $rightId : id)
    then
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end

根据我的理解,删除not Lecture(..行并离开Lecture(period == $period, room == $room)应该可以解决问题。我的理解是正确的还是我错过了一些用例?

2 个答案:

答案 0 :(得分:1)

给定2个女王A和B,“同一水平行上的2个女王”约束中的id比较确保我们只匹配A-B而不是B-A,A-A和B-B。

讲座的原则相同。

答案 1 :(得分:1)

您应该了解一种模式,例如

$a: Lecture()
$b: Lecture()

有两个讲座事实A系统中的B将产生以下匹配和发射:

$a-A, $b-B   (1)
$a-B, $b-A   (2)
$a-A, $b-A
$a-B, $b-B

因此,为了减少不需要的组合,你需要一种方法来确定没有相同的事实匹配(绑定)$ a和$ b:

$a: Lecture( $ida: id )
$b: Lecture( $idb: id != $ida )

然而,使用不相等仍会产生组合(1)和(2)。