001package org.cpsolver.exam.reports;
002
003import java.text.DecimalFormat;
004
005import org.cpsolver.exam.criteria.StudentBackToBackConflicts;
006import org.cpsolver.exam.criteria.StudentDirectConflicts;
007import org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts;
008import org.cpsolver.exam.criteria.StudentMoreThan2ADayConflicts;
009import org.cpsolver.exam.criteria.StudentNotAvailableConflicts;
010import org.cpsolver.exam.model.Exam;
011import org.cpsolver.exam.model.ExamModel;
012import org.cpsolver.exam.model.ExamPlacement;
013import org.cpsolver.ifs.assignment.Assignment;
014import org.cpsolver.ifs.util.CSVFile;
015import org.cpsolver.ifs.util.CSVFile.CSVField;
016
017
018/**
019 * Export student direct, back-to-back, and more than two exams a day conflicts
020 * summarized for each exam into a CSV file. <br>
021 * <br>
022 * Usage:
023 * <pre><code>
024 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamStudentConflictsPerExam(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 ExamStudentConflictsPerExam {
048    private ExamModel iModel = null;
049
050    /**
051     * Constructor
052     * 
053     * @param model
054     *            examination timetabling model
055     */
056    public ExamStudentConflictsPerExam(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        csv.setHeader(new CSVField[] { new CSVField("Exam"), new CSVField("Enrl"), new CSVField("Direct"),
068                new CSVField("Direct [%]"), new CSVField("More-2-Day"), new CSVField("More-2-Day [%]"),
069                new CSVField("Back-To-Back"), new CSVField("Back-To-Back [%]"), new CSVField("Dist Back-To-Back"),
070                new CSVField("Dist Back-To-Back [%]") });
071        DecimalFormat df = new DecimalFormat("0.0");
072        for (Exam exam : iModel.variables()) {
073            ExamPlacement placement = assignment.getValue(exam);
074            if (placement == null)
075                continue;
076            int dc = (int)iModel.getCriterion(StudentDirectConflicts.class).getValue(assignment, placement, null) +
077                     (int)iModel.getCriterion(StudentNotAvailableConflicts.class).getValue(assignment, placement, null);
078            int btb = (int)iModel.getCriterion(StudentBackToBackConflicts.class).getValue(assignment, placement, null);
079            int dbtb = (int)iModel.getCriterion(StudentDistanceBackToBackConflicts.class).getValue(assignment, placement, null);
080            int m2d = (int)iModel.getCriterion(StudentMoreThan2ADayConflicts.class).getValue(assignment, placement, null);
081            if (dc == 0 && m2d == 0 && btb == 0 && dbtb == 0)
082                continue;
083            /*
084             * String section = ""; for (Enumeration
085             * f=exam.getCourseSections().elements();f.hasMoreElements();) {
086             * ExamCourseSection cs = (ExamCourseSection)f.nextElement(); if
087             * (section.length()>0) section+="\n"; section += cs.getName(); }
088             */
089            csv.addLine(new CSVField[] {
090                    new CSVField(exam.getName()),
091                    new CSVField(exam.getStudents().size()),
092                    new CSVField(dc),
093                    new CSVField(df.format(100.0 * dc / exam.getStudents().size())),
094                    new CSVField(m2d),
095                    new CSVField(df.format(100.0 * m2d / exam.getStudents().size())),
096                    new CSVField(btb),
097                    new CSVField(df.format(100.0 * btb / exam.getStudents().size())),
098                    new CSVField(dbtb),
099                    new CSVField(df.format(100.0 * dbtb / exam.getStudents().size())) });
100        }
101        return csv;
102    }
103}