Samir Aydi
Samir Aydi

Reputation: 17

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);
}

Upvotes: 0

Views: 95

Answers (1)

Radovan Synek
Radovan Synek

Reputation: 1029

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.

Upvotes: 1

Related Questions