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