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 * @author  Tomáš Müller
022 * @version ExamTT 1.3 (Examination Timetabling)<br>
023 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
024 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
025 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
026 * <br>
027 *          This library is free software; you can redistribute it and/or modify
028 *          it under the terms of the GNU Lesser General Public License as
029 *          published by the Free Software Foundation; either version 3 of the
030 *          License, or (at your option) any later version. <br>
031 * <br>
032 *          This library is distributed in the hope that it will be useful, but
033 *          WITHOUT ANY WARRANTY; without even the implied warranty of
034 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
035 *          Lesser General Public License for more details. <br>
036 * <br>
037 *          You should have received a copy of the GNU Lesser General Public
038 *          License along with this library; if not see
039 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
040 */
041public class PredefinedExamRoomSharing extends ExamRoomSharing {
042    private Map<Long, Set<Long>> iSharingMatrix = new HashMap<Long, Set<Long>>();
043    
044    public PredefinedExamRoomSharing(Model<Exam, ExamPlacement> model, DataProperties config) {
045        super(model, config);
046    }
047
048    @Override
049    public boolean canShareRoom(Exam x1, Exam x2) {
050        if (x1.getId() < x2.getId()) {
051            Set<Long> exams = iSharingMatrix.get(x1.getId());
052            return exams != null && exams.contains(x2.getId());
053        } else {
054            Set<Long> exams = iSharingMatrix.get(x2.getId());
055            return exams != null && exams.contains(x1.getId());
056        }
057    }
058    
059    /** Add a pair of exams that are allowed to share a room 
060     * @param x1 first exam
061     * @param x2 second exam
062     **/
063    public void addPair(Exam x1, Exam x2) {
064        addPair(x1.getId(), x2.getId());
065    }
066    
067    /** Add a pair of exams that are allowed to share a room 
068     * @param examId1 first exam unique id
069     * @param examId2 second exam unique id
070     **/
071    public void addPair(Long examId1, Long examId2) {
072        if (examId1 < examId2) {
073            Set<Long> exams = iSharingMatrix.get(examId1);
074            if (exams == null) { exams = new HashSet<Long>(); iSharingMatrix.put(examId1, exams); }
075            exams.add(examId2);
076        } else {
077            Set<Long> exams = iSharingMatrix.get(examId2);
078            if (exams == null) { exams = new HashSet<Long>(); iSharingMatrix.put(examId2, exams); }
079            exams.add(examId1);
080        }        
081    }
082    
083    /** Clear examination pairs */
084    public void clear() {
085        iSharingMatrix.clear();
086    }
087    
088    @Override
089    public void save(Exam exam, Element element, IdConvertor idConvertor) {
090        Set<Long> exams = iSharingMatrix.get(exam.getId());
091        if (exams != null) {
092            String ids = "";
093            for (Long id: exams) {
094                if (!ids.isEmpty()) ids += ",";
095                ids += (idConvertor == null ? id.toString() : idConvertor.convert("exam", id.toString()));
096            }
097            element.addElement("canShareRoom").setText(ids);
098        }
099    }
100    
101    @Override
102    public void load(Exam exam, Element element) {
103        Element canShareRoom = element.element("canShareRoom");
104        if (canShareRoom == null) return;
105        for (String id: canShareRoom.getTextTrim().split(","))
106            addPair(exam.getId(), Long.valueOf(id.trim()));
107    }
108}