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