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