    001package org.cpsolver.exam.reports;
    002
    003import java.text.DecimalFormat;
    004import java.util.List;
    005
    006import org.cpsolver.exam.criteria.StudentBackToBackConflicts;
    007import org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts;
    008import org.cpsolver.exam.model.Exam;
    009import org.cpsolver.exam.model.ExamModel;
    010import org.cpsolver.exam.model.ExamPlacement;
    011import org.cpsolver.exam.model.ExamStudent;
    012import org.cpsolver.ifs.assignment.Assignment;
    013import org.cpsolver.ifs.util.CSVFile;
    014import org.cpsolver.ifs.util.CSVFile.CSVField;
    015
    016
    017/**
    018 * Export student back-to-back conflicts between pairs of exams into a CSV file. <br>
    019 * <br>
    020 * Usage:
    021 * <pre><code>
    022 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamStudentBackToBackConflicts(model).report().save(file);
    023 * </code></pre>
    024 * <br>
    025 *
    026 * @author  Tomáš Müller
    027 * @version ExamTT 1.3 (Examination Timetabling)<br>
    028 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    029 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    030 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    031 * <br>
    032 *          This library is free software; you can redistribute it and/or modify
    033 *          it under the terms of the GNU Lesser General Public License as
    034 *          published by the Free Software Foundation; either version 3 of the
    035 *          License, or (at your option) any later version. <br>
    036 * <br>
    037 *          This library is distributed in the hope that it will be useful, but
    038 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    039 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    040 *          Lesser General Public License for more details. <br>
    041 * <br>
    042 *          You should have received a copy of the GNU Lesser General Public
    043 *          License along with this library; if not see
    044 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    045 */
    046public class ExamStudentBackToBackConflicts {
    047    private ExamModel iModel = null;
    048
    049    /**
    050     * Constructor
    051     *
    052     * @param model
    053     *            examination timetabling model
    054     */
    055    public ExamStudentBackToBackConflicts(ExamModel model) {
    056        iModel = model;
    057    }
    058
    059    /**
    060     * generate report
    061     * @param assignment current assignment
    062     * @return resultant report
    063     */
    064    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    065        CSVFile csv = new CSVFile();
    066        csv.setHeader(new CSVField[] { new CSVField("Exam 1"), new CSVField("Enrl 1"), new CSVField("Period 1"),
    067                new CSVField("Date 1"), new CSVField("Time 1"), new CSVField("Exam 2"), new CSVField("Enrl 2"),
    068                new CSVField("Back-To-Back"), new CSVField("Back-To-Back [%]"), new CSVField("Distance") });
    069        DecimalFormat df = new DecimalFormat("0.0");
    070        boolean isDayBreakBackToBack = ((StudentBackToBackConflicts)iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
    071        double backToBackDistance = ((StudentDistanceBackToBackConflicts)iModel.getCriterion(StudentDistanceBackToBackConflicts.class)).getBackToBackDistance();
    072        for (Exam ex1 : iModel.variables()) {
    073            ExamPlacement p1 = assignment.getValue(ex1);
    074            if (p1 == null || p1.getPeriod().next() == null)
    075                continue;
    076            if (!isDayBreakBackToBack && p1.getPeriod().getDay() != p1.getPeriod().next().getDay())
    077                continue;
    078            for (Exam ex2 : iModel.variables()) {
    079                ExamPlacement p2 = assignment.getValue(ex2);
    080                if (p2 == null || !p2.getPeriod().equals(p1.getPeriod().next()))
    081                    continue;
    082                List<ExamStudent> students = ex1.getJointEnrollments().get(ex2);
    083                if (students == null || students.isEmpty())
    084                    continue;
    085                String distStr = "";
    086                if (backToBackDistance >= 0) {
    087                    double dist = p1.getDistanceInMeters(p2);
    088                    if (dist > 0)
    089                        distStr = String.valueOf(dist);
    090                }
    091                csv
    092                        .addLine(new CSVField[] {
    093                                new CSVField(ex1.getName()),
    094                                new CSVField(ex1.getStudents().size()),
    095                                new CSVField(p1.getPeriod().getIndex() + 1),
    096                                new CSVField(p1.getPeriod().getDayStr()),
    097                                new CSVField(p1.getPeriod().getTimeStr()),
    098                                new CSVField(ex2.getName()),
    099                                new CSVField(ex2.getStudents().size()),
    100                                new CSVField(students.size()),
    101                                new CSVField(df.format(100.0 * students.size()
    102                                        / Math.min(ex1.getStudents().size(), ex2.getStudents().size()))),
    103                                new CSVField(distStr) });
    104            }
    105        }
    106        return csv;
    107    }
    108}