    001package org.cpsolver.studentsct.constraint;
    002
    003import java.util.Set;
    004
    005import org.cpsolver.ifs.assignment.Assignment;
    006import org.cpsolver.ifs.model.GlobalConstraint;
    007import org.cpsolver.studentsct.model.Config;
    008import org.cpsolver.studentsct.model.Enrollment;
    009import org.cpsolver.studentsct.model.Request;
    010import org.cpsolver.studentsct.reservation.Reservation;
    011
    012
    013/**
    014 * Required reservation constraint. This global constraint ensures that reservations
    015 * with {@link Reservation#mustBeUsed()} flags are used. That is, an enrollment
    016 * is conflicting when there is a reservation for the student that must be used,
    017 * but the given enrollment does not use it.
    018 *
    019 * @version StudentSct 1.3 (Student Sectioning)<br>
    020 *          Copyright (C) 2014 Tomáš Müller<br>
    021 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    022 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    023 * <br>
    024 *          This library is free software; you can redistribute it and/or modify
    025 *          it under the terms of the GNU Lesser General Public License as
    026 *          published by the Free Software Foundation; either version 3 of the
    027 *          License, or (at your option) any later version. <br>
    028 * <br>
    029 *          This library is distributed in the hope that it will be useful, but
    030 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    031 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    032 *          Lesser General Public License for more details. <br>
    033 * <br>
    034 *          You should have received a copy of the GNU Lesser General Public
    035 *          License along with this library; if not see
    036 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    037 */
    038public class RequiredReservation extends GlobalConstraint<Request, Enrollment> {
    039
    040    /**
    041     * A given enrollment is conflicting, if there is a reservation that
    042     * the student must use, but the given enrollment does not use it.
    043     *
    044     * @param enrollment {@link Enrollment} that is being considered
    045     * @param conflicts all computed conflicting requests are added into this set
    046     */
    047    @Override
    048    public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) {
    049        if (inConflict(assignment, enrollment))
    050            conflicts.add(enrollment);
    051    }
    052
    053    /**
    054     * A given enrollment is conflicting, if there is a reservation that
    055     * the student must use, but the given enrollment does not use it.
    056     *
    057     * @param enrollment {@link Enrollment} that is being considered
    058     * @return true, if the enrollment does not follow a reservation that must be used
    059     */
    060    @Override
    061    public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) {
    062        // enrollment's config
    063        Config config = enrollment.getConfig();
    064
    065        // exclude free time requests
    066        if (config == null) return false;
    067
    068        // no reservations
    069        if (!config.getOffering().hasReservations())
    070            return false;
    071
    072        // enrollment's reservation
    073        Reservation reservation = enrollment.getReservation();
    074
    075        // already has a reservation that must be used
    076        if (reservation != null && reservation.mustBeUsed()) return false;
    077
    078        // if a reservation is required for the student, fail
    079        for (Reservation r: config.getOffering().getReservations())
    080            if (r.mustBeUsed() && r.isApplicable(enrollment.getStudent()))
    081                return true;
    082
    083        return false;
    084    }
    085
    086    @Override
    087    public String toString() {
    088        return "RequiredReservation";
    089    }
    090}