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