    001package org.cpsolver.exam.reports;
    002
    003
    004import java.util.ArrayList;
    005import java.util.List;
    006
    007import org.cpsolver.exam.criteria.StudentBackToBackConflicts;
    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.ExamStudent;
    013import org.cpsolver.ifs.assignment.Assignment;
    014import org.cpsolver.ifs.util.CSVFile;
    015import org.cpsolver.ifs.util.CSVFile.CSVField;
    016
    017
    018/**
    019 * Export distribution of number of students by number of meetings per day into
    020 * a CSV file. <br>
    021 * <br>
    022 * Usage:
    023 * <pre><code>
    024 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamNbrMeetingsPerDay(model).report().save(file);
    025 * </code></pre>
    026 * <br>
    027 *
    028 * @author  Tomáš Müller
    029 * @version ExamTT 1.3 (Examination Timetabling)<br>
    030 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    031 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    032 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    033 * <br>
    034 *          This library is free software; you can redistribute it and/or modify
    035 *          it under the terms of the GNU Lesser General Public License as
    036 *          published by the Free Software Foundation; either version 3 of the
    037 *          License, or (at your option) any later version. <br>
    038 * <br>
    039 *          This library is distributed in the hope that it will be useful, but
    040 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    041 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    042 *          Lesser General Public License for more details. <br>
    043 * <br>
    044 *          You should have received a copy of the GNU Lesser General Public
    045 *          License along with this library; if not see
    046 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    047 */
    048public class ExamNbrMeetingsPerDay {
    049    private ExamModel iModel = null;
    050
    051    /**
    052     * Constructor
    053     *
    054     * @param model
    055     *            examination timetabling model
    056     */
    057    public ExamNbrMeetingsPerDay(ExamModel model) {
    058        iModel = model;
    059    }
    060
    061    /**
    062     * generate report
    063     * @param assignment current assignment
    064     * @return resultant report
    065     */
    066    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    067        CSVFile csv = new CSVFile();
    068        List<CSVField> header = new ArrayList<CSVField>();
    069        header.add(new CSVField("Date"));
    070        header.add(new CSVField("None"));
    071        boolean isDayBreakBackToBack = ((StudentBackToBackConflicts)iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
    072        for (int i = 1; i <= 5; i++)
    073            header.add(new CSVField(i == 5 ? "5+" : String.valueOf(i)));
    074        header.add(new CSVField("Back-To-Back"));
    075        csv.setHeader(header);
    076        int[] nrExamsTotal = new int[6];
    077        for (int i = 0; i <= 5; i++)
    078            nrExamsTotal[i] = 0;
    079        int btbTotal = 0;
    080        for (int d = 0; d < iModel.getNrDays(); d++) {
    081            ExamPeriod period = null;
    082            for (ExamPeriod p : iModel.getPeriods()) {
    083                if (p.getDay() == d) {
    084                    period = p;
    085                    break;
    086                }
    087            }
    088            int[] nrExams = new int[6];
    089            for (int i = 0; i <= 5; i++)
    090                nrExams[i] = 0;
    091            int btb = 0;
    092            for (ExamStudent student : iModel.getStudents()) {
    093                int ex = student.getExamsADay(assignment, d).size();
    094                nrExams[ex <= 5 ? ex : 5]++;
    095                ExamPeriod p = period;
    096                while (p.next() != null && (isDayBreakBackToBack ? p : p.next()).getDay() == d) {
    097                    btb += student.getExams(assignment, p).size() * student.getExams(assignment, p.next()).size();
    098                    p = p.next();
    099                }
    100            }
    101            List<CSVField> line = new ArrayList<CSVField>();
    102            line.add(new CSVField(period.getDayStr()));
    103            for (int i = 0; i <= 5; i++) {
    104                line.add(new CSVField(nrExams[i]));
    105                nrExamsTotal[i] += nrExams[i];
    106            }
    107            line.add(new CSVField(btb));
    108            btbTotal += btb;
    109            csv.addLine(line);
    110        }
    111        List<CSVField> line = new ArrayList<CSVField>();
    112        line.add(new CSVField("Total"));
    113        for (int i = 0; i <= 5; i++)
    114            line.add(new CSVField(nrExamsTotal[i]));
    115        line.add(new CSVField(btbTotal));
    116        csv.addLine(line);
    117        return csv;
    118    }
    119}