001package org.cpsolver.studentsct.check;
002
003import java.util.HashMap;
004
005import org.cpsolver.coursett.model.TimeLocation;
006import org.cpsolver.ifs.assignment.Assignment;
007import org.cpsolver.studentsct.StudentSectioningModel;
008import org.cpsolver.studentsct.model.Enrollment;
009import org.cpsolver.studentsct.model.Request;
010import org.cpsolver.studentsct.model.SctAssignment;
011import org.cpsolver.studentsct.model.Student;
012
013
014/**
015 * This class looks and reports cases when a student is enrolled into two
016 * sections that are overlapping in time.
017 * 
018 * <br>
019 * <br>
020 * 
021 * Usage: if (new OverlapCheck(model).check()) ...
022 * 
023 * <br>
024 * <br>
025 * 
026 * @author  Tomáš Müller
027 * @version StudentSct 1.3 (Student Sectioning)<br>
028 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
029 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
030 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
031 * <br>
032 *          This library is free software; you can redistribute it and/or modify
033 *          it under the terms of the GNU Lesser General Public License as
034 *          published by the Free Software Foundation; either version 3 of the
035 *          License, or (at your option) any later version. <br>
036 * <br>
037 *          This library is distributed in the hope that it will be useful, but
038 *          WITHOUT ANY WARRANTY; without even the implied warranty of
039 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
040 *          Lesser General Public License for more details. <br>
041 * <br>
042 *          You should have received a copy of the GNU Lesser General Public
043 *          License along with this library; if not see
044 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
045 */
046public class OverlapCheck {
047    private static org.apache.logging.log4j.Logger sLog = org.apache.logging.log4j.LogManager.getLogger(OverlapCheck.class);
048    private StudentSectioningModel iModel;
049
050    /**
051     * Constructor
052     * 
053     * @param model
054     *            student sectioning model
055     */
056    public OverlapCheck(StudentSectioningModel model) {
057        iModel = model;
058    }
059
060    /** Return student sectioning model 
061     * @return problem model
062     **/
063    public StudentSectioningModel getModel() {
064        return iModel;
065    }
066
067    /**
068     * Check for overlapping sections that are attended by the same student
069     * @param a current assignment
070     * @return false, if there is such a case
071     */
072    public boolean check(Assignment<Request, Enrollment> a) {
073        sLog.info("Checking for overlaps...");
074        boolean ret = true;
075        for (Student student : getModel().getStudents()) {
076            HashMap<TimeLocation, SctAssignment> times = new HashMap<TimeLocation, SctAssignment>();
077            for (Request request : student.getRequests()) {
078                Enrollment enrollment = a.getValue(request);
079                if (enrollment == null)
080                    continue;
081                for (SctAssignment assignment : enrollment.getAssignments()) {
082                    if (assignment.getTime() == null)
083                        continue;
084                    for (TimeLocation time: times.keySet()) {
085                        if (time.hasIntersection(assignment.getTime())) {
086                            sLog.error("Student " + student + " assignment " + assignment + " overlaps with "
087                                    + times.get(time));
088                            ret = false;
089                        }
090                    }
091                    times.put(assignment.getTime(), assignment);
092                }
093            }
094        }
095        return ret;
096    }
097
098}