    001package org.cpsolver.exam.model;
    002
    003import java.util.HashMap;
    004import java.util.HashSet;
    005import java.util.Map;
    006import java.util.Set;
    007
    008import org.cpsolver.coursett.IdConvertor;
    009import org.cpsolver.ifs.assignment.Assignment;
    010import org.cpsolver.ifs.model.Model;
    011import org.cpsolver.ifs.util.DataProperties;
    012import org.dom4j.Element;
    013
    014
    015/**
    016 * Room sharing model based on a pre-defined list of examination pairs. The relation needs to be populated
    017 * using {@link PredefinedExamRoomSharing#addPair(Exam, Exam)} and it is persisted with the solution XML (see
    018 * {@link ExamModel#save(Assignment)}, canShareRoom element for each exam containing a comma separated list of exam ids).
    019 * <br>
    020 *
    021 * @version ExamTT 1.3 (Examination Timetabling)<br>
    022 *          Copyright (C) 2008 - 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 PredefinedExamRoomSharing extends ExamRoomSharing {
    041    private Map<Long, Set<Long>> iSharingMatrix = new HashMap<Long, Set<Long>>();
    042
    043    public PredefinedExamRoomSharing(Model<Exam, ExamPlacement> model, DataProperties config) {
    044        super(model, config);
    045    }
    046
    047    @Override
    048    public boolean canShareRoom(Exam x1, Exam x2) {
    049        if (x1.getId() < x2.getId()) {
    050            Set<Long> exams = iSharingMatrix.get(x1.getId());
    051            return exams != null && exams.contains(x2.getId());
    052        } else {
    053            Set<Long> exams = iSharingMatrix.get(x2.getId());
    054            return exams != null && exams.contains(x1.getId());
    055        }
    056    }
    057
    058    /** Add a pair of exams that are allowed to share a room
    059     * @param x1 first exam
    060     * @param x2 second exam
    061     **/
    062    public void addPair(Exam x1, Exam x2) {
    063        addPair(x1.getId(), x2.getId());
    064    }
    065
    066    /** Add a pair of exams that are allowed to share a room
    067     * @param examId1 first exam unique id
    068     * @param examId2 second exam unique id
    069     **/
    070    public void addPair(Long examId1, Long examId2) {
    071        if (examId1 < examId2) {
    072            Set<Long> exams = iSharingMatrix.get(examId1);
    073            if (exams == null) { exams = new HashSet<Long>(); iSharingMatrix.put(examId1, exams); }
    074            exams.add(examId2);
    075        } else {
    076            Set<Long> exams = iSharingMatrix.get(examId2);
    077            if (exams == null) { exams = new HashSet<Long>(); iSharingMatrix.put(examId2, exams); }
    078            exams.add(examId1);
    079        }
    080    }
    081
    082    /** Clear examination pairs */
    083    public void clear() {
    084        iSharingMatrix.clear();
    085    }
    086
    087    @Override
    088    public void save(Exam exam, Element element, IdConvertor idConvertor) {
    089        Set<Long> exams = iSharingMatrix.get(exam.getId());
    090        if (exams != null) {
    091            String ids = "";
    092            for (Long id: exams) {
    093                if (!ids.isEmpty()) ids += ",";
    094                ids += (idConvertor == null ? id.toString() : idConvertor.convert("exam", id.toString()));
    095            }
    096            element.addElement("canShareRoom").setText(ids);
    097        }
    098    }
    099
    100    @Override
    101    public void load(Exam exam, Element element) {
    102        Element canShareRoom = element.element("canShareRoom");
    103        if (canShareRoom == null) return;
    104        for (String id: canShareRoom.getTextTrim().split(","))
    105            addPair(exam.getId(), Long.valueOf(id.trim()));
    106    }
    107}