Search code examples
javadroolsoptaplanner

how to translate a java constraint into drools language


Constraint roomConflict(ConstraintFactory constraintFactory) {
    // A room can accommodate at most one lesson at the same time.
    return constraintFactory
            // Select each pair of 2 different lessons ...
            .forEachUniquePair(Lesson.class,
                    // ... in the same timeslot ...
                    Joiners.equal(Lesson::getTimeslot),
                    // ... in the same room ...
                    Joiners.equal(Lesson::getRoom))
            // ... and penalize each pair with a hard weight.
            .penalize("Room conflict", HardSoftScore.ONE_HARD);
}

Solution

  • There is no automated translation available; the constraint just has to be re-implemented in the Drools language.

    For this particular constraint, it would be something along these lines:

    rule "room conflict"
        when
            Lesson($id : id, $timeslot : timeslot, $room : room)
            Lesson(id > $id, timeslot == $timeslot, room == $room)
        then
            scoreHolder.addHardConstraintMatch(kcontext, 1);
    end
    

    It's actually similar to the original constraint definition: we penalize every pair of two distinct lessons that take place in the same room and at the same timeslot.