001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/evolve/
022     */
023    
024    package EVolve;
025    
026    import EVolve.data.*;
027    import EVolve.visualization.*;
028    import EVolve.visualization.VizFactory.*;
029    import java.util.*;
030    
031    public class Main {
032        private static DataSource dataSource;
033        private static VisualizationFactory[] factory;
034    
035        public static void main(String[] args) {
036            parseArgs(args);
037    
038            /* This code has been moved to 'addVizLibrary' */
039            /*
040            VisualizationFactory[] factory = new VisualizationFactory[10];
041            factory[0] = new TableVizFactory();
042            factory[1] = new BarChartVizFactory();
043            factory[2] = new ThreadHotspotVizFactory();
044            factory[3] = new HotspotVizFactory();
045            factory[4] = new StackHotspotVizFactory();
046            factory[5] = new PredictionVizFactory();
047            factory[6] = new CorrelationVizFactory();
048            factory[7] = new StackVizFactory();
049            factory[8] = new DotPlotVizFactory();
050            factory[9] = new MissPredictionMetricVizFactory();
051            //factory[9] = new EscObjectVizFactory();
052            ((PredictionVizFactory)(factory[5])).addPredictorFactory(new DefaultPredictorFactory());
053            ((MissPredictionMetricVizFactory)(factory[9])).addPredictorFactory(new DefaultPredictorFactory());
054            */
055    
056            Scene.start(dataSource, factory);
057        }
058    
059        public static void parseArgs(String[] args) {
060            dataSource = null;
061            factory = null;
062    
063            HashMap vizFactories = new HashMap();
064    
065            addVizLibrary(vizFactories);
066    
067            try {
068                for (int i = 0; i < args.length; i++) {
069                    if (args[i].equals("-d") || args[i].equals("--datasource")) {
070                        Object dsource = instanciateFromName(args[++i]);
071                        if (dsource == null) {
072                            System.err.println("EVolve> Failed to instanciate class: \"" + args[i] + "\"");
073                            System.exit(1);
074                        } else if (dsource instanceof DataSource) {
075                            dataSource = (DataSource) dsource;
076                        } else {
077                            System.err.println("EVolve> \"" + args[i] + "\" is not a DataSource");
078                            System.exit(2);
079                        }
080                    } else if (args[i].equals("+v") || args[i].equals("--addviz")) {
081                        Object viz = instanciateFromName(args[++i]);
082                        if (viz == null) {
083                            System.err.println("EVolve> Failed to instanciate class: \"" + args[i] + "\"");
084                            System.exit(1);
085                        } else if (viz instanceof VisualizationFactory) {
086                            vizFactories.put(args[i], viz);
087                        } else {
088                            System.err.println("EVolve> \"" + args[i] + "\" is not a VisualizationFactory");
089                            System.exit(2);
090                        }
091                    } else if (args[i].equals("-v") || args[i].equals("--removeviz")) {
092                        if (vizFactories.containsKey(args[++i])) {
093                            vizFactories.remove(args[i]);
094                        } else {
095                            System.err.println("EVolve> Unknown visualization factory: \"" + args[i] + "\"");
096                            System.exit(1);
097                        }
098                    } else if (args[i].equals("-h") || args[i].equals("--help")) {
099                        System.out.println("Usage: java evolve.Main [options]");
100                        System.out.println("  where options can be:");
101                        System.out.println("  -h | --help");
102                        System.out.println("      Prints this help message");
103                        System.out.println("  -d | --datasource <data source>");
104                        System.out.println("      Specifies the class name of the data source to be used");
105                        System.out.println("  +v | --addviz <viz factory>");
106                        System.out.println("      Specifies the name of a visualization factory to be registered with EVolve");
107                        System.out.println("  -v | --removeviz <viz factory>");
108                        System.out.println("      Specifies the name of a visualization factory to be unregistered with EVolve");
109                        System.out.println("");
110                        System.out.println("Visit the EVolve page at http://www.sable.mcgill.ca/evolve");
111                        System.exit(0);
112                    } else {
113                        System.err.println("EVolve> Unrecognized option: \"" + args[i] + "\"");
114                        System.exit(1);
115                    }
116                }
117            } catch (ArrayIndexOutOfBoundsException e) {
118                System.err.println("EVolve> Missing argument!!");
119                System.exit(3);
120            }
121    
122            if (dataSource == null) {
123                dataSource = new DemoSource(); // provide a default data source
124            }
125    
126            Collection factories = vizFactories.values();
127            factory = new VisualizationFactory[factories.size()];
128            int j = 0;
129            Iterator it = factories.iterator();
130            while (it.hasNext()) {
131                factory[j++] = (VisualizationFactory) it.next();
132            }
133        }
134    
135        private static void addVizLibrary(HashMap vizFactories) {
136            vizFactories.put("TableVizFactory", new TableVizFactory());
137            vizFactories.put("BarChartVizFactory", new BarChartVizFactory());
138            vizFactories.put("ThreadHotspotVizFactory", new ThreadHotspotVizFactory());
139            vizFactories.put("HotspotVizFactory", new HotspotVizFactory());
140            vizFactories.put("StackHotspotVizFactory", new StackHotspotVizFactory());
141            PredictionVizFactory predViz = new PredictionVizFactory();
142            predViz.addPredictorFactory(new DefaultPredictorFactory());
143            vizFactories.put("PredictionVizFactory", predViz);
144            vizFactories.put("CorrelationVizFactory", new CorrelationVizFactory());
145            vizFactories.put("StackVizFactory", new StackVizFactory());
146            vizFactories.put("DotPlotVizFactory", new DotPlotVizFactory());
147            MissPredictionMetricVizFactory missPredViz = new MissPredictionMetricVizFactory();
148            missPredViz.addPredictorFactory(new DefaultPredictorFactory());
149            vizFactories.put("MissPredictionMetricVizFactory", missPredViz);
150        }
151    
152        private static Object instanciateFromName(String className) {
153            /* Get a Class object corresponding to the given
154               class name using Java's Reflection API */
155            try {
156                Class klass = null;
157                try {
158                    klass = Class.forName(className);
159                } catch (ClassNotFoundException e) {
160                    System.err.println("EVolve> Class not found: \"" + className + "\"");
161                    System.exit(1);
162                }
163    
164                return klass.newInstance();
165            } catch (InstantiationException e) {
166            } catch (IllegalAccessException e) {
167            }
168    
169            return null;
170        }
171    }
172