Example usage for org.apache.commons.cli Option Option

List of usage examples for org.apache.commons.cli Option Option

Introduction

In this page you can find the example usage for org.apache.commons.cli Option Option.

Prototype

public Option(String opt, String longOpt, boolean hasArg, String description) throws IllegalArgumentException 

Source Link

Document

Creates an Option using the specified parameters.

Usage

From source file:com.googlecode.hiberpcml.generator.Tool.java

public static void main(String arg[]) throws Exception {
    CommandLineParser parser = new PosixParser();
    Options options = new Options();

    Option destinyOpt = new Option("t", "target", true, "target of the generated classes");
    destinyOpt.setArgName("target");
    options.addOption(destinyOpt);//from  w ww  .  j av a  2s.c  o  m

    Option packageOpt = new Option("p", "package", true, "Java Package of the generated classes");
    packageOpt.setArgName("package");
    options.addOption(packageOpt);

    Option webServiceOpt = new Option("w", "webservice", true, "build webservice classes");
    webServiceOpt.setArgs(2);
    webServiceOpt.setArgName("serviceName service");
    options.addOption(webServiceOpt);

    options.addOption(webServiceOpt);

    CommandLine cmd = parser.parse(options, arg);
    if (cmd.getArgs().length == 0) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("generator [options] FILE|DIRECTORY.", options);
        System.exit(1);
    }

    Pcml pcml;
    ArrayList<File> filesToParse = getFilesToParse(cmd.getArgs()[0]);
    JCodeModel cm = new JCodeModel();
    JPackage _package = cm._package(cmd.getOptionValue("p", ""));
    Generator generator;
    WSGenerator wsGenerator = null;
    File targetFile = new File(cmd.getOptionValue("t", "./target"));

    targetFile.mkdirs();
    if (cmd.hasOption("w")) {
        wsGenerator = new WSGenerator(_package, cmd.getOptionValues("w")[0], cmd.getOptionValues("w")[1]);
    }

    for (File file : filesToParse) {
        pcml = Util.load(file);
        if (cmd.hasOption("w")) {
            wsGenerator.addMethod(pcml);
        } else {
            generator = new Generator();
            generator.generate(_package, pcml);
        }
    }
    cm.build(targetFile);
}

From source file:eu.freme.bpt.Main.java

public static void main(String[] args) {
    final List<String> services = new ArrayList<>();
    for (EService eService : EService.values()) {
        services.add(eService.getName());
    }//from  w  ww  .j  av  a2s.  co m

    Pair<EService, String[]> serviceAndArgs = extractService(args, services);

    EService service = serviceAndArgs.getName();

    // create options that will be parsed from the args
    /////// General BPT options ///////
    Option helpOption = new Option("h", "help", false, "Prints this message");
    Option inputOption = Option.builder("if").longOpt("input-file").argName("input file").desc(
            "The input file or directory to process. In case of a directory, each file in that directory is processed. "
                    + "If not given, standard in is used.")
            .hasArg().build();
    Option outputOption = Option.builder("od").longOpt("output-dir").argName("output dir")
            .desc("The output directory. If not given, output is written to standard out.").hasArg().build();
    Option propertiesOption = Option.builder("prop").longOpt("properties").argName("properties file")
            .desc("The properties file that contains configuration of the tool.").hasArg().build();

    Options options = new Options().addOption(helpOption).addOption(inputOption).addOption(outputOption)
            .addOption(propertiesOption);

    /////// Common service options ///////
    Option informatOption = Option.builder("f").longOpt("informat").argName("FORMAT")
            .desc("The format of the input document(s). Defaults to 'turtle'").hasArg().build();
    Option outformatOption = Option.builder("o").longOpt("outformat").argName("FORMAT")
            .desc("The desired output format of the service. Defaults to 'turtle'").hasArg().build();
    options.addOption(informatOption).addOption(outformatOption);

    /////// Service specific options ///////
    if (service != null) {
        switch (service) {
        case E_TRANSLATION:
            ETranslation.addOptions(options);
            break;
        case E_ENTITY:
            EEntity.addOptions(options);
            break;
        case E_LINK:
            ELink.addOptions(options);
            break;
        case E_TERMINOLOGY:
            ETerminology.addOptions(options);
            break;
        case PIPELINING:
            Pipelining.addOptions(options);
            break;
        case E_PUBLISHING:
            // TODO !
        default:
            logger.warn("Unknown service {}. Skipping!", service);
            break;
        }
    } else {
        ETranslation.addOptions(options);
        EEntity.addOptions(options);
        ELink.addOptions(options);
        ETerminology.addOptions(options);
        Pipelining.addOptions(options);
    }

    CommandLine commandLine = null;
    int exitValue;
    try {
        CommandLineParser parser = new DefaultParser();
        commandLine = parser.parse(options, serviceAndArgs.getValue());
        exitValue = 0;
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        exitValue = 1;
    }
    if ((exitValue != 0) || commandLine.hasOption("h") || service == null) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(132);
        formatter.printHelp("java -jar <this jar file> <e-service> ", options, true);
        System.exit(exitValue);
    }

    logger.debug("Commandline successfully parsed!");

    try {
        BPT batchProcessingTool = new BPT();
        if (commandLine.hasOption("prop")) {
            batchProcessingTool.loadProperties(commandLine.getOptionValue("prop"));
        }
        if (commandLine.hasOption("if")) {
            batchProcessingTool.setInput(commandLine.getOptionValue("if"));
        }
        if (commandLine.hasOption("od")) {
            batchProcessingTool.setOutput(commandLine.getOptionValue("od"));
        }
        if (commandLine.hasOption('f')) {
            batchProcessingTool.setInFormat(Format.valueOf(commandLine.getOptionValue('f').replace('-', '_')));
        }
        if (commandLine.hasOption('o')) {
            batchProcessingTool.setOutFormat(Format.valueOf(commandLine.getOptionValue('o').replace('-', '_')));
        }

        switch (service) {
        case E_TRANSLATION:
            batchProcessingTool.eTranslation(commandLine.getOptionValue("source-lang"),
                    commandLine.getOptionValue("target-lang"), commandLine.getOptionValue("system"),
                    commandLine.getOptionValue("domain"), commandLine.getOptionValue("key"));
            break;
        case E_ENTITY:
            batchProcessingTool.eEntity(commandLine.getOptionValue("language"),
                    commandLine.getOptionValue("dataset"), commandLine.getOptionValue("mode"));
            break;
        case E_LINK:
            batchProcessingTool.eLink(commandLine.getOptionValue("templateid"));
            break;
        case E_TERMINOLOGY:
            batchProcessingTool.eTerminology(commandLine.getOptionValue("source-lang"),
                    commandLine.getOptionValue("target-lang"), commandLine.getOptionValue("collection"),
                    commandLine.getOptionValue("domain"), commandLine.getOptionValue("key"),
                    commandLine.getOptionValue("mode"));
            break;
        case PIPELINING:
            batchProcessingTool.pipelining(commandLine.getOptionValue("templateid"));
            break;
        case E_PUBLISHING:
        default:
            logger.error("Unknown service {}. Aborting!", service);
            System.exit(3);
        }
    } catch (Exception e) {
        logger.error("Cannot handle input or output. Reason: ", e);
        System.exit(2);
    }
}

From source file:illarion.compile.Compiler.java

public static void main(final String[] args) {
    ByteArrayOutputStream stdOutBuffer = new ByteArrayOutputStream();
    PrintStream orgStdOut = System.out;
    System.setOut(new PrintStream(stdOutBuffer));

    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();/*from www .  j a  va2  s .  c o  m*/

    Options options = new Options();

    final Option npcDir = new Option("n", "npc-dir", true,
            "The place where the compiled NPC files are stored.");
    npcDir.setArgs(1);
    npcDir.setArgName("directory");
    npcDir.setRequired(false);
    options.addOption(npcDir);

    final Option questDir = new Option("q", "quest-dir", true,
            "The place where the compiled Quest files are stored.");
    questDir.setArgs(1);
    questDir.setArgName("directory");
    questDir.setRequired(false);
    options.addOption(questDir);

    final Option type = new Option("t", "type", true,
            "This option is used to set what kind of parser is supposed to be used in case"
                    + " the content of standard input is processed.");
    type.setArgs(1);
    type.setArgName("type");
    type.setRequired(false);
    options.addOption(type);

    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        String[] files = cmd.getArgs();
        if (files.length > 0) {
            System.setOut(orgStdOut);
            stdOutBuffer.writeTo(orgStdOut);

            processFileMode(cmd);
        } else {
            System.setOut(orgStdOut);
            processStdIn(cmd);
        }
    } catch (final ParseException e) {
        final HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java -jar compiler.jar [Options] File", options, true);
        System.exit(-1);
    } catch (final IOException e) {
        LOGGER.error(e.getLocalizedMessage());
        System.exit(-1);
    }
}

From source file:de.onyxbits.raccoon.cli.Router.java

public static void main(String[] args) {
    Options options = new Options();

    Option property = Option.builder("D").argName("property=value").numberOfArgs(2).valueSeparator()
            .desc(Messages.getString(DESC + "D")).build();
    options.addOption(property);//from  w  w  w . j  a  va  2 s.  co  m

    Option help = new Option("h", "help", false, Messages.getString(DESC + "h"));
    options.addOption(help);

    Option version = new Option("v", "version", false, Messages.getString(DESC + "v"));
    options.addOption(version);

    // GPA: Google Play Apps (we might add different markets later)
    Option playAppDetails = new Option(null, "gpa-details", true, Messages.getString(DESC + "gpa-details"));
    playAppDetails.setArgName("package");
    options.addOption(playAppDetails);

    Option playAppBulkDetails = new Option(null, "gpa-bulkdetails", true,
            Messages.getString(DESC + "gpa-bulkdetails"));
    playAppBulkDetails.setArgName("file");
    options.addOption(playAppBulkDetails);

    Option playAppBatchDetails = new Option(null, "gpa-batchdetails", true,
            Messages.getString(DESC + "gpa-batchdetails"));
    playAppBatchDetails.setArgName("file");
    options.addOption(playAppBatchDetails);

    Option playAppSearch = new Option(null, "gpa-search", true, Messages.getString(DESC + "gpa-search"));
    playAppSearch.setArgName("query");
    options.addOption(playAppSearch);

    CommandLine commandLine = null;
    try {
        commandLine = new DefaultParser().parse(options, args);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }

    if (commandLine.hasOption(property.getOpt())) {
        System.getProperties().putAll(commandLine.getOptionProperties(property.getOpt()));
    }

    if (commandLine.hasOption(help.getOpt())) {
        new HelpFormatter().printHelp("raccoon", Messages.getString("header"), options,
                Messages.getString("footer"), true);
        System.exit(0);
    }

    if (commandLine.hasOption(version.getOpt())) {
        System.out.println(GlobalsProvider.getGlobals().get(Version.class));
        System.exit(0);
    }

    if (commandLine.hasOption(playAppDetails.getLongOpt())) {
        Play.details(commandLine.getOptionValue(playAppDetails.getLongOpt()));
        System.exit(0);
    }

    if (commandLine.hasOption(playAppBulkDetails.getLongOpt())) {
        Play.bulkDetails(new File(commandLine.getOptionValue(playAppBulkDetails.getLongOpt())));
        System.exit(0);
    }

    if (commandLine.hasOption(playAppBatchDetails.getLongOpt())) {
        Play.details(new File(commandLine.getOptionValue(playAppBatchDetails.getLongOpt())));
        System.exit(0);
    }

    if (commandLine.hasOption(playAppSearch.getLongOpt())) {
        Play.search(commandLine.getOptionValue(playAppSearch.getLongOpt()));
        System.exit(0);
    }
}

From source file:edu.wisc.doit.tcrypt.cli.TokenCrypt.java

public static void main(String[] args) throws IOException {
    // create Options object
    final Options options = new Options();

    // operation opt group
    final OptionGroup cryptTypeGroup = new OptionGroup();
    cryptTypeGroup.addOption(new Option("e", "encrypt", false, "Encrypt a token"));
    cryptTypeGroup.addOption(new Option("d", "decrypt", false, "Decrypt a token"));
    cryptTypeGroup/*from   w  ww. ja  va2 s  . co m*/
            .addOption(new Option("c", "check", false, "Check if the string looks like an encrypted token"));
    cryptTypeGroup.setRequired(true);
    options.addOptionGroup(cryptTypeGroup);

    // token source opt group
    final OptionGroup tokenGroup = new OptionGroup();
    final Option tokenOpt = new Option("t", "token", true, "The token(s) to operate on");
    tokenOpt.setArgs(Option.UNLIMITED_VALUES);
    tokenGroup.addOption(tokenOpt);
    final Option tokenFileOpt = new Option("f", "file", true,
            "A file with one token per line to operate on, if - is specified stdin is used");
    tokenGroup.addOption(tokenFileOpt);
    tokenGroup.setRequired(true);
    options.addOptionGroup(tokenGroup);

    final Option keyOpt = new Option("k", "keyFile", true,
            "Key file to use. Must be a private key for decryption and a public key for encryption");
    keyOpt.setRequired(true);
    options.addOption(keyOpt);

    // create the parser
    final CommandLineParser parser = new GnuParser();
    CommandLine line = null;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (ParseException exp) {
        // automatically generate the help statement
        System.err.println(exp.getMessage());
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java " + TokenCrypt.class.getName(), options, true);
        System.exit(1);
    }

    final Reader keyReader = createKeyReader(line);

    final TokenHandler tokenHandler = createTokenHandler(line, keyReader);

    if (line.hasOption("t")) {
        //tokens on cli
        final String[] tokens = line.getOptionValues("t");
        for (final String token : tokens) {
            handleToken(tokenHandler, token);
        }
    } else {
        //tokens from a file
        final String tokenFile = line.getOptionValue("f");
        final BufferedReader fileReader;
        if ("-".equals(tokenFile)) {
            fileReader = new BufferedReader(new InputStreamReader(System.in));
        } else {
            fileReader = new BufferedReader(new FileReader(tokenFile));
        }

        while (true) {
            final String token = fileReader.readLine();
            if (token == null) {
                break;
            }

            handleToken(tokenHandler, token);
        }
    }
}

From source file:com.chezzverse.timelogger.server.TimeLoggerServerMain.java

public static void main(String[] args) {

    // Initialize the default configuration file to the hard coded default path value.
    String configFileName = _DEFAULT_CONFIG_FILE;

    // Create all the available options
    Option portOpt = new Option("p", "port", true, "Set the listen port for the http server " + "instance.");
    portOpt.setArgName("number");

    Option htmlOpt = new Option("h", "html-root", true,
            "Set the root directory that " + "contains the required html files.");
    htmlOpt.setArgName("directory");

    Option configOpt = new Option("c", "config", true,
            "Get configuration options from the " + "specified file");
    Option backlogOpt = new Option("b", "backlog", true,
            "Set the number of connections to " + "queue for the server.");

    Options opts = new Options();
    opts.addOption(portOpt);//from w  w  w . j  a  v a  2  s .co  m
    opts.addOption(htmlOpt);
    opts.addOption(configOpt);
    opts.addOption(backlogOpt);

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    HelpFormatter formatter = new HelpFormatter();

    try {
        // Parse the command line arguments.
        cmd = parser.parse(opts, args);

        // Fist check to see if there is a custom defined config file, and load the config file.
        if (cmd.hasOption("c")) {
            configFileName = cmd.getOptionValue("c");
        }

        TimeLoggerConfig.getInstance().LoadConfigFile(configFileName);

        // Process the rest of the args and override any settings in the config file.
        if (cmd.hasOption("h")) {
            TimeLoggerConfig.getInstance().htmlRoot = cmd.getOptionValue("h");
        }

        if (cmd.hasOption("p")) {
            TimeLoggerConfig.getInstance().port = Integer.parseInt(cmd.getOptionValue("p"));
        }

        if (cmd.hasOption("b")) {
            TimeLoggerConfig.getInstance().maxBackLog = Integer.parseInt(cmd.getOptionValue("b"));
        }

    } catch (ParseException e) {
        System.out.println("Invalid argument.");
        formatter.printHelp("TimeLoggerServer", opts);
        System.exit(1);
    } catch (NumberFormatException e) {
        System.out.println("Invalid argument.");
        formatter.printHelp("TimeLoggerServer", opts);
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
    }

    TimeLoggerApp app = new TimeLoggerApp();

    if (app != null) {
        app.run();
    } else {
        System.exit(3); // Strange termination
    }
}

From source file:com.gordcorp.jira2db.App.java

public static void main(String[] args) throws Exception {
    File log4jFile = new File("log4j.xml");
    if (log4jFile.exists()) {
        DOMConfigurator.configure("log4j.xml");
    }// ww w .j a va2 s .  co  m

    Option help = new Option("h", "help", false, "print this message");

    @SuppressWarnings("static-access")
    Option project = OptionBuilder.withLongOpt("project").withDescription("Only sync Jira project PROJECT")
            .hasArg().withArgName("PROJECT").create();

    @SuppressWarnings("static-access")
    Option property = OptionBuilder.withArgName("property=value").hasArgs(2).withValueSeparator()
            .withDescription("use value for given property").create("D");

    @SuppressWarnings("static-access")
    Option testJira = OptionBuilder.withLongOpt("test-jira")
            .withDescription("Test the connection to Jira and print the results").create();

    @SuppressWarnings("static-access")
    Option forever = OptionBuilder.withLongOpt("forever")
            .withDescription("Will continue polling Jira and syncing forever").create();

    Options options = new Options();
    options.addOption(help);
    options.addOption(project);
    options.addOption(property);
    options.addOption(testJira);
    options.addOption(forever);

    CommandLineParser parser = new GnuParser();
    try {

        CommandLine line = parser.parse(options, args);

        if (line.hasOption(help.getOpt())) {
            printHelp(options);
            return;
        }

        // Overwrite the properties file with command line arguments
        if (line.hasOption("D")) {
            String[] values = line.getOptionValues("D");
            for (int i = 0; i < values.length; i = i + 2) {
                String key = values[i];
                // If user does not provide a value for this property, use
                // an empty string
                String value = "";
                if (i + 1 < values.length) {
                    value = values[i + 1];
                }

                log.info("Setting key=" + key + " value=" + value);
                PropertiesWrapper.set(key, value);
            }

        }

        if (line.hasOption("test-jira")) {
            testJira();
        } else {
            JiraSynchroniser jira = new JiraSynchroniser();

            if (line.hasOption("project")) {

                jira.setProjects(Arrays.asList(new String[] { line.getOptionValue("project") }));
            }

            if (line.hasOption("forever")) {
                jira.syncForever();
            } else {
                jira.syncAll();
            }
        }
    } catch (ParseException exp) {

        log.error("Parsing failed: " + exp.getMessage());
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}

From source file:microbiosima.SelectiveMicrobiosima.java

/**
 * @param args//from w w  w  . ja  va  2s  .c  om
 *            the command line arguments
 * @throws java.io.FileNotFoundException
 * @throws java.io.UnsupportedEncodingException
 */

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
    int populationSize = 500;//Integer.parseInt(parameters[1]);
    int microSize = 1000;//Integer.parseInt(parameters[2]);
    int numberOfSpecies = 150;//Integer.parseInt(parameters[3]);
    int numberOfGeneration = 10000;
    int Ngene = 10;
    int numberOfObservation = 100;
    int numberOfReplication = 10;
    double Ngenepm = 5;
    double pctEnv = 0;
    double pctPool = 0;
    double msCoeff = 1;
    double hsCoeff = 1;
    boolean HMS_or_TMS = true;

    Options options = new Options();

    Option help = new Option("h", "help", false, "print this message");
    Option version = new Option("v", "version", false, "print the version information and exit");
    options.addOption(help);
    options.addOption(version);

    options.addOption(Option.builder("o").longOpt("obs").hasArg().argName("OBS")
            .desc("Number generation for observation [default: 100]").build());
    options.addOption(Option.builder("r").longOpt("rep").hasArg().argName("REP")
            .desc("Number of replication [default: 1]").build());

    Builder C = Option.builder("c").longOpt("config").numberOfArgs(6).argName("Pop Micro Spec Gen")
            .desc("Four Parameters in the following orders: "
                    + "(1) population size, (2) microbe size, (3) number of species, (4) number of generation, (5) number of total traits, (6)number of traits per microbe"
                    + " [default: 500 1000 150 10000 10 5]");
    options.addOption(C.build());

    HelpFormatter formatter = new HelpFormatter();
    String syntax = "microbiosima pctEnv pctPool";
    String header = "\nSimulates the evolutionary and ecological dynamics of microbiomes within a population of hosts.\n\n"
            + "required arguments:\n" + "  pctEnv             Percentage of environmental acquisition\n"
            + "  pctPool            Percentage of pooled environmental component\n"
            + "  msCoeff            Parameter related to microbe selection strength\n"
            + "  hsCoeff            Parameter related to host selection strength\n"
            + "  HMS_or_TMS         String HMS or TMS to specify host-mediated or trait-mediated microbe selection\n"
            + "\noptional arguments:\n";
    String footer = "\n";

    formatter.setWidth(80);

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;

    try {
        cmd = parser.parse(options, args);
        String[] pct_config = cmd.getArgs();

        if (cmd.hasOption("h") || args.length == 0) {
            formatter.printHelp(syntax, header, options, footer, true);
            System.exit(0);
        }
        if (cmd.hasOption("v")) {
            System.out.println("Microbiosima " + VERSION);
            System.exit(0);
        }
        if (pct_config.length != 5) {
            System.out.println(
                    "ERROR! Required exactly five argumennts for pct_env, pct_pool, msCoeff, hsCoeff and HMS_or_TMS. It got "
                            + pct_config.length + ": " + Arrays.toString(pct_config));
            formatter.printHelp(syntax, header, options, footer, true);
            System.exit(3);
        } else {
            pctEnv = Double.parseDouble(pct_config[0]);
            pctPool = Double.parseDouble(pct_config[1]);
            msCoeff = Double.parseDouble(pct_config[2]);
            hsCoeff = Double.parseDouble(pct_config[3]);
            if (pct_config[4].equals("HMS"))
                HMS_or_TMS = true;
            if (pct_config[4].equals("TMS"))
                HMS_or_TMS = false;
            if (pctEnv < 0 || pctEnv > 1) {
                System.out.println(
                        "ERROR: pctEnv (Percentage of environmental acquisition) must be between 0 and 1 (pctEnv="
                                + pctEnv + ")! EXIT");
                System.exit(3);
            }
            if (pctPool < 0 || pctPool > 1) {
                System.out.println(
                        "ERROR: pctPool (Percentage of pooled environmental component must) must be between 0 and 1 (pctPool="
                                + pctPool + ")! EXIT");
                System.exit(3);
            }
            if (msCoeff < 1) {
                System.out.println(
                        "ERROR: msCoeff (parameter related to microbe selection strength) must be not less than 1 (msCoeff="
                                + msCoeff + ")! EXIT");
                System.exit(3);
            }
            if (hsCoeff < 1) {
                System.out.println(
                        "ERROR: hsCoeff (parameter related to host selection strength) must be not less than 1 (hsCoeff="
                                + hsCoeff + ")! EXIT");
                System.exit(3);
            }
            if (!(pct_config[4].equals("HMS") || pct_config[4].equals("TMS"))) {
                System.out.println(
                        "ERROR: HMS_or_TMS (parameter specifying host-mediated or trait-mediated selection) must be either 'HMS' or 'TMS' (HMS_or_TMS="
                                + pct_config[4] + ")! EXIT");
                System.exit(3);
            }

        }
        if (cmd.hasOption("config")) {
            String[] configs = cmd.getOptionValues("config");
            populationSize = Integer.parseInt(configs[0]);
            microSize = Integer.parseInt(configs[1]);
            numberOfSpecies = Integer.parseInt(configs[2]);
            numberOfGeneration = Integer.parseInt(configs[3]);
            Ngene = Integer.parseInt(configs[4]);
            Ngenepm = Double.parseDouble(configs[5]);
            if (Ngenepm > Ngene) {
                System.out.println(
                        "ERROR: number of traits per microbe must not be greater than number of total traits! EXIT");
                System.exit(3);
            }
        }
        if (cmd.hasOption("obs")) {
            numberOfObservation = Integer.parseInt(cmd.getOptionValue("obs"));
        }
        if (cmd.hasOption("rep")) {
            numberOfReplication = Integer.parseInt(cmd.getOptionValue("rep"));
        }

    } catch (ParseException e) {
        e.printStackTrace();
        System.exit(3);
    }

    StringBuilder sb = new StringBuilder();
    sb.append("Configuration Summary:").append("\n\tPopulation size: ").append(populationSize)
            .append("\n\tMicrobe size: ").append(microSize).append("\n\tNumber of species: ")
            .append(numberOfSpecies).append("\n\tNumber of generation: ").append(numberOfGeneration)
            .append("\n\tNumber generation for observation: ").append(numberOfObservation)
            .append("\n\tNumber of replication: ").append(numberOfReplication)
            .append("\n\tNumber of total traits: ").append(Ngene).append("\n\tNumber of traits per microbe: ")
            .append(Ngenepm).append("\n");
    System.out.println(sb.toString());

    double[] environment = new double[numberOfSpecies];
    for (int i = 0; i < numberOfSpecies; i++) {
        environment[i] = 1 / (double) numberOfSpecies;
    }
    int[] fitnessToHost = new int[Ngene];
    int[] fitnessToMicrobe = new int[Ngene];

    for (int rep = 0; rep < numberOfReplication; rep++) {
        String prefix = "" + (rep + 1) + "_";
        String sufix;
        if (HMS_or_TMS)
            sufix = "_E" + pctEnv + "_P" + pctPool + "_HS" + hsCoeff + "_HMS" + msCoeff + ".txt";
        else
            sufix = "_E" + pctEnv + "_P" + pctPool + "_HS" + hsCoeff + "_TMS" + msCoeff + ".txt";
        System.out.println("Output 5 result files in the format of: " + prefix + "[****]" + sufix);
        try {
            PrintWriter file1 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "gamma_diversity" + sufix)));
            PrintWriter file2 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "alpha_diversity" + sufix)));
            PrintWriter file3 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "beta_diversity" + sufix)));
            PrintWriter file4 = new PrintWriter(new BufferedWriter(new FileWriter(prefix + "sum" + sufix)));
            PrintWriter file5 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "inter_generation_distance" + sufix)));
            PrintWriter file6 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "environment_population_distance" + sufix)));
            PrintWriter file7 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "host_fitness" + sufix)));
            PrintWriter file8 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "cos_theta" + sufix)));
            PrintWriter file9 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "host_fitness_distribution" + sufix)));
            PrintWriter file10 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "microbiome_fitness_distribution" + sufix)));
            PrintWriter file11 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "bacteria_contents" + sufix)));
            PrintWriter file12 = new PrintWriter(
                    new BufferedWriter(new FileWriter(prefix + "individual_bacteria_contents" + sufix)));
            for (int i = 0; i < Ngene; i++) {
                fitnessToMicrobe[i] = MathUtil.getNextInt(2) - 1;
                fitnessToHost[i] = MathUtil.getNextInt(2) - 1;
            }
            MathUtil.setSeed(rep % numberOfReplication);
            SelectiveSpeciesRegistry ssr = new SelectiveSpeciesRegistry(numberOfSpecies, Ngene, Ngenepm,
                    msCoeff, fitnessToHost, fitnessToMicrobe);
            MathUtil.setSeed();
            SelectivePopulation population = new SelectivePopulation(microSize, environment, populationSize,
                    pctEnv, pctPool, 0, 0, ssr, hsCoeff, HMS_or_TMS);

            while (population.getNumberOfGeneration() < numberOfGeneration) {
                population.sumSpecies();
                if (population.getNumberOfGeneration() % numberOfObservation == 0) {
                    //file1.print(population.gammaDiversity(false));
                    //file2.print(population.alphaDiversity(false));
                    //file1.print("\t");
                    //file2.print("\t");
                    file1.println(population.gammaDiversity(true));
                    file2.println(population.alphaDiversity(true));
                    //file3.print(population.betaDiversity(true));
                    //file3.print("\t");
                    file3.println(population.BrayCurtis(true));
                    file4.println(population.printOut());
                    file5.println(population.interGenerationDistance());
                    file6.println(population.environmentPopulationDistance());
                    file7.print(population.averageHostFitness());
                    file7.print("\t");
                    file7.println(population.varianceHostFitness());
                    file8.println(population.cosOfMH());
                    file9.println(population.printOutHFitness());
                    file10.println(population.printOutMFitness());
                    file11.println(population.printBacteriaContents());
                }
                population.getNextGen();
            }
            for (SelectiveIndividual host : population.getIndividuals()) {
                file12.println(host.printBacteriaContents());
            }
            file1.close();
            file2.close();
            file3.close();
            file4.close();
            file5.close();
            file6.close();
            file7.close();
            file8.close();
            file9.close();
            file10.close();
            file11.close();
            file12.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.nyu.vida.data_polygamy.pre_processing.PreProcessing.java

/**
 * @param args/*w ww .  j  a v a 2s  . co m*/
 * @throws IOException 
 * @throws ClassNotFoundException 
 * @throws InterruptedException 
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

    Options options = new Options();

    Option nameOption = new Option("dn", "name", true, "the name of the dataset");
    nameOption.setRequired(true);
    nameOption.setArgName("DATASET NAME");
    options.addOption(nameOption);

    Option headerOption = new Option("dh", "header", true, "the file that contains the header of the dataset");
    headerOption.setRequired(true);
    headerOption.setArgName("DATASET HEADER FILE");
    options.addOption(headerOption);

    Option deafultsOption = new Option("dd", "defaults", true,
            "the file that contains the default values of the dataset");
    deafultsOption.setRequired(true);
    deafultsOption.setArgName("DATASET DEFAULTS FILE");
    options.addOption(deafultsOption);

    Option tempResOption = new Option("t", "temporal", true,
            "desired temporal resolution (hour, day, week, or month)");
    tempResOption.setRequired(true);
    tempResOption.setArgName("TEMPORAL RESOLUTION");
    options.addOption(tempResOption);

    Option spatialResOption = new Option("s", "spatial", true,
            "desired spatial resolution (points, nbhd, zip, grid, or city)");
    spatialResOption.setRequired(true);
    spatialResOption.setArgName("SPATIAL RESOLUTION");
    options.addOption(spatialResOption);

    Option currentSpatialResOption = new Option("cs", "current-spatial", true,
            "current spatial resolution (points, nbhd, zip, grid, or city)");
    currentSpatialResOption.setRequired(true);
    currentSpatialResOption.setArgName("CURRENT SPATIAL RESOLUTION");
    options.addOption(currentSpatialResOption);

    Option indexResOption = new Option("i", "index", true, "indexes of the temporal and spatial attributes");
    indexResOption.setRequired(true);
    indexResOption.setArgName("INDEX OF SPATIO-TEMPORAL RESOLUTIONS");
    indexResOption.setArgs(Option.UNLIMITED_VALUES);
    options.addOption(indexResOption);

    Option machineOption = new Option("m", "machine", true, "machine identifier");
    machineOption.setRequired(true);
    machineOption.setArgName("MACHINE");
    machineOption.setArgs(1);
    options.addOption(machineOption);

    Option nodesOption = new Option("n", "nodes", true, "number of nodes");
    nodesOption.setRequired(true);
    nodesOption.setArgName("NODES");
    nodesOption.setArgs(1);
    options.addOption(nodesOption);

    Option s3Option = new Option("s3", "s3", false, "data on Amazon S3");
    s3Option.setRequired(false);
    options.addOption(s3Option);

    Option awsAccessKeyIdOption = new Option("aws_id", "aws-id", true,
            "aws access key id; " + "this is required if the execution is on aws");
    awsAccessKeyIdOption.setRequired(false);
    awsAccessKeyIdOption.setArgName("AWS-ACCESS-KEY-ID");
    awsAccessKeyIdOption.setArgs(1);
    options.addOption(awsAccessKeyIdOption);

    Option awsSecretAccessKeyOption = new Option("aws_key", "aws-id", true,
            "aws secrect access key; " + "this is required if the execution is on aws");
    awsSecretAccessKeyOption.setRequired(false);
    awsSecretAccessKeyOption.setArgName("AWS-SECRET-ACCESS-KEY");
    awsSecretAccessKeyOption.setArgs(1);
    options.addOption(awsSecretAccessKeyOption);

    Option bucketOption = new Option("b", "s3-bucket", true,
            "bucket on s3; " + "this is required if the execution is on aws");
    bucketOption.setRequired(false);
    bucketOption.setArgName("S3-BUCKET");
    bucketOption.setArgs(1);
    options.addOption(bucketOption);

    Option helpOption = new Option("h", "help", false, "display this message");
    helpOption.setRequired(false);
    options.addOption(helpOption);

    HelpFormatter formatter = new HelpFormatter();
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        formatter.printHelp(
                "hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.pre_processing.PreProcessing",
                options, true);
        System.exit(0);
    }

    if (cmd.hasOption("h")) {
        formatter.printHelp(
                "hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.pre_processing.PreProcessing",
                options, true);
        System.exit(0);
    }

    boolean s3 = cmd.hasOption("s3");
    String s3bucket = "";
    String awsAccessKeyId = "";
    String awsSecretAccessKey = "";

    if (s3) {
        if ((!cmd.hasOption("aws_id")) || (!cmd.hasOption("aws_key")) || (!cmd.hasOption("b"))) {
            System.out.println(
                    "Arguments 'aws_id', 'aws_key', and 'b'" + " are mandatory if execution is on AWS.");
            formatter.printHelp(
                    "hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.pre_processing.PreProcessing",
                    options, true);
            System.exit(0);
        }
        s3bucket = cmd.getOptionValue("b");
        awsAccessKeyId = cmd.getOptionValue("aws_id");
        awsSecretAccessKey = cmd.getOptionValue("aws_key");
    }

    boolean snappyCompression = false;
    boolean bzip2Compression = false;
    String machine = cmd.getOptionValue("m");
    int nbNodes = Integer.parseInt(cmd.getOptionValue("n"));

    Configuration s3conf = new Configuration();
    if (s3) {
        s3conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId);
        s3conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey);
        s3conf.set("bucket", s3bucket);
    }

    Configuration conf = new Configuration();
    Machine machineConf = new Machine(machine, nbNodes);
    String dataset = cmd.getOptionValue("dn");
    String header = cmd.getOptionValue("dh");
    String defaults = cmd.getOptionValue("dd");
    String temporalResolution = cmd.getOptionValue("t");
    String spatialResolution = cmd.getOptionValue("s");
    String gridResolution = "";
    String currentSpatialResolution = cmd.getOptionValue("cs");

    if (spatialResolution.contains("grid")) {
        String[] res = spatialResolution.split("-");
        spatialResolution = res[0];
        gridResolution = res[1];
    }

    conf.set("header", s3bucket + FrameworkUtils.dataDir + "/" + header);
    conf.set("defaults", s3bucket + FrameworkUtils.dataDir + "/" + defaults);
    conf.set("temporal-resolution", temporalResolution);
    conf.set("spatial-resolution", spatialResolution);
    conf.set("grid-resolution", gridResolution);
    conf.set("current-spatial-resolution", currentSpatialResolution);

    String[] indexes = cmd.getOptionValues("i");
    String temporalPos = "";
    Integer sizeSpatioTemp = 0;
    if (!(currentSpatialResolution.equals("points"))) {
        String spatialPos = "";
        for (int i = 0; i < indexes.length; i++) {
            temporalPos += indexes[i] + ",";
            spatialPos += indexes[++i] + ",";
            sizeSpatioTemp++;
        }
        conf.set("spatial-pos", spatialPos);
    } else {
        String xPositions = "", yPositions = "";
        for (int i = 0; i < indexes.length; i++) {
            temporalPos += indexes[i] + ",";
            xPositions += indexes[++i] + ",";
            yPositions += indexes[++i] + ",";
            sizeSpatioTemp++;
        }
        conf.set("xPositions", xPositions);
        conf.set("yPositions", yPositions);
    }
    conf.set("temporal-pos", temporalPos);

    conf.set("size-spatio-temporal", sizeSpatioTemp.toString());

    // checking resolutions

    if (utils.spatialResolution(spatialResolution) < 0) {
        System.out.println("Invalid spatial resolution: " + spatialResolution);
        System.exit(-1);
    }

    if (utils.spatialResolution(spatialResolution) == FrameworkUtils.POINTS) {
        System.out.println("The data needs to be reduced at least to neighborhoods or grid.");
        System.exit(-1);
    }

    if (utils.spatialResolution(currentSpatialResolution) < 0) {
        System.out.println("Invalid spatial resolution: " + currentSpatialResolution);
        System.exit(-1);
    }

    if (utils.spatialResolution(currentSpatialResolution) > utils.spatialResolution(spatialResolution)) {
        System.out.println("The current spatial resolution is coarser than "
                + "the desired one. You can only navigate from a fine resolution" + " to a coarser one.");
        System.exit(-1);
    }

    if (utils.temporalResolution(temporalResolution) < 0) {
        System.out.println("Invalid temporal resolution: " + temporalResolution);
        System.exit(-1);
    }

    String fileName = s3bucket + FrameworkUtils.preProcessingDir + "/" + dataset + "-" + temporalResolution
            + "-" + spatialResolution + gridResolution;
    conf.set("aggregates", fileName + ".aggregates");

    // making sure both files are removed, if they exist
    FrameworkUtils.removeFile(fileName, s3conf, s3);
    FrameworkUtils.removeFile(fileName + ".aggregates", s3conf, s3);

    /**
     * Hadoop Parameters
     * sources: http://www.slideshare.net/ImpetusInfo/ppt-on-advanced-hadoop-tuning-n-optimisation
     *          https://cloudcelebrity.wordpress.com/2013/08/14/12-key-steps-to-keep-your-hadoop-cluster-running-strong-and-performing-optimum/
     */

    conf.set("mapreduce.tasktracker.map.tasks.maximum", String.valueOf(machineConf.getMaximumTasks()));
    conf.set("mapreduce.tasktracker.reduce.tasks.maximum", String.valueOf(machineConf.getMaximumTasks()));
    conf.set("mapreduce.jobtracker.maxtasks.perjob", "-1");
    conf.set("mapreduce.reduce.shuffle.parallelcopies", "20");
    conf.set("mapreduce.input.fileinputformat.split.minsize", "0");
    conf.set("mapreduce.task.io.sort.mb", "200");
    conf.set("mapreduce.task.io.sort.factor", "100");

    // using SnappyCodec for intermediate and output data ?
    // TODO: for now, using SnappyCodec -- what about LZO + Protocol Buffer serialization?
    //   LZO - http://www.oberhumer.com/opensource/lzo/#download
    //   Hadoop-LZO - https://github.com/twitter/hadoop-lzo
    //   Protocol Buffer - https://github.com/twitter/elephant-bird
    //   General Info - http://www.devx.com/Java/Article/47913
    //   Compression - http://comphadoop.weebly.com/index.html
    if (snappyCompression) {
        conf.set("mapreduce.map.output.compress", "true");
        conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");
        conf.set("mapreduce.output.fileoutputformat.compress.codec",
                "org.apache.hadoop.io.compress.SnappyCodec");
    }
    if (bzip2Compression) {
        conf.set("mapreduce.map.output.compress", "true");
        conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec");
        conf.set("mapreduce.output.fileoutputformat.compress.codec",
                "org.apache.hadoop.io.compress.BZip2Codec");
    }

    // TODO: this is dangerous!
    if (s3) {
        conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId);
        conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey);
    }

    Job job = new Job(conf);
    job.setJobName(dataset + "-" + temporalResolution + "-" + spatialResolution);

    job.setMapOutputKeyClass(MultipleSpatioTemporalWritable.class);
    job.setMapOutputValueClass(AggregationArrayWritable.class);

    job.setOutputKeyClass(MultipleSpatioTemporalWritable.class);
    job.setOutputValueClass(AggregationArrayWritable.class);

    job.setMapperClass(PreProcessingMapper.class);
    job.setCombinerClass(PreProcessingCombiner.class);
    job.setReducerClass(PreProcessingReducer.class);
    job.setNumReduceTasks(machineConf.getNumberReduces());
    //job.setNumReduceTasks(1);

    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(SequenceFileOutputFormat.class);
    SequenceFileOutputFormat.setCompressOutput(job, true);
    SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.BLOCK);

    FileInputFormat.setInputPaths(job, new Path(s3bucket + FrameworkUtils.dataDir + "/" + dataset));
    FileOutputFormat.setOutputPath(job, new Path(fileName));

    job.setJarByClass(PreProcessing.class);

    long start = System.currentTimeMillis();
    job.submit();
    job.waitForCompletion(true);
    System.out.println(fileName + "\t" + (System.currentTimeMillis() - start));

}

From source file:ca.uqac.dim.net.verify.NetworkChecker.java

/**
 * Main loop. Processes command line arguments, loads the network, builds and
 * sends a file to NuSMV and handles its response.
 * @param args Command-line arguments//  w ww. j av a 2s .c  o  m
 */
public static void main(String[] args) {
    // Configuration variables
    boolean opt_show_file = false, opt_show_messages = true, opt_show_stats = false;

    // Create and parse command line options
    Option opt = null;
    Options options = new Options();
    options.addOption("f", "file", false, "Output NuSMV file to stdout");
    options.addOption("s", "time", false, "Display statistics");
    options.addOption("h", "help", false, "Show usage information");
    options.addOption("q", "quiet", false, "Do not display any message or explanation");
    opt = new Option("i", "file", true, "Input directory");
    opt.setRequired(true);
    options.addOption(opt);
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Error parsing command line arguments");
        showUsage(options);
        System.exit(1);
    }
    if (cmd == null) {
        System.err.println("Error parsing command line arguments");
        showUsage(options);
        System.exit(1);
    }

    // Get options
    assert cmd != null;
    opt_show_file = cmd.hasOption("f");
    opt_show_messages = !cmd.hasOption("q");
    opt_show_stats = cmd.hasOption("s");

    if (opt_show_messages)
        System.err.println(
                "Distributed anomaly verifier\n(C) 2012 Sylvain Hall, Universit du Qubec  Chicoutimi\n");

    // Get input directory and blob
    assert cmd.hasOption("i");
    String slash = System.getProperty("file.separator");
    String input_dir = cmd.getOptionValue("i");
    int loc = input_dir.lastIndexOf(slash);
    String dir_name = "", blob = "";
    if (loc == -1) {
        dir_name = "";
        blob = input_dir;
    } else {
        dir_name = input_dir.substring(0, loc);
        blob = input_dir.substring(loc + 1);
    }

    // Load network
    Network net = null;
    try {
        net = loadNetworkFromDirectory(dir_name, blob, slash);
    } catch (FileNotFoundException e) {
        System.err.print("Error reading ");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }

    // Build NuSMV file
    assert net != null;
    StringBuilder sb = new StringBuilder();
    if (opt_show_file) {
        sb.append("-- File auto-generated by DistributedChecker\n");
        sb.append("-- (C) 2012  Sylvain Hall, Universit du Qubec  Chicoutimi\n\n");
    }
    sb.append(net.toSmv(opt_show_file));
    // Simple shadowing
    String f_shadowing = "G (frozen -> !(interval_l <= rule_interval_l & interval_r >= rule_interval_r & decision != rule_decision))";
    sb.append("\n\nLTLSPEC\n").append(f_shadowing);

    // Run NuSMV and parse its return value
    if (opt_show_file) {
        System.out.println(sb);
        System.exit(0);
    }
    RuntimeInfos ri = null;
    try {
        ri = runNuSMV(sb.toString());
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (InterruptedException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    assert ri != null;

    // If requested, compute and show an explanation from NuSMV's returned contents
    if (opt_show_messages) {
        ExplanationTrace t = null;
        try {
            t = net.explain(ri.m_return_contents);
        } catch (NuSmvParseException e) {
            System.err.println("Error reading NuSMV's answer");
            System.exit(1);
        }
        if (t == null) {
            // No explanation => no anomaly found
            System.out.println("No anomaly found");
        } else
            System.out.println(t);
    }

    // If requested, show runtime statistics
    if (opt_show_stats) {
        StringBuilder out = new StringBuilder();
        out.append(net.getNodeSize()).append(",");
        out.append(net.getFirewallRuleSize()).append(",");
        out.append(net.getRoutingTableSize()).append(",");
        out.append(ri.m_runtime);
        System.out.println(out);
    }
    System.exit(0);
}