    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 * @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 ExamNbrMeetingsPerDay {
    048    private ExamModel iModel = null;
    049
    050    /**
    051     * Constructor
    052     *
    053     * @param model
    054     *            examination timetabling model
    055     */
    056    public ExamNbrMeetingsPerDay(ExamModel model) {
    057        iModel = model;
    058    }
    059
    060    /**
    061     * generate report
    062     * @param assignment current assignment
    063     * @return resultant report
    064     */
    065    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    066        CSVFile csv = new CSVFile();
    067        List<CSVField> header = new ArrayList<CSVField>();
    068        header.add(new CSVField("Date"));
    069        header.add(new CSVField("None"));
    070        boolean isDayBreakBackToBack = ((StudentBackToBackConflicts)iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
    071        for (int i = 1; i <= 5; i++)
    072            header.add(new CSVField(i == 5 ? "5+" : String.valueOf(i)));
    073        header.add(new CSVField("Back-To-Back"));
    074        csv.setHeader(header);
    075        int[] nrExamsTotal = new int[6];
    076        for (int i = 0; i <= 5; i++)
    077            nrExamsTotal[i] = 0;
    078        int btbTotal = 0;
    079        for (int d = 0; d < iModel.getNrDays(); d++) {
    080            ExamPeriod period = null;
    081            for (ExamPeriod p : iModel.getPeriods()) {
    082                if (p.getDay() == d) {
    083                    period = p;
    084                    break;
    085                }
    086            }
    087            int[] nrExams = new int[6];
    088            for (int i = 0; i <= 5; i++)
    089                nrExams[i] = 0;
    090            int btb = 0;
    091            for (ExamStudent student : iModel.getStudents()) {
    092                int ex = student.getExamsADay(assignment, d).size();
    093                nrExams[ex <= 5 ? ex : 5]++;
    094                ExamPeriod p = period;
    095                while (p.next() != null && (isDayBreakBackToBack ? p : p.next()).getDay() == d) {
    096                    btb += student.getExams(assignment, p).size() * student.getExams(assignment, p.next()).size();
    097                    p = p.next();
    098                }
    099            }
    100            List<CSVField> line = new ArrayList<CSVField>();
    101            line.add(new CSVField(period.getDayStr()));
    102            for (int i = 0; i <= 5; i++) {
    103                line.add(new CSVField(nrExams[i]));
    104                nrExamsTotal[i] += nrExams[i];
    105            }
    106            line.add(new CSVField(btb));
    107            btbTotal += btb;
    108            csv.addLine(line);
    109        }
    110        List<CSVField> line = new ArrayList<CSVField>();
    111        line.add(new CSVField("Total"));
    112        for (int i = 0; i <= 5; i++)
    113            line.add(new CSVField(nrExamsTotal[i]));
    114        line.add(new CSVField(btbTotal));
    115        csv.addLine(line);
    116        return csv;
    117    }
    118}