Example usage for java.util Map get

List of usage examples for java.util Map get

Introduction

In this page you can find the example usage for java.util Map get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:com.joliciel.talismane.fr.TalismaneFrench.java

/**
 * @param args//  w  w  w . j a va  2 s .c  om
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    Map<String, String> argsMap = TalismaneConfig.convertArgs(args);
    CorpusFormat corpusReaderType = null;
    String treebankDirPath = null;
    boolean keepCompoundPosTags = true;

    if (argsMap.containsKey("corpusReader")) {
        corpusReaderType = CorpusFormat.valueOf(argsMap.get("corpusReader"));
        argsMap.remove("corpusReader");
    }
    if (argsMap.containsKey("treebankDir")) {
        treebankDirPath = argsMap.get("treebankDir");
        argsMap.remove("treebankDir");
    }
    if (argsMap.containsKey("keepCompoundPosTags")) {
        keepCompoundPosTags = argsMap.get("keepCompoundPosTags").equalsIgnoreCase("true");
        argsMap.remove("keepCompoundPosTags");
    }

    Extensions extensions = new Extensions();
    extensions.pluckParameters(argsMap);

    TalismaneFrench talismaneFrench = new TalismaneFrench();

    TalismaneConfig config = new TalismaneConfig(argsMap, talismaneFrench);
    if (config.getCommand() == null)
        return;

    if (corpusReaderType != null) {
        if (corpusReaderType == CorpusFormat.ftbDep) {
            File inputFile = new File(config.getInFilePath());
            FtbDepReader ftbDepReader = new FtbDepReader(inputFile, config.getInputCharset());

            ftbDepReader.setKeepCompoundPosTags(keepCompoundPosTags);
            ftbDepReader.setPredictTransitions(config.isPredictTransitions());

            config.setParserCorpusReader(ftbDepReader);
            config.setPosTagCorpusReader(ftbDepReader);
            config.setTokenCorpusReader(ftbDepReader);

            if (config.getCommand().equals(Command.compare)) {
                File evaluationFile = new File(config.getEvaluationFilePath());
                FtbDepReader ftbDepEvaluationReader = new FtbDepReader(evaluationFile,
                        config.getInputCharset());
                ftbDepEvaluationReader.setKeepCompoundPosTags(keepCompoundPosTags);
                config.setParserEvaluationCorpusReader(ftbDepEvaluationReader);
                config.setPosTagEvaluationCorpusReader(ftbDepEvaluationReader);
            }
        } else if (corpusReaderType == CorpusFormat.ftb) {
            TalismaneServiceLocator talismaneServiceLocator = TalismaneServiceLocator.getInstance();
            TreebankServiceLocator treebankServiceLocator = TreebankServiceLocator
                    .getInstance(talismaneServiceLocator);
            TreebankUploadService treebankUploadService = treebankServiceLocator
                    .getTreebankUploadServiceLocator().getTreebankUploadService();
            TreebankExportService treebankExportService = treebankServiceLocator
                    .getTreebankExportServiceLocator().getTreebankExportService();
            File treebankFile = new File(treebankDirPath);
            TreebankReader treebankReader = treebankUploadService.getXmlReader(treebankFile);

            // we prepare both the tokeniser and pos-tag readers, just in case they are needed
            InputStream posTagMapStream = talismaneFrench.getDefaultPosTagMapFromStream();
            Scanner scanner = new Scanner(posTagMapStream, "UTF-8");
            List<String> descriptors = new ArrayList<String>();
            while (scanner.hasNextLine())
                descriptors.add(scanner.nextLine());
            FtbPosTagMapper ftbPosTagMapper = treebankExportService.getFtbPosTagMapper(descriptors,
                    talismaneFrench.getDefaultPosTagSet());
            PosTagAnnotatedCorpusReader posTagAnnotatedCorpusReader = treebankExportService
                    .getPosTagAnnotatedCorpusReader(treebankReader, ftbPosTagMapper, keepCompoundPosTags);
            config.setPosTagCorpusReader(posTagAnnotatedCorpusReader);

            TokeniserAnnotatedCorpusReader tokenCorpusReader = treebankExportService
                    .getTokeniserAnnotatedCorpusReader(treebankReader, ftbPosTagMapper, keepCompoundPosTags);
            config.setTokenCorpusReader(tokenCorpusReader);

            SentenceDetectorAnnotatedCorpusReader sentenceCorpusReader = treebankExportService
                    .getSentenceDetectorAnnotatedCorpusReader(treebankReader);
            config.setSentenceCorpusReader(sentenceCorpusReader);
        } else if (corpusReaderType == CorpusFormat.conll || corpusReaderType == CorpusFormat.spmrl) {
            File inputFile = new File(config.getInFilePath());

            ParserRegexBasedCorpusReader corpusReader = config.getParserService()
                    .getRegexBasedCorpusReader(inputFile, config.getInputCharset());

            corpusReader.setPredictTransitions(config.isPredictTransitions());

            config.setParserCorpusReader(corpusReader);
            config.setPosTagCorpusReader(corpusReader);
            config.setTokenCorpusReader(corpusReader);

            if (corpusReaderType == CorpusFormat.spmrl) {
                corpusReader.setRegex(
                        "%INDEX%\\t%TOKEN%\\t.*\\t.*\\t%POSTAG%\\t.*\\t.*\\t.*\\t%GOVERNOR%\\t%LABEL%");
            }

            if (config.getCommand().equals(Command.compare)) {
                File evaluationFile = new File(config.getEvaluationFilePath());
                ParserRegexBasedCorpusReader evaluationReader = config.getParserService()
                        .getRegexBasedCorpusReader(evaluationFile, config.getInputCharset());
                config.setParserEvaluationCorpusReader(evaluationReader);
                config.setPosTagEvaluationCorpusReader(evaluationReader);
            }
        } else {
            throw new TalismaneException("Unknown corpusReader: " + corpusReaderType);
        }
    }
    Talismane talismane = config.getTalismane();

    extensions.prepareCommand(config, talismane);

    talismane.process();
}

From source file:com.datatorrent.stram.StreamingAppMaster.java

/**
 * @param args/*from   w  w w  .j  av a 2s.  c  om*/
 *          Command line args
 * @throws Throwable
 */
public static void main(final String[] args) throws Throwable {
    StdOutErrLog.tieSystemOutAndErrToLog();
    LOG.info("Master starting with classpath: {}", System.getProperty("java.class.path"));

    LOG.info("version: {}", VersionInfo.APEX_VERSION.getBuildVersion());
    StringWriter sw = new StringWriter();
    for (Map.Entry<String, String> e : System.getenv().entrySet()) {
        sw.append("\n").append(e.getKey()).append("=").append(e.getValue());
    }
    LOG.info("appmaster env:" + sw.toString());

    Options opts = new Options();
    opts.addOption("app_attempt_id", true, "App Attempt ID. Not to be used unless for testing purposes");

    opts.addOption("help", false, "Print usage");
    CommandLine cliParser = new GnuParser().parse(opts, args);

    // option "help" overrides and cancels any run
    if (cliParser.hasOption("help")) {
        new HelpFormatter().printHelp("ApplicationMaster", opts);
        return;
    }

    Map<String, String> envs = System.getenv();
    ApplicationAttemptId appAttemptID = Records.newRecord(ApplicationAttemptId.class);
    if (!envs.containsKey(Environment.CONTAINER_ID.name())) {
        if (cliParser.hasOption("app_attempt_id")) {
            String appIdStr = cliParser.getOptionValue("app_attempt_id", "");
            appAttemptID = ConverterUtils.toApplicationAttemptId(appIdStr);
        } else {
            throw new IllegalArgumentException("Application Attempt Id not set in the environment");
        }
    } else {
        ContainerId containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));
        appAttemptID = containerId.getApplicationAttemptId();
    }

    boolean result = false;
    StreamingAppMasterService appMaster = null;
    try {
        appMaster = new StreamingAppMasterService(appAttemptID);
        LOG.info("Initializing Application Master.");

        Configuration conf = new YarnConfiguration();
        appMaster.init(conf);
        appMaster.start();
        result = appMaster.run();
    } catch (Throwable t) {
        LOG.error("Exiting Application Master", t);
        System.exit(1);
    } finally {
        if (appMaster != null) {
            appMaster.stop();
        }
    }

    if (result) {
        LOG.info("Application Master completed.");
        System.exit(0);
    } else {
        LOG.info("Application Master failed.");
        System.exit(2);
    }
}

From source file:EVT.java

/**
 * @param args/*from   w w  w . ja v  a  2  s.  com*/
 */
public static void main(final String[] args) throws MalformedURLException {
    try {
        URL url = new EVT().getClass().getClassLoader().getResource("general.properties");
        config = new PropertiesConfiguration(url);

    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    final Map parameters = parseParameters(args);

    if (parameters.containsKey("-v"))
        verboseMode = 1;
    if (parameters.containsKey("-V") || parameters.containsKey("-vv"))
        verboseMode = 2;
    //the from version and the to version must be put in property file
    String supportedVersions = PropertiesUtil.concatenatePropsValues(config, ALFRESCO_VERSION, ",");
    List<String> supportedVersionsList = Arrays.asList(supportedVersions.split(","));
    String alfrescoVersion = (String) parameters.get(ALFRESCO_VERSION);
    boolean supportedVersion = (alfrescoVersion != null) && supportedVersionsList.contains(alfrescoVersion);

    System.out.println(
            "\nAlfresco Environment Validation Tool (for Alfresco Enterprise " + supportedVersions + ")");
    System.out.println("------------------------------------------------------------------");

    if (parameters.isEmpty() || parameters.containsKey("-?") || parameters.containsKey("--help")
            || parameters.containsKey("/?") || !parameters.containsKey(DBValidator.PARAMETER_DATABASE_TYPE)
            || !parameters.containsKey(DBValidator.PARAMETER_DATABASE_HOSTNAME)
            || !parameters.containsKey(DBValidator.PARAMETER_DATABASE_LOGIN)
            || !parameters.containsKey(ALFRESCO_VERSION)
            || !parameters.containsKey(IndexDiskSpeedValidator.PARAMETER_DISK_LOCATION)) {
        System.out.println("");
        System.out.println("usage: evt[.sh|.cmd] [-?|--help] [-v] [-V|-vv]");
        System.out.println("            -a alfrescoversion -t databaseType -h databaseHost [-r databasePort]");
        System.out.println(
                "            [-d databaseName] -l databaseLogin [-p databasePassword] -i indexlocation");
        System.out.println("");
        System.out.println("where:      -?|--help        - display this help");
        System.out.println("            -v               - produce verbose output");
        System.out.println("            -V|-vv           - produce super-verbose output (stack traces)");
        System.out.println(
                "            alfrescoversion  - Version for which the verification is made .  May be one of:");
        System.out.println(
                "                               4.0.0,4.0.1,4.0.2,4.1.1,4.1.2,4.1.3,4.1.4,4.1.5,4.1.6,4.2");
        System.out.println("            databaseType     - the type of database.  May be one of:");
        System.out.println("                               mysql, postgresql, oracle, mssqlserver, db2");
        System.out.println("            databaseHost     - the hostname of the database server");
        System.out.println("            databasePort     - the port the database is listening on (optional -");
        System.out.println("                               defaults to default for the database type)");
        System.out.println("            databaseName     - the name of the Alfresco database (optional -");
        System.out.println("                               defaults to 'alfresco')");
        System.out.println("            databaseLogin    - the login Alfresco will use to connect to the");
        System.out.println("                               database");
        System.out.println("            databasePassword - the password for that user (optional)");
        System.out.println(
                "            indexlocation    - a path to a folder that will contain Alfresco indexes");
        System.out.println("");
        System.out.println("The tool must be run as the OS user that Alfreso will run as.  In particular");
        System.out.println("it will report erroneous results if run as \"root\" (or equivalent on other");
        System.out.println("OSes) if Alfresco is not intended to be run as that user.");
        System.out.println("");
    } else if (!supportedVersion) {
        System.out.println("");
        System.out.println("Version " + alfrescoVersion
                + " is not in the list of the Alfresco versions supported by this tool.");
        System.out.println("Please specify one of the following versions: " + supportedVersions);
    } else {
        final StdoutValidatorCallback callback = new StdoutValidatorCallback();

        (new AllValidators()).validate(parameters, callback);

        System.out.println("\n\n                         **** FINAL GRADE: "
                + TestResult.typeToString(callback.worstResult) + " ****\n");
    }
}

From source file:RandomGenTest.java

public static void main(String[] args) {
    RandomDataGenerator rand = new RandomDataGenerator();

    Map<Object, Integer> map = new HashMap();
    for (int i = 0; i < 101; i++)
        map.put(i, 0);/*from  w ww . ja v  a  2s.  co m*/

    Map<Object, Integer> user = new HashMap();
    for (int i = 0; i < 100; i++) {
        int age = rand.nextInt(0, 100);
        /*int age = (int) rand.nextGaussian(50, 10D);
        if ((age > 100) || (age < 0)) {
           age = rand.nextInt(0, 100);
        }*/
        user.put(i, age);
    }

    for (int i = 0; i < 100000; i++) {
        //int age = rand.nextInt(0, 100);
        /*int age = (int) rand.nextGaussian(50, 10D);
        if ((age > 100) || (age < 0)) {
           age = rand.nextInt(0, 100);
        }*/

        //Seq
        //map.put(user.get(i%100), map.get(user.get(i%100))+1);

        //Rand
        int j = rand.nextInt(0, 99);
        map.put(user.get(j), map.get(user.get(j)) + 1);
    }

    display(map);
}

From source file:com.jwm123.loggly.reporter.AppLauncher.java

public static void main(String args[]) throws Exception {
    try {/* w  ww . j  a  v a 2s.co  m*/
        CommandLine cl = parseCLI(args);
        try {
            config = new Configuration();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("ERROR: Failed to read in persisted configuration.");
        }
        if (cl.hasOption("h")) {

            HelpFormatter help = new HelpFormatter();
            String jarName = AppLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            if (jarName.contains("/")) {
                jarName = jarName.substring(jarName.lastIndexOf("/") + 1);
            }
            help.printHelp("java -jar " + jarName + " [options]", opts);
        }
        if (cl.hasOption("c")) {
            config.update();
        }
        if (cl.hasOption("q")) {
            Client client = new Client(config);
            client.setQuery(cl.getOptionValue("q"));
            if (cl.hasOption("from")) {
                client.setFrom(cl.getOptionValue("from"));
            }
            if (cl.hasOption("to")) {
                client.setTo(cl.getOptionValue("to"));
            }
            List<Map<String, Object>> report = client.getReport();

            if (report != null) {
                List<Map<String, String>> reportContent = new ArrayList<Map<String, String>>();
                ReportGenerator generator = null;
                if (cl.hasOption("file")) {
                    generator = new ReportGenerator(new File(cl.getOptionValue("file")));
                }
                byte reportFile[] = null;

                if (cl.hasOption("g")) {
                    System.out.println("Search results: " + report.size());
                    Set<Object> values = new TreeSet<Object>();
                    Map<Object, Integer> counts = new HashMap<Object, Integer>();
                    for (String groupBy : cl.getOptionValues("g")) {
                        for (Map<String, Object> result : report) {
                            if (mapContains(result, groupBy)) {
                                Object value = mapGet(result, groupBy);
                                values.add(value);
                                if (counts.containsKey(value)) {
                                    counts.put(value, counts.get(value) + 1);
                                } else {
                                    counts.put(value, 1);
                                }
                            }
                        }
                        System.out.println("For key: " + groupBy);
                        for (Object value : values) {
                            System.out.println("  " + value + ": " + counts.get(value));
                        }
                    }
                    if (cl.hasOption("file")) {
                        Map<String, String> reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Month", MONTH_FORMAT.format(new Date()));
                        reportContent.add(reportAddition);
                        for (Object value : values) {
                            reportAddition = new LinkedHashMap<String, String>();
                            reportAddition.put(value.toString(), "" + counts.get(value));
                            reportContent.add(reportAddition);
                        }
                        reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Total", "" + report.size());
                        reportContent.add(reportAddition);
                    }
                } else {
                    System.out.println("The Search [" + cl.getOptionValue("q") + "] yielded " + report.size()
                            + " results.");
                    if (cl.hasOption("file")) {
                        Map<String, String> reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Month", MONTH_FORMAT.format(new Date()));
                        reportContent.add(reportAddition);
                        reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Count", "" + report.size());
                        reportContent.add(reportAddition);
                    }
                }
                if (cl.hasOption("file")) {
                    reportFile = generator.build(reportContent);
                    File reportFileObj = new File(cl.getOptionValue("file"));
                    FileUtils.writeByteArrayToFile(reportFileObj, reportFile);
                    if (cl.hasOption("e")) {
                        ReportMailer mailer = new ReportMailer(config, cl.getOptionValues("e"),
                                cl.getOptionValue("s"), reportFileObj.getName(), reportFile);
                        mailer.send();
                    }
                }
            }
        }

    } catch (IllegalArgumentException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}

From source file:kafka.benchmark.AdvertisingTopology.java

public static void main(final String[] args) throws Exception {
    Options opts = new Options();
    opts.addOption("conf", true, "Path to the config file.");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(opts, args);
    String configPath = cmd.getOptionValue("conf");
    Map<?, ?> conf = Utils.findAndReadConfigFile(configPath, true);

    Map<String, ?> benchmarkParams = getKafkaConfs(conf);

    LOG.info("conf: {}", conf);
    LOG.info("Parameters used: {}", benchmarkParams);

    KStreamBuilder builder = new KStreamBuilder();
    //        builder.addStateStore(
    //                Stores.create("config-params")
    //                .withStringKeys().withStringValues()
    //                .inMemory().maxEntries(10).build(), "redis");

    String topicsArr[] = { (String) benchmarkParams.get("topic") };
    KStream<String, ?> source1 = builder.stream(topicsArr);

    Properties props = new Properties();
    props.putAll(benchmarkParams);/*w  ww . java2 s  .  c om*/
    //        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-benchmarks");
    //        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    ////        props.put(StreamsConfig.ZOOKEEPER_CONNECT_CONFIG, "localhost:2181");
    props.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
    props.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());

    // setting offset reset to earliest so that we can re-run the demo code with the same pre-loaded data
    //        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

    source1
            // Parse the String as JSON
            .mapValues(input -> {
                JSONObject obj = new JSONObject(input.toString());
                //System.out.println(obj.toString());
                String[] tuple = { obj.getString("user_id"), obj.getString("page_id"), obj.getString("ad_id"),
                        obj.getString("ad_type"), obj.getString("event_type"), obj.getString("event_time"),
                        obj.getString("ip_address") };
                return tuple;
            })

            // Filter the records if event type is "view"
            .filter(new Predicate<String, String[]>() {
                @Override
                public boolean test(String key, String[] value) {
                    return value[4].equals("view"); // "event_type"
                }
            })

            // project the event
            .mapValues(input -> {
                String[] arr = (String[]) input;
                return new String[] { arr[2], arr[5] }; // "ad_id" and "event_time"
            })

            // perform join with redis data
            .transformValues(new RedisJoinBolt(benchmarkParams)).filter((key, value) -> value != null)

            // create key from value
            .map((key, value) -> {
                String[] arr = (String[]) value;
                return new KeyValue<String, String[]>(arr[0], arr);
            })

            // process campaign
            .aggregateByKey(new Initializer<List<String[]>>() {
                @Override
                public List<String[]> apply() {
                    return new ArrayList<String[]>();
                }
            }, new Aggregator<String, String[], List<String[]>>() {
                @Override
                public List<String[]> apply(String aggKey, String[] value, List<String[]> aggregate) {
                    aggregate.add(value);
                    return aggregate;
                }
            },
                    // UnlimitedWindows.of("kafka-test-unlim"),
                    // HoppingWindows.of("kafka-test-hopping").with(12L).every(5L),
                    TumblingWindows.of("kafka-test-tumbling").with(WINDOW_SIZE), strSerde, arrSerde)
            .toStream().process(new CampaignProcessor(benchmarkParams));

    KafkaStreams streams = new KafkaStreams(builder, props);
    streams.start();
}

From source file:mase.MaseEvolve.java

public static void main(String[] args) throws Exception {
    File outDir = getOutDir(args);
    boolean force = Arrays.asList(args).contains(FORCE);
    if (!outDir.exists()) {
        outDir.mkdirs();/* www.  jav  a2  s .c  o  m*/
    } else if (!force) {
        System.out.println("Folder already exists: " + outDir.getAbsolutePath() + ". Waiting 5 sec.");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
        }
    }

    // Get config file
    Map<String, String> pars = readParams(args);

    // Copy config to outdir
    try {
        File rawConfig = writeConfig(args, pars, outDir, false);
        File destiny = new File(outDir, DEFAULT_CONFIG);
        destiny.delete();
        FileUtils.moveFile(rawConfig, new File(outDir, DEFAULT_CONFIG));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // JBOT INTEGRATION: copy jbot config file to the outdir
    // Does nothing when jbot is not used
    if (pars.containsKey("problem.jbot-config")) {
        File jbot = new File(pars.get("problem.jbot-config"));
        FileUtils.copyFile(jbot, new File(outDir, jbot.getName()));
    }

    // Write config to system temp file
    File config = writeConfig(args, pars, outDir, true);
    // Launch
    launchExperiment(config);
}

From source file:com.curecomp.primefaces.migrator.PrimefacesMigration.java

public static void main(String[] args) throws Exception {
    // Let's use some colors :)
    //        AnsiConsole.systemInstall();
    CommandLineParser cliParser = new BasicParser();
    CommandLine cli = null;/* www  . j  a v a 2 s  .  c  o  m*/
    try {
        cli = cliParser.parse(OPTIONS, args);
    } catch (ParseException e) {
        printHelp();
    }
    if (!cli.hasOption("s")) {
        printHelp();
    }

    String sourcePattern;
    if (cli.hasOption("p")) {
        sourcePattern = cli.getOptionValue("p");
    } else {
        sourcePattern = DEFAULT_SOURCE_PATTERN;
    }

    String defaultAnswer;
    if (cli.hasOption("default-answer")) {
        defaultAnswer = cli.getOptionValue("default-answer");
    } else {
        defaultAnswer = DEFAULT_DEFAULT_PROMPT_ANSWER;
    }

    boolean defaultAnswerYes = defaultAnswer.equalsIgnoreCase("y");
    boolean quiet = cli.hasOption("q");
    boolean testWrite = cli.hasOption("t");
    Path sourceDirectory = Paths.get(cli.getOptionValue("s")).toAbsolutePath();
    // Since we use IO we will have some blocking threads hanging around
    int threadCount = Runtime.getRuntime().availableProcessors() * 2;
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>());
    BlockingQueue<WidgetVarLocation> foundUsages = new LinkedBlockingQueue<>();
    BlockingQueue<WidgetVarLocation> unusedOrAmbiguous = new LinkedBlockingQueue<>();
    BlockingQueue<WidgetVarLocation> skippedUsages = new LinkedBlockingQueue<>();
    List<Future<?>> futures = new ArrayList<>();

    findWidgetVars(sourceDirectory, sourcePattern, threadPool).forEach(widgetVarLocation -> {
        // We can't really find usages of widget vars that use EL expressions :(
        if (widgetVarLocation.widgetVar.contains("#")) {
            unusedOrAmbiguous.add(widgetVarLocation);
            return;
        }

        try {
            FileActionVisitor visitor = new FileActionVisitor(sourceDirectory, sourcePattern,
                    sourceFile -> futures.add(threadPool.submit((Callable<?>) () -> {
                        findWidgetVarUsages(sourceFile, widgetVarLocation, foundUsages, skippedUsages,
                                unusedOrAmbiguous);
                        return null;
                    })));

            Files.walkFileTree(sourceDirectory, visitor);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    });

    awaitAll(futures);

    new TreeSet<>(skippedUsages).forEach(widgetUsage -> {
        int startIndex = widgetUsage.columnNr;
        int endIndex = startIndex + widgetUsage.widgetVar.length();
        String relativePath = widgetUsage.location.toAbsolutePath().toString()
                .substring(sourceDirectory.toString().length());
        String previous = replace(widgetUsage.line, startIndex, endIndex,
                Ansi.ansi().bold().fg(Ansi.Color.RED).a(widgetUsage.widgetVar).reset().toString());
        System.out.println("Skipped " + relativePath + " at line " + widgetUsage.lineNr + " and col "
                + widgetUsage.columnNr + " for widgetVar '" + widgetUsage.widgetVar + "'");
        System.out.println("\t" + previous);
    });

    Map<WidgetVarLocation, List<WidgetVarLocation>> written = new HashMap<>();

    new TreeSet<>(foundUsages).forEach(widgetUsage -> {
        WidgetVarLocation key = new WidgetVarLocation(null, widgetUsage.location, widgetUsage.lineNr, -1, null);
        List<WidgetVarLocation> writtenList = written.get(key);
        int existing = writtenList == null ? 0 : writtenList.size();
        int startIndex = widgetUsage.columnNr;
        int endIndex = startIndex + widgetUsage.widgetVar.length();
        String relativePath = widgetUsage.location.toAbsolutePath().toString()
                .substring(sourceDirectory.toString().length());
        String next = replace(widgetUsage.line, startIndex, endIndex, Ansi.ansi().bold().fg(Ansi.Color.RED)
                .a("PF('" + widgetUsage.widgetVar + "')").reset().toString());
        System.out
                .println(relativePath + " at line " + widgetUsage.lineNr + " and col " + widgetUsage.columnNr);
        System.out.println("\t" + next);
        System.out.print("Replace (Y/N)? [" + (defaultAnswerYes ? "Y" : "N") + "]: ");

        String input;

        if (quiet) {
            input = "";
            System.out.println();
        } else {
            try {
                do {
                    input = in.readLine();
                } while (input != null && !input.isEmpty() && !"y".equalsIgnoreCase(input)
                        && !"n".equalsIgnoreCase(input));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        if (input == null) {
            System.out.println("Aborted!");
        } else if (input.isEmpty() && defaultAnswerYes || !input.isEmpty() && !"n".equalsIgnoreCase(input)) {
            System.out.println("Replaced!");
            System.out.print("\t");
            if (writtenList == null) {
                writtenList = new ArrayList<>();
                written.put(key, writtenList);
            }

            writtenList.add(widgetUsage);
            List<String> lines;
            try {
                lines = Files.readAllLines(widgetUsage.location);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

            try (OutputStream os = testWrite ? new ByteArrayOutputStream()
                    : Files.newOutputStream(widgetUsage.location);
                    PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) {
                String line;

                for (int i = 0; i < lines.size(); i++) {
                    int lineNr = i + 1;
                    line = lines.get(i);

                    if (lineNr == widgetUsage.lineNr) {
                        int begin = widgetUsage.columnNr + (testWrite ? 0 : existing * 6);
                        int end = begin + widgetUsage.widgetVar.length();
                        String newLine = replace(line, begin, end, "PF('" + widgetUsage.widgetVar + "')",
                                false);

                        if (testWrite) {
                            System.out.println(newLine);
                        } else {
                            pw.println(newLine);
                        }
                    } else {
                        if (!testWrite) {
                            pw.println(line);
                        }
                    }
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            System.out.println("Skipped!");
        }
    });

    new TreeSet<>(unusedOrAmbiguous).forEach(widgetUsage -> {
        int startIndex = widgetUsage.columnNr;
        int endIndex = startIndex + widgetUsage.widgetVar.length();
        String relativePath = widgetUsage.location.toAbsolutePath().toString()
                .substring(sourceDirectory.toString().length());
        String previous = replace(widgetUsage.line, startIndex, endIndex,
                Ansi.ansi().bold().fg(Ansi.Color.RED).a(widgetUsage.widgetVar).reset().toString());
        System.out.println("Skipped unused or ambiguous " + relativePath + " at line " + widgetUsage.lineNr
                + " and col " + widgetUsage.columnNr);
        System.out.println("\t" + previous);
    });

    threadPool.shutdown();
}

From source file:com.tengen.MultiArrayFindTest.java

public static void main(String[] args) throws UnknownHostException {

    MongoClient client = new MongoClient();
    DB db = client.getDB("school");
    DBCollection collection = db.getCollection("students");

    System.out.println("Find one:");
    DBObject one = collection.findOne();
    System.out.println(one);//from  w w w .  ja  va2  s .  co  m

    System.out.println("\nFind all: ");
    DBCursor cursor = collection.find().sort(new BasicDBObject("_id", 1));
    System.out.println(cursor.count());

    try {
        while (cursor.hasNext()) {

            int id = (Integer) cursor.next().get("_id");
            //String s =  cursor.next().get("name");

            Map<Integer, String> myMap = new HashMap<Integer, String>();

            BasicBSONList bl = (BasicBSONList) cursor.next().get("scores");
            for (Object bo : bl) {

                BasicBSONObject bo1 = (BasicBSONObject) bo;
                System.out.println(bo);
                System.out.println(Integer.toString(id));

                if (1 > 1) {
                }
                double total1 = Double.parseDouble(bo1.get("score").toString());
                System.out.println("score1: " + total1);

                myMap.put(id, bo1.get("score").toString());
                System.out.println("score: " + myMap.get(id));

                double total = Double.parseDouble(myMap.get(id).toString());
                System.out.println("score: " + total);

                //}
            }

        }

    } finally {
        cursor.close();
    }

    System.out.println("\nCount:");
    long count = collection.count();
    System.out.println(count);
}

From source file:examples.cnn.ImagesClassification.java

public static void main(String[] args) {

    SparkConf conf = new SparkConf();
    conf.setAppName("Images CNN Classification");
    conf.setMaster(String.format("local[%d]", NUM_CORES));
    conf.set(SparkDl4jMultiLayer.AVERAGE_EACH_ITERATION, String.valueOf(true));

    try (JavaSparkContext sc = new JavaSparkContext(conf)) {

        JavaRDD<String> raw = sc.textFile("data/images-data-rgb.csv");
        String first = raw.first();

        JavaPairRDD<String, String> labelData = raw.filter(f -> f.equals(first) == false).mapToPair(r -> {
            String[] tab = r.split(";");
            return new Tuple2<>(tab[0], tab[1]);
        });/*from   w w  w .  ja  va  2  s  .  co  m*/

        Map<String, Long> labels = labelData.map(t -> t._1).distinct().zipWithIndex()
                .mapToPair(t -> new Tuple2<>(t._1, t._2)).collectAsMap();

        log.info("Number of labels {}", labels.size());
        labels.forEach((a, b) -> log.info("{}: {}", a, b));

        NetworkTrainer trainer = new NetworkTrainer.Builder().model(ModelLibrary.net1)
                .networkToSparkNetwork(net -> new SparkDl4jMultiLayer(sc, net)).numLabels(labels.size())
                .cores(NUM_CORES).build();

        JavaRDD<Tuple2<INDArray, double[]>> labelsWithData = labelData.map(t -> {
            INDArray label = FeatureUtil.toOutcomeVector(labels.get(t._1).intValue(), labels.size());
            double[] arr = Arrays.stream(t._2.split(" ")).map(normalize1).mapToDouble(Double::doubleValue)
                    .toArray();
            return new Tuple2<>(label, arr);
        });

        JavaRDD<Tuple2<INDArray, double[]>>[] splited = labelsWithData.randomSplit(new double[] { .8, .2 },
                seed);

        JavaRDD<DataSet> testDataset = splited[1].map(t -> {
            INDArray features = Nd4j.create(t._2, new int[] { 1, t._2.length });
            return new DataSet(features, t._1);
        }).cache();
        log.info("Number of test images {}", testDataset.count());

        JavaRDD<DataSet> plain = splited[0].map(t -> {
            INDArray features = Nd4j.create(t._2, new int[] { 1, t._2.length });
            return new DataSet(features, t._1);
        });

        /*
         * JavaRDD<DataSet> flipped = splited[0].randomSplit(new double[] { .5, .5 }, seed)[0].
         */
        JavaRDD<DataSet> flipped = splited[0].map(t -> {
            double[] arr = t._2;
            int idx = 0;
            double[] farr = new double[arr.length];
            for (int i = 0; i < arr.length; i += trainer.width) {
                double[] temp = Arrays.copyOfRange(arr, i, i + trainer.width);
                ArrayUtils.reverse(temp);
                for (int j = 0; j < trainer.height; ++j) {
                    farr[idx++] = temp[j];
                }
            }
            INDArray features = Nd4j.create(farr, new int[] { 1, farr.length });
            return new DataSet(features, t._1);
        });

        JavaRDD<DataSet> trainDataset = plain.union(flipped).cache();
        log.info("Number of train images {}", trainDataset.count());

        trainer.train(trainDataset, testDataset);
    }
}