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