Example usage for java.util ArrayList size

List of usage examples for java.util ArrayList size

Introduction

In this page you can find the example usage for java.util ArrayList size.

Prototype

int size

To view the source code for java.util ArrayList size.

Click Source Link

Document

The size of the ArrayList (the number of elements it contains).

Usage

From source file:com.aestel.chemistry.openEye.fp.DistMatrix.java

public static void main(String... args) throws IOException {
    long start = System.currentTimeMillis();

    // create command line Options object
    Options options = new Options();
    Option opt = new Option("i", true, "input file [.tsv from FingerPrinter]");
    opt.setRequired(true);/*from w  w  w .  j av  a2 s  . co m*/
    options.addOption(opt);

    opt = new Option("o", true, "outpur file [.tsv ");
    opt.setRequired(true);
    options.addOption(opt);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        exitWithHelp(options);
    }
    args = cmd.getArgs();

    if (args.length != 0)
        exitWithHelp(options);

    String file = cmd.getOptionValue("i");
    BufferedReader in = new BufferedReader(new FileReader(file));

    file = cmd.getOptionValue("o");
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));

    ArrayList<Fingerprint> fps = new ArrayList<Fingerprint>();
    ArrayList<String> ids = new ArrayList<String>();
    String line;
    while ((line = in.readLine()) != null) {
        String[] parts = line.split("\t");
        if (parts.length == 3) {
            ids.add(parts[0]);
            fps.add(new ByteFingerprint(parts[2]));
        }
    }
    in.close();

    out.print("ID");
    for (int i = 0; i < ids.size(); i++) {
        out.print('\t');
        out.print(ids.get(i));
    }
    out.println();

    for (int i = 0; i < ids.size(); i++) {
        out.print(ids.get(i));
        Fingerprint fp1 = fps.get(i);

        for (int j = 0; j <= i; j++) {
            out.printf("\t%.4g", fp1.tanimoto(fps.get(j)));
        }
        out.println();
    }
    out.close();

    System.err.printf("Done %d fingerprints in %.2gsec\n", fps.size(),
            (System.currentTimeMillis() - start) / 1000D);
}

From source file:edu.cmu.lti.oaqa.knn4qa.apps.ExtractDataAndQueryAsSparseVectors.java

public static void main(String[] args) {
    String optKeys[] = { CommonParams.MAX_NUM_QUERY_PARAM, MAX_NUM_DATA_PARAM, CommonParams.MEMINDEX_PARAM,
            IN_QUERIES_PARAM, OUT_QUERIES_PARAM, OUT_DATA_PARAM, TEXT_FIELD_PARAM, TEST_QTY_PARAM, };
    String optDescs[] = { CommonParams.MAX_NUM_QUERY_DESC, MAX_NUM_DATA_DESC, CommonParams.MEMINDEX_DESC,
            IN_QUERIES_DESC, OUT_QUERIES_DESC, OUT_DATA_DESC, TEXT_FIELD_DESC, TEST_QTY_DESC };
    boolean hasArg[] = { true, true, true, true, true, true, true, true };

    ParamHelper prmHlp = null;/*from   ww  w.j  a  v a2s.c  o  m*/

    try {

        prmHlp = new ParamHelper(args, optKeys, optDescs, hasArg);

        CommandLine cmd = prmHlp.getCommandLine();
        Options opt = prmHlp.getOptions();

        int maxNumQuery = Integer.MAX_VALUE;

        String tmpn = cmd.getOptionValue(CommonParams.MAX_NUM_QUERY_PARAM);
        if (tmpn != null) {
            try {
                maxNumQuery = Integer.parseInt(tmpn);
            } catch (NumberFormatException e) {
                UsageSpecify(CommonParams.MAX_NUM_QUERY_PARAM, opt);
            }
        }

        int maxNumData = Integer.MAX_VALUE;
        tmpn = cmd.getOptionValue(MAX_NUM_DATA_PARAM);
        if (tmpn != null) {
            try {
                maxNumData = Integer.parseInt(tmpn);
            } catch (NumberFormatException e) {
                UsageSpecify(MAX_NUM_DATA_PARAM, opt);
            }
        }
        String memIndexPref = cmd.getOptionValue(CommonParams.MEMINDEX_PARAM);
        if (null == memIndexPref) {
            UsageSpecify(CommonParams.MEMINDEX_PARAM, opt);
        }
        String textField = cmd.getOptionValue(TEXT_FIELD_PARAM);
        if (null == textField) {
            UsageSpecify(TEXT_FIELD_PARAM, opt);
        }

        textField = textField.toLowerCase();
        int fieldId = -1;
        for (int i = 0; i < FeatureExtractor.mFieldNames.length; ++i)
            if (FeatureExtractor.mFieldNames[i].compareToIgnoreCase(textField) == 0) {
                fieldId = i;
                break;
            }
        if (-1 == fieldId) {
            Usage("Wrong field index, should be one of the following: "
                    + String.join(",", FeatureExtractor.mFieldNames), opt);
        }

        InMemForwardIndex indx = new InMemForwardIndex(
                FeatureExtractor.indexFileName(memIndexPref, FeatureExtractor.mFieldNames[fieldId]));

        BM25SimilarityLucene bm25simil = new BM25SimilarityLucene(FeatureExtractor.BM25_K1,
                FeatureExtractor.BM25_B, indx);

        String inQueryFile = cmd.getOptionValue(IN_QUERIES_PARAM);
        String outQueryFile = cmd.getOptionValue(OUT_QUERIES_PARAM);
        if ((inQueryFile == null) != (outQueryFile == null)) {
            Usage("You should either specify both " + IN_QUERIES_PARAM + " and " + OUT_QUERIES_PARAM
                    + " or none of them", opt);
        }
        String outDataFile = cmd.getOptionValue(OUT_DATA_PARAM);

        tmpn = cmd.getOptionValue(TEST_QTY_PARAM);
        int testQty = 0;
        if (tmpn != null) {
            try {
                testQty = Integer.parseInt(tmpn);
            } catch (NumberFormatException e) {
                UsageSpecify(TEST_QTY_PARAM, opt);
            }
        }

        ArrayList<DocEntry> testDocEntries = new ArrayList<DocEntry>();
        ArrayList<DocEntry> testQueryEntries = new ArrayList<DocEntry>();
        ArrayList<TrulySparseVector> testDocVectors = new ArrayList<TrulySparseVector>();
        ArrayList<TrulySparseVector> testQueryVectors = new ArrayList<TrulySparseVector>();

        if (outDataFile != null) {
            BufferedWriter out = new BufferedWriter(
                    new OutputStreamWriter(CompressUtils.createOutputStream(outDataFile)));

            ArrayList<DocEntryExt> docEntries = indx.getDocEntries();

            for (int id = 0; id < Math.min(maxNumData, docEntries.size()); ++id) {
                DocEntry e = docEntries.get(id).mDocEntry;
                TrulySparseVector v = bm25simil.getDocSparseVector(e, false);
                if (id < testQty) {
                    testDocEntries.add(e);
                    testDocVectors.add(v);
                }
                outputVector(out, v);
            }

            out.close();

        }

        Splitter splitOnSpace = Splitter.on(' ').trimResults().omitEmptyStrings();

        if (outQueryFile != null) {
            BufferedReader inpText = new BufferedReader(
                    new InputStreamReader(CompressUtils.createInputStream(inQueryFile)));
            BufferedWriter out = new BufferedWriter(
                    new OutputStreamWriter(CompressUtils.createOutputStream(outQueryFile)));

            String queryText = XmlHelper.readNextXMLIndexEntry(inpText);

            for (int queryQty = 0; queryText != null && queryQty < maxNumQuery; queryText = XmlHelper
                    .readNextXMLIndexEntry(inpText), queryQty++) {
                Map<String, String> queryFields = null;
                // 1. Parse a query

                try {
                    queryFields = XmlHelper.parseXMLIndexEntry(queryText);
                } catch (Exception e) {
                    System.err.println("Parsing error, offending QUERY:\n" + queryText);
                    throw new Exception("Parsing error.");
                }

                String fieldText = queryFields.get(FeatureExtractor.mFieldsSOLR[fieldId]);

                if (fieldText == null) {
                    fieldText = "";
                }

                ArrayList<String> tmpa = new ArrayList<String>();
                for (String s : splitOnSpace.split(fieldText))
                    tmpa.add(s);

                DocEntry e = indx.createDocEntry(tmpa.toArray(new String[tmpa.size()]));

                TrulySparseVector v = bm25simil.getDocSparseVector(e, true);
                if (queryQty < testQty) {
                    testQueryEntries.add(e);
                    testQueryVectors.add(v);
                }
                outputVector(out, v);
            }

            out.close();
        }

        int testedQty = 0, diffQty = 0;
        // Now let's do some testing
        for (int iq = 0; iq < testQueryEntries.size(); ++iq) {
            DocEntry queryEntry = testQueryEntries.get(iq);
            TrulySparseVector queryVector = testQueryVectors.get(iq);
            for (int id = 0; id < testDocEntries.size(); ++id) {
                DocEntry docEntry = testDocEntries.get(id);
                TrulySparseVector docVector = testDocVectors.get(id);
                float val1 = bm25simil.compute(queryEntry, docEntry);
                float val2 = TrulySparseVector.scalarProduct(queryVector, docVector);
                ++testedQty;
                if (Math.abs(val1 - val2) > 1e5) {
                    System.err.println(
                            String.format("Potential mismatch BM25=%f <-> scalar product=%f", val1, val2));
                    ++diffQty;
                }
            }
        }
        if (testedQty > 0)
            System.out.println(String.format("Tested %d Mismatched %d", testedQty, diffQty));

    } catch (ParseException e) {
        Usage("Cannot parse arguments: " + e, prmHlp != null ? prmHlp.getOptions() : null);
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Terminating due to an exception: " + e);
        System.exit(1);
    }
}

From source file:cooccurrence.Omer_Levy.java

public static void main(String args[]) {
    String path = "";
    String writePath = "";
    BufferedReader br = null;//from ww  w .j ava 2 s .  c om
    ArrayList<String> files = new ArrayList<>();
    //reading all the files in the directory
    //each file is PPMI matrix for an year
    listFilesForFolder(new File(path), files);
    for (String filePath : files) {
        System.out.println(filePath);
        String fileName = new File(filePath).getName();

        //data structure to store the PPMI matrix in the file
        HashMap<String, HashMap<String, Double>> cooccur = new HashMap<>();
        readFileContents(filePath, cooccur); //reading the file and storing the content in the hashmap
        //Because Matrices are identified by row and col id, the following 
        //lists maps id to corresponding string. Note that matrix is symmetric. 
        ArrayList<String> rowStrings = new ArrayList<>(cooccur.keySet());
        ArrayList<String> colStrings = new ArrayList<>(cooccur.keySet());

        //creating matrix with given dimensions and initializing it to 0
        RealMatrix matrixR = MatrixUtils.createRealMatrix(rowStrings.size(), colStrings.size());

        //creating the matrices for storing top rank-d matrices of SVD 
        RealMatrix matrixUd = MatrixUtils.createRealMatrix(D, D);
        RealMatrix matrixVd = MatrixUtils.createRealMatrix(D, D);
        RealMatrix coVarD = MatrixUtils.createRealMatrix(D, D);

        //populating the matrices based on the co-occur hashmap
        populateMatrixR(matrixR, cooccur, rowStrings, colStrings);

        //computing the svd
        SingularValueDecomposition svd = new SingularValueDecomposition(matrixR);

        //extracting the components of SVD factorization
        RealMatrix U = svd.getU();
        RealMatrix V = svd.getV();
        RealMatrix coVariance = svd.getCovariance(-1);

        //list to store indices of top-D singular values of coVar. 
        //Use this with rowsString (colStrings) to get the corresponding word and context
        ArrayList<Integer> indicesD = new ArrayList<>();
        //Extract topD singular value from covariance to store in coVarD and
        //extract corresponding columns from U and V to store in Ud and Vd
        getTopD(U, V, coVariance, matrixUd, matrixVd, coVarD, indicesD);
        //calulate the squareRoot of coVarD
        RealMatrix squareRootCoVarD = squareRoot(coVarD);
        RealMatrix W_svd = matrixUd.multiply(squareRootCoVarD);
        RealMatrix C_svd = matrixVd.multiply(squareRootCoVarD);
    }
}

From source file:edu.oregonstate.eecs.mcplan.ml.KMeans.java

/**
 * @param args/*from w  ww  . java  2 s  . c  om*/
 */
public static void main(final String[] args) {
    final int nclusters = 2;
    final ArrayList<RealVector> data = new ArrayList<RealVector>();

    for (int x = -1; x <= 1; ++x) {
        for (int y = -1; y <= 1; ++y) {
            data.add(new ArrayRealVector(new double[] { x, y }));
            data.add(new ArrayRealVector(new double[] { x + 10, y + 10 }));
        }
    }

    final KMeans kmeans = new KMeans(nclusters, data.toArray(new RealVector[data.size()])); /* {
                                                                                            @Override
                                                                                            public double distance( final RealVector a, final RealVector b ) {
                                                                                            return a.getL1Distance( b );
                                                                                            }
                                                                                            };
                                                                                            */

    kmeans.run();
    for (int i = 0; i < kmeans.centers().length; ++i) {
        System.out.println("Center " + i + ": " + kmeans.centers()[i]);
        for (int j = 0; j < kmeans.clusters().length; ++j) {
            if (kmeans.clusters()[j] == i) {
                System.out.println("\tPoint " + data.get(j));
            }
        }
    }

}

From source file:edu.oregonstate.eecs.mcplan.domains.toy.RelevantIrrelevant.java

public static void main(final String[] argv) throws NumberFormatException, IOException {
    final RandomGenerator rng = new MersenneTwister(42);
    final int T = 30;
    final int nr = 3;
    final int ni = 3;

    final Parameters params = new Parameters(T, nr, ni);
    final Actions actions = new Actions(params);
    final FsssModel model = new FsssModel(rng, params);

    State s = model.initialState();
    while (!s.isTerminal()) {
        System.out.println(s);// w  ww  . j a  v  a 2  s  .co m
        System.out.println("R(s): " + model.reward(s));
        actions.setState(s, 0);
        final ArrayList<Action> action_list = Fn.takeAll(actions);
        for (int i = 0; i < action_list.size(); ++i) {
            System.out.println(i + ": " + action_list.get(i));
        }
        System.out.print(">>> ");
        final BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        final int choice = Integer.parseInt(cin.readLine());
        final Action a = action_list.get(choice);
        System.out.println("R(s, a): " + model.reward(s, a));
        s = model.sampleTransition(s, a);
    }

    //      // Estimate the value of a "good" policy.
    //      // Note: The "good" policy is to Invest when you can, and Sell if the
    //      // price is >= 2. This is not necessarily optimal because:
    //      //    1. You should Borrow once the episode will end before the loan must be repaid
    //      //   2. For some values of invest_period, you should pass on a low price
    //      //      early in the period to try to get a better one later.
    //      final int Ngames = 10000;
    //      double V = 0;
    //      int Ninvest = 0;
    //      for( int i = 0; i < Ngames; ++i ) {
    //         State s = model.initialState();
    //         double Vi = model.reward( s );
    //         while( !s.isTerminal() ) {
    //            final Action a;
    //
    //            // "Good" policy
    //            if( s.investment == 0 ) {
    //               a = new InvestAction();
    //               Ninvest += 1;
    //            }
    //            else if( s.investment > 0 && s.price >= 2 ) {
    //               if( s.invest_t < (params.invest_period - 1) || s.price > 2 ) {
    //                  a = new SellAction();
    //               }
    //               else {
    //                  a = new SaveAction();
    //               }
    ////               a = new SellAction();
    //            }
    //            else {
    //               a = new SaveAction();
    //            }
    //
    //            // "Borrow" policy
    ////            if( s.loan == 0 ) {
    ////               a = new BorrowAction();
    ////            }
    ////            else {
    ////               a = new SaveAction();
    ////            }
    //
    //            final double ra = model.reward( s, a );
    //            s = model.sampleTransition( s, a );
    //            Vi += ra + model.reward( s );
    //         }
    //         V += Vi;
    //      }
    //
    //      final double Vavg = V / Ngames;
    //      final double Navg = (Ninvest / ((double) Ngames));
    //      System.out.println( "Avg. value: " + Vavg );
    //      System.out.println( "Avg. Invest actions: " + Navg );
    //      System.out.println( "V(Invest) ~= " + ( 1 + (Vavg - params.T)/Navg ) );
}

From source file:GIST.IzbirkomExtractor.IzbirkomExtractor.java

/**
 * @param args/*www.j av  a2  s  . c  o m*/
 */
public static void main(String[] args) {

    // process command-line options
    Options options = new Options();
    options.addOption("n", "noaddr", false, "do not do any address matching (for testing)");
    options.addOption("i", "info", false, "create and populate address information table");
    options.addOption("h", "help", false, "this message");

    // database connection
    options.addOption("s", "server", true, "database server to connect to");
    options.addOption("d", "database", true, "OSM database name");
    options.addOption("u", "user", true, "OSM database user name");
    options.addOption("p", "pass", true, "OSM database password");

    // logging options
    options.addOption("l", "logdir", true, "log file directory (default './logs')");
    options.addOption("e", "loglevel", true, "log level (default 'FINEST')");

    // automatically generate the help statement
    HelpFormatter help_formatter = new HelpFormatter();

    // database URI for connection
    String dburi = null;

    // Information message for help screen
    String info_msg = "IzbirkomExtractor [options] <html_directory>";

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

        if (cmd.hasOption('h') || cmd.getArgs().length != 1) {
            help_formatter.printHelp(info_msg, options);
            System.exit(1);
        }

        /* prohibit n and i together */
        if (cmd.hasOption('n') && cmd.hasOption('i')) {
            System.err.println("Options 'n' and 'i' cannot be used together.");
            System.exit(1);
        }

        /* require database arguments without -n */
        if (cmd.hasOption('n')
                && (cmd.hasOption('s') || cmd.hasOption('d') || cmd.hasOption('u') || cmd.hasOption('p'))) {
            System.err.println("Options 'n' and does not need any databse parameters.");
            System.exit(1);
        }

        /* require all 4 database options to be used together */
        if (!cmd.hasOption('n')
                && !(cmd.hasOption('s') && cmd.hasOption('d') && cmd.hasOption('u') && cmd.hasOption('p'))) {
            System.err.println(
                    "For database access all of the following arguments have to be specified: server, database, user, pass");
            System.exit(1);
        }

        /* useful variables */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm");
        String dateString = formatter.format(new Date());

        /* setup logging */
        File logdir = new File(cmd.hasOption('l') ? cmd.getOptionValue('l') : "logs");
        FileUtils.forceMkdir(logdir);
        File log_file_name = new File(
                logdir + "/" + IzbirkomExtractor.class.getName() + "-" + formatter.format(new Date()) + ".log");
        FileHandler log_file = new FileHandler(log_file_name.getPath());

        /* create "latest" link to currently created log file */
        Path latest_log_link = Paths.get(logdir + "/latest");
        Files.deleteIfExists(latest_log_link);
        Files.createSymbolicLink(latest_log_link, Paths.get(log_file_name.getName()));

        log_file.setFormatter(new SimpleFormatter());
        LogManager.getLogManager().reset(); // prevents logging to console
        logger.addHandler(log_file);
        logger.setLevel(cmd.hasOption('e') ? Level.parse(cmd.getOptionValue('e')) : Level.FINEST);

        // open directory with HTML files and create file list
        File dir = new File(cmd.getArgs()[0]);
        if (!dir.isDirectory()) {
            System.err.println("Unable to find directory '" + cmd.getArgs()[0] + "', exiting");
            System.exit(1);
        }
        PathMatcher pmatcher = FileSystems.getDefault()
                .getPathMatcher("glob:?  * ?*.html");
        ArrayList<File> html_files = new ArrayList<>();
        for (Path file : Files.newDirectoryStream(dir.toPath()))
            if (pmatcher.matches(file.getFileName()))
                html_files.add(file.toFile());
        if (html_files.size() == 0) {
            System.err.println("No matching HTML files found in '" + dir.getAbsolutePath() + "', exiting");
            System.exit(1);
        }

        // create csvResultSink
        FileOutputStream csvout_file = new FileOutputStream("parsed_addresses-" + dateString + ".csv");
        OutputStreamWriter csvout = new OutputStreamWriter(csvout_file, "UTF-8");
        ResultSink csvResultSink = new CSVResultSink(csvout, new CSVStrategy('|', '"', '#'));

        // Connect to DB and osmAddressMatcher
        AddressMatcher osmAddressMatcher;
        DBSink dbSink = null;
        DBInfoSink dbInfoSink = null;
        if (cmd.hasOption('n')) {
            osmAddressMatcher = new DummyAddressMatcher();
        } else {
            dburi = "jdbc:postgresql://" + cmd.getOptionValue('s') + "/" + cmd.getOptionValue('d');
            Connection con = DriverManager.getConnection(dburi, cmd.getOptionValue('u'),
                    cmd.getOptionValue('p'));
            osmAddressMatcher = new OsmAddressMatcher(con);
            dbSink = new DBSink(con);
            if (cmd.hasOption('i'))
                dbInfoSink = new DBInfoSink(con);
        }

        /* create resultsinks */
        SinkMultiplexor sm = SinkMultiplexor.newSinkMultiplexor();
        sm.addResultSink(csvResultSink);
        if (dbSink != null) {
            sm.addResultSink(dbSink);
            if (dbInfoSink != null)
                sm.addResultSink(dbInfoSink);
        }

        // create tableExtractor
        TableExtractor te = new TableExtractor(osmAddressMatcher, sm);

        // TODO: printout summary of options: processing date/time, host, directory of HTML files, jdbc uri, command line with parameters

        // iterate through files
        logger.info("Start processing " + html_files.size() + " files in " + dir);
        for (int i = 0; i < html_files.size(); i++) {
            System.err.println("Parsing #" + i + ": " + html_files.get(i));
            te.processHTMLfile(html_files.get(i));
        }

        System.err.println("Processed " + html_files.size() + " HTML files");
        logger.info("Finished processing " + html_files.size() + " files in " + dir);

    } catch (ParseException e1) {
        System.err.println("Failed to parse CLI: " + e1.getMessage());
        help_formatter.printHelp(info_msg, options);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("I/O Exception: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    } catch (SQLException e) {
        System.err.println("Database '" + dburi + "': " + e.getMessage());
        System.exit(1);
    } catch (ResultSinkException e) {
        System.err.println("Failed to initialize ResultSink: " + e.getMessage());
        System.exit(1);
    } catch (TableExtractorException e) {
        System.err.println("Failed to initialize Table Extractor: " + e.getMessage());
        System.exit(1);
    } catch (CloneNotSupportedException | IllegalAccessException | InstantiationException e) {
        System.err.println("Something really odd happened: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:at.tuwien.ifs.somtoolbox.apps.VisualisationImageSaver.java

public static void main(String[] args) {
    JSAPResult res = OptionFactory.parseResults(args, OPTIONS);

    String uFile = res.getString("unitDescriptionFile");
    String wFile = res.getString("weightVectorFile");
    String dwmFile = res.getString("dataWinnerMappingFile");
    String cFile = res.getString("classInformationFile");
    String vFile = res.getString("inputVectorFile");
    String tFile = res.getString("templateVectorFile");
    String ftype = res.getString("filetype");
    boolean unitGrid = res.getBoolean("unitGrid");

    String basename = res.getString("basename");
    if (basename == null) {
        basename = FileUtils.extractSOMLibInputPrefix(uFile);
    }/*ww w . jav  a2  s. co  m*/
    basename = new File(basename).getAbsolutePath();
    int unitW = res.getInt("width");
    int unitH = res.getInt("height", unitW);

    String[] vizs = res.getStringArray("vis");

    GrowingSOM gsom = null;
    CommonSOMViewerStateData state = CommonSOMViewerStateData.getInstance();
    try {
        SOMLibFormatInputReader inputReader = new SOMLibFormatInputReader(wFile, uFile, null);
        gsom = new GrowingSOM(inputReader);

        SharedSOMVisualisationData d = new SharedSOMVisualisationData(cFile, null, null, dwmFile, vFile, tFile,
                null);
        d.readAvailableData();
        state.inputDataObjects = d;
        gsom.setSharedInputObjects(d);

        Visualizations.initVisualizations(d, inputReader, 0, Palettes.getDefaultPalette(),
                Palettes.getAvailablePalettes());

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    } catch (SOMLibFileFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }

    if (ArrayUtils.isEmpty(vizs)) {
        System.out.println("No specific visualisation specified - saving all available visualisations.");
        vizs = Visualizations.getReadyVisualizationNames();
        System.out.println("Found " + vizs.length + ": " + Arrays.toString(vizs));
    }

    for (String viz : vizs) {
        BackgroundImageVisualizerInstance v = Visualizations.getVisualizationByName(viz);
        if (v == null) {
            System.out.println("Visualization '" + viz + "' not found!");
            continue;
        }
        BackgroundImageVisualizer i = v.getVis();

        GrowingLayer layer = gsom.getLayer();
        try {
            int height = unitH * layer.getYSize();
            int width = unitW * layer.getXSize();
            HashMap<String, BufferedImage> visualizationFlavours = i.getVisualizationFlavours(v.getVariant(),
                    gsom, width, height);
            ArrayList<String> keys = new ArrayList<String>(visualizationFlavours.keySet());
            Collections.sort(keys);

            // if the visualisation has more than 5 flavours, we create a sub-dir for it
            String subDirName = "";
            String oldBasename = basename; // save original base name for later
            if (keys.size() > 5) {
                String parentDir = new File(basename).getParentFile().getPath(); // get the parent path
                String filePrefix = basename.substring(parentDir.length()); // end the file name prefix
                subDirName = parentDir + File.separator + filePrefix + "_" + viz + File.separator; // compose a new
                // subdir name
                new File(subDirName).mkdir(); // create the dir
                basename = subDirName + filePrefix; // and extend the base name by the subdir
            }
            for (String key : keys) {
                File out = new File(basename + "_" + viz + key + "." + ftype);
                System.out.println("Generating visualisation '" + viz + "' as '" + out.getPath() + "'.");
                BufferedImage image = visualizationFlavours.get(key);
                if (unitGrid) {
                    VisualisationUtils.drawUnitGrid(image, gsom, width, height);
                }
                ImageIO.write(image, ftype, out);
            }
            basename = oldBasename; // reset base name
        } catch (SOMToolboxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    System.exit(0);
}

From source file:ch.cyclops.gatekeeper.Main.java

public static void main(String[] args) throws Exception {
    CompositeConfiguration config = new CompositeConfiguration();
    config.addConfiguration(new SystemConfiguration());
    if (args.length > 0)
        config.addConfiguration(new PropertiesConfiguration(args[args.length - 1]));

    //setting up the logging framework now
    Logger.getRootLogger().getLoggerRepository().resetConfiguration();
    ConsoleAppender console = new ConsoleAppender(); //create appender
    //configure the appender
    String PATTERN = "%d [%p|%C{1}|%M|%L] %m%n";
    console.setLayout(new PatternLayout(PATTERN));
    String logConsoleLevel = config.getProperty("log.level.console").toString();
    switch (logConsoleLevel) {
    case ("INFO"):
        console.setThreshold(Level.INFO);
        break;/*from  w ww  .j a va  2s.  c om*/
    case ("DEBUG"):
        console.setThreshold(Level.DEBUG);
        break;
    case ("WARN"):
        console.setThreshold(Level.WARN);
        break;
    case ("ERROR"):
        console.setThreshold(Level.ERROR);
        break;
    case ("FATAL"):
        console.setThreshold(Level.FATAL);
        break;
    case ("OFF"):
        console.setThreshold(Level.OFF);
        break;
    default:
        console.setThreshold(Level.ALL);
    }

    console.activateOptions();
    //add appender to any Logger (here is root)
    Logger.getRootLogger().addAppender(console);

    String logFileLevel = config.getProperty("log.level.file").toString();
    String logFile = config.getProperty("log.file").toString();
    if (logFile != null && logFile.length() > 0) {
        FileAppender fa = new FileAppender();
        fa.setName("FileLogger");

        fa.setFile(logFile);
        fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));

        switch (logFileLevel) {
        case ("INFO"):
            fa.setThreshold(Level.INFO);
            break;
        case ("DEBUG"):
            fa.setThreshold(Level.DEBUG);
            break;
        case ("WARN"):
            fa.setThreshold(Level.WARN);
            break;
        case ("ERROR"):
            fa.setThreshold(Level.ERROR);
            break;
        case ("FATAL"):
            fa.setThreshold(Level.FATAL);
            break;
        case ("OFF"):
            fa.setThreshold(Level.OFF);
            break;
        default:
            fa.setThreshold(Level.ALL);
        }

        fa.setAppend(true);
        fa.activateOptions();

        //add appender to any Logger (here is root)
        Logger.getRootLogger().addAppender(fa);
    }
    //now logger configuration is done, we can start using it.
    Logger mainLogger = Logger.getLogger("gatekeeper-driver.Main");

    mainLogger.debug("Driver loaded properly");
    if (args.length > 0) {
        GKDriver gkDriver = new GKDriver(args[args.length - 1], 1, "Eq7K8h9gpg");
        System.out.println("testing if admin: " + gkDriver.isAdmin(1, 0));
        ArrayList<String> uList = gkDriver.getUserList(0); //the argument is the starting count of number of allowed
                                                           //internal attempts.
        if (uList != null) {
            mainLogger.info("Received user list from Gatekeeper! Count: " + uList.size());
            for (int i = 0; i < uList.size(); i++)
                mainLogger.info(uList.get(i));
        }

        boolean authResponse = gkDriver.simpleAuthentication(1, "Eq7K8h9gpg");
        if (authResponse)
            mainLogger.info("Authentication attempt was successful.");
        else
            mainLogger.warn("Authentication attempt failed!");

        String sName = "myservice-" + System.currentTimeMillis();
        HashMap<String, String> newService = gkDriver.registerService(sName, "this is my new cool service", 0);
        String sKey = "";
        if (newService != null) {
            mainLogger.info("Service registration was successful! Got:" + newService.get("uri") + ", Key="
                    + newService.get("key"));
            sKey = newService.get("key");
        } else {
            mainLogger.warn("Service registration failed!");
        }

        int newUserId = gkDriver.registerUser("user-" + System.currentTimeMillis(), "pass1234", false, sName,
                0);
        if (newUserId != -1)
            mainLogger.info("User registration was successful. Received new id: " + newUserId);
        else
            mainLogger.warn("User registration failed!");

        String token = gkDriver.generateToken(newUserId, "pass1234");
        boolean isValidToken = gkDriver.validateToken(token, newUserId);

        if (isValidToken)
            mainLogger.info("The token: " + token + " is successfully validated for user-id: " + newUserId);
        else
            mainLogger.warn("Token validation was unsuccessful! Token: " + token + ", user-id: " + newUserId);

        ArrayList<String> sList = gkDriver.getServiceList(0); //the argument is the starting count of number of allowed
                                                              //internal attempts.
        if (sList != null) {
            mainLogger.info("Received service list from Gatekeeper! Count: " + sList.size());
            for (int i = 0; i < sList.size(); i++)
                mainLogger.info(sList.get(i));
        }

        isValidToken = gkDriver.validateToken(token, sKey);
        if (isValidToken)
            mainLogger.info("The token: " + token + " is successfully validated for user-id: " + newUserId
                    + " against s-key:" + sKey);
        else
            mainLogger.warn("Token validation was unsuccessful! Token: " + token + ", user-id: " + newUserId
                    + ", s-key: " + sKey);

        boolean deleteResult = gkDriver.deleteUser(newUserId, 0);
        if (deleteResult)
            mainLogger.info("User with id: " + newUserId + " was deleted successfully.");
        else
            mainLogger.warn("User with id: " + newUserId + " could not be deleted successfully!");
    }
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step7CollectMTurkResults.java

public static void main(String[] args) throws Exception {
    // input dir - list of xml query containers
    // /home/user-ukp/research/data/dip/wp1-documents/step4-boiler-plate/
    File inputDir = new File(args[0] + "/");

    // MTurk result file

    // output dir
    File outputDir = new File(args[2]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();//  w  w w.  jav a 2  s  . c  o  m

    }

    // Folder with success files
    File mturkSuccessDir = new File(args[1]);

    Collection<File> files = FileUtils.listFiles(mturkSuccessDir, new String[] { "result" }, false);
    if (files.isEmpty()) {
        throw new IllegalArgumentException("Input folder is empty. " + mturkSuccessDir);
    }

    HashMap<String, List<MTurkAnnotation>> mturkAnnotations = new HashMap<>();

    // parsing all CSV files
    for (File mturkCSVResultFile : files) {
        System.out.println("Parsing " + mturkCSVResultFile.getName());

        MTurkOutputReader outputReader = new MTurkOutputReader(
                new HashSet<>(Arrays.asList("annotation", "workerid")), mturkCSVResultFile);

        // for fixing broken data input: for each hit, collect all sentence IDs
        Map<String, SortedSet<String>> hitSentences = new HashMap<>();

        // first iteration: collect the sentences
        for (Map<String, String> record : outputReader) {
            String hitID = record.get("hitid");
            if (!hitSentences.containsKey(hitID)) {
                hitSentences.put(hitID, new TreeSet<>());
            }

            String relevantSentences = record.get("Answer.relevant_sentences");
            String irrelevantSentences = record.get("Answer.irrelevant_sentences");

            if (relevantSentences != null) {
                hitSentences.get(hitID).addAll(Arrays.asList(relevantSentences.split(",")));
            }

            if (irrelevantSentences != null) {
                hitSentences.get(hitID).addAll(Arrays.asList(irrelevantSentences.split(",")));
            }
        }

        // and now second iteration
        for (Map<String, String> record : outputReader) {
            String hitID = record.get("hitid");
            String annotatorID = record.get("workerid");
            String acceptTime = record.get("assignmentaccepttime");
            String submitTime = record.get("assignmentsubmittime");
            String relevantSentences = record.get("Answer.relevant_sentences");
            String irrelevantSentences = record.get("Answer.irrelevant_sentences");
            String reject = record.get("reject");
            String filename[];
            String comment;
            String clueWeb;
            String[] relevant = {};
            String[] irrelevant = {};

            filename = record.get("annotation").split("_");
            String fileXml = filename[0];
            clueWeb = filename[1].trim();
            comment = record.get("Answer.comment");

            if (relevantSentences != null) {
                relevant = relevantSentences.split(",");
            }

            if (irrelevantSentences != null) {
                irrelevant = irrelevantSentences.split(",");
            }

            // sanitizing data: if both relevant and irrelevant are empty, that's a bug
            // we're gonna look up all sentences from this HIT and treat this assignment
            // as if there were only irrelevant ones
            if (relevant.length == 0 && irrelevant.length == 0) {
                SortedSet<String> strings = hitSentences.get(hitID);
                irrelevant = new String[strings.size()];
                strings.toArray(irrelevant);
            }

            if (reject != null) {
                System.out.println(" HIT " + hitID + " annotated by " + annotatorID + " was rejected ");
            } else {
                /*
                // relevant sentences is a comma-delimited string,
                // this regular expression is rather strange
                // it must contain digits, it might be that there is only one space or a comma or some other char
                // digits are the sentence ids. if relevant sentences do not contain digits then it is wrong
                if (relevantSentences.matches("^\\D*$") &&
                    irrelevantSentences.matches("^\\D*$")) {
                try {
                    throw new IllegalStateException(
                            "No annotations found for HIT " + hitID + " in " +
                                    fileXml + " for document " + clueWeb);
                }
                catch (IllegalStateException ex) {
                    ex.printStackTrace();
                }
                        
                }
                */
                MTurkAnnotation mturkAnnotation;
                try {
                    mturkAnnotation = new MTurkAnnotation(hitID, annotatorID, acceptTime, submitTime, comment,
                            clueWeb, relevant, irrelevant);
                } catch (IllegalArgumentException ex) {
                    throw new IllegalArgumentException("Record: " + record, ex);
                }

                List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileXml);

                if (listOfAnnotations == null) {
                    listOfAnnotations = new ArrayList<>();
                }
                listOfAnnotations.add(mturkAnnotation);
                mturkAnnotations.put(fileXml, listOfAnnotations);
            }

        }
        //            parser.close();
    }

    // Debugging: output number of HITs of a query
    System.out.println("Accepted HITs for a query:");
    for (Map.Entry e : mturkAnnotations.entrySet()) {
        ArrayList<MTurkAnnotation> a = (ArrayList<MTurkAnnotation>) e.getValue();
        System.out.println(e.getKey() + " " + a.size());
    }

    for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));
        String fileName = f.getName();
        List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileName);

        if (listOfAnnotations == null || listOfAnnotations.isEmpty()) {
            throw new IllegalStateException("No annotations for " + f.getName());
        }

        for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) {
            for (MTurkAnnotation mtAnnotation : listOfAnnotations) {
                String clueWeb = mtAnnotation.clueWeb;
                if (rankedResults.clueWebID.equals(clueWeb)) {
                    List<QueryResultContainer.MTurkRelevanceVote> mTurkRelevanceVotes = rankedResults.mTurkRelevanceVotes;
                    QueryResultContainer.MTurkRelevanceVote relevanceVote = new QueryResultContainer.MTurkRelevanceVote();
                    String annotatorID = mtAnnotation.annotatorID;
                    String hitID = mtAnnotation.hitID;
                    String acceptTime = mtAnnotation.acceptTime;
                    String submitTime = mtAnnotation.submitTime;
                    String comment = mtAnnotation.comment;
                    String[] relevant = mtAnnotation.relevant;
                    String[] irrelevant = mtAnnotation.irrelevant;
                    relevanceVote.turkID = annotatorID.trim();
                    relevanceVote.hitID = hitID.trim();
                    relevanceVote.acceptTime = acceptTime.trim();
                    relevanceVote.submitTime = submitTime.trim();
                    relevanceVote.comment = comment != null ? comment.trim() : null;
                    if (relevant.length == 0 && irrelevant.length == 0) {
                        try {
                            throw new IllegalStateException("the length of the annotations is 0"
                                    + rankedResults.clueWebID + " for HIT " + relevanceVote.hitID);
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        }
                    }
                    for (String r : relevant) {
                        String sentenceId = r.trim();
                        if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) {
                            QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote();
                            singleSentenceVote.sentenceID = sentenceId;
                            singleSentenceVote.relevant = "true";
                            relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote);
                        }
                    }
                    for (String r : irrelevant) {
                        String sentenceId = r.trim();
                        if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) {
                            QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote();
                            singleSentenceVote.sentenceID = sentenceId;
                            singleSentenceVote.relevant = "false";
                            relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote);
                        }
                    }
                    mTurkRelevanceVotes.add(relevanceVote);
                }
            }

        }
        File outputFile = new File(outputDir, f.getName());
        FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8");
        System.out.println("Finished " + outputFile);
    }

}

From source file:edu.oregonstate.eecs.mcplan.domains.spbj.SpBjSimulator.java

public static void main(final String[] argv) throws IOException {
    final int seed = 43;
    final RandomGenerator rng = new MersenneTwister(seed);
    //      final Deck deck = new InfiniteSpanishDeck( rng );

    // TODO: Debugging code
    final Deque<Card> stacked = new ArrayDeque<Card>();
    for (int i = 0; i < 20; ++i) {
        stacked.push(Card.C_2c);//from  www.  j a  v  a  2s . c  om
        stacked.push(Card.C_2d);
        stacked.push(Card.C_2h);
        stacked.push(Card.C_2s);
    }
    final Deck deck = new StackedDeck(stacked);

    //      final ArrayList<ArrayList<Card>> test_hands = new ArrayList<ArrayList<Card>>();
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_Kc, Card.C_9h, Card.C_2c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_6c, Card.C_7h, Card.C_8c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_6c, Card.C_7c, Card.C_8c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_6s, Card.C_7s, Card.C_8s ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_7c, Card.C_7h, Card.C_7c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_7c, Card.C_7c, Card.C_7c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_7s, Card.C_7s, Card.C_7s ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_9c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_6c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_2c, Card.C_Ac ) ) );
    //
    //      final ArrayList<ArrayList<Card>> dealer_test_hands = new ArrayList<ArrayList<Card>>();
    //      dealer_test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_Kc, Card.C_Kc ) ) );
    //      dealer_test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_Kc, Card.C_Ac ) ) );
    //
    //      for( final ArrayList<Card> dealer_cards : dealer_test_hands ) {
    //         for( final ArrayList<Card> cards : test_hands ) {
    //            final SpBjState s = new SpBjState( deck );
    //            s.init();
    //            s.dealer_hand.clear();
    //            s.dealer_hand.addAll( dealer_cards );
    //            s.player_hand.hands.set( 0, cards );
    //            final SpBjSimulator sim = new SpBjSimulator( s );
    //            sim.takeAction( new JointAction<SpBjAction>(
    //               new SpBjAction( new SpBjActionCategory[] { SpBjActionCategory.Pass } ) ) );
    //
    //            System.out.print( "Hand: " );
    //            System.out.print( sim.state().player_hand );
    //            System.out.print( " (" );
    //            final ArrayList<int[]> values = sim.state().player_hand.values();
    //            for( int i = 0; i < values.size(); ++i ) {
    //               if( i > 0 ) {
    //                  System.out.print( ", " );
    //               }
    //               System.out.print( Arrays.toString( values.get( i ) ) );
    //            }
    //            System.out.println( ")" );
    //
    //            System.out.print( "Reward: " );
    //            System.out.println( Arrays.toString( sim.reward() ) );
    //            System.out.print( "Dealer hand: " );
    //            System.out.print( sim.state().dealerHand().toString() );
    //            System.out.print( " (" );
    //            System.out.print( SpBjHand.handValue( sim.state().dealerHand() )[0] );
    //            System.out.println( ")" );
    //            System.out.println( "----------------------------------------" );
    //         }
    //      }

    while (true) {
        final SpBjState s = new SpBjState(deck);
        s.init();
        final SpBjSimulator sim = new SpBjSimulator(s);

        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (!s.isTerminal()) {
            System.out.print("Dealer showing: ");
            System.out.println(sim.state().dealerUpcard());

            System.out.print("Hand: ");
            System.out.print(sim.state().player_hand);
            System.out.print(" (");
            final ArrayList<int[]> values = sim.state().player_hand.values();
            for (int i = 0; i < values.size(); ++i) {
                if (i > 0) {
                    System.out.print(", ");
                }
                System.out.print(Arrays.toString(values.get(i)));
            }
            System.out.println(")");

            final SpBjActionGenerator actions = new SpBjActionGenerator();
            actions.setState(sim.state(), 0);
            for (final SpBjAction a : Fn.in(actions)) {
                System.out.println(a);
            }

            final String cmd = reader.readLine();
            assert (cmd.length() == sim.state().player_hand.Nhands);
            final SpBjActionCategory[] cat = new SpBjActionCategory[cmd.length()];
            for (int i = 0; i < cmd.length(); ++i) {
                final char c = cmd.charAt(i);
                if ('h' == c) {
                    cat[i] = SpBjActionCategory.Hit;
                } else if ('p' == c) {
                    cat[i] = SpBjActionCategory.Pass;
                } else if ('d' == c) {
                    cat[i] = SpBjActionCategory.Double;
                } else if ('s' == c) {
                    cat[i] = SpBjActionCategory.Split;
                }
            }
            sim.takeAction(new JointAction<SpBjAction>(new SpBjAction(cat)));
        }

        System.out.print("Hand: ");
        System.out.print(sim.state().player_hand);
        System.out.print(" (");
        final ArrayList<int[]> values = sim.state().player_hand.values();
        for (int i = 0; i < values.size(); ++i) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(Arrays.toString(values.get(i)));
        }
        System.out.println(")");

        System.out.print("Reward: ");
        System.out.println(Arrays.toString(sim.reward()));
        System.out.print("Dealer hand: ");
        System.out.print(sim.state().dealerHand().toString());
        System.out.print(" (");
        System.out.print(SpBjHand.handValue(sim.state().dealerHand())[0]);
        System.out.println(")");
        System.out.println("----------------------------------------");
    }
}