Example usage for java.lang NumberFormatException printStackTrace

List of usage examples for java.lang NumberFormatException printStackTrace

Introduction

In this page you can find the example usage for java.lang NumberFormatException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:edu.cmu.lti.oaqa.apps.Client.java

public static void main(String args[]) {
    BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));

    Options opt = new Options();

    Option o = new Option(PORT_SHORT_PARAM, PORT_LONG_PARAM, true, PORT_DESC);
    o.setRequired(true);//from w  w  w.  ja va2 s .  c  om
    opt.addOption(o);
    o = new Option(HOST_SHORT_PARAM, HOST_LONG_PARAM, true, HOST_DESC);
    o.setRequired(true);
    opt.addOption(o);
    opt.addOption(K_SHORT_PARAM, K_LONG_PARAM, true, K_DESC);
    opt.addOption(R_SHORT_PARAM, R_LONG_PARAM, true, R_DESC);
    opt.addOption(QUERY_TIME_SHORT_PARAM, QUERY_TIME_LONG_PARAM, true, QUERY_TIME_DESC);
    opt.addOption(RET_OBJ_SHORT_PARAM, RET_OBJ_LONG_PARAM, false, RET_OBJ_DESC);
    opt.addOption(RET_EXTERN_ID_SHORT_PARAM, RET_EXTERN_ID_LONG_PARAM, false, RET_EXTERN_ID_DESC);

    CommandLineParser parser = new org.apache.commons.cli.GnuParser();

    try {
        CommandLine cmd = parser.parse(opt, args);

        String host = cmd.getOptionValue(HOST_SHORT_PARAM);

        String tmp = null;

        tmp = cmd.getOptionValue(PORT_SHORT_PARAM);

        int port = -1;

        try {
            port = Integer.parseInt(tmp);
        } catch (NumberFormatException e) {
            Usage("Port should be integer!");
        }

        boolean retObj = cmd.hasOption(RET_OBJ_SHORT_PARAM);
        boolean retExternId = cmd.hasOption(RET_EXTERN_ID_SHORT_PARAM);

        String queryTimeParams = cmd.getOptionValue(QUERY_TIME_SHORT_PARAM);
        if (null == queryTimeParams)
            queryTimeParams = "";

        SearchType searchType = SearchType.kKNNSearch;
        int k = 0;
        double r = 0;

        if (cmd.hasOption(K_SHORT_PARAM)) {
            if (cmd.hasOption(R_SHORT_PARAM)) {
                Usage("Range search is not allowed if the KNN search is specified!");
            }
            tmp = cmd.getOptionValue(K_SHORT_PARAM);
            try {
                k = Integer.parseInt(tmp);
            } catch (NumberFormatException e) {
                Usage("K should be integer!");
            }
            searchType = SearchType.kKNNSearch;
        } else if (cmd.hasOption(R_SHORT_PARAM)) {
            if (cmd.hasOption(K_SHORT_PARAM)) {
                Usage("KNN search is not allowed if the range search is specified!");
            }
            searchType = SearchType.kRangeSearch;
            tmp = cmd.getOptionValue(R_SHORT_PARAM);
            try {
                r = Double.parseDouble(tmp);
            } catch (NumberFormatException e) {
                Usage("The range value should be numeric!");
            }
        } else {
            Usage("One has to specify either range or KNN-search parameter");
        }

        String separator = System.getProperty("line.separator");

        StringBuffer sb = new StringBuffer();
        String s;

        while ((s = inp.readLine()) != null) {
            sb.append(s);
            sb.append(separator);
        }

        String queryObj = sb.toString();

        try {
            TTransport transport = new TSocket(host, port);
            transport.open();

            TProtocol protocol = new TBinaryProtocol(transport);
            QueryService.Client client = new QueryService.Client(protocol);

            if (!queryTimeParams.isEmpty())
                client.setQueryTimeParams(queryTimeParams);

            List<ReplyEntry> res = null;

            long t1 = System.nanoTime();

            if (searchType == SearchType.kKNNSearch) {
                System.out.println(String.format("Running a %d-NN search", k));
                res = client.knnQuery(k, queryObj, retExternId, retObj);
            } else {
                System.out.println(String.format("Running a range search (r=%g)", r));
                res = client.rangeQuery(r, queryObj, retExternId, retObj);
            }

            long t2 = System.nanoTime();

            System.out.println(String.format("Finished in %g ms", (t2 - t1) / 1e6));

            for (ReplyEntry e : res) {
                System.out.println(String.format("id=%d dist=%g %s", e.getId(), e.getDist(),
                        retExternId ? "externId=" + e.getExternId() : ""));
                if (retObj)
                    System.out.println(e.getObj());
            }

            transport.close(); // Close transport/socket !
        } catch (TException te) {
            System.err.println("Apache Thrift exception: " + te);
            te.printStackTrace();
        }

    } catch (ParseException e) {
        Usage("Cannot parse arguments");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:apps.LuceneQuery.java

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

    options.addOption("d", null, true, "index directory");
    options.addOption("i", null, true, "input file");
    options.addOption("s", null, true, "stop word file");
    options.addOption("n", null, true, "max # of results");
    options.addOption("o", null, true, "a TREC-style output file");
    options.addOption("r", null, true, "an optional QREL file, if specified,"
            + "we save results only for queries for which we find at least one relevant entry.");

    options.addOption("prob", null, true, "question sampling probability");
    options.addOption("max_query_qty", null, true, "a maximum number of queries to run");
    options.addOption("bm25_b", null, true, "BM25 parameter: b");
    options.addOption("bm25_k1", null, true, "BM25 parameter: k1");
    options.addOption("bm25fixed", null, false, "use the fixed BM25 similarity");

    options.addOption("seed", null, true, "random seed");

    Joiner commaJoin = Joiner.on(',');
    Joiner spaceJoin = Joiner.on(' ');

    options.addOption("source_type", null, true,
            "query source type: " + commaJoin.join(SourceFactory.getQuerySourceList()));

    CommandLineParser parser = new org.apache.commons.cli.GnuParser();

    QrelReader qrels = null;/*from w w w.  j  a va2 s.  c  o  m*/

    try {

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

        String indexDir = null;

        if (cmd.hasOption("d")) {
            indexDir = cmd.getOptionValue("d");
        } else {
            Usage("Specify 'index directory'", options);
        }

        String inputFileName = null;

        if (cmd.hasOption("i")) {
            inputFileName = cmd.getOptionValue("i");
        } else {
            Usage("Specify 'input file'", options);
        }

        DictNoComments stopWords = null;

        if (cmd.hasOption("s")) {
            String stopWordFileName = cmd.getOptionValue("s");
            stopWords = new DictNoComments(new File(stopWordFileName), true /* lowercasing */);
            System.out.println("Using the stopword file: " + stopWordFileName);
        }

        String sourceName = cmd.getOptionValue("source_type");

        if (sourceName == null)
            Usage("Specify document source type", options);

        int numRet = 100;

        if (cmd.hasOption("n")) {
            numRet = Integer.parseInt(cmd.getOptionValue("n"));
            System.out.println("Retrieving at most " + numRet + " candidate entries.");
        }

        String trecOutFileName = null;

        if (cmd.hasOption("o")) {
            trecOutFileName = cmd.getOptionValue("o");
        } else {
            Usage("Specify 'a TREC-style output file'", options);
        }

        double fProb = 1.0f;

        if (cmd.hasOption("prob")) {
            try {
                fProb = Double.parseDouble(cmd.getOptionValue("prob"));
            } catch (NumberFormatException e) {
                Usage("Wrong format for 'question sampling probability'", options);
            }
        }

        if (fProb <= 0 || fProb > 1) {
            Usage("Question sampling probability should be >0 and <=1", options);
        }

        System.out.println("Sample the following fraction of questions: " + fProb);

        float bm25_k1 = UtilConst.BM25_K1_DEFAULT, bm25_b = UtilConst.BM25_B_DEFAULT;

        if (cmd.hasOption("bm25_k1")) {
            try {
                bm25_k1 = Float.parseFloat(cmd.getOptionValue("bm25_k1"));
            } catch (NumberFormatException e) {
                Usage("Wrong format for 'bm25_k1'", options);
            }
        }

        if (cmd.hasOption("bm25_b")) {
            try {
                bm25_b = Float.parseFloat(cmd.getOptionValue("bm25_b"));
            } catch (NumberFormatException e) {
                Usage("Wrong format for 'bm25_b'", options);
            }
        }

        long seed = 0;

        String tmpl = cmd.getOptionValue("seed");

        if (tmpl != null)
            seed = Long.parseLong(tmpl);

        System.out.println("Using seed: " + seed);

        Random randGen = new Random(seed);

        System.out.println(String.format("BM25 parameters k1=%f b=%f ", bm25_k1, bm25_b));

        boolean useFixedBM25 = cmd.hasOption("bm25fixed");

        EnglishAnalyzer analyzer = new EnglishAnalyzer();
        Similarity similarity = null;

        if (useFixedBM25) {
            System.out.println(String.format("Using fixed BM25Simlarity, k1=%f b=%f", bm25_k1, bm25_b));
            similarity = new BM25SimilarityFix(bm25_k1, bm25_b);
        } else {
            System.out.println(String.format("Using Lucene BM25Similarity, k1=%f b=%f", bm25_k1, bm25_b));
            similarity = new BM25Similarity(bm25_k1, bm25_b);
        }

        int maxQueryQty = Integer.MAX_VALUE;

        if (cmd.hasOption("max_query_qty")) {
            try {
                maxQueryQty = Integer.parseInt(cmd.getOptionValue("max_query_qty"));
            } catch (NumberFormatException e) {
                Usage("Wrong format for 'max_query_qty'", options);
            }
        }

        System.out.println(String.format("Executing at most %d queries", maxQueryQty));

        if (cmd.hasOption("r")) {
            String qrelFile = cmd.getOptionValue("r");
            System.out.println("Using the qrel file: '" + qrelFile
                    + "', queries not returning a relevant entry will be ignored.");
            qrels = new QrelReader(qrelFile);
        }

        System.out.println(String.format("Using indexing directory %s", indexDir));

        LuceneCandidateProvider candProvider = new LuceneCandidateProvider(indexDir, analyzer, similarity);
        TextCleaner textCleaner = new TextCleaner(stopWords);

        QuerySource inpQuerySource = SourceFactory.createQuerySource(sourceName, inputFileName);
        QueryEntry inpQuery = null;

        BufferedWriter trecOutFile = new BufferedWriter(new FileWriter(new File(trecOutFileName)));

        int questNum = 0, questQty = 0;

        long totalTimeMS = 0;

        while ((inpQuery = inpQuerySource.next()) != null) {
            if (questQty >= maxQueryQty)
                break;
            ++questNum;

            String queryID = inpQuery.mQueryId;

            if (randGen.nextDouble() <= fProb) {
                ++questQty;

                String tokQuery = spaceJoin.join(textCleaner.cleanUp(inpQuery.mQueryText));
                String query = TextCleaner.luceneSafeCleanUp(tokQuery).trim();

                ResEntry[] results = null;

                if (query.isEmpty()) {
                    results = new ResEntry[0];
                    System.out.println(String.format("WARNING, empty query id = '%s'", inpQuery.mQueryId));
                } else {

                    try {
                        long start = System.currentTimeMillis();

                        results = candProvider.getCandidates(questNum, query, numRet);

                        long end = System.currentTimeMillis();
                        long searchTimeMS = end - start;
                        totalTimeMS += searchTimeMS;

                        System.out.println(String.format(
                                "Obtained results for the query # %d (answered %d queries), queryID %s the search took %d ms, we asked for max %d entries got %d",
                                questNum, questQty, queryID, searchTimeMS, numRet, results.length));

                    } catch (ParseException e) {
                        e.printStackTrace();
                        System.err.println(
                                "Error parsing query: " + query + " orig question is :" + inpQuery.mQueryText);
                        System.exit(1);
                    }
                }

                boolean bSave = true;

                if (qrels != null) {
                    boolean bOk = false;
                    for (ResEntry r : results) {
                        String label = qrels.get(queryID, r.mDocId);
                        if (candProvider.isRelevLabel(label, 1)) {
                            bOk = true;
                            break;
                        }
                    }
                    if (!bOk)
                        bSave = false;
                }

                //            System.out.println(String.format("Ranking results the query # %d queryId='%s' save results? %b", 
                //                                              questNum, queryID, bSave));          
                if (bSave) {
                    saveTrecResults(queryID, results, trecOutFile, TREC_RUN, numRet);
                }
            }

            if (questNum % 1000 == 0)
                System.out.println(String.format("Proccessed %d questions", questNum));

        }

        System.out.println(String.format("Proccessed %d questions, the search took %f MS on average", questQty,
                (float) totalTimeMS / questQty));

        trecOutFile.close();

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

From source file:at.ac.tuwien.inso.subcat.postprocessor.PostProcessor.java

public static void main(String[] args) {
    Map<String, PostProcessorTask> steps = new HashMap<String, PostProcessorTask>();
    PostProcessorTask _step = new ClassificationTask();
    steps.put(_step.getName(), _step);//  ww w .j  av a2  s .c  o m
    CommentAnalyserTask commentAnalysisStep = new CommentAnalyserTask();
    steps.put(commentAnalysisStep.getName(), commentAnalysisStep);
    AccountInterlinkingTask interlinkingTask = new AccountInterlinkingTask();
    steps.put(interlinkingTask.getName(), interlinkingTask);
    _step = new CommitBugInterlinkingTask();
    steps.put(_step.getName(), _step);

    Options options = new Options();
    options.addOption("h", "help", false, "Show this options");
    options.addOption("d", "db", true, "The database to process (required)");
    options.addOption("v", "verbose", false, "Show details");
    options.addOption("p", "project", true, "The project ID to process");
    options.addOption("P", "list-projects", false, "List all registered projects");
    options.addOption("S", "list-processor-steps", false, "List all registered processor steps");
    options.addOption("s", "processor-step", true, "A processor step name");
    options.addOption("c", "commit-dictionary", true,
            "Path to a classification dictionary for commit message classification");
    options.addOption("b", "bug-dictionary", true,
            "Path to a classification dictionary for bug classification");
    options.addOption("m", "smart-matching", true,
            "Smart user matching configuration. Syntax: <method>:<distance>");
    options.addOption("M", "list-matching-methods", false, "List smart matching methods");

    final Reporter reporter = new Reporter(true);
    reporter.startTimer();

    Settings settings = new Settings();
    ModelPool pool = null;

    boolean printTraces = false;
    CommandLineParser parser = new PosixParser();

    try {
        CommandLine cmd = parser.parse(options, args);
        printTraces = cmd.hasOption("verbose");

        if (cmd.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("postprocessor", options);
            return;
        }

        if (cmd.hasOption("list-processor-steps")) {
            for (String proj : steps.keySet()) {
                System.out.println("  " + proj);
            }
            return;
        }

        if (cmd.hasOption("list-matching-methods")) {
            for (String method : HashFunc.getHashFuncNames()) {
                System.out.println("  " + method);
            }
            return;
        }

        if (cmd.hasOption("db") == false) {
            reporter.error("post-processor", "Option --db is required");
            reporter.printSummary();
            return;
        }

        File dbf = new File(cmd.getOptionValue("db"));
        if (dbf.exists() == false || dbf.isFile() == false) {
            reporter.error("post-processor", "Invalid database file path");
            reporter.printSummary();
            return;
        }

        pool = new ModelPool(cmd.getOptionValue("db"), 2);

        if (cmd.hasOption("list-projects")) {
            Model model = pool.getModel();

            for (Project proj : model.getProjects()) {
                System.out.println("  " + proj.getId() + ": " + proj.getDate());
            }

            model.close();
            return;
        }

        Integer projId = null;
        if (cmd.hasOption("project") == false) {
            reporter.error("post-processor", "Option --project is required");
            reporter.printSummary();
            return;
        } else {
            try {
                projId = Integer.parseInt(cmd.getOptionValue("project"));
            } catch (NumberFormatException e) {
                reporter.error("post-processor", "Invalid project ID");
                reporter.printSummary();
                return;
            }
        }

        Model model = pool.getModel();
        Project project = model.getProject(projId);
        model.close();

        if (project == null) {
            reporter.error("post-processor", "Invalid project ID");
            reporter.printSummary();
            return;
        }

        if (cmd.hasOption("bug-dictionary")) {
            DictionaryParser dp = new DictionaryParser();

            for (String path : cmd.getOptionValues("bug-dictionary")) {
                try {
                    Dictionary dict = dp.parseFile(path);
                    settings.bugDictionaries.add(dict);
                } catch (FileNotFoundException e) {
                    reporter.error("post-processor", "File  not found: " + path + ": " + e.getMessage());
                    reporter.printSummary();
                    return;
                } catch (XmlReaderException e) {
                    reporter.error("post-processor", "XML Error: " + path + ": " + e.getMessage());
                    reporter.printSummary();
                    return;
                }
            }
        }

        if (cmd.hasOption("commit-dictionary")) {
            DictionaryParser dp = new DictionaryParser();

            for (String path : cmd.getOptionValues("commit-dictionary")) {
                try {
                    Dictionary dict = dp.parseFile(path);
                    settings.srcDictionaries.add(dict);
                } catch (FileNotFoundException e) {
                    reporter.error("post-processor", "File  not found: " + path + ": " + e.getMessage());
                    reporter.printSummary();
                    return;
                } catch (XmlReaderException e) {
                    reporter.error("post-processor", "XML Error: " + path + ": " + e.getMessage());
                    reporter.printSummary();
                    return;
                }
            }
        }

        if (cmd.hasOption("smart-matching")) {
            String str = cmd.getOptionValue("smart-matching");
            String[] parts = str.split(":");
            if (parts.length != 2) {
                reporter.error("post-processor", "Unexpected smart-matching format");
                reporter.printSummary();
                return;
            }

            HashFunc func = HashFunc.getHashFunc(parts[0]);
            if (func == null) {
                reporter.error("post-processor", "Unknown smart matching hash function");
                reporter.printSummary();
                return;
            }

            int dist = -1;
            try {
                dist = Integer.parseInt(parts[1]);
            } catch (NumberFormatException e) {
                dist = -1;
            }

            if (dist < 0) {
                reporter.error("post-processor", "Invalid smart matching edist distance");
                reporter.printSummary();
                return;
            }

            interlinkingTask.setDistance(dist);
            interlinkingTask.setHashFunc(func);
        }

        PostProcessor processor = new PostProcessor(project, pool, settings);
        if (cmd.hasOption("processor-step")) {
            for (String stepName : cmd.getOptionValues("processor-step")) {
                PostProcessorTask step = steps.get(stepName);
                if (step == null) {
                    reporter.error("post-processor", "Unknown processor step: '" + stepName + "'");
                    reporter.printSummary();
                    return;
                }

                processor.register(step);
            }
        } else {
            processor.register(steps.values());
        }

        if (printTraces == true) {
            model = pool.getModel();
            final Stats stats = model.getStats(project);
            model.close();

            processor.addListener(new PostProcessorListener() {
                private int commitCount = 0;
                private int bugCount = 0;

                @Override
                public void commit(PostProcessor proc) {
                    commitCount++;
                    reporter.note("post-processor", "status: Commit " + commitCount + "/" + stats.commitCount);
                }

                @Override
                public void bug(PostProcessor proc) {
                    bugCount++;
                    reporter.note("post-processor", "status: Bug " + bugCount + "/" + stats.bugCount);
                }
            });
        }

        processor.process();
    } catch (ParseException e) {
        reporter.error("post-processor", "Parsing failed: " + e.getMessage());
        if (printTraces == true) {
            e.printStackTrace();
        }
    } catch (ClassNotFoundException e) {
        reporter.error("post-processor", "Failed to create a database connection: " + e.getMessage());
        if (printTraces == true) {
            e.printStackTrace();
        }
    } catch (SQLException e) {
        reporter.error("post-processor", "Failed to create a database connection: " + e.getMessage());
        if (printTraces == true) {
            e.printStackTrace();
        }
    } catch (PostProcessorException e) {
        reporter.error("post-processor", "Post-Processor Error: " + e.getMessage());
        if (printTraces == true) {
            e.printStackTrace();
        }
    } finally {
        if (pool != null) {
            pool.close();
        }
    }

    reporter.printSummary(true);
}

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.ja  va  2  s .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:com.oltpbenchmark.multitenancy.MuTeBench.java

/**
 * @param args/*from ww  w.  j  a v a 2s  .c o m*/
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    String duration = null;
    String scenarioFile = null;

    // -------------------------------------------------------------------
    // INITIALIZE LOGGING
    // -------------------------------------------------------------------
    String log4jPath = System.getProperty("log4j.configuration");
    if (log4jPath != null) {
        org.apache.log4j.PropertyConfigurator.configure(log4jPath);
    } else {
        throw new RuntimeException("Missing log4j.properties file");
    }

    // -------------------------------------------------------------------
    // PARSE COMMAND LINE PARAMETERS
    // -------------------------------------------------------------------
    CommandLineParser parser = new PosixParser();
    XMLConfiguration pluginConfig = null;
    try {
        pluginConfig = new XMLConfiguration("config/plugin.xml");
    } catch (ConfigurationException e1) {
        LOG.info("Plugin configuration file config/plugin.xml is missing");
        e1.printStackTrace();
    }
    pluginConfig.setExpressionEngine(new XPathExpressionEngine());
    Options options = new Options();
    options.addOption("s", "scenario", true, "[required] Workload scenario file");
    options.addOption("a", "analysis-buckets", true, "sampling buckets for result aggregation");
    options.addOption("r", "runtime", true,
            "maximum runtime  (no events will be started after finishing runtime)");
    options.addOption("v", "verbose", false, "Display Messages");
    options.addOption("g", "gui", false, "Show controlling GUI");
    options.addOption("h", "help", false, "Print this help");
    options.addOption("o", "output", true, "Output file (default System.out)");
    options.addOption("b", "baseline", true, "Output files of previous baseline run");
    options.addOption(null, "histograms", false, "Print txn histograms");
    options.addOption("d", "dialects-export", true, "Export benchmark SQL to a dialects file");

    // parse the command line arguments
    CommandLine argsLine = parser.parse(options, args);
    if (argsLine.hasOption("h")) {
        printUsage(options);
        return;
    } else if (!argsLine.hasOption("scenario")) {
        INIT_LOG.fatal("Missing scenario description file");
        System.exit(-1);
    } else
        scenarioFile = argsLine.getOptionValue("scenario");
    if (argsLine.hasOption("r"))
        duration = argsLine.getOptionValue("r");
    if (argsLine.hasOption("runtime"))
        duration = argsLine.getOptionValue("runtime");

    // -------------------------------------------------------------------
    // CREATE TENANT SCHEDULE
    // -------------------------------------------------------------------
    INIT_LOG.info("Create schedule");
    Schedule schedule = new Schedule(duration, scenarioFile);
    HashMap<Integer, ScheduleEvents> tenantEvents = schedule.getTenantEvents();
    ArrayList<Integer> tenantList = schedule.getTenantList();

    List<BenchmarkModule> benchList = new ArrayList<BenchmarkModule>();

    for (int tenInd = 0; tenInd < tenantList.size(); tenInd++) {
        int tenantID = tenantList.get(tenInd);
        for (int tenEvent = 0; tenEvent < tenantEvents.get(tenantID).size(); tenEvent++) {

            BenchmarkSettings benchmarkSettings = (BenchmarkSettings) tenantEvents.get(tenantID)
                    .getBenchmarkSettings(tenEvent);

            // update benchmark Settings
            benchmarkSettings.setTenantID(tenantID);

            // -------------------------------------------------------------------
            // GET PLUGIN LIST
            // -------------------------------------------------------------------
            String plugins = benchmarkSettings.getBenchmark();
            String[] pluginList = plugins.split(",");

            String configFile = benchmarkSettings.getConfigFile();
            XMLConfiguration xmlConfig = new XMLConfiguration(configFile);
            xmlConfig.setExpressionEngine(new XPathExpressionEngine());
            int lastTxnId = 0;

            for (String plugin : pluginList) {

                // ----------------------------------------------------------------
                // WORKLOAD CONFIGURATION
                // ----------------------------------------------------------------

                String pluginTest = "";

                pluginTest = "[@bench='" + plugin + "']";

                WorkloadConfiguration wrkld = new WorkloadConfiguration();
                wrkld.setTenantId(tenantID);
                wrkld.setBenchmarkName(setTenantIDinString(plugin, tenantID));
                wrkld.setXmlConfig(xmlConfig);
                wrkld.setDBType(DatabaseType.get(setTenantIDinString(xmlConfig.getString("dbtype"), tenantID)));
                wrkld.setDBDriver(setTenantIDinString(xmlConfig.getString("driver"), tenantID));
                wrkld.setDBConnection(setTenantIDinString(xmlConfig.getString("DBUrl"), tenantID));
                wrkld.setDBName(setTenantIDinString(xmlConfig.getString("DBName"), tenantID));
                wrkld.setDBUsername(setTenantIDinString(xmlConfig.getString("username"), tenantID));
                wrkld.setDBPassword(setTenantIDinString(xmlConfig.getString("password"), tenantID));
                String terminalString = setTenantIDinString(xmlConfig.getString("terminals[not(@bench)]", "0"),
                        tenantID);
                int terminals = Integer.parseInt(xmlConfig.getString("terminals" + pluginTest, terminalString));
                wrkld.setTerminals(terminals);
                int taSize = Integer.parseInt(xmlConfig.getString("taSize", "1"));
                if (taSize < 0)
                    INIT_LOG.fatal("taSize must not be negative!");
                wrkld.setTaSize(taSize);
                wrkld.setProprietaryTaSyntax(xmlConfig.getBoolean("proprietaryTaSyntax", false));
                wrkld.setIsolationMode(setTenantIDinString(
                        xmlConfig.getString("isolation", "TRANSACTION_SERIALIZABLE"), tenantID));
                wrkld.setScaleFactor(Double
                        .parseDouble(setTenantIDinString(xmlConfig.getString("scalefactor", "1.0"), tenantID)));
                wrkld.setRecordAbortMessages(xmlConfig.getBoolean("recordabortmessages", false));

                int size = xmlConfig.configurationsAt("/works/work").size();

                for (int i = 1; i < size + 1; i++) {
                    SubnodeConfiguration work = xmlConfig.configurationAt("works/work[" + i + "]");
                    List<String> weight_strings;

                    // use a workaround if there multiple workloads or
                    // single
                    // attributed workload
                    if (pluginList.length > 1 || work.containsKey("weights[@bench]")) {
                        weight_strings = get_weights(plugin, work);
                    } else {
                        weight_strings = work.getList("weights[not(@bench)]");
                    }
                    int rate = 1;
                    boolean rateLimited = true;
                    boolean disabled = false;

                    // can be "disabled", "unlimited" or a number
                    String rate_string;
                    rate_string = setTenantIDinString(work.getString("rate[not(@bench)]", ""), tenantID);
                    rate_string = setTenantIDinString(work.getString("rate" + pluginTest, rate_string),
                            tenantID);
                    if (rate_string.equals(RATE_DISABLED)) {
                        disabled = true;
                    } else if (rate_string.equals(RATE_UNLIMITED)) {
                        rateLimited = false;
                    } else if (rate_string.isEmpty()) {
                        LOG.fatal(String.format(
                                "Tenant " + tenantID + ": Please specify the rate for phase %d and workload %s",
                                i, plugin));
                        System.exit(-1);
                    } else {
                        try {
                            rate = Integer.parseInt(rate_string);
                            if (rate < 1) {
                                LOG.fatal("Tenant " + tenantID
                                        + ": Rate limit must be at least 1. Use unlimited or disabled values instead.");
                                System.exit(-1);
                            }
                        } catch (NumberFormatException e) {
                            LOG.fatal(String.format(
                                    "Tenant " + tenantID + ": Rate string must be '%s', '%s' or a number",
                                    RATE_DISABLED, RATE_UNLIMITED));
                            System.exit(-1);
                        }
                    }
                    Phase.Arrival arrival = Phase.Arrival.REGULAR;
                    String arrive = setTenantIDinString(work.getString("@arrival", "regular"), tenantID);
                    if (arrive.toUpperCase().equals("POISSON"))
                        arrival = Phase.Arrival.POISSON;

                    int activeTerminals;
                    activeTerminals = Integer.parseInt(setTenantIDinString(
                            work.getString("active_terminals[not(@bench)]", String.valueOf(terminals)),
                            tenantID));
                    activeTerminals = Integer.parseInt(setTenantIDinString(
                            work.getString("active_terminals" + pluginTest, String.valueOf(activeTerminals)),
                            tenantID));
                    if (activeTerminals > terminals) {
                        LOG.fatal("Tenant " + tenantID + ": Configuration error in work " + i
                                + ": number of active terminals" + ""
                                + "is bigger than the total number of terminals");
                        System.exit(-1);
                    }
                    wrkld.addWork(Integer.parseInt(setTenantIDinString(work.getString("/time"), tenantID)),
                            rate, weight_strings, rateLimited, disabled, activeTerminals, arrival);
                } // FOR

                int numTxnTypes = xmlConfig
                        .configurationsAt("transactiontypes" + pluginTest + "/transactiontype").size();
                if (numTxnTypes == 0 && pluginList.length == 1) {
                    // if it is a single workload run, <transactiontypes />
                    // w/o attribute is used
                    pluginTest = "[not(@bench)]";
                    numTxnTypes = xmlConfig
                            .configurationsAt("transactiontypes" + pluginTest + "/transactiontype").size();
                }
                wrkld.setNumTxnTypes(numTxnTypes);

                // CHECKING INPUT PHASES
                int j = 0;
                for (Phase p : wrkld.getAllPhases()) {
                    j++;
                    if (p.getWeightCount() != wrkld.getNumTxnTypes()) {
                        LOG.fatal(String.format("Tenant " + tenantID
                                + ": Configuration files is inconsistent, phase %d contains %d weights but you defined %d transaction types",
                                j, p.getWeightCount(), wrkld.getNumTxnTypes()));
                        System.exit(-1);
                    }
                } // FOR

                // Generate the dialect map
                wrkld.init();

                assert (wrkld.getNumTxnTypes() >= 0);
                assert (xmlConfig != null);

                // ----------------------------------------------------------------
                // BENCHMARK MODULE
                // ----------------------------------------------------------------

                String classname = pluginConfig.getString("/plugin[@name='" + plugin + "']");

                if (classname == null) {
                    throw new ParseException("Plugin " + plugin + " is undefined in config/plugin.xml");
                }
                BenchmarkModule bench = ClassUtil.newInstance(classname, new Object[] { wrkld },
                        new Class<?>[] { WorkloadConfiguration.class });
                assert (benchList.get(0) != null);

                Map<String, Object> initDebug = new ListOrderedMap<String, Object>();
                initDebug.put("Benchmark", String.format("%s {%s}", plugin.toUpperCase(), classname));
                initDebug.put("Configuration", configFile);
                initDebug.put("Type", wrkld.getDBType());
                initDebug.put("Driver", wrkld.getDBDriver());
                initDebug.put("URL", wrkld.getDBConnection());
                initDebug.put("Isolation", setTenantIDinString(
                        xmlConfig.getString("isolation", "TRANSACTION_SERIALIZABLE [DEFAULT]"), tenantID));
                initDebug.put("Scale Factor", wrkld.getScaleFactor());
                INIT_LOG.info(SINGLE_LINE + "\n\n" + StringUtil.formatMaps(initDebug));
                INIT_LOG.info(SINGLE_LINE);

                // Load TransactionTypes
                List<TransactionType> ttypes = new ArrayList<TransactionType>();

                // Always add an INVALID type for Carlo
                ttypes.add(TransactionType.INVALID);
                int txnIdOffset = lastTxnId;
                for (int i = 1; i < wrkld.getNumTxnTypes() + 1; i++) {
                    String key = "transactiontypes" + pluginTest + "/transactiontype[" + i + "]";
                    String txnName = setTenantIDinString(xmlConfig.getString(key + "/name"), tenantID);
                    int txnId = i + 1;
                    if (xmlConfig.containsKey(key + "/id")) {
                        txnId = Integer
                                .parseInt(setTenantIDinString(xmlConfig.getString(key + "/id"), tenantID));
                    }
                    ttypes.add(bench.initTransactionType(txnName, txnId + txnIdOffset));
                    lastTxnId = i;
                } // FOR
                TransactionTypes tt = new TransactionTypes(ttypes);
                wrkld.setTransTypes(tt);
                if (benchmarkSettings.getBenchmarkSlaFile() != null)
                    wrkld.setSlaFromFile(benchmarkSettings.getBenchmarkSlaFile());
                LOG.debug("Tenant " + tenantID + ": Using the following transaction types: " + tt);

                bench.setTenantOffset(tenantEvents.get(tenantID).getTime(tenEvent));
                bench.setTenantID(tenantID);
                bench.setBenchmarkSettings(benchmarkSettings);
                benchList.add(bench);
            }
        }
    }
    // create result collector
    ResultCollector rCollector = new ResultCollector(tenantList);

    // execute benchmarks in parallel
    ArrayList<Thread> benchThreads = new ArrayList<Thread>();
    for (BenchmarkModule benchmark : benchList) {
        BenchmarkExecutor benchThread = new BenchmarkExecutor(benchmark, argsLine);
        Thread t = new Thread(benchThread);
        t.start();
        benchThreads.add(t);
        benchmark.getWorkloadConfiguration().setrCollector(rCollector);
    }

    // waiting for completion of all benchmarks
    for (Thread t : benchThreads) {
        t.join();
    }

    // print statistics
    int analysisBuckets = -1;
    if (argsLine.hasOption("analysis-buckets"))
        analysisBuckets = Integer.parseInt(argsLine.getOptionValue("analysis-buckets"));
    String output = null;
    if (argsLine.hasOption("o"))
        output = argsLine.getOptionValue("o");
    String baseline = null;
    if (argsLine.hasOption("b"))
        baseline = argsLine.getOptionValue("b");

    rCollector.printStatistics(output, analysisBuckets, argsLine.hasOption("histograms"), baseline);

    // create GUI
    if (argsLine.hasOption("g") && (!rCollector.getAllResults().isEmpty())) {
        try {
            Gui gui = new Gui(Integer.parseInt(argsLine.getOptionValue("analysis-buckets", "10")), rCollector,
                    output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static int parseStringToInt(String sContent, int iDefaultValue) {
    try {//from   ww w  . j  a v a2 s  . c o  m
        int iRes = Integer.parseInt(sContent);
        return iRes;
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return iDefaultValue;
    }
}

From source file:Main.java

public static boolean isNumber(String n) {
    try {//  w w  w. j  av  a2  s  .c om
        double d = Double.valueOf(n).doubleValue();
        return true;
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static int getPhoneSDKInt() {
    int version = 0;
    try {/*w  w  w . j  a  v a2  s .c  om*/
        version = Integer.valueOf(android.os.Build.VERSION.SDK);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return version;
}

From source file:Main.java

public static String binaryStringToDecimalString(String octal) {
    String decimal = "0";
    try {//w w w .j  a va2s.c o  m
        decimal = Integer.valueOf(octal, 2).toString();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return decimal;
}

From source file:Main.java

public static int hexStringToOctalString(String hexString) {
    int result = 0;
    try {/*from  www.j a va2  s. c  om*/
        result = Integer.valueOf(hexString, 16);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return result;
}