001package org.cpsolver.exam.reports;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.Comparator;
006import java.util.List;
007import java.util.TreeSet;
008
009import org.cpsolver.exam.criteria.StudentBackToBackConflicts;
010import org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts;
011import org.cpsolver.exam.model.Exam;
012import org.cpsolver.exam.model.ExamModel;
013import org.cpsolver.exam.model.ExamOwner;
014import org.cpsolver.exam.model.ExamPeriod;
015import org.cpsolver.exam.model.ExamPlacement;
016import org.cpsolver.exam.model.ExamRoomPlacement;
017import org.cpsolver.exam.model.ExamStudent;
018import org.cpsolver.ifs.assignment.Assignment;
019import org.cpsolver.ifs.util.CSVFile;
020import org.cpsolver.ifs.util.CSVFile.CSVField;
021
022
023/**
024 * Export student direct, back-to-back, and more than two exams a day conflicts
025 * into a CSV file. <br>
026 * <br>
027 * Usage:
028 * <pre><code>
029 * &nbsp;&nbsp;&nbsp;&nbsp;new ExamStudentConflictsBySectionCourse(model).report().save(file);
030 * </code></pre>
031 * <br>
032 * 
033 * @author  Tomáš Müller
034 * @version ExamTT 1.3 (Examination Timetabling)<br>
035 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
036 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
037 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
038 * <br>
039 *          This library is free software; you can redistribute it and/or modify
040 *          it under the terms of the GNU Lesser General Public License as
041 *          published by the Free Software Foundation; either version 3 of the
042 *          License, or (at your option) any later version. <br>
043 * <br>
044 *          This library is distributed in the hope that it will be useful, but
045 *          WITHOUT ANY WARRANTY; without even the implied warranty of
046 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
047 *          Lesser General Public License for more details. <br>
048 * <br>
049 *          You should have received a copy of the GNU Lesser General Public
050 *          License along with this library; if not see
051 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
052 */
053public class ExamStudentConflictsBySectionCourse {
054    private ExamModel iModel = null;
055
056    /**
057     * Constructor
058     * 
059     * @param model
060     *            examination timetabling model
061     */
062    public ExamStudentConflictsBySectionCourse(ExamModel model) {
063        iModel = model;
064    }
065
066    private List<ExamOwner> getOwners(Exam exam) {
067        if (!exam.getOwners().isEmpty())
068            return exam.getOwners();
069        ExamOwner cs = new ExamOwner(exam, exam.getId(), exam.getName());
070        cs.getStudents().addAll(exam.getStudents());
071        List<ExamOwner> ret = new ArrayList<ExamOwner>(1);
072        ret.add(cs);
073        return ret;
074    }
075
076    private List<ExamOwner> getOwners(Exam exam, ExamStudent student) {
077        List<ExamOwner> ret = new ArrayList<ExamOwner>(exam.getOwners(student));
078        if (ret.isEmpty()) {
079            ExamOwner cs = new ExamOwner(exam, exam.getId(), exam.getName());
080            cs.getStudents().add(student);
081            ret.add(cs);
082        }
083        Collections.sort(ret);
084        return ret;
085    }
086
087    /**
088     * generate report
089     * @param assignment current assignment
090     * @return resultant report
091     */
092    public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
093        CSVFile csv = new CSVFile();
094        csv.setHeader(new CSVField[] { new CSVField("Section/Course"), new CSVField("Period"), new CSVField("Day"),
095                new CSVField("Time"), new CSVField("Room"), new CSVField("Student"), new CSVField("Type"),
096                new CSVField("Section/Course"), new CSVField("Period"), new CSVField("Time"), new CSVField("Room"),
097                new CSVField("Distance") });
098        boolean isDayBreakBackToBack = ((StudentBackToBackConflicts)iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
099        double backToBackDistance = ((StudentDistanceBackToBackConflicts)iModel.getCriterion(StudentDistanceBackToBackConflicts.class)).getBackToBackDistance();
100        TreeSet<ExamOwner> courseSections = new TreeSet<ExamOwner>();
101        for (Exam exam : iModel.variables()) {
102            courseSections.addAll(getOwners(exam));
103        }
104        for (ExamOwner cs : courseSections) {
105            Exam exam = cs.getExam();
106            ExamPlacement placement = assignment.getValue(exam);
107            if (placement == null)
108                continue;
109            String roomsThisExam = "";
110            for (ExamRoomPlacement room : placement.getRoomPlacements()) {
111                if (roomsThisExam.length() > 0)
112                    roomsThisExam += ", ";
113                roomsThisExam += room.getName();
114            }
115            ExamPeriod period = placement.getPeriod();
116            boolean csPrinted = false;
117            List<ExamStudent> students = new ArrayList<ExamStudent>(cs.getStudents());
118            Collections.sort(students, new Comparator<ExamStudent>() {
119                @Override
120                public int compare(ExamStudent s1, ExamStudent s2) {
121                    int cmp = s1.getName().compareTo(s2.getName());
122                    if (cmp != 0)
123                        return cmp;
124                    return Double.compare(s1.getId(), s2.getId());
125                }
126            });
127            for (ExamStudent student : students) {
128                boolean stdPrinted = false;
129                int nrExams = student.getExams(assignment, period).size();
130                if (nrExams > 1) {
131                    boolean typePrinted = false;
132                    for (Exam otherExam : student.getExams(assignment, period)) {
133                        if (otherExam.equals(exam))
134                            continue;
135                        ExamPlacement otherPlacement = assignment.getValue(otherExam);
136                        ExamPeriod otherPeriod = otherPlacement.getPeriod();
137                        String roomsOtherExam = "";
138                        for (ExamRoomPlacement room : otherPlacement.getRoomPlacements()) {
139                            if (roomsOtherExam.length() > 0)
140                                roomsOtherExam += ", ";
141                            roomsOtherExam += room.getName();
142                        }
143                        boolean otherPrinted = false;
144                        for (ExamOwner ocs : getOwners(otherExam, student)) {
145                            csv.addLine(new CSVField[] { new CSVField(csPrinted ? "" : cs.getName()),
146                                    new CSVField(csPrinted ? "" : String.valueOf(1 + period.getIndex())),
147                                    new CSVField(csPrinted ? "" : period.getDayStr()),
148                                    new CSVField(csPrinted ? "" : period.getTimeStr()),
149                                    new CSVField(csPrinted ? "" : roomsThisExam),
150                                    new CSVField(stdPrinted ? "" : student.getName()),
151                                    new CSVField(typePrinted ? "" : "direct"), new CSVField(ocs.getName()),
152                                    new CSVField(otherPrinted ? "" : String.valueOf(1 + otherPeriod.getIndex())),
153                                    new CSVField(otherPrinted ? "" : otherPeriod.getTimeStr()),
154                                    new CSVField(otherPrinted ? "" : roomsOtherExam) });
155                            csPrinted = true;
156                            stdPrinted = true;
157                            typePrinted = true;
158                            otherPrinted = true;
159                        }
160                    }
161                }
162                if (nrExams > 0) {
163                    boolean typePrinted = false;
164                    List<ExamPeriod> periods = new ArrayList<ExamPeriod>(2);
165                    if (period.prev() != null && !student.getExams(assignment, period.prev()).isEmpty()
166                            && (!isDayBreakBackToBack || period.prev().getDay() == period.getDay()))
167                        periods.add(period.prev());
168                    if (period.next() != null && !student.getExams(assignment, period.next()).isEmpty()
169                            && (!isDayBreakBackToBack || period.next().getDay() == period.getDay()))
170                        periods.add(period.next());
171                    for (ExamPeriod otherPeriod : periods) {
172                        for (Exam otherExam : student.getExams(assignment, otherPeriod)) {
173                            ExamPlacement otherPlacement = assignment.getValue(otherExam);
174                            String roomsOtherExam = "";
175                            for (ExamRoomPlacement room : otherPlacement.getRoomPlacements()) {
176                                if (roomsOtherExam.length() > 0)
177                                    roomsOtherExam += ", ";
178                                roomsOtherExam += room.getName();
179                            }
180                            String distStr = "";
181                            if (backToBackDistance >= 0) {
182                                double dist = placement.getDistanceInMeters(otherPlacement);
183                                if (dist > 0)
184                                    distStr = String.valueOf(dist);
185                            }
186                            boolean otherPrinted = false;
187                            for (ExamOwner ocs : getOwners(otherExam, student)) {
188                                csv.addLine(new CSVField[] { new CSVField(csPrinted ? "" : cs.getName()),
189                                        new CSVField(csPrinted ? "" : String.valueOf(1 + period.getIndex())),
190                                        new CSVField(csPrinted ? "" : period.getDayStr()),
191                                        new CSVField(csPrinted ? "" : period.getTimeStr()),
192                                        new CSVField(csPrinted ? "" : roomsThisExam),
193                                        new CSVField(stdPrinted ? "" : student.getName()),
194                                        new CSVField(typePrinted ? "" : "back-to-back"), new CSVField(ocs.getName()),
195                                        new CSVField(otherPrinted ? "" : String.valueOf(1 + otherPeriod.getIndex())),
196                                        new CSVField(otherPrinted ? "" : otherPeriod.getTimeStr()),
197                                        new CSVField(otherPrinted ? "" : roomsOtherExam),
198                                        new CSVField(otherPrinted ? "" : distStr), });
199                                csPrinted = true;
200                                stdPrinted = true;
201                                typePrinted = true;
202                                otherPrinted = true;
203                            }
204                        }
205                    }
206                }
207                int nrExamsADay = student.getExamsADay(assignment, period.getDay()).size();
208                if (nrExamsADay > 2) {
209                    boolean typePrinted = false;
210                    for (Exam otherExam : student.getExamsADay(assignment, period.getDay())) {
211                        if (otherExam.equals(exam))
212                            continue;
213                        ExamPlacement otherPlacement = assignment.getValue(otherExam);
214                        ExamPeriod otherPeriod = otherPlacement.getPeriod();
215                        String roomsOtherExam = "";
216                        for (ExamRoomPlacement room : otherPlacement.getRoomPlacements()) {
217                            if (roomsOtherExam.length() > 0)
218                                roomsOtherExam += ", ";
219                            roomsOtherExam += room.getName();
220                        }
221                        boolean otherPrinted = false;
222                        for (ExamOwner ocs : getOwners(otherExam, student)) {
223                            csv.addLine(new CSVField[] { new CSVField(csPrinted ? "" : cs.getName()),
224                                    new CSVField(csPrinted ? "" : String.valueOf(1 + period.getIndex())),
225                                    new CSVField(csPrinted ? "" : period.getDayStr()),
226                                    new CSVField(csPrinted ? "" : period.getTimeStr()),
227                                    new CSVField(csPrinted ? "" : roomsThisExam),
228                                    new CSVField(stdPrinted ? "" : student.getName()),
229                                    new CSVField(typePrinted ? "" : "more-2-day"), new CSVField(ocs.getName()),
230                                    new CSVField(otherPrinted ? "" : String.valueOf(1 + otherPeriod.getIndex())),
231                                    new CSVField(otherPrinted ? "" : otherPeriod.getTimeStr()),
232                                    new CSVField(otherPrinted ? "" : roomsOtherExam) });
233                            csPrinted = true;
234                            stdPrinted = true;
235                            typePrinted = true;
236                            otherPrinted = true;
237                        }
238                    }
239                }
240            }
241        }
242        return csv;
243    }
244}