Example usage for java.lang String equals

List of usage examples for java.lang String equals

Introduction

In this page you can find the example usage for java.lang String equals.

Prototype

public boolean equals(Object anObject) 

Source Link

Document

Compares this string to the specified object.

Usage

From source file:com.ibm.crail.tools.CrailBenchmark.java

public static void main(String[] args) throws Exception {
    String type = "";
    String filename = "/tmp.dat";
    int size = CrailConstants.BUFFER_SIZE;
    int loop = 1;
    int batch = 1;
    int warmup = 32;
    int experiments = 1;
    boolean keepOpen = false;
    int storageClass = 0;
    int locationClass = 0;
    boolean useBuffered = true;

    String benchmarkTypes = "write|writeAsync|readSequential|readRandom|readSequentialAsync|readMultiStream|"
            + "createFile|createFileAsync|createMultiFile|getKey|getFile|getFileAsync|enumerateDir|browseDir|"
            + "writeInt|readInt|seekInt|readMultiStreamInt|printLocationclass";
    Option typeOption = Option.builder("t").desc("type of experiment [" + benchmarkTypes + "]").hasArg()
            .build();//from  w ww  . j a  va  2  s .  c om
    Option fileOption = Option.builder("f").desc("filename").hasArg().build();
    Option sizeOption = Option.builder("s").desc("buffer size [bytes]").hasArg().build();
    Option loopOption = Option.builder("k").desc("loop [1..n]").hasArg().build();
    Option batchOption = Option.builder("b").desc("batch size [1..n]").hasArg().build();
    Option storageOption = Option.builder("c").desc("storageClass for file [1..n]").hasArg().build();
    Option locationOption = Option.builder("p").desc("locationClass for file [1..n]").hasArg().build();
    Option warmupOption = Option.builder("w").desc("number of warmup operations [1..n]").hasArg().build();
    Option experimentOption = Option.builder("e").desc("number of experiments [1..n]").hasArg().build();
    Option openOption = Option.builder("o").desc("whether to keep the file system open [true|false]").hasArg()
            .build();
    Option bufferedOption = Option.builder("m").desc("use buffer streams [true|false]").hasArg().build();

    Options options = new Options();
    options.addOption(typeOption);
    options.addOption(fileOption);
    options.addOption(sizeOption);
    options.addOption(loopOption);
    options.addOption(batchOption);
    options.addOption(storageOption);
    options.addOption(locationOption);
    options.addOption(warmupOption);
    options.addOption(experimentOption);
    options.addOption(openOption);
    options.addOption(bufferedOption);

    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, Arrays.copyOfRange(args, 0, args.length));
    if (line.hasOption(typeOption.getOpt())) {
        type = line.getOptionValue(typeOption.getOpt());
    }
    if (line.hasOption(fileOption.getOpt())) {
        filename = line.getOptionValue(fileOption.getOpt());
    }
    if (line.hasOption(sizeOption.getOpt())) {
        size = Integer.parseInt(line.getOptionValue(sizeOption.getOpt()));
    }
    if (line.hasOption(loopOption.getOpt())) {
        loop = Integer.parseInt(line.getOptionValue(loopOption.getOpt()));
    }
    if (line.hasOption(batchOption.getOpt())) {
        batch = Integer.parseInt(line.getOptionValue(batchOption.getOpt()));
    }
    if (line.hasOption(storageOption.getOpt())) {
        storageClass = Integer.parseInt(line.getOptionValue(storageOption.getOpt()));
    }
    if (line.hasOption(locationOption.getOpt())) {
        locationClass = Integer.parseInt(line.getOptionValue(locationOption.getOpt()));
    }
    if (line.hasOption(warmupOption.getOpt())) {
        warmup = Integer.parseInt(line.getOptionValue(warmupOption.getOpt()));
    }
    if (line.hasOption(experimentOption.getOpt())) {
        experiments = Integer.parseInt(line.getOptionValue(experimentOption.getOpt()));
    }
    if (line.hasOption(openOption.getOpt())) {
        keepOpen = Boolean.parseBoolean(line.getOptionValue(openOption.getOpt()));
    }
    if (line.hasOption(bufferedOption.getOpt())) {
        useBuffered = Boolean.parseBoolean(line.getOptionValue(bufferedOption.getOpt()));
    }

    CrailBenchmark benchmark = new CrailBenchmark(warmup);
    if (type.equals("write")) {
        benchmark.open();
        benchmark.write(filename, size, loop, storageClass, locationClass, useBuffered);
        benchmark.close();
    } else if (type.equalsIgnoreCase("writeAsync")) {
        benchmark.open();
        benchmark.writeAsync(filename, size, loop, batch, storageClass, locationClass);
        benchmark.close();
    } else if (type.equalsIgnoreCase("readSequential")) {
        if (keepOpen)
            benchmark.open();
        for (int i = 0; i < experiments; i++) {
            System.out.println("experiment " + i);
            if (!keepOpen)
                benchmark.open();
            benchmark.readSequential(filename, size, loop, useBuffered);
            if (!keepOpen)
                benchmark.close();
        }
        if (keepOpen)
            benchmark.close();
    } else if (type.equals("readRandom")) {
        if (keepOpen)
            benchmark.open();
        for (int i = 0; i < experiments; i++) {
            System.out.println("experiment " + i);
            if (!keepOpen)
                benchmark.open();
            benchmark.readRandom(filename, size, loop, useBuffered);
            if (!keepOpen)
                benchmark.close();
        }
        if (keepOpen)
            benchmark.close();
    } else if (type.equalsIgnoreCase("readSequentialAsync")) {
        if (keepOpen)
            benchmark.open();
        for (int i = 0; i < experiments; i++) {
            System.out.println("experiment " + i);
            if (!keepOpen)
                benchmark.open();
            benchmark.readSequentialAsync(filename, size, loop, batch);
            if (!keepOpen)
                benchmark.close();
        }
        if (keepOpen)
            benchmark.close();
    } else if (type.equalsIgnoreCase("readMultiStream")) {
        if (keepOpen)
            benchmark.open();
        for (int i = 0; i < experiments; i++) {
            System.out.println("experiment " + i);
            if (!keepOpen)
                benchmark.open();
            benchmark.readMultiStream(filename, size, loop, batch);
            if (!keepOpen)
                benchmark.close();
        }
        if (keepOpen)
            benchmark.close();
    } else if (type.equals("createFile")) {
        benchmark.open();
        benchmark.createFile(filename, loop);
        benchmark.close();
    } else if (type.equals("createFileAsync")) {
        benchmark.open();
        benchmark.createFileAsync(filename, loop, batch);
        benchmark.close();
    } else if (type.equalsIgnoreCase("createMultiFile")) {
        benchmark.open();
        benchmark.createMultiFile(filename, storageClass);
        benchmark.close();
    } else if (type.equalsIgnoreCase("getKey")) {
        benchmark.open();
        benchmark.getKey(filename, size, loop);
        benchmark.close();
    } else if (type.equals("getFile")) {
        benchmark.open();
        benchmark.getFile(filename, loop);
        benchmark.close();
    } else if (type.equals("getFileAsync")) {
        benchmark.open();
        benchmark.getFileAsync(filename, loop, batch);
        benchmark.close();
    } else if (type.equalsIgnoreCase("enumerateDir")) {
        benchmark.open();
        benchmark.enumerateDir(filename, batch);
        benchmark.close();
    } else if (type.equalsIgnoreCase("browseDir")) {
        benchmark.open();
        benchmark.browseDir(filename);
        benchmark.close();
    } else if (type.equalsIgnoreCase("early")) {
        benchmark.open();
        benchmark.early(filename);
        benchmark.close();
    } else if (type.equalsIgnoreCase("writeInt")) {
        benchmark.open();
        benchmark.writeInt(filename, loop);
        benchmark.close();
    } else if (type.equalsIgnoreCase("readInt")) {
        benchmark.open();
        benchmark.readInt(filename, loop);
        benchmark.close();
    } else if (type.equalsIgnoreCase("seekInt")) {
        benchmark.open();
        benchmark.seekInt(filename, loop);
        benchmark.close();
    } else if (type.equalsIgnoreCase("readMultiStreamInt")) {
        benchmark.open();
        benchmark.readMultiStreamInt(filename, loop, batch);
        benchmark.close();
    } else if (type.equalsIgnoreCase("printLocationClass")) {
        benchmark.open();
        benchmark.printLocationClass();
        benchmark.close();
    } else if (type.equalsIgnoreCase("collection")) {
        for (int i = 0; i < experiments; i++) {
            benchmark.collectionTest(size, loop);
        }
    } else if (type.equalsIgnoreCase("locationMap")) {
        benchmark.locationMap();
    } else {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("crail iobench", options);
        System.exit(-1);
    }
}

From source file:hyperheuristics.main.comparisons.ComputeIndicators.java

public static void main(String[] args) throws IOException, InterruptedException {
    int[] numberOfObjectivesArray = new int[] { 2, 4 };

    String[] problems = new String[] { "OO_MyBatis", "OA_AJHsqldb", "OA_AJHotDraw", "OO_BCEL", "OO_JHotDraw",
            "OA_HealthWatcher",
            //                "OA_TollSystems",
            "OO_JBoss" };

    String[] heuristicFunctions = new String[] { LowLevelHeuristic.CHOICE_FUNCTION,
            LowLevelHeuristic.MULTI_ARMED_BANDIT, LowLevelHeuristic.RANDOM };

    String[] algorithms = new String[] { "NSGA-II",
            //            "SPEA2"
    };//  w w  w  .ja va 2 s.co  m

    MetricsUtil metricsUtil = new MetricsUtil();
    DecimalFormat decimalFormatter = new DecimalFormat("0.00E0");
    Mean mean = new Mean();
    StandardDeviation standardDeviation = new StandardDeviation();

    InvertedGenerationalDistance igd = new InvertedGenerationalDistance();
    GenerationalDistance gd = new GenerationalDistance();
    Spread spread = new Spread();
    Coverage coverage = new Coverage();

    for (int objectives : numberOfObjectivesArray) {
        try (FileWriter IGDWriter = new FileWriter("experiment/IGD_" + objectives + ".tex");
                FileWriter spreadWriter = new FileWriter("experiment/SPREAD_" + objectives + ".tex");
                FileWriter GDWriter = new FileWriter("experiment/GD_" + objectives + ".tex");
                FileWriter coverageWriter = new FileWriter("experiment/COVERAGE_" + objectives + ".tex")) {

            StringBuilder latexTableBuilder = new StringBuilder();

            latexTableBuilder.append("\\documentclass{paper}\n").append("\n")
                    .append("\\usepackage[T1]{fontenc}\n").append("\\usepackage[latin1]{inputenc}\n")
                    .append("\\usepackage[hidelinks]{hyperref}\n").append("\\usepackage{tabulary}\n")
                    .append("\\usepackage{booktabs}\n").append("\\usepackage{multirow}\n")
                    .append("\\usepackage{amsmath}\n").append("\\usepackage{mathtools}\n")
                    .append("\\usepackage{graphicx}\n").append("\\usepackage{array}\n")
                    .append("\\usepackage[linesnumbered,ruled,inoutnumbered]{algorithm2e}\n")
                    .append("\\usepackage{subfigure}\n").append("\\usepackage[hypcap]{caption}\n")
                    .append("\\usepackage{pdflscape}\n").append("\n").append("\\begin{document}\n").append("\n")
                    .append("\\begin{landscape}\n").append("\n");

            pfKnown: {

                latexTableBuilder.append("\\begin{table}[!htb]\n").append("\t\\centering\n")
                        .append("\t\\def\\arraystretch{1.5}\n")
                        //                        .append("\t\\setlength{\\tabcolsep}{10pt}\n")
                        //                        .append("\t\\fontsize{8pt}{10pt}\\selectfont\n")
                        .append("\t\\caption{INDICATOR found for $PF_{known}$ for ").append(objectives)
                        .append(" objectives}\n").append("\t\\label{tab:INDICATOR ").append(objectives)
                        .append(" objectives}\n").append("\t\\begin{tabulary}{\\linewidth}{c");

                for (String algorithm : algorithms) {
                    latexTableBuilder.append("c");
                    for (String heuristicFunction : heuristicFunctions) {
                        latexTableBuilder.append("c");
                    }
                }

                latexTableBuilder.append("}\n").append("\t\t\\toprule\n").append("\t\t\\textbf{System}");
                for (String algorithm : algorithms) {
                    latexTableBuilder.append(" & \\textbf{").append(algorithm).append("}");
                    for (String heuristicFunction : heuristicFunctions) {
                        latexTableBuilder.append(" & \\textbf{").append(algorithm).append("-")
                                .append(heuristicFunction).append("}");
                    }
                }
                latexTableBuilder.append("\\\\\n").append("\t\t\\midrule\n");

                for (String problem : problems) {

                    NonDominatedSolutionList trueFront = new NonDominatedSolutionList();
                    pfTrueComposing: {
                        for (String algorithm : algorithms) {
                            SolutionSet mecbaFront = metricsUtil.readNonDominatedSolutionSet(
                                    "resultado/" + algorithm.toLowerCase().replaceAll("-", "") + "/" + problem
                                            + "_Comb_" + objectives + "obj/All_FUN_"
                                            + algorithm.toLowerCase().replaceAll("-", "") + "-" + problem);
                            trueFront.addAll(mecbaFront);

                            for (String hyperHeuristic : heuristicFunctions) {
                                SolutionSet front = metricsUtil.readNonDominatedSolutionSet(
                                        "experiment/" + algorithm + "/" + objectives + "objectives/"
                                                + hyperHeuristic + "/" + problem + "/FUN.txt");
                                trueFront.addAll(front);
                            }
                        }
                    }
                    double[][] trueFrontMatrix = trueFront.writeObjectivesToMatrix();

                    HashMap<String, Double> igdMap = new HashMap<>();
                    HashMap<String, Double> gdMap = new HashMap<>();
                    HashMap<String, Double> spreadMap = new HashMap<>();
                    HashMap<String, Double> coverageMap = new HashMap<>();

                    for (String algorithm : algorithms) {
                        double[][] mecbaFront = metricsUtil
                                .readFront("resultado/" + algorithm.toLowerCase().replaceAll("-", "") + "/"
                                        + problem + "_Comb_" + objectives + "obj/All_FUN_"
                                        + algorithm.toLowerCase().replaceAll("-", "") + "-" + problem);
                        igdMap.put(algorithm,
                                igd.invertedGenerationalDistance(mecbaFront, trueFrontMatrix, objectives));
                        gdMap.put(algorithm, gd.generationalDistance(mecbaFront, trueFrontMatrix, objectives));
                        spreadMap.put(algorithm, spread.spread(mecbaFront, trueFrontMatrix, objectives));
                        coverageMap.put(algorithm, coverage.coverage(mecbaFront, trueFrontMatrix));
                        for (String heuristic : heuristicFunctions) {
                            double[][] heuristicFront = metricsUtil.readFront("experiment/" + algorithm + "/"
                                    + objectives + "objectives/" + heuristic + "/" + problem + "/FUN.txt");
                            igdMap.put(algorithm + "-" + heuristic, igd
                                    .invertedGenerationalDistance(heuristicFront, trueFrontMatrix, objectives));
                            gdMap.put(algorithm + "-" + heuristic,
                                    gd.generationalDistance(heuristicFront, trueFrontMatrix, objectives));
                            spreadMap.put(algorithm + "-" + heuristic,
                                    spread.spread(heuristicFront, trueFrontMatrix, objectives));
                            coverageMap.put(algorithm + "-" + heuristic,
                                    coverage.coverage(heuristicFront, trueFrontMatrix));
                        }
                    }

                    latexTableBuilder.append("\t\t").append(problem);

                    String latexTable = latexTableBuilder.toString();

                    latexTableBuilder = new StringBuilder();

                    latexTable = latexTable.replaceAll("O[OA]\\_", "").replaceAll("ChoiceFunction", "CF")
                            .replaceAll("MultiArmedBandit", "MAB");

                    IGDWriter.write(latexTable.replaceAll("INDICATOR", "IGD"));
                    spreadWriter.write(latexTable.replaceAll("INDICATOR", "Spread"));
                    GDWriter.write(latexTable.replaceAll("INDICATOR", "GD"));
                    coverageWriter.write(latexTable.replaceAll("INDICATOR", "Coverage"));

                    String bestHeuristicIGD = "NULL";
                    String bestHeuristicGD = "NULL";
                    String bestHeuristicSpread = "NULL";
                    String bestHeuristicCoverage = "NULL";

                    getBest: {
                        double bestMeanIGD = Double.POSITIVE_INFINITY;
                        double bestMeanGD = Double.POSITIVE_INFINITY;
                        double bestMeanSpread = Double.NEGATIVE_INFINITY;
                        double bestMeanCoverage = Double.NEGATIVE_INFINITY;

                        for (String heuristic : igdMap.keySet()) {
                            double heuristicIGD = igdMap.get(heuristic);
                            double heuristicGD = gdMap.get(heuristic);
                            double heuristicSpread = spreadMap.get(heuristic);
                            double heuristicCoverage = coverageMap.get(heuristic);

                            if (heuristicIGD < bestMeanIGD) {
                                bestMeanIGD = heuristicIGD;
                                bestHeuristicIGD = heuristic;
                            }
                            if (heuristicGD < bestMeanGD) {
                                bestMeanGD = heuristicGD;
                                bestHeuristicGD = heuristic;
                            }
                            if (heuristicSpread > bestMeanSpread) {
                                bestMeanSpread = heuristicSpread;
                                bestHeuristicSpread = heuristic;
                            }
                            if (heuristicCoverage > bestMeanCoverage) {
                                bestMeanCoverage = heuristicCoverage;
                                bestHeuristicCoverage = heuristic;
                            }
                        }
                    }

                    StringBuilder igdBuilder = new StringBuilder();
                    StringBuilder gdBuilder = new StringBuilder();
                    StringBuilder spreadBuilder = new StringBuilder();
                    StringBuilder coverageBuilder = new StringBuilder();

                    String[] newHeuristicFunctions = new String[heuristicFunctions.length * algorithms.length
                            + algorithms.length];
                    fulfillNewHeuristics: {
                        int i = 0;
                        for (String algorithm : algorithms) {
                            newHeuristicFunctions[i++] = algorithm;
                            for (String heuristicFunction : heuristicFunctions) {
                                newHeuristicFunctions[i++] = algorithm + "-" + heuristicFunction;
                            }
                        }
                    }

                    for (String heuristic : newHeuristicFunctions) {
                        igdBuilder.append(" & ");
                        boolean bold = heuristic.equals(bestHeuristicIGD)
                                || igdMap.get(heuristic).equals(igdMap.get(bestHeuristicIGD));
                        if (bold) {
                            igdBuilder.append("\\textbf{");
                        }
                        igdBuilder.append(decimalFormatter.format(igdMap.get(heuristic)));
                        if (bold) {
                            igdBuilder.append("}");
                        }

                        gdBuilder.append(" & ");
                        bold = heuristic.equals(bestHeuristicGD)
                                || gdMap.get(heuristic).equals(gdMap.get(bestHeuristicGD));
                        if (bold) {
                            gdBuilder.append("\\textbf{");
                        }
                        gdBuilder.append(decimalFormatter.format(gdMap.get(heuristic)));
                        if (bold) {
                            gdBuilder.append("}");
                        }

                        spreadBuilder.append(" & ");
                        bold = heuristic.equals(bestHeuristicSpread)
                                || spreadMap.get(heuristic).equals(spreadMap.get(bestHeuristicSpread));
                        if (bold) {
                            spreadBuilder.append("\\textbf{");
                        }
                        spreadBuilder.append(decimalFormatter.format(spreadMap.get(heuristic)));
                        if (bold) {
                            spreadBuilder.append("}");
                        }

                        coverageBuilder.append(" & ");
                        bold = heuristic.equals(bestHeuristicCoverage)
                                || coverageMap.get(heuristic).equals(coverageMap.get(bestHeuristicCoverage));
                        if (bold) {
                            coverageBuilder.append("\\textbf{");
                        }
                        coverageBuilder.append(decimalFormatter.format(coverageMap.get(heuristic)));
                        if (bold) {
                            coverageBuilder.append("}");
                        }
                    }

                    IGDWriter.write(igdBuilder + "\\\\\n");
                    spreadWriter.write(spreadBuilder + "\\\\\n");
                    GDWriter.write(gdBuilder + "\\\\\n");
                    coverageWriter.write(coverageBuilder + "\\\\\n");
                }
                latexTableBuilder = new StringBuilder();

                latexTableBuilder.append("\t\t\\bottomrule\n").append("\t\\end{tabulary}\n")
                        .append("\\end{table}\n\n");
            }

            averages: {

                latexTableBuilder.append("\\begin{table}[!htb]\n").append("\t\\centering\n")
                        .append("\t\\def\\arraystretch{1.5}\n")
                        //                        .append("\t\\setlength{\\tabcolsep}{10pt}\n")
                        //                        .append("\t\\fontsize{8pt}{10pt}\\selectfont\n")
                        .append("\t\\caption{INDICATOR averages found for ").append(objectives)
                        .append(" objectives}\n").append("\t\\label{tab:INDICATOR ").append(objectives)
                        .append(" objectives}\n").append("\t\\begin{tabulary}{\\linewidth}{c");

                for (String algorithm : algorithms) {
                    latexTableBuilder.append("c");
                    for (String heuristicFunction : heuristicFunctions) {
                        latexTableBuilder.append("c");
                    }
                }

                latexTableBuilder.append("}\n").append("\t\t\\toprule\n").append("\t\t\\textbf{System}");
                for (String algorithm : algorithms) {
                    latexTableBuilder.append(" & \\textbf{").append(algorithm).append("}");
                    for (String heuristicFunction : heuristicFunctions) {
                        latexTableBuilder.append(" & \\textbf{").append(algorithm).append("-")
                                .append(heuristicFunction).append("}");
                    }
                }
                latexTableBuilder.append("\\\\\n").append("\t\t\\midrule\n");

                for (String problem : problems) {

                    NonDominatedSolutionList trueFront = new NonDominatedSolutionList();
                    pfTrueComposing: {
                        for (String algorithm : algorithms) {
                            SolutionSet mecbaFront = metricsUtil.readNonDominatedSolutionSet(
                                    "resultado/" + algorithm.toLowerCase().replaceAll("-", "") + "/" + problem
                                            + "_Comb_" + objectives + "obj/All_FUN_"
                                            + algorithm.toLowerCase().replaceAll("-", "") + "-" + problem);
                            trueFront.addAll(mecbaFront);

                            for (String hyperHeuristic : heuristicFunctions) {
                                SolutionSet front = metricsUtil.readNonDominatedSolutionSet(
                                        "experiment/" + algorithm + "/" + objectives + "objectives/"
                                                + hyperHeuristic + "/" + problem + "/FUN.txt");
                                trueFront.addAll(front);
                            }
                        }
                    }
                    double[][] trueFrontMatrix = trueFront.writeObjectivesToMatrix();

                    HashMap<String, double[]> igdMap = new HashMap<>();
                    HashMap<String, double[]> gdMap = new HashMap<>();
                    HashMap<String, double[]> spreadMap = new HashMap<>();
                    HashMap<String, double[]> coverageMap = new HashMap<>();

                    mocaito: {
                        for (String algorithm : algorithms) {
                            double[] mecbaIGDs = new double[EXECUTIONS];
                            double[] mecbaGDs = new double[EXECUTIONS];
                            double[] mecbaSpreads = new double[EXECUTIONS];
                            double[] mecbaCoverages = new double[EXECUTIONS];
                            for (int i = 0; i < EXECUTIONS; i++) {
                                double[][] mecbaFront = metricsUtil.readFront("resultado/"
                                        + algorithm.toLowerCase().replaceAll("-", "") + "/" + problem + "_Comb_"
                                        + objectives + "obj/FUN_" + algorithm.toLowerCase().replaceAll("-", "")
                                        + "-" + problem + "-" + i + ".NaoDominadas");

                                mecbaIGDs[i] = igd.invertedGenerationalDistance(mecbaFront, trueFrontMatrix,
                                        objectives);
                                mecbaGDs[i] = gd.generationalDistance(mecbaFront, trueFrontMatrix, objectives);
                                mecbaSpreads[i] = spread.spread(mecbaFront, trueFrontMatrix, objectives);
                                mecbaCoverages[i] = coverage.coverage(mecbaFront, trueFrontMatrix);
                            }
                            igdMap.put(algorithm, mecbaIGDs);
                            gdMap.put(algorithm, mecbaGDs);
                            spreadMap.put(algorithm, mecbaSpreads);
                            coverageMap.put(algorithm, mecbaCoverages);
                        }
                    }

                    for (String algorithm : algorithms) {
                        for (String heuristic : heuristicFunctions) {
                            double[] hhIGDs = new double[EXECUTIONS];
                            double[] hhGDs = new double[EXECUTIONS];
                            double[] hhSpreads = new double[EXECUTIONS];
                            double[] hhCoverages = new double[EXECUTIONS];
                            for (int i = 0; i < EXECUTIONS; i++) {
                                double[][] hhFront = metricsUtil
                                        .readFront("experiment/" + algorithm + "/" + objectives + "objectives/"
                                                + heuristic + "/" + problem + "/EXECUTION_" + i + "/FUN.txt");

                                hhIGDs[i] = igd.invertedGenerationalDistance(hhFront, trueFrontMatrix,
                                        objectives);
                                hhGDs[i] = gd.generationalDistance(hhFront, trueFrontMatrix, objectives);
                                hhSpreads[i] = spread.spread(hhFront, trueFrontMatrix, objectives);
                                hhCoverages[i] = coverage.coverage(hhFront, trueFrontMatrix);
                            }
                            igdMap.put(algorithm + "-" + heuristic, hhIGDs);
                            gdMap.put(algorithm + "-" + heuristic, hhGDs);
                            spreadMap.put(algorithm + "-" + heuristic, hhSpreads);
                            coverageMap.put(algorithm + "-" + heuristic, hhCoverages);
                        }
                    }

                    HashMap<String, HashMap<String, Boolean>> igdResult = KruskalWallisTest.test(igdMap);
                    HashMap<String, HashMap<String, Boolean>> gdResult = KruskalWallisTest.test(gdMap);
                    HashMap<String, HashMap<String, Boolean>> spreadResult = KruskalWallisTest.test(spreadMap);
                    HashMap<String, HashMap<String, Boolean>> coverageResult = KruskalWallisTest
                            .test(coverageMap);

                    latexTableBuilder.append("\t\t").append(problem);

                    String latexTable = latexTableBuilder.toString();
                    latexTable = latexTable.replaceAll("O[OA]\\_", "").replaceAll("ChoiceFunction", "CF")
                            .replaceAll("MultiArmedBandit", "MAB");

                    IGDWriter.write(latexTable.replaceAll("INDICATOR", "IGD"));
                    spreadWriter.write(latexTable.replaceAll("INDICATOR", "Spread"));
                    GDWriter.write(latexTable.replaceAll("INDICATOR", "GD"));
                    coverageWriter.write(latexTable.replaceAll("INDICATOR", "Coverage"));

                    latexTableBuilder = new StringBuilder();

                    String bestHeuristicIGD = "NULL";
                    String bestHeuristicGD = "NULL";
                    String bestHeuristicSpread = "NULL";
                    String bestHeuristicCoverage = "NULL";

                    getBest: {
                        double bestMeanIGD = Double.POSITIVE_INFINITY;
                        double bestMeanGD = Double.POSITIVE_INFINITY;
                        double bestMeanSpread = Double.NEGATIVE_INFINITY;
                        double bestMeanCoverage = Double.NEGATIVE_INFINITY;

                        for (String heuristic : igdMap.keySet()) {
                            double heuristicMeanIGD = mean.evaluate(igdMap.get(heuristic));
                            double heuristicMeanGD = mean.evaluate(gdMap.get(heuristic));
                            double heuristicMeanSpread = mean.evaluate(spreadMap.get(heuristic));
                            double heuristicMeanCoverage = mean.evaluate(coverageMap.get(heuristic));

                            if (heuristicMeanIGD < bestMeanIGD) {
                                bestMeanIGD = heuristicMeanIGD;
                                bestHeuristicIGD = heuristic;
                            }
                            if (heuristicMeanGD < bestMeanGD) {
                                bestMeanGD = heuristicMeanGD;
                                bestHeuristicGD = heuristic;
                            }
                            if (heuristicMeanSpread > bestMeanSpread) {
                                bestMeanSpread = heuristicMeanSpread;
                                bestHeuristicSpread = heuristic;
                            }
                            if (heuristicMeanCoverage > bestMeanCoverage) {
                                bestMeanCoverage = heuristicMeanCoverage;
                                bestHeuristicCoverage = heuristic;
                            }
                        }
                    }

                    StringBuilder igdBuilder = new StringBuilder();
                    StringBuilder gdBuilder = new StringBuilder();
                    StringBuilder spreadBuilder = new StringBuilder();
                    StringBuilder coverageBuilder = new StringBuilder();

                    String[] newHeuristicFunctions = new String[heuristicFunctions.length * algorithms.length
                            + algorithms.length];
                    fulfillNewHeuristics: {
                        int i = 0;
                        for (String algorithm : algorithms) {
                            newHeuristicFunctions[i++] = algorithm;
                            for (String heuristicFunction : heuristicFunctions) {
                                newHeuristicFunctions[i++] = algorithm + "-" + heuristicFunction;
                            }
                        }
                    }

                    for (String heuristic : newHeuristicFunctions) {
                        igdBuilder.append(" & ");
                        boolean bold = heuristic.equals(bestHeuristicIGD)
                                || !igdResult.get(heuristic).get(bestHeuristicIGD);
                        if (bold) {
                            igdBuilder.append("\\textbf{");
                        }
                        igdBuilder.append(decimalFormatter.format(mean.evaluate(igdMap.get(heuristic))) + " ("
                                + decimalFormatter.format(standardDeviation.evaluate(igdMap.get(heuristic)))
                                + ")");
                        if (bold) {
                            igdBuilder.append("}");
                        }

                        gdBuilder.append(" & ");
                        bold = heuristic.equals(bestHeuristicGD)
                                || !gdResult.get(heuristic).get(bestHeuristicGD);
                        if (bold) {
                            gdBuilder.append("\\textbf{");
                        }
                        gdBuilder.append(decimalFormatter.format(mean.evaluate(gdMap.get(heuristic))) + " ("
                                + decimalFormatter.format(standardDeviation.evaluate(gdMap.get(heuristic)))
                                + ")");
                        if (bold) {
                            gdBuilder.append("}");
                        }

                        spreadBuilder.append(" & ");
                        bold = heuristic.equals(bestHeuristicSpread)
                                || !spreadResult.get(heuristic).get(bestHeuristicSpread);
                        if (bold) {
                            spreadBuilder.append("\\textbf{");
                        }
                        spreadBuilder.append(decimalFormatter.format(mean.evaluate(spreadMap.get(heuristic)))
                                + " ("
                                + decimalFormatter.format(standardDeviation.evaluate(spreadMap.get(heuristic)))
                                + ")");
                        if (bold) {
                            spreadBuilder.append("}");
                        }

                        coverageBuilder.append(" & ");
                        bold = heuristic.equals(bestHeuristicCoverage)
                                || !coverageResult.get(heuristic).get(bestHeuristicCoverage);
                        if (bold) {
                            coverageBuilder.append("\\textbf{");
                        }
                        coverageBuilder
                                .append(decimalFormatter.format(mean.evaluate(coverageMap.get(heuristic))))
                                .append(" (")
                                .append(decimalFormatter
                                        .format(standardDeviation.evaluate(coverageMap.get(heuristic))))
                                .append(")");
                        if (bold) {
                            coverageBuilder.append("}");
                        }
                    }

                    IGDWriter.write(igdBuilder + "\\\\\n");
                    spreadWriter.write(spreadBuilder + "\\\\\n");
                    GDWriter.write(gdBuilder + "\\\\\n");
                    coverageWriter.write(coverageBuilder + "\\\\\n");
                }
                latexTableBuilder.append("\t\t\\bottomrule\n").append("\t\\end{tabulary}\n")
                        .append("\\end{table}\n\n");
            }

            latexTableBuilder.append("\\end{landscape}\n\n").append("\\end{document}");

            String latexTable = latexTableBuilder.toString();

            IGDWriter.write(latexTable);
            spreadWriter.write(latexTable);
            GDWriter.write(latexTable);
            coverageWriter.write(latexTable);
        }
    }
}

From source file:com.xiangzhurui.util.ftp.FTPClientExample.java

public static void main(String[] args) throws UnknownHostException {
    boolean storeFile = false, binaryTransfer = false, error = false, listFiles = false, listNames = false,
            hidden = false;/*w ww . j a  va  2 s  .com*/
    boolean localActive = false, useEpsvWithIPv4 = false, feat = false, printHash = false;
    boolean mlst = false, mlsd = false, mdtm = false, saveUnparseable = false;
    boolean lenient = false;
    long keepAliveTimeout = -1;
    int controlKeepAliveReplyTimeout = -1;
    int minParams = 5; // listings require 3 params
    String protocol = null; // SSL protocol
    String doCommand = null;
    String trustmgr = null;
    String proxyHost = null;
    int proxyPort = 80;
    String proxyUser = null;
    String proxyPassword = null;
    String username = null;
    String password = null;
    String encoding = null;
    String serverTimeZoneId = null;
    String displayTimeZoneId = null;
    String serverType = null;
    String defaultDateFormat = null;
    String recentDateFormat = null;

    int base = 0;
    for (base = 0; base < args.length; base++) {
        if (args[base].equals("-s")) {
            storeFile = true;
        } else if (args[base].equals("-a")) {
            localActive = true;
        } else if (args[base].equals("-A")) {
            username = "anonymous";
            password = System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName();
        } else if (args[base].equals("-b")) {
            binaryTransfer = true;
        } else if (args[base].equals("-c")) {
            doCommand = args[++base];
            minParams = 3;
        } else if (args[base].equals("-d")) {
            mlsd = true;
            minParams = 3;
        } else if (args[base].equals("-e")) {
            useEpsvWithIPv4 = true;
        } else if (args[base].equals("-E")) {
            encoding = args[++base];
        } else if (args[base].equals("-f")) {
            feat = true;
            minParams = 3;
        } else if (args[base].equals("-h")) {
            hidden = true;
        } else if (args[base].equals("-k")) {
            keepAliveTimeout = Long.parseLong(args[++base]);
        } else if (args[base].equals("-l")) {
            listFiles = true;
            minParams = 3;
        } else if (args[base].equals("-m")) {
            mdtm = true;
            minParams = 3;
        } else if (args[base].equals("-L")) {
            lenient = true;
        } else if (args[base].equals("-n")) {
            listNames = true;
            minParams = 3;
        } else if (args[base].equals("-p")) {
            protocol = args[++base];
        } else if (args[base].equals("-S")) {
            serverType = args[++base];
        } else if (args[base].equals("-t")) {
            mlst = true;
            minParams = 3;
        } else if (args[base].equals("-U")) {
            saveUnparseable = true;
        } else if (args[base].equals("-w")) {
            controlKeepAliveReplyTimeout = Integer.parseInt(args[++base]);
        } else if (args[base].equals("-T")) {
            trustmgr = args[++base];
        } else if (args[base].equals("-y")) {
            defaultDateFormat = args[++base];
        } else if (args[base].equals("-Y")) {
            recentDateFormat = args[++base];
        } else if (args[base].equals("-Z")) {
            serverTimeZoneId = args[++base];
        } else if (args[base].equals("-z")) {
            displayTimeZoneId = args[++base];
        } else if (args[base].equals("-PrH")) {
            proxyHost = args[++base];
            String[] parts = proxyHost.split(":");
            if (parts.length == 2) {
                proxyHost = parts[0];
                proxyPort = Integer.parseInt(parts[1]);
            }
        } else if (args[base].equals("-PrU")) {
            proxyUser = args[++base];
        } else if (args[base].equals("-PrP")) {
            proxyPassword = args[++base];
        } else if (args[base].equals("-#")) {
            printHash = true;
        } else {
            break;
        }
    }

    int remain = args.length - base;
    if (username != null) {
        minParams -= 2;
    }
    if (remain < minParams) // server, user, pass, remote, local [protocol]
    {
        if (args.length > 0) {
            System.err.println("Actual Parameters: " + Arrays.toString(args));
        }
        System.err.println(USAGE);
        System.exit(1);
    }

    String server = args[base++];
    int port = 0;
    String[] parts = server.split(":");
    if (parts.length == 2) {
        server = parts[0];
        port = Integer.parseInt(parts[1]);
    }
    if (username == null) {
        username = args[base++];
        password = args[base++];
    }

    String remote = null;
    if (args.length - base > 0) {
        remote = args[base++];
    }

    String local = null;
    if (args.length - base > 0) {
        local = args[base++];
    }

    final FTPClient ftp;
    if (protocol == null) {
        if (proxyHost != null) {
            System.out.println("Using HTTP proxy server: " + proxyHost);
            ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
        } else {
            ftp = new FTPClient();
        }
    } else {
        FTPSClient ftps;
        if (protocol.equals("true")) {
            ftps = new FTPSClient(true);
        } else if (protocol.equals("false")) {
            ftps = new FTPSClient(false);
        } else {
            String[] prot = protocol.split(",");
            if (prot.length == 1) { // Just protocol
                ftps = new FTPSClient(protocol);
            } else { // protocol,true|false
                ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1]));
            }
        }
        ftp = ftps;
        if ("all".equals(trustmgr)) {
            ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
        } else if ("valid".equals(trustmgr)) {
            ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
        } else if ("none".equals(trustmgr)) {
            ftps.setTrustManager(null);
        }
    }

    if (printHash) {
        ftp.setCopyStreamListener(createListener());
    }
    if (keepAliveTimeout >= 0) {
        ftp.setControlKeepAliveTimeout(keepAliveTimeout);
    }
    if (controlKeepAliveReplyTimeout >= 0) {
        ftp.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout);
    }
    if (encoding != null) {
        ftp.setControlEncoding(encoding);
    }
    ftp.setListHiddenFiles(hidden);

    // suppress login details
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

    final FTPClientConfig config;
    if (serverType != null) {
        config = new FTPClientConfig(serverType);
    } else {
        config = new FTPClientConfig();
    }
    config.setUnparseableEntries(saveUnparseable);
    if (defaultDateFormat != null) {
        config.setDefaultDateFormatStr(defaultDateFormat);
    }
    if (recentDateFormat != null) {
        config.setRecentDateFormatStr(recentDateFormat);
    }
    ftp.configure(config);

    try {
        int reply;
        if (port > 0) {
            ftp.connect(server, port);
        } else {
            ftp.connect(server);
        }
        System.out.println("Connected to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()));

        // After connection attempt, you should check the reply code to
        // verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    __main: try {
        if (!ftp.login(username, password)) {
            ftp.logout();
            error = true;
            break __main;
        }

        System.out.println("Remote system is " + ftp.getSystemType());

        if (binaryTransfer) {
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        } else {
            // in theory this should not be necessary as servers should
            // default to ASCII
            // but they don't all do so - see NET-500
            ftp.setFileType(FTP.ASCII_FILE_TYPE);
        }

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        if (localActive) {
            ftp.enterLocalActiveMode();
        } else {
            ftp.enterLocalPassiveMode();
        }

        ftp.setUseEPSVwithIPv4(useEpsvWithIPv4);

        if (storeFile) {
            InputStream input;

            input = new FileInputStream(local);

            ftp.storeFile(remote, input);

            input.close();
        }
        // Allow multiple list types for single invocation
        else if (listFiles || mlsd || mdtm || mlst || listNames) {
            if (mlsd) {
                for (FTPFile f : ftp.mlistDir(remote)) {
                    System.out.println(f.getRawListing());
                    System.out.println(f.toFormattedString(displayTimeZoneId));
                }
            }
            if (mdtm) {
                FTPFile f = ftp.mdtmFile(remote);
                System.out.println(f.getRawListing());
                System.out.println(f.toFormattedString(displayTimeZoneId));
            }
            if (mlst) {
                FTPFile f = ftp.mlistFile(remote);
                if (f != null) {
                    System.out.println(f.toFormattedString(displayTimeZoneId));
                }
            }
            if (listNames) {
                for (String s : ftp.listNames(remote)) {
                    System.out.println(s);
                }
            }
            // Do this last because it changes the client
            if (listFiles) {
                if (lenient || serverTimeZoneId != null) {
                    config.setLenientFutureDates(lenient);
                    if (serverTimeZoneId != null) {
                        config.setServerTimeZoneId(serverTimeZoneId);
                    }
                    ftp.configure(config);
                }

                for (FTPFile f : ftp.listFiles(remote)) {
                    System.out.println(f.getRawListing());
                    System.out.println(f.toFormattedString(displayTimeZoneId));
                }
            }
        } else if (feat) {
            // boolean feature check
            if (remote != null) { // See if the command is present
                if (ftp.hasFeature(remote)) {
                    System.out.println("Has feature: " + remote);
                } else {
                    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        System.out.println("FEAT " + remote + " was not detected");
                    } else {
                        System.out.println("Command failed: " + ftp.getReplyString());
                    }
                }

                // Strings feature check
                String[] features = ftp.featureValues(remote);
                if (features != null) {
                    for (String f : features) {
                        System.out.println("FEAT " + remote + "=" + f + ".");
                    }
                } else {
                    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        System.out.println("FEAT " + remote + " is not present");
                    } else {
                        System.out.println("Command failed: " + ftp.getReplyString());
                    }
                }
            } else {
                if (ftp.features()) {
                    // Command listener has already printed the output
                } else {
                    System.out.println("Failed: " + ftp.getReplyString());
                }
            }
        } else if (doCommand != null) {
            if (ftp.doCommand(doCommand, remote)) {
                // Command listener has already printed the output
                // for(String s : com.xzr.practice.util.ftp.getReplyStrings()) {
                // System.out.println(s);
                // }
            } else {
                System.out.println("Failed: " + ftp.getReplyString());
            }
        } else {
            OutputStream output;

            output = new FileOutputStream(local);

            ftp.retrieveFile(remote, output);

            output.close();
        }

        ftp.noop(); // check that control connection is working OK

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        error = true;
        System.err.println("Server closed connection.");
        e.printStackTrace();
    } catch (IOException e) {
        error = true;
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    System.exit(error ? 1 : 0);
}

From source file:demo.FTP.FTPClientExample.java

public static void main(String[] args) throws UnknownHostException {
    boolean storeFile = false, binaryTransfer = false, error = false, listFiles = false, listNames = false,
            hidden = false;//from  w w  w . j  ava  2s .c  o m
    boolean localActive = false, useEpsvWithIPv4 = false, feat = false, printHash = false;
    boolean mlst = false, mlsd = false, mdtm = false, saveUnparseable = false;
    boolean lenient = false;
    long keepAliveTimeout = -1;
    int controlKeepAliveReplyTimeout = -1;
    int minParams = 5; // listings require 3 params
    String protocol = null; // SSL protocol
    String doCommand = null;
    String trustmgr = null;
    String proxyHost = null;
    int proxyPort = 80;
    String proxyUser = null;
    String proxyPassword = null;
    String username = null;
    String password = null;
    String encoding = null;
    String serverTimeZoneId = null;
    String displayTimeZoneId = null;
    String serverType = null;
    String defaultDateFormat = null;
    String recentDateFormat = null;

    int base = 0;
    for (base = 0; base < args.length; base++) {
        if (args[base].equals("-s")) {
            storeFile = true;
        } else if (args[base].equals("-a")) {
            localActive = true;
        } else if (args[base].equals("-A")) {
            username = "anonymous";
            password = System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName();
        } else if (args[base].equals("-b")) {
            binaryTransfer = true;
        } else if (args[base].equals("-c")) {
            doCommand = args[++base];
            minParams = 3;
        } else if (args[base].equals("-d")) {
            mlsd = true;
            minParams = 3;
        } else if (args[base].equals("-e")) {
            useEpsvWithIPv4 = true;
        } else if (args[base].equals("-E")) {
            encoding = args[++base];
        } else if (args[base].equals("-f")) {
            feat = true;
            minParams = 3;
        } else if (args[base].equals("-h")) {
            hidden = true;
        } else if (args[base].equals("-k")) {
            keepAliveTimeout = Long.parseLong(args[++base]);
        } else if (args[base].equals("-l")) {
            listFiles = true;
            minParams = 3;
        } else if (args[base].equals("-m")) {
            mdtm = true;
            minParams = 3;
        } else if (args[base].equals("-L")) {
            lenient = true;
        } else if (args[base].equals("-n")) {
            listNames = true;
            minParams = 3;
        } else if (args[base].equals("-p")) {
            protocol = args[++base];
            System.out.println("protocal:" + protocol);
        } else if (args[base].equals("-S")) {
            serverType = args[++base];
        } else if (args[base].equals("-t")) {
            mlst = true;
            minParams = 3;
        } else if (args[base].equals("-U")) {
            saveUnparseable = true;
        } else if (args[base].equals("-w")) {
            controlKeepAliveReplyTimeout = Integer.parseInt(args[++base]);
        } else if (args[base].equals("-T")) {
            trustmgr = args[++base];
        } else if (args[base].equals("-y")) {
            defaultDateFormat = args[++base];
        } else if (args[base].equals("-Y")) {
            recentDateFormat = args[++base];
        } else if (args[base].equals("-Z")) {
            serverTimeZoneId = args[++base];
        } else if (args[base].equals("-z")) {
            displayTimeZoneId = args[++base];
        } else if (args[base].equals("-PrH")) {
            proxyHost = args[++base];
            String parts[] = proxyHost.split(":");
            if (parts.length == 2) {
                proxyHost = parts[0];
                proxyPort = Integer.parseInt(parts[1]);
            }
        } else if (args[base].equals("-PrU")) {
            proxyUser = args[++base];
        } else if (args[base].equals("-PrP")) {
            proxyPassword = args[++base];
        } else if (args[base].equals("-#")) {
            printHash = true;
        } else {
            break;
        }
    }

    int remain = args.length - base;
    if (username != null) {
        minParams -= 2;
    }
    if (remain < minParams) // server, user, pass, remote, local [protocol]
    {
        if (args.length > 0) {
            System.err.println("Actual Parameters: " + Arrays.toString(args));
        }
        System.err.println(USAGE);
        System.exit(1);
    }

    String server = args[base++];
    int port = 0;
    String parts[] = server.split(":");
    if (parts.length == 2) {
        server = parts[0];
        port = Integer.parseInt(parts[1]);
        System.out.println("server:" + server);
        System.out.println("port:" + port);
    }
    if (username == null) {
        username = args[base++];
        password = args[base++];
        System.out.println("username:" + username);
        System.out.println("password:" + password);
    }

    String remote = null;
    if (args.length - base > 0) {
        remote = args[base++];
    }

    String local = null;
    if (args.length - base > 0) {
        local = args[base++];
    }

    final FTPClient ftp;
    if (protocol == null) {
        if (proxyHost != null) {
            System.out.println("Using HTTP proxy server: " + proxyHost);
            ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
        } else {
            ftp = new FTPClient();
        }
    } else {
        FTPSClient ftps;
        if (protocol.equals("true")) {
            ftps = new FTPSClient(true);
        } else if (protocol.equals("false")) {
            ftps = new FTPSClient(false);
        } else {
            String prot[] = protocol.split(",");
            if (prot.length == 1) { // Just protocol
                ftps = new FTPSClient(protocol);
            } else { // protocol,true|false
                ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1]));
            }
        }
        ftp = ftps;
        if ("all".equals(trustmgr)) {
            ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
        } else if ("valid".equals(trustmgr)) {
            ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
        } else if ("none".equals(trustmgr)) {
            ftps.setTrustManager(null);
        }
    }

    if (printHash) {
        ftp.setCopyStreamListener(createListener());
    }
    if (keepAliveTimeout >= 0) {
        ftp.setControlKeepAliveTimeout(keepAliveTimeout);
    }
    if (controlKeepAliveReplyTimeout >= 0) {
        ftp.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout);
    }
    if (encoding != null) {
        ftp.setControlEncoding(encoding);
    }
    ftp.setListHiddenFiles(hidden);

    // suppress login details
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

    final FTPClientConfig config;
    if (serverType != null) {
        config = new FTPClientConfig(serverType);
    } else {
        config = new FTPClientConfig();
    }
    config.setUnparseableEntries(saveUnparseable);
    if (defaultDateFormat != null) {
        config.setDefaultDateFormatStr(defaultDateFormat);
    }
    if (recentDateFormat != null) {
        config.setRecentDateFormatStr(recentDateFormat);
    }
    ftp.configure(config);

    try {
        int reply;
        if (port > 0) {
            ftp.connect(server, port);
        } else {
            ftp.connect(server);
        }
        System.out.println("Connected to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()));

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    __main: try {
        if (!ftp.login(username, password)) {
            ftp.logout();
            error = true;
            break __main;
        }

        System.out.println("Remote system is " + ftp.getSystemType());

        if (binaryTransfer) {
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        } else {
            // in theory this should not be necessary as servers should default to ASCII
            // but they don't all do so - see NET-500
            ftp.setFileType(FTP.ASCII_FILE_TYPE);
        }

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        if (localActive) {
            ftp.enterLocalActiveMode();
        } else {
            ftp.enterLocalPassiveMode();
        }

        ftp.setUseEPSVwithIPv4(useEpsvWithIPv4);

        if (storeFile) {
            InputStream input;

            input = new FileInputStream(local);

            ftp.storeFile(remote, input);

            input.close();
        }
        // Allow multiple list types for single invocation
        else if (listFiles || mlsd || mdtm || mlst || listNames) {
            if (mlsd) {
                for (FTPFile f : ftp.mlistDir(remote)) {
                    System.out.println(f.getRawListing());
                    System.out.println(f.toFormattedString(displayTimeZoneId));
                }
            }
            if (mdtm) {
                FTPFile f = ftp.mdtmFile(remote);
                System.out.println(f.getRawListing());
                System.out.println(f.toFormattedString(displayTimeZoneId));
            }
            if (mlst) {
                FTPFile f = ftp.mlistFile(remote);
                if (f != null) {
                    System.out.println(f.toFormattedString(displayTimeZoneId));
                }
            }
            if (listNames) {
                for (String s : ftp.listNames(remote)) {
                    System.out.println(s);
                }
            }
            // Do this last because it changes the client
            if (listFiles) {
                if (lenient || serverTimeZoneId != null) {
                    config.setLenientFutureDates(lenient);
                    if (serverTimeZoneId != null) {
                        config.setServerTimeZoneId(serverTimeZoneId);
                    }
                    ftp.configure(config);
                }

                for (FTPFile f : ftp.listFiles(remote)) {
                    System.out.println(f.getRawListing());
                    System.out.println(f.toFormattedString(displayTimeZoneId));
                }
            }
        } else if (feat) {
            // boolean feature check
            if (remote != null) { // See if the command is present
                if (ftp.hasFeature(remote)) {
                    System.out.println("Has feature: " + remote);
                } else {
                    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        System.out.println("FEAT " + remote + " was not detected");
                    } else {
                        System.out.println("Command failed: " + ftp.getReplyString());
                    }
                }

                // Strings feature check
                String[] features = ftp.featureValues(remote);
                if (features != null) {
                    for (String f : features) {
                        System.out.println("FEAT " + remote + "=" + f + ".");
                    }
                } else {
                    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        System.out.println("FEAT " + remote + " is not present");
                    } else {
                        System.out.println("Command failed: " + ftp.getReplyString());
                    }
                }
            } else {
                if (ftp.features()) {
                    //                        Command listener has already printed the output
                } else {
                    System.out.println("Failed: " + ftp.getReplyString());
                }
            }
        } else if (doCommand != null) {
            if (ftp.doCommand(doCommand, remote)) {
                //                  Command listener has already printed the output
                //                    for(String s : ftp.getReplyStrings()) {
                //                        System.out.println(s);
                //                    }
            } else {
                System.out.println("Failed: " + ftp.getReplyString());
            }
        } else {
            OutputStream output;

            output = new FileOutputStream(local);

            ftp.retrieveFile(remote, output);

            output.close();
        }

        ftp.noop(); // check that control connection is working OK

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        error = true;
        System.err.println("Server closed connection.");
        e.printStackTrace();
    } catch (IOException e) {
        error = true;
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    System.exit(error ? 1 : 0);
}

From source file:com.kactech.otj.examples.App_otj.java

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    String command = null;/*from  ww  w  . j av a 2s. c  o  m*/
    String hisacct = null;
    String hisacctName = null;
    String hisacctAsset = null;
    String asset = null;
    String assetName = null;
    List<String> argList = null;
    boolean newAccount = false;
    File dir = null;
    ConnectionInfo connection = null;
    List<ScriptFilter> filters = null;

    CommandLineParser parser = new GnuParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        System.err.println("Command-line parsing error: " + e.getMessage());
        help();
        System.exit(-1);
    }
    if (cmd.hasOption('h')) {
        help();
        System.exit(0);
    }
    @SuppressWarnings("unchecked")
    List<String> list = cmd.getArgList();
    if (list.size() > 1) {
        System.err.println("only one command is supported, you've typed " + list);
        help();
        System.exit(-1);
    }
    if (list.size() > 0)
        command = list.get(0).trim();

    List<SampleAccount> accounts = ExamplesUtils.getSampleAccounts();

    if (cmd.hasOption('s')) {
        String v = cmd.getOptionValue('s').trim();
        connection = ExamplesUtils.findServer(v);
        if (connection == null) {
            System.err.println("unknown server: " + v);
            System.exit(-1);
        }
    } else {
        connection = ExamplesUtils.findServer(DEF_SERVER_NAME);
        if (connection == null) {
            System.err.println("default server not found server: " + DEF_SERVER_NAME);
            System.exit(-1);
        }
    }

    if (cmd.hasOption('t')) {
        String v = cmd.getOptionValue('t');
        for (SampleAccount ac : accounts)
            if (ac.accountName.startsWith(v)) {
                hisacct = ac.accountID;
                hisacctName = ac.accountName;
                hisacctAsset = ac.assetID;
                break;
            }
        if (hisacct == null)
            if (mayBeValid(v))
                hisacct = v;
            else {
                System.err.println("invalid hisacct: " + v);
                System.exit(-1);
            }
    }
    if (cmd.hasOption('p')) {
        String v = cmd.getOptionValue('p');
        for (SampleAccount ac : accounts)
            if (ac.assetName.startsWith(v)) {
                asset = ac.assetID;
                assetName = ac.assetName;
                break;
            }
        if (asset == null)
            if (mayBeValid(v))
                asset = v;
            else {
                System.err.println("invalid asset: " + v);
                System.exit(-1);
            }
    }

    if (cmd.hasOption('a')) {
        String v = cmd.getOptionValue('a');
        argList = new ArrayList<String>();
        boolean q = false;
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < v.length(); i++) {
            char c = v.charAt(i);
            if (c == '"') {
                if (q) {
                    argList.add(b.toString());
                    b = null;
                    q = false;
                    continue;
                }
                if (b != null)
                    argList.add(b.toString());
                b = new StringBuilder();
                q = true;
                continue;
            }
            if (c == ' ' || c == '\t') {
                if (q) {
                    b.append(c);
                    continue;
                }
                if (b != null)
                    argList.add(b.toString());
                b = null;
                continue;
            }
            if (b == null)
                b = new StringBuilder();
            b.append(c);
        }
        if (b != null)
            argList.add(b.toString());
        if (q) {
            System.err.println("unclosed quote in args: " + v);
            System.exit(-1);
        }
    }

    dir = new File(cmd.hasOption('d') ? cmd.getOptionValue('d') : DEF_CLIENT_DIR);

    if (cmd.hasOption('x'))
        del(dir);

    newAccount = cmd.hasOption('n');

    if (cmd.hasOption('f')) {
        filters = new ArrayList<ScriptFilter>();
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        Compilable compilingEngine = (Compilable) engine;
        for (String fn : cmd.getOptionValue('f').split(",")) {
            fn = fn.trim();
            if (fn.isEmpty())
                continue;
            fn += ".js";
            Reader r = null;
            try {
                r = new InputStreamReader(new FileInputStream(new File("filters", fn)), Utils.UTF8);
            } catch (Exception e) {
                try {
                    r = new InputStreamReader(
                            App_otj.class.getResourceAsStream("/com/kactech/otj/examples/filters/" + fn));
                } catch (Exception e2) {
                }
            }
            if (r == null) {
                System.err.println("filter not found: " + fn);
                System.exit(-1);
            } else
                try {
                    CompiledScript compiled = compilingEngine.compile(r);
                    ScriptFilter sf = new ScriptFilter(compiled);
                    filters.add(sf);
                } catch (Exception ex) {
                    System.err.println("error while loading " + fn + ": " + ex);
                    System.exit(-1);
                }
        }
    }

    System.out.println("server: " + connection.getEndpoint() + " " + connection.getID());
    System.out.println("command: '" + command + "'");
    System.out.println("args: " + argList);
    System.out.println("hisacct: " + hisacct);
    System.out.println("hisacctName: " + hisacctName);
    System.out.println("hisacctAsset: " + hisacctAsset);
    System.out.println("asset: " + asset);
    System.out.println("assetName: " + assetName);

    if (asset != null && hisacctAsset != null && !asset.equals(hisacctAsset)) {
        System.err.println("asset differs from hisacctAsset");
        System.exit(-1);
    }

    EClient client = new EClient(dir, connection);
    client.setAssetType(asset != null ? asset : hisacctAsset);
    client.setCreateNewAccount(newAccount);
    if (filters != null)
        client.setFilters(filters);

    try {
        Utils.init();
        Client.DEBUG_JSON = true;
        client.init();

        if ("balance".equals(command))
            System.out.println("Balance: " + client.getAccount().getBalance().getAmount());
        else if ("acceptall".equals(command))
            client.processInbox();
        else if ("transfer".equals(command)) {
            if (hisacct == null)
                System.err.println("please specify --hisacct");
            else {
                int idx = argList != null ? argList.indexOf("amount") : -1;
                if (idx < 0)
                    System.err.println("please specify amount");
                else if (idx == argList.size())
                    System.err.println("amount argument needs value");
                else {
                    Long amount = -1l;
                    try {
                        amount = new Long(argList.get(idx + 1));
                    } catch (Exception e) {

                    }
                    if (amount <= 0)
                        System.err.println("invalid amount");
                    else {
                        client.notarizeTransaction(hisacct, amount);
                    }
                }
            }
        } else if ("reload".equals(command))
            client.reloadState();
        else if ("procnym".equals(command))
            client.processNymbox();
    } finally {
        client.saveState();
        client.close();
    }
}

From source file:com.mgreau.jboss.as7.cli.CliLauncher.java

public static void main(String[] args) throws Exception {
    int exitCode = 0;
    CommandContext cmdCtx = null;/* w w w  .  jav a2s .co m*/
    boolean gui = false;
    String appName = "";
    try {
        String argError = null;
        List<String> commands = null;
        File file = null;
        boolean connect = false;
        String defaultControllerProtocol = "http-remoting";
        String defaultControllerHost = null;
        int defaultControllerPort = -1;
        boolean version = false;
        String username = null;
        char[] password = null;
        int connectionTimeout = -1;

        //App deployment
        boolean isAppDeployment = false;
        final Properties props = new Properties();

        for (String arg : args) {
            if (arg.startsWith("--controller=") || arg.startsWith("controller=")) {
                final String fullValue;
                final String value;
                if (arg.startsWith("--")) {
                    fullValue = arg.substring(13);
                } else {
                    fullValue = arg.substring(11);
                }
                final int protocolEnd = fullValue.lastIndexOf("://");
                if (protocolEnd == -1) {
                    value = fullValue;
                } else {
                    value = fullValue.substring(protocolEnd + 3);
                    defaultControllerProtocol = fullValue.substring(0, protocolEnd);
                }

                String portStr = null;
                int colonIndex = value.lastIndexOf(':');
                if (colonIndex < 0) {
                    // default port
                    defaultControllerHost = value;
                } else if (colonIndex == 0) {
                    // default host
                    portStr = value.substring(1);
                } else {
                    final boolean hasPort;
                    int closeBracket = value.lastIndexOf(']');
                    if (closeBracket != -1) {
                        //possible ip v6
                        if (closeBracket > colonIndex) {
                            hasPort = false;
                        } else {
                            hasPort = true;
                        }
                    } else {
                        //probably ip v4
                        hasPort = true;
                    }
                    if (hasPort) {
                        defaultControllerHost = value.substring(0, colonIndex).trim();
                        portStr = value.substring(colonIndex + 1).trim();
                    } else {
                        defaultControllerHost = value;
                    }
                }

                if (portStr != null) {
                    int port = -1;
                    try {
                        port = Integer.parseInt(portStr);
                        if (port < 0) {
                            argError = "The port must be a valid non-negative integer: '" + args + "'";
                        } else {
                            defaultControllerPort = port;
                        }
                    } catch (NumberFormatException e) {
                        argError = "The port must be a valid non-negative integer: '" + arg + "'";
                    }
                }
            } else if ("--connect".equals(arg) || "-c".equals(arg)) {
                connect = true;
            } else if ("--version".equals(arg)) {
                version = true;
            } else if ("--gui".equals(arg)) {
                gui = true;
            } else if (arg.startsWith("--appDeployment=") || arg.startsWith("appDeployment=")) {
                isAppDeployment = true;
                appName = arg.startsWith("--") ? arg.substring(16) : arg.substring(14);
            } else if (arg.startsWith("--file=") || arg.startsWith("file=")) {
                if (file != null) {
                    argError = "Duplicate argument '--file'.";
                    break;
                }
                if (commands != null) {
                    argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time.";
                    break;
                }

                final String fileName = arg.startsWith("--") ? arg.substring(7) : arg.substring(5);
                if (!fileName.isEmpty()) {
                    file = new File(fileName);
                    if (!file.exists()) {
                        argError = "File " + file.getAbsolutePath() + " doesn't exist.";
                        break;
                    }
                } else {
                    argError = "Argument '--file' is missing value.";
                    break;
                }
            } else if (arg.startsWith("--commands=") || arg.startsWith("commands=")) {
                if (file != null) {
                    argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time.";
                    break;
                }
                if (commands != null) {
                    argError = "Duplicate argument '--command'/'--commands'.";
                    break;
                }
                final String value = arg.startsWith("--") ? arg.substring(11) : arg.substring(9);
                commands = Util.splitCommands(value);
            } else if (arg.startsWith("--command=") || arg.startsWith("command=")) {
                if (file != null) {
                    argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time.";
                    break;
                }
                if (commands != null) {
                    argError = "Duplicate argument '--command'/'--commands'.";
                    break;
                }
                final String value = arg.startsWith("--") ? arg.substring(10) : arg.substring(8);
                commands = Collections.singletonList(value);
            } else if (arg.startsWith("--user=")) {
                username = arg.startsWith("--") ? arg.substring(7) : arg.substring(5);
            } else if (arg.startsWith("--password=")) {
                password = (arg.startsWith("--") ? arg.substring(11) : arg.substring(9)).toCharArray();
            } else if (arg.startsWith("--timeout=")) {
                if (connectionTimeout > 0) {
                    argError = "Duplicate argument '--timeout'";
                    break;
                }
                final String value = arg.substring(10);
                try {
                    connectionTimeout = Integer.parseInt(value);
                } catch (final NumberFormatException e) {
                    //
                }
                if (connectionTimeout <= 0) {
                    argError = "The timeout must be a valid positive integer: '" + value + "'";
                }
            } else if (arg.equals("--help") || arg.equals("-h")) {
                commands = Collections.singletonList("help");
            } else if (arg.startsWith("--properties=")) {
                final String value = arg.substring(13);
                final File propertiesFile = new File(value);
                if (!propertiesFile.exists()) {
                    argError = "File doesn't exist: " + propertiesFile.getAbsolutePath();
                    break;
                }

                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(propertiesFile);
                    props.load(fis);
                } catch (FileNotFoundException e) {
                    argError = e.getLocalizedMessage();
                    break;
                } catch (java.io.IOException e) {
                    argError = "Failed to load properties from " + propertiesFile.getAbsolutePath() + ": "
                            + e.getLocalizedMessage();
                    break;
                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (java.io.IOException e) {
                        }
                    }
                }
                for (final Object prop : props.keySet()) {
                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            System.setProperty((String) prop, (String) props.get(prop));
                            return null;
                        }
                    });
                }
            } else if (!(arg.startsWith("-D") || arg.equals("-XX:"))) {// skip system properties and jvm options
                // assume it's commands
                if (file != null) {
                    argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time: "
                            + arg;
                    break;
                }
                if (commands != null) {
                    argError = "Duplicate argument '--command'/'--commands'.";
                    break;
                }
                commands = Util.splitCommands(arg);
            }
        }

        if (argError != null) {
            System.err.println(argError);
            exitCode = 1;
            return;
        }

        if (version) {
            cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort,
                    username, password, false, connect, connectionTimeout);
            VersionHandler.INSTANCE.handle(cmdCtx);
            return;
        }

        if (file != null) {
            cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort,
                    username, password, false, connect, connectionTimeout);
            processFile(file, cmdCtx, isAppDeployment, appName, props);
            return;
        }

        if (commands != null) {
            cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort,
                    username, password, false, connect, connectionTimeout);
            processCommands(commands, cmdCtx);
            return;
        }

        // Interactive mode
        cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort,
                username, password, true, connect, connectionTimeout);
        cmdCtx.interact();
    } catch (Throwable t) {
        t.printStackTrace();
        exitCode = 1;
    } finally {
        if (cmdCtx != null && cmdCtx.getExitCode() != 0) {
            exitCode = cmdCtx.getExitCode();
        }
        if (!gui) {
            System.exit(exitCode);
        }
    }
    System.exit(exitCode);
}

From source file:com.l2jfree.loginserver.tools.L2GameServerRegistrar.java

/**
 * Launches the interactive game server registration.
 * /*from   w  w  w  .  ja v a  2s  . c  o  m*/
 * @param args ignored
 */
public static void main(String[] args) {
    // LOW rework this crap
    Util.printSection("Game Server Registration");
    _log.info("Please choose:");
    _log.info("list - list registered game servers");
    _log.info("reg - register a game server");
    _log.info("rem - remove a registered game server");
    _log.info("hexid - generate a legacy hexid file");
    _log.info("quit - exit this application");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    L2GameServerRegistrar reg = new L2GameServerRegistrar();

    String line;
    try {
        RegistrationState next = RegistrationState.INITIAL_CHOICE;
        while ((line = br.readLine()) != null) {
            line = line.trim().toLowerCase();
            switch (reg.getState()) {
            case GAMESERVER_ID:
                try {
                    int id = Integer.parseInt(line);
                    if (id < 1 || id > 127)
                        throw new IllegalArgumentException("ID must be in [1;127].");
                    reg.setId(id);
                    reg.setState(next);
                } catch (RuntimeException e) {
                    _log.info("You must input a number between 1 and 127");
                }

                if (reg.getState() == RegistrationState.ALLOW_BANS) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con
                                .prepareStatement("SELECT allowBans FROM gameserver WHERE id = ?");
                        ps.setInt(1, reg.getId());
                        ResultSet rs = ps.executeQuery();
                        if (rs.next()) {
                            _log.info("A game server is already registered on ID " + reg.getId());
                            reg.setState(RegistrationState.INITIAL_CHOICE);
                        } else
                            _log.info("Allow account bans from this game server? [y/n]:");
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not remove a game server!", e);
                    } finally {
                        L2Database.close(con);
                    }
                } else if (reg.getState() == RegistrationState.REMOVE) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con.prepareStatement("DELETE FROM gameserver WHERE id = ?");
                        ps.setInt(1, reg.getId());
                        int cnt = ps.executeUpdate();
                        if (cnt == 0)
                            _log.info("No game server registered on ID " + reg.getId());
                        else
                            _log.info("Game server removed.");
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not remove a game server!", e);
                    } finally {
                        L2Database.close(con);
                    }
                    reg.setState(RegistrationState.INITIAL_CHOICE);
                } else if (reg.getState() == RegistrationState.GENERATE) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con
                                .prepareStatement("SELECT authData FROM gameserver WHERE id = ?");
                        ps.setInt(1, reg.getId());
                        ResultSet rs = ps.executeQuery();

                        if (rs.next()) {
                            reg.setAuth(rs.getString("authData"));
                            byte[] b = HexUtil.hexStringToBytes(reg.getAuth());

                            Properties pro = new Properties();
                            pro.setProperty("ServerID", String.valueOf(reg.getId()));
                            pro.setProperty("HexID", HexUtil.hexToString(b));

                            BufferedOutputStream os = new BufferedOutputStream(
                                    new FileOutputStream("hexid.txt"));
                            pro.store(os, "the hexID to auth into login");
                            IOUtils.closeQuietly(os);
                            _log.info("hexid.txt has been generated.");
                        } else
                            _log.info("No game server registered on ID " + reg.getId());

                        rs.close();
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not generate hexid.txt!", e);
                    } finally {
                        L2Database.close(con);
                    }
                    reg.setState(RegistrationState.INITIAL_CHOICE);
                }
                break;
            case ALLOW_BANS:
                try {
                    if (line.length() != 1)
                        throw new IllegalArgumentException("One char required.");
                    else if (line.charAt(0) == 'y')
                        reg.setTrusted(true);
                    else if (line.charAt(0) == 'n')
                        reg.setTrusted(false);
                    else
                        throw new IllegalArgumentException("Invalid choice.");

                    byte[] auth = Rnd.nextBytes(new byte[BYTES]);
                    reg.setAuth(HexUtil.bytesToHexString(auth));

                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con.prepareStatement(
                                "INSERT INTO gameserver (id, authData, allowBans) VALUES (?, ?, ?)");
                        ps.setInt(1, reg.getId());
                        ps.setString(2, reg.getAuth());
                        ps.setBoolean(3, reg.isTrusted());
                        ps.executeUpdate();
                        ps.close();

                        _log.info("Registered game server on ID " + reg.getId());
                        _log.info("The authorization string is:");
                        _log.info(reg.getAuth());
                        _log.info("Use it when registering this login server.");
                        _log.info("If you need a legacy hexid file, use the 'hexid' command.");
                    } catch (SQLException e) {
                        _log.error("Could not register gameserver!", e);
                    } finally {
                        L2Database.close(con);
                    }

                    reg.setState(RegistrationState.INITIAL_CHOICE);
                } catch (IllegalArgumentException e) {
                    _log.info("[y/n]?");
                }
                break;
            default:
                if (line.equals("list")) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con.prepareStatement("SELECT id, allowBans FROM gameserver");
                        ResultSet rs = ps.executeQuery();
                        while (rs.next())
                            _log.info("ID: " + rs.getInt("id") + ", trusted: " + rs.getBoolean("allowBans"));
                        rs.close();
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not register gameserver!", e);
                    } finally {
                        L2Database.close(con);
                    }
                    reg.setState(RegistrationState.INITIAL_CHOICE);
                } else if (line.equals("reg")) {
                    _log.info("Enter the desired ID:");
                    reg.setState(RegistrationState.GAMESERVER_ID);
                    next = RegistrationState.ALLOW_BANS;
                } else if (line.equals("rem")) {
                    _log.info("Enter game server ID:");
                    reg.setState(RegistrationState.GAMESERVER_ID);
                    next = RegistrationState.REMOVE;
                } else if (line.equals("hexid")) {
                    _log.info("Enter game server ID:");
                    reg.setState(RegistrationState.GAMESERVER_ID);
                    next = RegistrationState.GENERATE;
                } else if (line.equals("quit"))
                    Shutdown.exit(TerminationStatus.MANUAL_SHUTDOWN);
                else
                    _log.info("Incorrect command.");
                break;
            }
        }
    } catch (IOException e) {
        _log.fatal("Could not process input!", e);
    } finally {
        IOUtils.closeQuietly(br);
    }
}

From source file:ms1quant.MS1Quant.java

/**
 * @param args the command line arguments MS1Quant parameterfile
 *//*from www  .  j  av  a 2  s  . c  om*/
public static void main(String[] args) throws Exception {

    BufferedReader reader = null;
    try {
        System.out.println(
                "=================================================================================================");
        System.out.println("Umpire MS1 quantification and feature detection analysis (version: "
                + UmpireInfo.GetInstance().Version + ")");
        if (args.length < 3 || !args[1].startsWith("-mode")) {
            System.out
                    .println("command : java -jar -Xmx10G MS1Quant.jar ms1quant.params -mode[1 or 2] [Option]");
            System.out.println("\n-mode");
            System.out.println("\t1:Single file mode--> mzXML_file PepXML_file");
            System.out.println("\t\tEx: -mode1 file1.mzXML file1.pep.xml");
            System.out.println(
                    "\t2:Folder mode--> mzXML_Folder PepXML_Folder, all generated csv tables will be merged into a single csv file");
            System.out.println("\t\tEx: -mode2 /data/mzxml/ /data/pepxml/");
            System.out.println("\nOptions");
            System.out.println(
                    "\t-C\tNo of concurrent files to be processed (only for folder mode), Ex. -C5, default:1");
            System.out.println("\t-p\tMinimum probability, Ex. -p0.9, default:0.9");
            System.out.println("\t-ID\tDetect identified feature only");
            System.out.println("\t-O\toutput folder, Ex. -O/data/");
            return;
        }
        ConsoleLogger consoleLogger = new ConsoleLogger();
        consoleLogger.SetConsoleLogger(Level.DEBUG);
        consoleLogger.SetFileLogger(Level.DEBUG, FilenameUtils.getFullPath(args[0]) + "ms1quant_debug.log");
        Logger logger = Logger.getRootLogger();
        logger.debug("Command: " + Arrays.toString(args));
        logger.info("MS1Quant version: " + UmpireInfo.GetInstance().Version);

        String parameterfile = args[0];
        logger.info("Parameter file: " + parameterfile);
        File paramfile = new File(parameterfile);
        if (!paramfile.exists()) {
            logger.error("Parameter file " + paramfile.getAbsolutePath()
                    + " cannot be found. The program will exit.");
        }

        reader = new BufferedReader(new FileReader(paramfile.getAbsolutePath()));
        String line = "";
        InstrumentParameter param = new InstrumentParameter(InstrumentParameter.InstrumentType.TOF5600);
        int NoCPUs = 2;
        int NoFile = 1;
        param.DetermineBGByID = false;
        param.EstimateBG = true;

        //<editor-fold defaultstate="collapsed" desc="Read parameter file">
        while ((line = reader.readLine()) != null) {
            if (!"".equals(line) && !line.startsWith("#")) {
                logger.info(line);
                //System.out.println(line);
                if (line.split("=").length < 2) {
                    continue;
                }
                if (line.split("=").length < 2) {
                    continue;
                }
                String type = line.split("=")[0].trim();
                if (type.startsWith("para.")) {
                    type = type.replace("para.", "SE.");
                }
                String value = line.split("=")[1].trim();
                switch (type) {
                case "Thread": {
                    NoCPUs = Integer.parseInt(value);
                    break;
                }
                //<editor-fold defaultstate="collapsed" desc="instrument parameters">

                case "SE.MS1PPM": {
                    param.MS1PPM = Float.parseFloat(value);
                    break;
                }
                case "SE.MS2PPM": {
                    param.MS2PPM = Float.parseFloat(value);
                    break;
                }
                case "SE.SN": {
                    param.SNThreshold = Float.parseFloat(value);
                    break;
                }
                case "SE.MS2SN": {
                    param.MS2SNThreshold = Float.parseFloat(value);
                    break;
                }
                case "SE.MinMSIntensity": {
                    param.MinMSIntensity = Float.parseFloat(value);
                    break;
                }
                case "SE.MinMSMSIntensity": {
                    param.MinMSMSIntensity = Float.parseFloat(value);
                    break;
                }
                case "SE.MinRTRange": {
                    param.MinRTRange = Float.parseFloat(value);
                    break;
                }
                case "SE.MaxNoPeakCluster": {
                    param.MaxNoPeakCluster = Integer.parseInt(value);
                    param.MaxMS2NoPeakCluster = Integer.parseInt(value);
                    break;
                }
                case "SE.MinNoPeakCluster": {
                    param.MinNoPeakCluster = Integer.parseInt(value);
                    param.MinMS2NoPeakCluster = Integer.parseInt(value);
                    break;
                }
                case "SE.MinMS2NoPeakCluster": {
                    param.MinMS2NoPeakCluster = Integer.parseInt(value);
                    break;
                }
                case "SE.MaxCurveRTRange": {
                    param.MaxCurveRTRange = Float.parseFloat(value);
                    break;
                }
                case "SE.Resolution": {
                    param.Resolution = Integer.parseInt(value);
                    break;
                }
                case "SE.RTtol": {
                    param.RTtol = Float.parseFloat(value);
                    break;
                }
                case "SE.NoPeakPerMin": {
                    param.NoPeakPerMin = Integer.parseInt(value);
                    break;
                }
                case "SE.StartCharge": {
                    param.StartCharge = Integer.parseInt(value);
                    break;
                }
                case "SE.EndCharge": {
                    param.EndCharge = Integer.parseInt(value);
                    break;
                }
                case "SE.MS2StartCharge": {
                    param.MS2StartCharge = Integer.parseInt(value);
                    break;
                }
                case "SE.MS2EndCharge": {
                    param.MS2EndCharge = Integer.parseInt(value);
                    break;
                }
                case "SE.NoMissedScan": {
                    param.NoMissedScan = Integer.parseInt(value);
                    break;
                }
                case "SE.Denoise": {
                    param.Denoise = Boolean.valueOf(value);
                    break;
                }
                case "SE.EstimateBG": {
                    param.EstimateBG = Boolean.valueOf(value);
                    break;
                }
                case "SE.RemoveGroupedPeaks": {
                    param.RemoveGroupedPeaks = Boolean.valueOf(value);
                    break;
                }
                case "SE.MinFrag": {
                    param.MinFrag = Integer.parseInt(value);
                    break;
                }
                case "SE.IsoPattern": {
                    param.IsoPattern = Float.valueOf(value);
                    break;
                }
                case "SE.StartRT": {
                    param.startRT = Float.valueOf(value);
                }
                case "SE.EndRT": {
                    param.endRT = Float.valueOf(value);
                }

                //</editor-fold>
                }
            }
        }
        //</editor-fold>

        int mode = 1;
        if (args[1].equals("-mode2")) {
            mode = 2;
        } else if (args[1].equals("-mode1")) {
            mode = 1;
        } else {
            logger.error("-mode number not recongized. The program will exit.");
        }

        String mzXML = "";
        String pepXML = "";
        String mzXMLPath = "";
        String pepXMLPath = "";
        File mzXMLfile = null;
        File pepXMLfile = null;
        File mzXMLfolder = null;
        File pepXMLfolder = null;
        int idx = 0;
        if (mode == 1) {
            mzXML = args[2];
            logger.info("Mode1 mzXML file: " + mzXML);
            mzXMLfile = new File(mzXML);
            if (!mzXMLfile.exists()) {
                logger.error("Mode1 mzXML file " + mzXMLfile.getAbsolutePath()
                        + " cannot be found. The program will exit.");
                return;
            }
            pepXML = args[3];
            logger.info("Mode1 pepXML file: " + pepXML);
            pepXMLfile = new File(pepXML);
            if (!pepXMLfile.exists()) {
                logger.error("Mode1 pepXML file " + pepXMLfile.getAbsolutePath()
                        + " cannot be found. The program will exit.");
                return;
            }
            idx = 4;
        } else if (mode == 2) {
            mzXMLPath = args[2];
            logger.info("Mode2 mzXML folder: " + mzXMLPath);
            mzXMLfolder = new File(mzXMLPath);
            if (!mzXMLfolder.exists()) {
                logger.error("Mode2 mzXML folder " + mzXMLfolder.getAbsolutePath()
                        + " does not exist. The program will exit.");
                return;
            }
            pepXMLPath = args[3];
            logger.info("Mode2 pepXML folder: " + pepXMLPath);
            pepXMLfolder = new File(pepXMLPath);
            if (!pepXMLfolder.exists()) {
                logger.error("Mode2 pepXML folder " + pepXMLfolder.getAbsolutePath()
                        + " does not exist. The program will exit.");
                return;
            }
            idx = 4;
        }

        String outputfolder = "";
        float MinProb = 0f;
        for (int i = idx; i < args.length; i++) {
            if (args[i].startsWith("-")) {
                if (args[i].equals("-ID")) {
                    param.TargetIDOnly = true;
                    logger.info("Detect ID feature only: true");
                }
                if (args[i].startsWith("-O")) {
                    outputfolder = args[i].substring(2);
                    logger.info("Output folder: " + outputfolder);

                    File outputfile = new File(outputfolder);
                    if (!outputfolder.endsWith("\\") | outputfolder.endsWith("/")) {
                        outputfolder += "/";
                    }
                    if (!outputfile.exists()) {
                        outputfile.mkdir();
                    }
                }
                if (args[i].startsWith("-C")) {
                    try {
                        NoFile = Integer.parseInt(args[i].substring(2));
                        logger.info("No of concurrent files: " + NoFile);
                    } catch (Exception ex) {
                        logger.error(args[i]
                                + " is not a correct integer format, will process only one file at a time.");
                    }
                }
                if (args[i].startsWith("-p")) {
                    try {
                        MinProb = Float.parseFloat(args[i].substring(2));
                        logger.info("probability threshold: " + MinProb);
                    } catch (Exception ex) {
                        logger.error(args[i] + " is not a correct format, will use 0 as threshold instead.");
                    }
                }
            }
        }

        reader.close();
        TandemParam tandemparam = new TandemParam(DBSearchParam.SearchInstrumentType.TOF5600);
        PTMManager.GetInstance();

        if (param.TargetIDOnly) {
            param.EstimateBG = false;
            param.ApexDelta = 1.5f;
            param.NoMissedScan = 10;
            param.MiniOverlapP = 0.2f;
            param.RemoveGroupedPeaks = false;
            param.CheckMonoIsotopicApex = false;
            param.DetectByCWT = false;
            param.FillGapByBK = false;
            param.IsoCorrThreshold = -1f;
            param.SmoothFactor = 3;
        }

        if (mode == 1) {
            logger.info("Processing " + mzXMLfile.getAbsolutePath() + "....");
            long time = System.currentTimeMillis();
            LCMSPeakMS1 LCMS1 = new LCMSPeakMS1(mzXMLfile.getAbsolutePath(), NoCPUs);
            LCMS1.SetParameter(param);

            LCMS1.Resume = false;
            if (!param.TargetIDOnly) {
                LCMS1.CreatePeakFolder();
            }
            LCMS1.ExportPeakClusterTable = true;

            if (pepXMLfile.exists()) {
                tandemparam.InteractPepXMLPath = pepXMLfile.getAbsolutePath();
                LCMS1.ParsePepXML(tandemparam, MinProb);
                logger.info("No. of PSMs included: " + LCMS1.IDsummary.PSMList.size());
                logger.info("No. of Peptide ions included: " + LCMS1.IDsummary.GetPepIonList().size());
            }

            if (param.TargetIDOnly) {
                LCMS1.SaveSerializationFile = false;
            }

            if (param.TargetIDOnly || !LCMS1.ReadPeakCluster()) {
                LCMS1.PeakClusterDetection();
            }

            if (pepXMLfile.exists()) {
                LCMS1.AssignQuant(false);
                LCMS1.IDsummary.ExportPepID(outputfolder);
            }
            time = System.currentTimeMillis() - time;
            logger.info(LCMS1.ParentmzXMLName + " processed time:"
                    + String.format("%d hour, %d min, %d sec", TimeUnit.MILLISECONDS.toHours(time),
                            TimeUnit.MILLISECONDS.toMinutes(time)
                                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(time)),
                            TimeUnit.MILLISECONDS.toSeconds(time)
                                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time))));
            LCMS1.BaseClearAllPeaks();
            LCMS1.SetSpectrumParser(null);
            LCMS1.IDsummary = null;
            LCMS1 = null;
            System.gc();
        } else if (mode == 2) {

            LCMSID IDsummary = new LCMSID("", "", "");
            logger.info("Parsing all pepXML files in " + pepXMLPath + "....");
            for (File file : pepXMLfolder.listFiles()) {
                if (file.getName().toLowerCase().endsWith("pep.xml")
                        || file.getName().toLowerCase().endsWith("pepxml")) {
                    PepXMLParser pepXMLParser = new PepXMLParser(IDsummary, file.getAbsolutePath(), MinProb);
                }
            }
            HashMap<String, LCMSID> LCMSIDMap = IDsummary.GetLCMSIDFileMap();

            ExecutorService executorPool = null;
            executorPool = Executors.newFixedThreadPool(NoFile);

            logger.info("Processing all mzXML files in " + mzXMLPath + "....");
            for (File file : mzXMLfolder.listFiles()) {
                if (file.getName().toLowerCase().endsWith("mzxml")) {
                    LCMSID id = LCMSIDMap.get(FilenameUtils.getBaseName(file.getName()));
                    if (id == null || id.PSMList == null) {
                        logger.warn("No IDs found in :" + FilenameUtils.getBaseName(file.getName())
                                + ". Quantification for this file is skipped");
                        continue;
                    }
                    if (!id.PSMList.isEmpty()) {
                        MS1TargetQuantThread thread = new MS1TargetQuantThread(file, id, NoCPUs, outputfolder,
                                param);
                        executorPool.execute(thread);
                    }
                }
            }
            LCMSIDMap.clear();
            LCMSIDMap = null;
            IDsummary = null;
            executorPool.shutdown();
            try {
                executorPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
            } catch (InterruptedException e) {
                logger.info("interrupted..");
            }

            if (outputfolder == null | outputfolder.equals("")) {
                outputfolder = mzXMLPath;
            }

            logger.info("Merging PSM files..");
            File output = new File(outputfolder);
            FileWriter writer = new FileWriter(output.getAbsolutePath() + "/PSM_merge.csv");
            boolean header = false;
            for (File csvfile : output.listFiles()) {
                if (csvfile.getName().toLowerCase().endsWith("_psms.csv")) {
                    BufferedReader outreader = new BufferedReader(new FileReader(csvfile));
                    String outline = outreader.readLine();
                    if (!header) {
                        writer.write(outline + "\n");
                        header = true;
                    }
                    while ((outline = outreader.readLine()) != null) {
                        writer.write(outline + "\n");
                    }
                    outreader.close();
                    csvfile.delete();
                }
            }
            writer.close();
        }
        logger.info("MS1 quant module is complete.");
    } catch (Exception e) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(e));
        throw e;
    }
}

From source file:com.samsung.sjs.Compiler.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException, SolverException, InterruptedException {
    boolean debug = false;
    boolean use_gc = true;
    CompilerOptions.Platform p = CompilerOptions.Platform.Native;
    CompilerOptions opts = null;//from  w  w  w .  j  a  v  a  2s .  com
    boolean field_opts = false;
    boolean typecheckonly = false;
    boolean showconstraints = false;
    boolean showconstraintsolution = false;
    String[] decls = null;
    String[] links = null;
    String[] objs = null;
    boolean guest = false;
    boolean stop_at_c = false;
    String external_compiler = null;
    boolean encode_vals = false;
    boolean x32 = false;
    boolean validate = true;
    boolean interop = false;
    boolean boot_interop = false;
    boolean oldExpl = false;
    String explanationStrategy = null;
    boolean efl = false;

    Options options = new Options();
    options.addOption("debugcompiler", false, "Enable compiler debug spew");
    //options.addOption("o", true, "Set compiler output file (must be .c)");
    options.addOption(OptionBuilder //.withArgName("o")
            .withLongOpt("output-file").withDescription("Output file (must be .c)").hasArg().withArgName("file")
            .create("o"));
    options.addOption(OptionBuilder.withLongOpt("target")
            .withDescription("Select target platform: 'native' or 'web'").hasArg().create());
    options.addOption(OptionBuilder.withLongOpt("gc")
            .withDescription("Disable GC: param is 'on' (default) or 'off'").hasArg().create());

    options.addOption(OptionBuilder
            .withDescription("Enable field access optimizations: param is 'true' (default) or 'false'").hasArg()
            .create("Xfields"));

    options.addOption(OptionBuilder
            .withDescription("Compile for encoded values.  TEMPORARY.  For testing interop codegen").hasArg()
            .create("XEncodedValues"));

    options.addOption(OptionBuilder.withLongOpt("typecheck-only")
            .withDescription("Only typecheck the file, don't compile").create());
    options.addOption(OptionBuilder.withLongOpt("show-constraints")
            .withDescription("Show constraints generated during type inference").create());
    options.addOption(OptionBuilder.withLongOpt("show-constraint-solution")
            .withDescription("Show solution to type inference constraints").create());
    options.addOption(OptionBuilder.withLongOpt("extra-decls")
            .withDescription("Specify extra declaration files, comma-separated").hasArg()
            .withValueSeparator(',').create());
    options.addOption(OptionBuilder.withLongOpt("native-libs")
            .withDescription("Specify extra linkage files, comma-separated").hasArg().withValueSeparator(',')
            .create());
    Option extraobjs = OptionBuilder.withLongOpt("extra-objs")
            .withDescription("Specify extra .c/.cpp files, comma-separated").hasArg().withValueSeparator(',')
            .create();
    extraobjs.setArgs(Option.UNLIMITED_VALUES);
    options.addOption(extraobjs);
    options.addOption(OptionBuilder.withLongOpt("guest-runtime")
            .withDescription(
                    "Emit code to be called by another runtime (i.e., main() is written in another language).")
            .create());
    options.addOption(OptionBuilder.withLongOpt("only-c")
            .withDescription("Generate C code, but do not attempt to compile it").create());
    options.addOption(OptionBuilder.withLongOpt("c-compiler")
            .withDescription("Disable GC: param is 'on' (default) or 'off'").hasArg().create("cc"));
    options.addOption(OptionBuilder.withLongOpt("runtime-src")
            .withDescription("Specify path to runtime system source").hasArg().create());
    options.addOption(OptionBuilder.withLongOpt("ext-path")
            .withDescription("Specify path to external dependency dir (GC, etc.)").hasArg().create());
    options.addOption(OptionBuilder.withLongOpt("skip-validation")
            .withDescription("Run the backend without validating the results of type inference").create());

    options.addOption(OptionBuilder.withLongOpt("m32").withDescription("Force 32-bit compilation").create());
    options.addOption(OptionBuilder.withLongOpt("Xbootinterop")
            .withDescription("Programs start with global interop dirty flag set (experimental)").create());
    options.addOption(OptionBuilder.withLongOpt("Xinterop")
            .withDescription("Enable (experimental) interoperability backend").create());

    options.addOption(OptionBuilder.withDescription("C compiler default optimization level").create("O"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 0").create("O0"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 1").create("O1"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 2").create("O2"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 3").create("O3"));

    options.addOption(
            OptionBuilder.withLongOpt("oldExpl").withDescription("Use old error explanations").create());

    String explanationStrategyHelp = "default: " + FixingSetFinder.defaultStrategy() + "; other choices: "
            + FixingSetFinder.strategyNames().stream().filter(s -> !s.equals(FixingSetFinder.defaultStrategy()))
                    .collect(Collectors.joining(", "));

    options.addOption(OptionBuilder.withLongOpt("explanation-strategy")
            .withDescription("Error explanation strategy to use (" + explanationStrategyHelp + ')').hasArg()
            .create());

    options.addOption(
            OptionBuilder.withLongOpt("efl").withDescription("Set up efl environment in main()").create());

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

        String[] newargs = cmd.getArgs();
        if (newargs.length != 1) {
            throw new ParseException("Invalid number of arguments");
        }

        String sourcefile = newargs[0];
        if (!sourcefile.endsWith(".js")) {
            throw new ParseException("Invalid file extension on input file: " + sourcefile);
        }

        String gc = cmd.getOptionValue("gc", "on");
        if (gc.equals("on")) {
            use_gc = true;
        } else if (gc.equals("off")) {
            use_gc = false;
        } else {
            throw new ParseException("Invalid GC option: " + gc);
        }
        String fields = cmd.getOptionValue("Xfields", "true");
        if (fields.equals("true")) {
            field_opts = true;
        } else if (fields.equals("false")) {
            field_opts = false;
        } else {
            throw new ParseException("Invalid field optimization option: " + fields);
        }
        String encoding = cmd.getOptionValue("XEncodedValues", "false");
        if (encoding.equals("true")) {
            encode_vals = true;
        } else if (encoding.equals("false")) {
            encode_vals = false;
        } else {
            throw new ParseException("Invalid value encoding option: " + encode_vals);
        }
        String plat = cmd.getOptionValue("target", "native");
        if (plat.equals("native")) {
            p = CompilerOptions.Platform.Native;
        } else if (plat.equals("web")) {
            p = CompilerOptions.Platform.Web;
        } else {
            throw new ParseException("Invalid target platform: " + plat);
        }
        if (cmd.hasOption("cc")) {
            external_compiler = cmd.getOptionValue("cc");
        }
        if (cmd.hasOption("skip-validation")) {
            validate = false;
        }
        if (cmd.hasOption("typecheck-only")) {
            typecheckonly = true;
        }
        if (cmd.hasOption("show-constraints")) {
            showconstraints = true;
        }
        if (cmd.hasOption("show-constraint-solution")) {
            showconstraintsolution = true;
        }
        if (cmd.hasOption("debugcompiler")) {
            debug = true;
        }
        if (cmd.hasOption("m32")) {
            x32 = true;
        }
        if (cmd.hasOption("Xinterop")) {
            interop = true;
        }
        if (cmd.hasOption("Xbootinterop")) {
            boot_interop = true;
            if (!interop) {
                System.err.println("WARNING: --Xbootinterop enabled without --Xinterop (no effect)");
            }
        }
        if (cmd.hasOption("oldExpl")) {
            oldExpl = true;
        }
        if (cmd.hasOption("explanation-strategy")) {
            explanationStrategy = cmd.getOptionValue("explanation-strategy");
        }
        String output = cmd.getOptionValue("o");
        if (output == null) {
            output = sourcefile.replaceFirst(".js$", ".c");
        } else {
            if (!output.endsWith(".c")) {
                throw new ParseException("Invalid file extension on output file: " + output);
            }
        }
        String runtime_src = cmd.getOptionValue("runtime-src");
        String ext_path = cmd.getOptionValue("ext-path");
        if (ext_path == null) {
            ext_path = new File(".").getCanonicalPath() + "/external";
        }

        if (cmd.hasOption("extra-decls")) {
            decls = cmd.getOptionValues("extra-decls");
        }
        if (cmd.hasOption("native-libs")) {
            links = cmd.getOptionValues("native-libs");
        }
        if (cmd.hasOption("extra-objs")) {
            objs = cmd.getOptionValues("extra-objs");
        }
        if (cmd.hasOption("guest-runtime")) {
            guest = true;
        }
        if (cmd.hasOption("only-c")) {
            stop_at_c = true;
        }
        if (cmd.hasOption("efl")) {
            efl = true;
        }

        int coptlevel = -1; // default optimization
        if (cmd.hasOption("O3")) {
            coptlevel = 3;
        } else if (cmd.hasOption("O2")) {
            coptlevel = 2;
        } else if (cmd.hasOption("O1")) {
            coptlevel = 1;
        } else if (cmd.hasOption("O0")) {
            coptlevel = 0;
        } else if (cmd.hasOption("O")) {
            coptlevel = -1;
        } else {
            coptlevel = 3;
        }

        if (!Files.exists(Paths.get(sourcefile))) {
            System.err.println("File " + sourcefile + " was not found.");
            throw new FileNotFoundException(sourcefile);
        }

        String cwd = new java.io.File(".").getCanonicalPath();

        opts = new CompilerOptions(p, sourcefile, debug, output, use_gc,
                external_compiler == null ? "clang" : external_compiler,
                external_compiler == null ? "emcc" : external_compiler, cwd + "/a.out", // emcc automatically adds .js
                new File(".").getCanonicalPath(), true, field_opts, showconstraints, showconstraintsolution,
                runtime_src, encode_vals, x32, oldExpl, explanationStrategy, efl, coptlevel);
        if (guest) {
            opts.setGuestRuntime();
        }
        if (interop) {
            opts.enableInteropMode();
        }
        if (boot_interop) {
            opts.startInInteropMode();
        }
        opts.setExternalDeps(ext_path);
        if (decls != null) {
            for (String s : decls) {
                Path fname = FileSystems.getDefault().getPath(s);
                opts.addDeclarationFile(fname);
            }
        }
        if (links != null) {
            for (String s : links) {
                Path fname = FileSystems.getDefault().getPath(s);
                opts.addLinkageFile(fname);
            }
        }
        if (objs == null) {
            objs = new String[0];
        }

    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("sjsc", options);
    }

    if (opts != null) {
        // This typechecks, and depending on flags, also generates C
        compile(opts, typecheckonly, validate); // don't worry about type validation on command line for now
        if (!typecheckonly && !stop_at_c) {
            int ret = 0;
            // Kept around for debugging 32-bit...
            if (p == CompilerOptions.Platform.Native) {
                if (opts.m32()) {
                    String[] x = new String[2];
                    x[0] = "-m32";
                    x[1] = opts.getExternalDeps() + "/gc/x86/lib/libgc.a";
                    ret = clang_compile(opts, objs, x);
                } else {
                    ret = clang_compile(opts, objs, new String[0]);
                }
            } else {
                ret = emcc_compile(opts, objs, new String[0]);
            }
            // If clang failed, propagate the failure outwards
            if (ret != 0) {
                System.exit(ret);
            }
        }
    }
}

From source file:com.yahoo.labs.yamall.local.Yamall.java

public static void main(String[] args) {
    String[] remainingArgs = null;
    String inputFile = null;/*  w w  w.  j a  v  a2s  . com*/
    String predsFile = null;
    String saveModelFile = null;
    String initialModelFile = null;
    String lossName = null;
    String parserName = null;
    String linkName = null;
    String invertHashName = null;
    double learningRate = 1;
    String minPredictionString = null;
    String maxPredictionString = null;
    String fmNumberFactorsString = null;
    int bitsHash;
    int numberPasses;
    int holdoutPeriod = 10;

    boolean testOnly = false;
    boolean exponentialProgress;
    double progressInterval;

    options.addOption("h", "help", false, "displays this help");
    options.addOption("t", false, "ignore label information and just test");
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("binary")
            .desc("reports loss as binary classification with -1,1 labels").build());
    options.addOption(
            Option.builder().hasArg(false).required(false).longOpt("solo").desc("uses SOLO optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pcsolo")
            .desc("uses Per Coordinate SOLO optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pistol")
            .desc("uses PiSTOL optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("kt")
            .desc("(EXPERIMENTAL) uses KT optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pckt")
            .desc("(EXPERIMENTAL) uses Per Coordinate KT optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("pccocob")
            .desc("(EXPERIMENTAL) uses Per Coordinate COCOB optimizer").build());
    options.addOption(Option.builder().hasArg(false).required(false).longOpt("cocob")
            .desc("(EXPERIMENTAL) uses COCOB optimizer").build());
    options.addOption(
            Option.builder().hasArg(false).required(false).longOpt("fm").desc("Factorization Machine").build());
    options.addOption(Option.builder("f").hasArg(true).required(false).desc("final regressor to save")
            .type(String.class).longOpt("final_regressor").build());
    options.addOption(Option.builder("p").hasArg(true).required(false).desc("file to output predictions to")
            .longOpt("predictions").type(String.class).build());
    options.addOption(
            Option.builder("i").hasArg(true).required(false).desc("initial regressor(s) to load into memory")
                    .longOpt("initial_regressor").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc(
            "specify the loss function to be used. Currently available ones are: absolute, squared (default), hinge, logistic")
            .longOpt("loss_function").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc(
            "specify the link function used in the output of the predictions. Currently available ones are: identity (default), logistic")
            .longOpt("link").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("output human-readable final regressor with feature names").longOpt("invert_hash")
            .type(String.class).build());
    options.addOption(
            Option.builder("l").hasArg(true).required(false).desc("set (initial) learning Rate, default = 1.0")
                    .longOpt("learning_rate").type(String.class).build());
    options.addOption(Option.builder("b").hasArg(true).required(false)
            .desc("number of bits in the feature table, default = 18").longOpt("bit_precision")
            .type(String.class).build());
    options.addOption(Option.builder("P").hasArg(true).required(false)
            .desc("progress update frequency, integer: additive; float: multiplicative, default = 2.0")
            .longOpt("progress").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("smallest prediction to output, before the link function, default = -50")
            .longOpt("min_prediction").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("smallest prediction to output, before the link function, default = 50")
            .longOpt("max_prediction").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("ignore namespaces beginning with the characters in <arg>").longOpt("ignore")
            .type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc("number of training passes")
            .longOpt("passes").type(String.class).build());
    options.addOption(
            Option.builder().hasArg(true).required(false).desc("holdout period for test only, default = 10")
                    .longOpt("holdout_period").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("number of factors for Factorization Machines default = 8").longOpt("fmNumberFactors")
            .type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false)
            .desc("specify the parser to use. Currently available ones are: vw (default), libsvm, tsv")
            .longOpt("parser").type(String.class).build());
    options.addOption(Option.builder().hasArg(true).required(false).desc("schema file for the TSV input")
            .longOpt("schema").type(String.class).build());

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Unrecognized option");
        help();
    }
    if (cmd.hasOption("h"))
        help();
    if (cmd.hasOption("t"))
        testOnly = true;
    if (cmd.hasOption("binary")) {
        binary = true;
        System.out.println("Reporting binary loss");
    }
    initialModelFile = cmd.getOptionValue("i");
    predsFile = cmd.getOptionValue("p");
    lossName = cmd.getOptionValue("loss_function", "squared");
    linkName = cmd.getOptionValue("link", "identity");
    saveModelFile = cmd.getOptionValue("f");
    learningRate = Double.parseDouble(cmd.getOptionValue("l", "1.0"));
    bitsHash = Integer.parseInt(cmd.getOptionValue("b", "18"));
    invertHashName = cmd.getOptionValue("invert_hash");
    minPredictionString = cmd.getOptionValue("min_prediction", "-50");
    maxPredictionString = cmd.getOptionValue("max_prediction", "50");
    fmNumberFactorsString = cmd.getOptionValue("fmNumberFactors", "8");
    parserName = cmd.getOptionValue("parser", "vw");

    numberPasses = Integer.parseInt(cmd.getOptionValue("passes", "1"));
    System.out.println("Number of passes = " + numberPasses);
    if (numberPasses > 1) {
        holdoutPeriod = Integer.parseInt(cmd.getOptionValue("holdout_period", "10"));
        System.out.println("Holdout period = " + holdoutPeriod);
    }

    remainingArgs = cmd.getArgs();
    if (remainingArgs.length == 1)
        inputFile = remainingArgs[0];

    InstanceParser instanceParser = null;
    if (parserName.equals("vw"))
        instanceParser = new VWParser(bitsHash, cmd.getOptionValue("ignore"), (invertHashName != null));
    else if (parserName.equals("libsvm"))
        instanceParser = new LIBSVMParser(bitsHash, (invertHashName != null));
    else if (parserName.equals("tsv")) {
        String schema = cmd.getOptionValue("schema");
        if (schema == null) {
            System.out.println("TSV parser requires a schema file.");
            System.exit(0);
        } else {
            String spec = null;
            try {
                spec = new String(Files.readAllBytes(Paths.get(schema)));
            } catch (IOException e) {
                System.out.println("Error reading the TSV schema file.");
                e.printStackTrace();
                System.exit(0);
            }
            instanceParser = new TSVParser(bitsHash, cmd.getOptionValue("ignore"), (invertHashName != null),
                    spec);
        }
    } else {
        System.out.println("Unknown parser.");
        System.exit(0);
    }
    System.out.println("Num weight bits = " + bitsHash);

    // setup progress
    String progress = cmd.getOptionValue("P", "2.0");
    if (progress.indexOf('.') >= 0) {
        exponentialProgress = true;
        progressInterval = (double) Double.parseDouble(progress);
    } else {
        exponentialProgress = false;
        progressInterval = (double) Integer.parseInt(progress);
    }

    // min and max predictions
    minPrediction = (double) Double.parseDouble(minPredictionString);
    maxPrediction = (double) Double.parseDouble(maxPredictionString);

    // number of factors for Factorization Machines
    fmNumberFactors = (int) Integer.parseInt(fmNumberFactorsString);

    // configure the learner
    Loss lossFnc = null;
    LinkFunction link = null;
    if (initialModelFile == null) {
        if (cmd.hasOption("kt")) {
            learner = new KT(bitsHash);
        } else if (cmd.hasOption("pckt")) {
            learner = new PerCoordinateKT(bitsHash);
        } else if (cmd.hasOption("pcsolo")) {
            learner = new PerCoordinateSOLO(bitsHash);
        } else if (cmd.hasOption("solo")) {
            learner = new SOLO(bitsHash);
        } else if (cmd.hasOption("pccocob")) {
            learner = new PerCoordinateCOCOB(bitsHash);
        } else if (cmd.hasOption("cocob")) {
            learner = new COCOB(bitsHash);
        } else if (cmd.hasOption("pistol")) {
            learner = new PerCoordinatePiSTOL(bitsHash);
        } else if (cmd.hasOption("fm")) {
            learner = new SGD_FM(bitsHash, fmNumberFactors);
        } else
            learner = new SGD_VW(bitsHash);
    } else {
        learner = IOLearner.loadLearner(initialModelFile);
    }

    // setup link function
    if (linkName.equals("identity")) {
        link = new IdentityLinkFunction();
    } else if (linkName.equals("logistic")) {
        link = new LogisticLinkFunction();
    } else {
        System.out.println("Unknown link function.");
        System.exit(0);
    }

    // setup loss function
    if (lossName.equals("squared")) {
        lossFnc = new SquareLoss();
    } else if (lossName.equals("hinge")) {
        lossFnc = new HingeLoss();
    } else if (lossName.equals("logistic")) {
        lossFnc = new LogisticLoss();
    } else if (lossName.equals("absolute")) {
        lossFnc = new AbsLoss();
    } else {
        System.out.println("Unknown loss function.");
        System.exit(0);
    }

    learner.setLoss(lossFnc);
    learner.setLearningRate(learningRate);

    // maximum range predictions
    System.out.println("Max prediction = " + maxPrediction + ", Min Prediction = " + minPrediction);
    // print information about the learner
    System.out.println(learner.toString());
    // print information about the link function
    System.out.println(link.toString());
    // print information about the parser
    System.out.println(instanceParser.toString());
    // print information about ignored namespaces
    System.out.println("Ignored namespaces = " + cmd.getOptionValue("ignore", ""));

    long start = System.nanoTime();
    FileInputStream fstream;
    try {
        BufferedReader br = null;
        if (inputFile != null) {
            fstream = new FileInputStream(inputFile);
            System.out.println("Reading datafile = " + inputFile);
            br = new BufferedReader(new InputStreamReader(fstream));
        } else {
            System.out.println("Reading from console");
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        File fout = null;
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        if (predsFile != null) {
            fout = new File(predsFile);
            fos = new FileOutputStream(fout);
            bw = new BufferedWriter(new OutputStreamWriter(fos));
        }

        try {
            System.out.println("average       example  current  current  current");
            System.out.println("loss          counter    label  predict  features");
            int iter = 0;
            double cumLoss = 0;
            double weightedSampleSum = 0;
            double sPlus = 0;
            double sMinus = 0;
            Instance sample = null;
            boolean justPrinted = false;
            int pass = 0;
            ObjectOutputStream ooutTr = null;
            ObjectOutputStream ooutHO = null;
            ObjectInputStream oinTr = null;
            double pred = 0;
            int limit = 1;
            double hError = Double.MAX_VALUE;
            double lastHError = Double.MAX_VALUE;
            int numTestSample = 0;
            int numTrainingSample = 0;
            int idx = 0;

            if (numberPasses > 1) {
                ooutTr = new ObjectOutputStream(new FileOutputStream("cache_training.bin"));
                ooutHO = new ObjectOutputStream(new FileOutputStream("cache_holdout.bin"));
                oinTr = new ObjectInputStream(new FileInputStream("cache_training.bin"));
            }

            do {
                while (true) {
                    double score;

                    if (pass > 0 && numberPasses > 1) {
                        Instance tmp = (Instance) oinTr.readObject();
                        if (tmp != null)
                            sample = tmp;
                        else
                            break;
                    } else {
                        String strLine = br.readLine();
                        if (strLine != null)
                            sample = instanceParser.parse(strLine);
                        else
                            break;
                    }

                    justPrinted = false;
                    idx++;

                    if (numberPasses > 1 && pass == 0 && idx % holdoutPeriod == 0) {
                        // store the current sample for the holdout set
                        ooutHO.writeObject(sample);
                        ooutHO.reset();
                        numTestSample++;
                    } else {
                        if (numberPasses > 1 && pass == 0) {
                            ooutTr.writeObject(sample);
                            ooutTr.reset();
                            numTrainingSample++;
                        }

                        iter++;
                        if (testOnly) {
                            // predict the sample
                            score = learner.predict(sample);
                        } else {
                            // predict the sample and update the classifier using the sample
                            score = learner.update(sample);
                        }
                        score = Math.min(Math.max(score, minPrediction), maxPrediction);
                        pred = link.apply(score);
                        if (!binary)
                            cumLoss += learner.getLoss().lossValue(score, sample.getLabel())
                                    * sample.getWeight();
                        else if (Math.signum(score) != sample.getLabel())
                            cumLoss += sample.getWeight();

                        weightedSampleSum += sample.getWeight();
                        if (sample.getLabel() > 0)
                            sPlus = sPlus + sample.getWeight();
                        else
                            sMinus = sMinus + sample.getWeight();

                        // output predictions to file
                        if (predsFile != null) {
                            bw.write(String.format("%.6f %s", pred, sample.getTag()));
                            bw.newLine();
                        }

                        // print statistics to screen
                        if (iter == limit) {
                            justPrinted = true;
                            System.out.printf("%.6f %12d  % .4f  % .4f  %d\n", cumLoss / weightedSampleSum,
                                    iter, sample.getLabel(), pred, sample.getVector().size());
                            if (exponentialProgress)
                                limit *= progressInterval;
                            else
                                limit += progressInterval;
                        }
                    }
                }
                if (numberPasses > 1) {
                    if (pass == 0) { // finished first pass of many
                        // write a null at the end of the files
                        ooutTr.writeObject(null);
                        ooutHO.writeObject(null);
                        ooutTr.flush();
                        ooutHO.flush();
                        ooutTr.close();
                        ooutHO.close();

                        System.out.println("finished first epoch");
                        System.out.println(numTrainingSample + " training samples");
                        System.out.println(numTestSample + " holdout samples saved");
                    }
                    lastHError = hError;
                    hError = evalHoldoutError();
                }
                if (numberPasses > 1) {
                    System.out.printf("Weighted loss on holdout on epoch %d = %.6f\n", pass + 1, hError);

                    oinTr.close();
                    oinTr = new ObjectInputStream(new FileInputStream("cache_training.bin"));

                    if (hError > lastHError) {
                        System.out.println("Early stopping");
                        break;
                    }
                }
                pass++;
            } while (pass < numberPasses);

            if (justPrinted == false) {
                System.out.printf("%.6f %12d  % .4f  % .4f  %d\n", cumLoss / weightedSampleSum, iter,
                        sample.getLabel(), pred, sample.getVector().size());
            }
            System.out.println("finished run");

            System.out.println(String.format("average loss best constant predictor: %.6f",
                    lossFnc.lossConstantBinaryLabels(sPlus, sMinus)));

            if (saveModelFile != null)
                IOLearner.saveLearner(learner, saveModelFile);
            if (invertHashName != null)
                IOLearner.saveInvertHash(learner.getWeights(), instanceParser.getInvertHashMap(),
                        invertHashName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // close the input stream
        try {
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // close the output stream
        if (predsFile != null) {
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        long millis = System.nanoTime() - start;
        System.out.printf("Elapsed time: %d min, %d sec\n", TimeUnit.NANOSECONDS.toMinutes(millis),
                TimeUnit.NANOSECONDS.toSeconds(millis) - 60 * TimeUnit.NANOSECONDS.toMinutes(millis));
    } catch (

    FileNotFoundException e) {
        System.out.println("Error opening the input file");
        e.printStackTrace();
    }

}