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