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 * @author  Tomáš Müller
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 RequiredReservation extends GlobalConstraint<Request, Enrollment> {
040    
041    /**
042     * A given enrollment is conflicting, if there is a reservation that
043     * the student must use, but the given enrollment does not use it.
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 reservation that
056     * the student must use, but the given enrollment does not use it.
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        // no reservations
070        if (!config.getOffering().hasReservations())
071            return false;
072        
073        // enrollment's reservation
074        Reservation reservation = enrollment.getReservation();
075        
076        // already has a reservation that must be used
077        if (reservation != null && reservation.mustBeUsed()) return false;
078        
079        // if a reservation is required for the student, fail
080        for (Reservation r: config.getOffering().getReservations())
081            if (r.mustBeUsed() && r.isApplicable(enrollment.getStudent()))
082                return true;
083        
084        return false;
085    }
086    
087    @Override
088    public String toString() {
089        return "RequiredReservation";
090    }
091}