001package org.cpsolver.exam.reports;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.TreeSet;
006
007import org.cpsolver.exam.model.Exam;
008import org.cpsolver.exam.model.ExamModel;
009import org.cpsolver.exam.model.ExamPlacement;
010import org.cpsolver.exam.model.ExamRoomPlacement;
011import org.cpsolver.ifs.assignment.Assignment;
012import org.cpsolver.ifs.util.CSVFile;
013import org.cpsolver.ifs.util.CSVFile.CSVField;
014
015
016/**
017 * Export room splitting into a CSV file. <br>
018 * <br>
019 * Usage:
020 * <pre><code>
021 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamRoomSplit(model).report().save(file);
022 * </code></pre>
023 * <br>
024 * 
025 * @author  Tomáš Müller
026 * @version ExamTT 1.3 (Examination Timetabling)<br>
027 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
028 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
029 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
030 * <br>
031 *          This library is free software; you can redistribute it and/or modify
032 *          it under the terms of the GNU Lesser General Public License as
033 *          published by the Free Software Foundation; either version 3 of the
034 *          License, or (at your option) any later version. <br>
035 * <br>
036 *          This library is distributed in the hope that it will be useful, but
037 *          WITHOUT ANY WARRANTY; without even the implied warranty of
038 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
039 *          Lesser General Public License for more details. <br>
040 * <br>
041 *          You should have received a copy of the GNU Lesser General Public
042 *          License along with this library; if not see
043 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
044 */
045public class ExamRoomSplit {
046    private ExamModel iModel = null;
047
048    /**
049     * Constructor
050     * 
051     * @param model
052     *            examination timetabling model
053     */
054    public ExamRoomSplit(ExamModel model) {
055        iModel = model;
056    }
057
058    /**
059     * generate report
060     * @param assignment current assignment
061     * @return resultant report
062     */
063    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
064        CSVFile csv = new CSVFile();
065        csv.setHeader(new CSVField[] { new CSVField("Exam"), new CSVField("Enrl"), new CSVField("Period"),
066                new CSVField("Date"), new CSVField("Time"), new CSVField("Room 1"), new CSVField("Cap 1"),
067                new CSVField("Room 2"), new CSVField("Cap 2"), new CSVField("Room 3"), new CSVField("Cap 3"),
068                new CSVField("Room 4"), new CSVField("Cap 4") });
069        for (Exam exam : iModel.variables()) {
070            ExamPlacement placement = assignment.getValue(exam);
071            if (placement == null || placement.getRoomPlacements().size() <= 1)
072                continue;
073            List<CSVField> fields = new ArrayList<CSVField>();
074            fields.add(new CSVField(exam.getName()));
075            fields.add(new CSVField(exam.getStudents().size()));
076            fields.add(new CSVField(placement.getPeriod().getIndex() + 1));
077            fields.add(new CSVField(placement.getPeriod().getDayStr()));
078            fields.add(new CSVField(placement.getPeriod().getTimeStr()));
079            TreeSet<ExamRoomPlacement> rooms = new TreeSet<ExamRoomPlacement>(new ExamRoomComparator(exam, false));
080            rooms.addAll(placement.getRoomPlacements());
081            for (ExamRoomPlacement room : rooms) {
082                fields.add(new CSVField(room.getName()));
083                fields.add(new CSVField(room.getSize(exam.hasAltSeating())));
084            }
085            csv.addLine(fields);
086        }
087        return csv;
088    }
089}