co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksCliLauncher.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksCliLauncher.java

Source

/* 
 * TURNUS, the co-exploration framework
 * 
 * Copyright (C) 2014 EPFL SCI STI MM
 *
 * This file is part of TURNUS.
 *
 * TURNUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TURNUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TURNUS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7
 * 
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or 
 * an Eclipse library), containing parts covered by the terms of the 
 * Eclipse Public License (EPL), the licensors of this Program grant you 
 * additional permission to convey the resulting work.  Corresponding Source 
 * for a non-source form of such a combination shall include the source code 
 * for the parts of Eclipse libraries used as well as that of the  covered work.
 * 
 */
package co.turnus.analysis.bottlenecks;

import static co.turnus.analysis.AnalysisOptions.OUTPUT_PATH;
import static co.turnus.analysis.AnalysisOptions.TRACE_PROJECT;
import static co.turnus.analysis.AnalysisOptions.VERBOSE;
import static co.turnus.analysis.AnalysisOptions.XLS;
import static co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksOptions.IMPACT_ANALYSIS_ACTIONS;
import static co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksOptions.IMPACT_ANALYSIS_ACTORLEVEL;
import static co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksOptions.IMPACT_ANALYSIS_POINTS;
import static co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksOptions.IMPACT_ANALYSIS_RUN;
import static co.turnus.analysis.bottlenecks.AlgorithmicBottlenecksOptions.PROFILING_WEIGHTS;

import java.io.File;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import co.turnus.TurnusExtension;
import co.turnus.analysis.AnalysisActivator;
import co.turnus.analysis.data.DataFactory;
import co.turnus.analysis.data.Report;
import co.turnus.analysis.data.bottlenecks.AlgoBottlenecksData;
import co.turnus.analysis.data.bottlenecks.io.XlsAlgoBottlenecksDataWriter;
import co.turnus.trace.weighter.statistical.ActionWeightsDistribution;
import co.turnus.trace.weighter.statistical.StatisticalTraceWeighter;
import co.turnus.profiling.ProfilingWeights;
import co.turnus.profiling.io.XmlProfilingWeightsReader;
import co.turnus.trace.TraceProject;
import co.turnus.util.EcoreHelper;
import co.turnus.util.TurnusLogger;
import co.turnus.util.TurnusLogger.TurnusLevel;

@SuppressWarnings("static-access")
public class AlgorithmicBottlenecksCliLauncher {

    private static final Options cliOptions = new Options();

    static {
        cliOptions.addOption("t", true, "trace project directory");
        cliOptions.addOption("o", true, "output root directory");
        cliOptions.addOption("w", true, "profiling weights file");
        cliOptions.addOption("xls", true, "xls report file name (without extension)");
        cliOptions.addOption("v", false, "activate the logger verbosity: all messages from TURNUS are printed");
        Option impactOptions = OptionBuilder.withArgName("actions> <points> <level").withValueSeparator(' ')
                .hasArgs(3)
                .withDescription("run the impact analysis between  [1:N] actions, "
                        + "a grid between [1:100] points, "
                        + "and with a granularity level among {a, c} respectively for actor and actor-class")
                .create("i");
        cliOptions.addOption(impactOptions);
    }

    public static void main(String[] args) {
        try {
            CommandLineParser parser = new GnuParser();
            CommandLine cmd = parser.parse(cliOptions, args);
            Configuration config = parseCommandLine(cmd);

            // init models
            AnalysisActivator.init();

            // set logger verbosity
            if (config.getBoolean(VERBOSE, false)) {
                TurnusLogger.setLevel(TurnusLevel.ALL);
            }

            // load trace project
            File tDir = new File(config.getString(TRACE_PROJECT));
            TraceProject project = TraceProject.load(tDir);

            // load profiling weights
            File wFile = new File(config.getString(PROFILING_WEIGHTS));
            ProfilingWeights weights = new XmlProfilingWeightsReader().read(project.getNetwork(), wFile);

            // build the trace weighter
            StatisticalTraceWeighter tw = new StatisticalTraceWeighter();
            tw.configure(weights, ActionWeightsDistribution.class);

            // run the analysis
            AlgorithmicBottlenecks ab = new AlgorithmicBottlenecks(project, tw);
            ab.setConfiguration(config);
            AlgoBottlenecksData data = ab.run();

            TurnusLogger.info("Storing results...");
            File outPath = new File(config.getString(OUTPUT_PATH));

            // store the analysis report
            String uuid = UUID.randomUUID().toString();
            File rFile = new File(outPath, uuid + "." + TurnusExtension.REPORT);
            Report report = DataFactory.eINSTANCE.createReport();
            report.setDate(new Date());
            report.setComment("Report with only Algorithmic Bottlenecks results analysis");
            report.getDataSet().add(data);
            EcoreHelper.storeEObject(report, new ResourceSetImpl(), rFile);
            TurnusLogger.info("TURNUS report stored in " + rFile);

            // store formatted reports
            String xlsName = config.getString(XLS, "");
            if (!xlsName.isEmpty()) {
                File xlsFile = new File(outPath, xlsName + ".xls");
                new XlsAlgoBottlenecksDataWriter().write(data, xlsFile);
                TurnusLogger.info("XLS report stored in " + xlsFile);
            }

            TurnusLogger.info("Analysis Done!");

        } catch (ParseException e) {
            TurnusLogger.error(e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(AlgorithmicBottlenecksCliLauncher.class.getSimpleName(), cliOptions);
        } catch (Exception e) {
            TurnusLogger.error(e.getMessage());
        }
    }

    private static Configuration parseCommandLine(CommandLine cmd) throws ParseException {
        Configuration config = new BaseConfiguration();

        StringBuffer s = new StringBuffer();

        config.setProperty(VERBOSE, cmd.hasOption("v"));

        if (!cmd.hasOption("t")) {
            s.append("Trace project directory not specified. ");
        } else {
            String fileName = cmd.getOptionValue("t", "");
            File file = new File(fileName);
            if (file == null || !file.exists()) {
                s.append("Trace project does not exists. ");
            } else {
                config.setProperty(TRACE_PROJECT, fileName);
            }
        }

        if (!cmd.hasOption("w")) {
            s.append("Profiling weights file not specified. ");
        } else {
            String fileName = cmd.getOptionValue("w", "");
            File file = new File(fileName);
            if (file == null || !file.exists()) {
                s.append("Profiling weights file does not exists. ");
            } else {
                config.setProperty(PROFILING_WEIGHTS, fileName);
            }
        }

        if (!cmd.hasOption("o")) {
            s.append("Output directory not specified. ");
        } else {
            String fileName = cmd.getOptionValue("o", "");
            File file = new File(fileName);
            if (file == null || !file.exists()) {
                s.append("Output directory does not exists. ");
            } else {
                config.setProperty(OUTPUT_PATH, fileName);
            }
        }

        if (cmd.hasOption("xls")) {
            String fileName = cmd.getOptionValue("xls", "");
            if (fileName == null || fileName.isEmpty()) {
                s.append("XLS file name is not correct. ");
            } else {
                config.setProperty(XLS, fileName);
            }

        }

        if (cmd.hasOption("i")) {
            config.setProperty(IMPACT_ANALYSIS_RUN, true);

            String[] values = cmd.getOptionValues("i");
            if (values.length < 3) {
                s.append("No enought parameters for the impact analysis. ");
            } else {
                try {
                    int intValue = Integer.parseInt(values[0]);
                    config.setProperty(IMPACT_ANALYSIS_ACTIONS, intValue);
                } catch (Exception e) {
                    s.append("The number of actions for the impact analysis should be an integer value. ");
                }
                try {
                    int intValue = Integer.parseInt(values[1]);
                    config.setProperty(IMPACT_ANALYSIS_POINTS, intValue);
                } catch (Exception e) {
                    s.append("The number of points for the impact analysis should be an integer value. ");
                }

                String value = values[2];
                if (value.equals("a")) {
                    config.setProperty(IMPACT_ANALYSIS_ACTORLEVEL, true);
                } else if (value.equals("c")) {
                    config.setProperty(IMPACT_ANALYSIS_ACTORLEVEL, false);
                } else {
                    s.append("The impact analysis granularity is not correct. ");
                }
            }

        } else {
            config.setProperty(IMPACT_ANALYSIS_RUN, false);
        }

        String error = s.toString();
        if (!error.isEmpty()) {
            throw new ParseException(error);
        }

        return config;
    }

}