我正在将Optaplanner与Drools结合使用,并且有Drools规则:

rule "No student double booked"
    when
        ScheduledLesson($timeslot : timeslot, $student : lesson.student)
        ScheduledLesson(timeslot == $timeslot, lesson.student == $student)
    then
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end


这很好。

我要确保学生没有背对背的课程:

rule "Student does not have contiguous lessons"
    when
        ScheduledLesson($timeslot : timeslot, $student : lesson.student)
        ScheduledLesson(timeslot == $timeslot + 1, lesson.student == $student)
    then
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end


注意第4行中的timeslot == $timeslot + 1件。这给出了以下错误:

Exception in thread "main" java.lang.RuntimeException: Error evaluating constraint 'timeslot == $timeslot + 1' in [Rule "Student does not have contiguous lessons" filename]


这似乎与Drools documentation which touches on maths不符。我要去哪里错了,如何在Drools中实施此规则?

ScheduledLesson看起来像这样:

@PlanningEntity
public class ScheduledLesson {
    public Lesson lesson;

    public Lesson getLesson() {
        return this.lesson;
    }

    public void setLesson(Lesson lesson) {
        this.lesson = lesson;
    }

    public Integer timeslot;

    @PlanningVariable(valueRangeProviderRefs = {"timeslot"})
    public Integer getTimeslot() {
        return this.timeslot;
    }
    public void setTimeslot(Integer timeslot) {
        this.timeslot = timeslot;
    }

    public Room room;
    @PlanningVariable(valueRangeProviderRefs = {"room"})
    public Room getRoom() {
        return this.room;
    }
    public void setRoom(Room room) {
        this.room = room;
    }
}


谢谢。

最佳答案

我怀疑返回实例ScheduledLesson.getTimeslot(),而不是Timeslot


在Java中,添加执行以下操作的int方法
ScheduledLesson.getTimeslotIndex()
然后在de DRL中执行:return timeslot.getIndex()

09-27 20:14