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.Enrollment;
008import org.cpsolver.studentsct.model.Request;
009import org.cpsolver.studentsct.model.Section;
010
011
012/**
013 * Cancelled sections constraint. This global constraint ensures that no enrollment
014 * containing a cancelled sections (using {@link Section#isCancelled()}) is used.
015 * 
016 * @version StudentSct 1.3 (Student Sectioning)<br>
017 *          Copyright (C) 2014 Tomáš Müller<br>
018 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
020 * <br>
021 *          This library is free software; you can redistribute it and/or modify
022 *          it under the terms of the GNU Lesser General Public License as
023 *          published by the Free Software Foundation; either version 3 of the
024 *          License, or (at your option) any later version. <br>
025 * <br>
026 *          This library is distributed in the hope that it will be useful, but
027 *          WITHOUT ANY WARRANTY; without even the implied warranty of
028 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029 *          Lesser General Public License for more details. <br>
030 * <br>
031 *          You should have received a copy of the GNU Lesser General Public
032 *          License along with this library; if not see
033 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
034 */
035public class CancelledSections extends GlobalConstraint<Request, Enrollment> {
036    
037    /**
038     * A given enrollment is conflicting, if there is a section that
039     * is cancelled.
040     * 
041     * @param enrollment {@link Enrollment} that is being considered
042     * @param conflicts all computed conflicting requests are added into this set
043     */
044    @Override
045    public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) {
046        if (inConflict(assignment, enrollment))
047            conflicts.add(enrollment);
048    }
049    
050    /**
051     * A given enrollment is conflicting, if there is a section that
052     * is cancelled.
053     * 
054     * @param enrollment {@link Enrollment} that is being considered
055     * @return true, if the enrollment does not follow a reservation that must be used 
056     */
057    @Override
058    public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) {
059        // exclude free time requests
060        if (enrollment.getConfig() == null) return false;
061
062        // check all sections of the given enrollment
063        for (Section section: enrollment.getSections())
064            if (section.isCancelled()) return true;
065        
066        return false;
067    }
068    
069    @Override
070    public String toString() {
071        return "CancelledSections";
072    }
073}