co.turnus.analysis.pipelining.SimplePipeliningCliLauncher.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.pipelining.SimplePipeliningCliLauncher.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.pipelining;

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 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.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.pipelining.SimplePipelingData;
import co.turnus.analysis.data.pipelining.io.XlsSimplePipeliningDataWriter;
import co.turnus.trace.TraceProject;
import co.turnus.util.EcoreHelper;
import co.turnus.util.TurnusLogger;
import co.turnus.util.TurnusLogger.TurnusLevel;

public class SimplePipeliningCliLauncher {

    private static final Options cliOptions = new Options();

    static {
        cliOptions.addOption("t", true, "trace project directory");
        cliOptions.addOption("o", true, "output root directory");
        cliOptions.addOption("xls", true, "xls report file name (without extension)");
        cliOptions.addOption("v", false, "activate the logger verbosity: all messages from TURNUS are printed");
    }

    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);

            SimplePipelining sp = new SimplePipelining(project);
            sp.setConfiguration(config);

            SimplePipelingData data = sp.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 Simple Pipeling 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 XlsSimplePipeliningDataWriter().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(SimplePipeliningCliLauncher.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("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);
            }

        }

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

        return config;
    }
}