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