001    package net.sf.cpsolver.coursett;
002    
003    import java.io.File;
004    import java.util.Comparator;
005    import java.util.Iterator;
006    import java.util.TreeSet;
007    
008    import net.sf.cpsolver.coursett.model.Lecture;
009    import net.sf.cpsolver.coursett.model.TimetableModel;
010    import net.sf.cpsolver.ifs.util.ToolBox;
011    
012    /**
013     * Create joint enrollment chart of the given input problem as CSV file
014     * (3 dimensions: 1st variable, 2nd variable, number of students in common)
015     * 
016     * @version
017     * CourseTT 1.1 (University Course Timetabling)<br>
018     * Copyright (C) 2007 Tomáš Müller<br>
019     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
020     * Lazenska 391, 76314 Zlin, Czech Republic<br>
021     * <br>
022     * This library is free software; you can redistribute it and/or
023     * modify it under the terms of the GNU Lesser General Public
024     * License as published by the Free Software Foundation; either
025     * version 2.1 of the License, or (at your option) any later version.
026     * <br><br>
027     * This library is distributed in the hope that it will be useful,
028     * but 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.
031     * <br><br>
032     * You should have received a copy of the GNU Lesser General Public
033     * License along with this library; if not, write to the Free Software
034     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
035     */
036    public 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        protected void computeTable() {
052            clearTable();
053            TreeSet vars = new TreeSet(new Comparator() {
054               public int compare(Object o1, Object o2) {
055                   Lecture l1 = (Lecture)o1;
056                   Lecture l2 = (Lecture)o2;
057                   int cmp = -Double.compare(l1.students().size(), l2.students().size());
058                   if (cmp!=0) return cmp;
059                   cmp = -Double.compare(l1.classLimit(),l2.classLimit());
060                   if (cmp!=0) return cmp;
061                   return Double.compare(l1.getId(),l2.getId());
062               }
063            });
064            vars.addAll(iModel.variables());
065            int x=1;
066            for (Iterator i=vars.iterator();i.hasNext();x++) {
067                Lecture l1 = (Lecture)i.next();
068                if (x>iMax) continue;
069                iHeader[x] = String.valueOf(l1.students().size());
070                int y=1;
071                for (Iterator j=vars.iterator();j.hasNext();y++) {
072                    Lecture l2 = (Lecture)j.next();
073                    if (y>iMax) continue;
074                    iTitle[y] = l2.getName();
075                    if (x>=y) continue;
076                    add(x, y, l1.sameStudents(l2).size());
077                }
078            }
079        }
080        
081        public static void main(String args[]) {
082            try {
083                ToolBox.configureLogging();
084                File input = new File(args[0]);
085                int max = Integer.parseInt(args[1]);
086                File output = null;
087                if (args.length>2) { 
088                    output = new File(args[2]);
089                    if (output.exists() && output.isDirectory())
090                        output = new File(output,input.getName().substring(0,input.getName().lastIndexOf('.'))+"_jenrl.csv");
091                } else {
092                    output = new File(input.getParentFile(),input.getName().substring(0,input.getName().lastIndexOf('.'))+"_jenrl.csv");
093                }
094                new JenrlChart(input, max).createTable().save(output);
095            } catch (Exception e) {
096                e.printStackTrace();
097            }
098        }
099    
100    }