    001package org.cpsolver.coursett;
    002
    003import java.io.File;
    004import java.util.Comparator;
    005import java.util.Iterator;
    006import java.util.TreeSet;
    007
    008import org.cpsolver.coursett.model.Lecture;
    009import org.cpsolver.coursett.model.TimetableModel;
    010import org.cpsolver.ifs.util.ToolBox;
    011
    012
    013/**
    014 * Create joint enrollment chart of the given input problem as CSV file (3
    015 * dimensions: 1st variable, 2nd variable, number of students in common)
    016 *
    017 * @version CourseTT 1.3 (University Course Timetabling)<br>
    018 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
    019 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    020 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    021 * <br>
    022 *          This library is free software; you can redistribute it and/or modify
    023 *          it under the terms of the GNU Lesser General Public License as
    024 *          published by the Free Software Foundation; either version 3 of the
    025 *          License, or (at your option) any later version. <br>
    026 * <br>
    027 *          This library is distributed in the hope that it will be useful, but
    028 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    029 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    030 *          Lesser General Public License for more details. <br>
    031 * <br>
    032 *          You should have received a copy of the GNU Lesser General Public
    033 *          License along with this library; if not see
    034 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    035 */
    036public class JenrlChart extends DomainChart {
    037    protected int iMax = 100;
    038
    039    public JenrlChart(String name, TimetableModel model, int max) {
    040        super(name, model, Math.min(max, model.variables().size()), Math.min(max, model.variables().size()));
    041        iMax = max;
    042    }
    043
    044    public JenrlChart(File xmlFile, int max) throws Exception {
    045        super(xmlFile, 0, 0);
    046        iMax = max;
    047        iSizeX = Math.min(iMax, iModel.variables().size());
    048        iSizeY = Math.min(iMax, iModel.variables().size());
    049    }
    050
    051    @Override
    052    protected void computeTable() {
    053        clearTable();
    054        TreeSet<Lecture> vars = new TreeSet<Lecture>(new Comparator<Lecture>() {
    055            @Override
    056            public int compare(Lecture l1, Lecture l2) {
    057                int cmp = -Double.compare(l1.students().size(), l2.students().size());
    058                if (cmp != 0)
    059                    return cmp;
    060                cmp = -Double.compare(l1.maxClassLimit(), l2.maxClassLimit());
    061                if (cmp != 0)
    062                    return cmp;
    063                return Double.compare(l1.getId(), l2.getId());
    064            }
    065        });
    066        vars.addAll(iModel.variables());
    067        int x = 1;
    068        for (Iterator<Lecture> i = vars.iterator(); i.hasNext(); x++) {
    069            Lecture l1 = i.next();
    070            if (x > iMax)
    071                continue;
    072            iHeader[x] = String.valueOf(l1.students().size());
    073            int y = 1;
    074            for (Iterator<Lecture> j = vars.iterator(); j.hasNext(); y++) {
    075                Lecture l2 = j.next();
    076                if (y > iMax)
    077                    continue;
    078                iTitle[y] = l2.getName();
    079                if (x >= y)
    080                    continue;
    081                add(x, y, l1.sameStudents(l2).size());
    082            }
    083        }
    084    }
    085
    086    public static void main(String args[]) {
    087        try {
    088            ToolBox.configureLogging();
    089            File input = new File(args[0]);
    090            int max = Integer.parseInt(args[1]);
    091            File output = null;
    092            if (args.length > 2) {
    093                output = new File(args[2]);
    094                if (output.exists() && output.isDirectory())
    095                    output = new File(output, input.getName().substring(0, input.getName().lastIndexOf('.'))
    096                            + "_jenrl.csv");
    097            } else {
    098                output = new File(input.getParentFile(), input.getName().substring(0, input.getName().lastIndexOf('.'))
    099                        + "_jenrl.csv");
    100            }
    101            new JenrlChart(input, max).createTable().save(output);
    102        } catch (Exception e) {
    103            e.printStackTrace();
    104        }
    105    }
    106
    107}