    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.model.Section;
    011import org.cpsolver.studentsct.reservation.Reservation;
    012
    013
    014/**
    015 * Disabled sections constraint. This global constraint ensures that no enrollment
    016 * containing a disabled sections (using {@link Section#isEnabled()}) is used, unless
    017 * there is a reservation allowing for the use of disabled sections
    018 * (using {@link Reservation#isAllowDisabled()}).
    019 *
    020 * @version StudentSct 1.3 (Student Sectioning)<br>
    021 *          Copyright (C) 2014 Tomáš Müller<br>
    022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    024 * <br>
    025 *          This library is free software; you can redistribute it and/or modify
    026 *          it under the terms of the GNU Lesser General Public License as
    027 *          published by the Free Software Foundation; either version 3 of the
    028 *          License, or (at your option) any later version. <br>
    029 * <br>
    030 *          This library is distributed in the hope that it will be useful, but
    031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    033 *          Lesser General Public License for more details. <br>
    034 * <br>
    035 *          You should have received a copy of the GNU Lesser General Public
    036 *          License along with this library; if not see
    037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    038 */
    039public class DisabledSections extends GlobalConstraint<Request, Enrollment> {
    040
    041    /**
    042     * A given enrollment is conflicting, if there is a section that
    043     * is disabled and there is not a matching reservation that would allow for that.
    044     *
    045     * @param enrollment {@link Enrollment} that is being considered
    046     * @param conflicts all computed conflicting requests are added into this set
    047     */
    048    @Override
    049    public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) {
    050        if (inConflict(assignment, enrollment))
    051            conflicts.add(enrollment);
    052    }
    053
    054    /**
    055     * A given enrollment is conflicting, if there is a section that
    056     * is disabled and there is not a matching reservation that would allow for that.
    057     *
    058     * @param enrollment {@link Enrollment} that is being considered
    059     * @return true, if the enrollment does not follow a reservation that must be used
    060     */
    061    @Override
    062    public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) {
    063        // enrollment's config
    064        Config config = enrollment.getConfig();
    065
    066        // exclude free time requests
    067        if (config == null) return false;
    068
    069        // student allows for disabled sections >> no problem
    070        if (enrollment.getStudent().isAllowDisabled()) return false;
    071
    072        boolean hasDisabledSection = false;
    073        // check all sections of the given enrollment
    074        for (Section section: enrollment.getSections())
    075            if (!section.isEnabled()) {
    076                hasDisabledSection = true;
    077                break;
    078            }
    079
    080        // no disabled section >> no conflict
    081        if (!hasDisabledSection) return false;
    082
    083        // no reservations >> conflict not allowed
    084        if (!config.getOffering().hasReservations())
    085            return true;
    086
    087        // enrollment's reservation
    088        Reservation reservation = enrollment.getReservation();
    089
    090        // already has a reservation that all for disabled sections
    091        if (reservation != null && reservation.isAllowDisabled()) return false;
    092
    093        // if a there is some other reservation that allows for disabled sections >> also fine
    094        for (Reservation r: config.getOffering().getReservations())
    095            if (r.isAllowDisabled() && r.isApplicable(enrollment.getStudent()) && r.isIncluded(enrollment))
    096                return false;
    097
    098        return true;
    099    }
    100
    101    @Override
    102    public String toString() {
    103        return "DisabledSections";
    104    }
    105}