Example usage for java.util Scanner hasNextLine

List of usage examples for java.util Scanner hasNextLine

Introduction

In this page you can find the example usage for java.util Scanner hasNextLine.

Prototype

public boolean hasNextLine() 

Source Link

Document

Returns true if there is another line in the input of this scanner.

Usage

From source file:com.joliciel.jochre.Jochre.java

/**
 * Apply a set of images to a given image or a given shape.
 * @param imageId// w ww .  ja va  2  s. c  o  m
 * @param shapeId
 */
public void doCommandApplyFeatures(int imageId, int shapeId, String letterFeatureFilePath) {
    try {
        if (letterFeatureFilePath.length() == 0)
            throw new RuntimeException("Missing argument: letterFeatures");
        LetterFeatureTester featureTester = letterFeatureService.getFeatureTester();

        File letterFeatureFile = new File(letterFeatureFilePath);
        Scanner scanner = new Scanner(letterFeatureFile);

        List<String> descriptors = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String descriptor = scanner.nextLine();
            descriptors.add(descriptor);
            LOG.debug(descriptor);
        }
        Set<LetterFeature<?>> features = letterFeatureService.getLetterFeatureSet(descriptors);
        Set<String> letters = new HashSet<String>();
        //         letters.add("");
        //         letters.add("");
        featureTester.applyFeatures(features, letters, imageId, shapeId);
    } catch (FileNotFoundException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:ch.puzzle.itc.mobiliar.business.deploy.boundary.DeploymentBoundary.java

public String getDeploymentLog(String logName) throws IllegalAccessException {
    String logsPath = ConfigurationService.getProperty(ConfigKey.LOGS_PATH);
    if (logName.contains(File.separator)) {
        throw new IllegalAccessException("The log file contains a file separator (\"" + File.separator
                + "\"). For security reasons, this is not permitted!");
    }/*from w w  w  .j  a va2s.  com*/

    StringBuilder content = new StringBuilder();
    Scanner scanner;
    try {
        scanner = new Scanner(new FileInputStream(logsPath + File.separator + logName));
        try {
            while (scanner.hasNextLine()) {
                content.append(scanner.nextLine()).append('\n');
            }
        } finally {
            scanner.close();
        }
        return content.toString();
    } catch (FileNotFoundException e) {
        String message = "The file " + logsPath + File.separator + logName
                + " was found, but couldn't be read!";
        log.log(Level.WARNING, message);
        throw new AMWRuntimeException(message, e);
    }

}

From source file:org.j2free.config.ConfigurationListener.java

/**
 *
 * @param event//from   w w  w .  ja  v  a  2  s .  co m
 */
public synchronized void contextInitialized(ServletContextEvent event) {
    context = event.getServletContext();

    // Get the configuration file
    String configPathTemp = (String) context.getInitParameter(INIT_PARAM_CONFIG_PATH);

    // Use the default path if it wasn't specified
    if (StringUtils.isBlank(configPathTemp))
        configPathTemp = DEFAULT_CONFIG_PATH;

    // Finalize the config path (needs to be final for inner-Runnable below)
    final String configPath = configPathTemp;
    context.setAttribute(CONTEXT_ATTR_CONFIG_PATH, configPath);

    try {
        // Load the configuration
        DefaultConfigurationBuilder configBuilder = new DefaultConfigurationBuilder();
        configBuilder.setFileName(configPath);

        final CombinedConfiguration config = configBuilder.getConfiguration(true);

        // Save the config where we can get at it later
        context.setAttribute(CONTEXT_ATTR_CONFIG, config);
        Global.put(CONTEXT_ATTR_CONFIG, config);

        // Determine the localhost
        String localhost = config.getString(PROP_LOCALHOST, "ip");
        if (localhost.equalsIgnoreCase("ip")) {
            try {
                localhost = InetAddress.getLocalHost().getHostAddress();
                log.info("Using localhost: " + localhost);
            } catch (Exception e) {
                log.warn("Error determining localhost", e);
                localhost = "localhost";
            }
        }

        context.setAttribute(CONTEXT_ATTR_LOCALHOST, localhost);
        Global.put(CONTEXT_ATTR_LOCALHOST, localhost);
        loadedConfigPropKeys.add(CONTEXT_ATTR_LOCALHOST);

        // Set application context attributes for all config properties
        String prop, value;
        Iterator itr = config.getKeys();
        while (itr.hasNext()) {
            prop = (String) itr.next();
            value = config.getString(prop);

            // Anything with the value "localhost" will be set to the IP if possible
            value = (value.equals("localhost") ? localhost : value);

            log.debug("Config: " + prop + " = " + value);
            context.setAttribute(prop, value);
            loadedConfigPropKeys.add(prop);
        }

        // Run Mode configuration
        String runMode = config.getString(PROP_RUNMODE);
        try {
            RUN_MODE = RunMode.valueOf(runMode);
        } catch (Exception e) {
            log.warn("Error setting runmode, invalid value: " + runMode);
        }

        context.setAttribute("devMode", RUN_MODE != RunMode.PRODUCTION);
        loadedConfigPropKeys.add("devMode");

        // Fragment Cache Configuration
        if (config.getBoolean(FragmentCache.Properties.ENABLED, false)) {
            log.info("Enabling fragment caching...");
            FragmentCacheTag.enable();

            // This is expected to be in seconds
            long temp = config.getLong(FragmentCache.Properties.REQUEST_TIMEOUT, -1l);
            if (temp != -1) {
                log.info("Setting FragmentCacheTag request timeout: " + temp);
                FragmentCacheTag.setRequestTimeout(temp);
            }

            // The property is in seconds, but WARNING_COMPUTE_DURATION does NOT use a TimeUnit, so it's in ms
            temp = config.getLong(FragmentCache.Properties.WARNING_DURATION, -1l);
            if (temp != -1) {
                log.info("Setting FragmentCacheTag warning duration: " + temp);
                FragmentCacheTag.setWarningComputeDuration(temp * 1000);
            }

            // Get the fragment cache names
            String[] cacheNames = config.getStringArray(FragmentCache.Properties.ENGINE_NAMES);
            for (String cacheName : cacheNames) {
                String cacheClassName = config
                        .getString(String.format(FragmentCache.Properties.ENGINE_CLASS_TEMPLATE, cacheName));
                try {
                    // Load up the class
                    Class<? extends FragmentCache> cacheClass = (Class<? extends FragmentCache>) Class
                            .forName(cacheClassName);

                    // Look for a constructor that takes a config
                    Constructor<? extends FragmentCache> constructor = null;
                    try {
                        constructor = cacheClass.getConstructor(Configuration.class);
                    } catch (Exception e) {
                    }

                    FragmentCache cache;

                    // If we found the configuration constructor, use it
                    if (constructor != null)
                        cache = constructor.newInstance(config);
                    else {
                        // otherwise use a default no-args constructor
                        log.warn("Could not find a " + cacheClass.getSimpleName()
                                + " constructor that takes a Configuration, defaulting to no-args constructor");
                        cache = cacheClass.newInstance();
                    }

                    // register the cache with the FragmentCacheTag using the config strategy-name, or the engineName
                    // if a strategy-name is not specified
                    log.info("Registering FragmentCache strategy: [name=" + cacheName + ", class="
                            + cacheClass.getName() + "]");
                    FragmentCacheTag.registerStrategy(cacheName, cache);
                } catch (Exception e) {
                    log.error("Error enabling FragmentCache engine: " + cacheName, e);
                }
            }

        } else {
            // Have to call this here, because reconfiguration could turn
            // the cache off after it was previously enabled.
            FragmentCacheTag.disable();
        }

        // For Task execution
        ScheduledExecutorService taskExecutor;

        if (config.getBoolean(PROP_TASK_EXECUTOR_ON, false)) {
            int threads = config.getInt(PROP_TASK_EXECUTOR_THREADS, DEFAULT_TASK_EXECUTOR_THREADS);

            if (threads == 1)
                taskExecutor = Executors.newSingleThreadScheduledExecutor();
            else
                taskExecutor = Executors.newScheduledThreadPool(threads);

            context.setAttribute(CONTEXT_ATTR_TASK_MANAGER, taskExecutor);
            loadedConfigPropKeys.add(CONTEXT_ATTR_TASK_MANAGER);

            Global.put(CONTEXT_ATTR_TASK_MANAGER, taskExecutor);
        } else {
            // Not allowed to shutdown the taskExecutor if dynamic reconfig is enabled
            if (reconfigTask == null) {
                // Shutdown and remove references to the taskManager previously created
                taskExecutor = (ScheduledExecutorService) Global.get(CONTEXT_ATTR_TASK_MANAGER);
                if (taskExecutor != null) {
                    taskExecutor.shutdown(); // will block until all tasks complete
                    taskExecutor = null;
                    Global.remove(CONTEXT_ATTR_TASK_MANAGER);
                }
            } else {
                // We could just log a warning that you can't do this, but the user
                // might not see that, so we're going to refuse to reset a configuration
                // that cannot be loaded in whole successfully.
                throw new ConfigurationException(
                        "Cannot disable task execution service, dynamic reconfiguration is enabled!");
            }
        }

        // Email Service
        if (config.getBoolean(PROP_MAIL_SERVICE_ON, false)) {
            if (!SimpleEmailService.isEnabled()) {
                // Get the SMTP properties
                Properties props = System.getProperties();
                props.put(PROP_SMTP_HOST, config.getString(PROP_SMTP_HOST));
                props.put(PROP_SMTP_PORT, config.getString(PROP_SMTP_PORT));
                props.put(PROP_SMTP_AUTH, config.getString(PROP_SMTP_AUTH));

                Session session;

                if (config.getBoolean(PROP_SMTP_AUTH)) {
                    final String user = config.getString(PROP_SMTP_USER);
                    final String pass = config.getString(PROP_SMTP_PASS);

                    Authenticator auth = new Authenticator() {
                        @Override
                        public PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(user, pass);
                        }
                    };
                    session = Session.getInstance(props, auth);

                } else {
                    session = Session.getInstance(props);
                }

                // Get the global headers
                Iterator headerNames = config.getKeys(PROP_MAIL_HEADER_PREFIX);
                List<KeyValuePair<String, String>> headers = new LinkedList<KeyValuePair<String, String>>();

                String headerName;
                while (headerNames.hasNext()) {
                    headerName = (String) headerNames.next();
                    headers.add(new KeyValuePair<String, String>(headerName, config.getString(headerName)));
                }

                // Initialize the service
                SimpleEmailService.init(session);
                SimpleEmailService.setGlobalHeaders(headers);

                // Set whether we actually send the e-mails
                SimpleEmailService.setDummyMode(config.getBoolean(PROP_MAIL_DUMMY_MODE, false));

                // Set the failure policy
                String policy = config.getString(PROP_MAIL_ERROR_POLICY);
                if (policy != null) {
                    if (policy.equals(VALUE_MAIL_POLICY_DISCARD)) {
                        SimpleEmailService.setErrorPolicy(new SimpleEmailService.DiscardPolicy());
                    } else if (policy.equals(VALUE_MAIL_POLICY_REQUEUE)) {
                        Priority priority = null;
                        try {
                            priority = Priority.valueOf(config.getString(PROP_MAIL_REQUEUE_PRIORITY));
                        } catch (Exception e) {
                            log.warn("Error reading requeue policy priority: "
                                    + config.getString(PROP_MAIL_REQUEUE_PRIORITY, "") + ", using default");
                        }

                        if (priority == null)
                            SimpleEmailService.setErrorPolicy(new SimpleEmailService.RequeuePolicy());
                        else
                            SimpleEmailService.setErrorPolicy(new SimpleEmailService.RequeuePolicy(priority));
                    }
                }

                // Parse templates
                String emailTemplateDir = config.getString(PROP_MAIL_TEMPLATE_DIR);

                // If the template
                if (StringUtils.isBlank(emailTemplateDir))
                    emailTemplateDir = DEFAULT_EMAIL_TEMPLATE_DIR;

                log.debug("Looking for e-mail templates in: " + emailTemplateDir);
                Set<String> templates = context.getResourcePaths(emailTemplateDir);

                // E-mail templates
                if (templates != null && !templates.isEmpty()) {
                    log.debug("Found " + templates.size() + " templates");

                    String key;
                    String defaultTemplate = config.getString(PROP_MAIL_DEFAULT_TEMPLATE, EMPTY);

                    InputStream in;
                    StringBuilder builder;
                    Scanner scanner;

                    try {
                        Template template;
                        String[] parts;

                        ContentType contentType;

                        for (String path : templates) {
                            path = path.trim();
                            parts = path.split("\\.");

                            contentType = ContentType.valueOfExt(parts[1]);

                            try {
                                in = context.getResourceAsStream(path.trim());

                                if (in != null && in.available() > 0) {
                                    scanner = new Scanner(in);
                                    builder = new StringBuilder();

                                    while (scanner.hasNextLine()) {
                                        builder.append(scanner.nextLine());
                                        if (contentType == ContentType.PLAIN) {
                                            builder.append("\n");
                                        }
                                    }

                                    template = new Template(builder.toString(), contentType);

                                    key = parts[0].replace(emailTemplateDir, EMPTY);
                                    SimpleEmailService.registerTemplate(key, template,
                                            key.equals(defaultTemplate));
                                }
                            } catch (IOException ioe) {
                                log.error("Error loading e-mail template: " + path, ioe);
                            }
                        }
                    } catch (Exception e) {
                        log.error("Error loading e-mail templates", e);
                    }
                } else
                    log.debug("No e-mail templates found.");
            }
        } else if (SimpleEmailService.isEnabled()) {
            boolean shutdown = false;
            try {
                shutdown = SimpleEmailService.shutdown(30, TimeUnit.SECONDS);
            } catch (InterruptedException ie) {
                log.warn("Interrupted while shutting down SimpleEmailService");
            }

            if (!shutdown)
                SimpleEmailService.shutdown();
        }

        // QueuedHttpCallService
        if (config.getBoolean(PROP_HTTP_SRVC_ON, false)) {
            if (!SimpleHttpService.isEnabled()) // Don't double init...
            {
                int defaultThreadCount = Runtime.getRuntime().availableProcessors() + 1; // threads to use if unspecified
                SimpleHttpService.init(config.getInt(PROP_HTTP_SRVC_CORE_POOL, defaultThreadCount),
                        config.getInt(PROP_HTTP_SRVC_MAX_POOL, defaultThreadCount),
                        config.getLong(PROP_HTTP_SRVC_POOL_IDLE, DEFAULT_HTTP_SRVC_THREAD_IDLE),
                        config.getInt(PROP_HTTP_SRVC_CONNECT_TOUT, DEFAULT_HTTP_SRVC_CONNECT_TOUT),
                        config.getInt(PROP_HTTP_SRVE_SOCKET_TOUT, DEFAULT_HTTP_SRVE_SOCKET_TOUT));
            }
        } else if (SimpleHttpService.isEnabled()) {
            boolean shutdown = false;
            try {
                // Try to shutdown the service while letting currently waiting tasks complete
                shutdown = SimpleHttpService.shutdown(30, TimeUnit.SECONDS);
            } catch (InterruptedException ie) {
                log.warn("Interrupted while waiting for SimpleHttpService to shutdown");
            }
            if (!shutdown) {
                // But if that doesn't finish in 60 seconds, just cut it off
                int count = SimpleHttpService.shutdown().size();
                log.warn("SimpleHttpService failed to shutdown in 60 seconds, so it was terminated with "
                        + count + " tasks waiting");
            }
        }

        // Spymemcached Client
        if (config.getBoolean(PROP_SPYMEMCACHED_ON, false)) {
            String addresses = config.getString(PROP_SPYMEMCACHED_ADDRESSES);
            if (addresses == null) {
                log.error("Error configuring spymemcached; enabled but no addresses!");
            } else {
                try {
                    // Reflect our way to the constructor, this is all so that the
                    // spymemcached jar does not need to be included in a J2Free app
                    // unless it is actually to be used.
                    Class klass = Class.forName("net.spy.memcached.MemcachedClient");
                    Constructor constructor = klass.getConstructor(List.class);

                    klass = Class.forName("net.spy.memcached.AddrUtil");
                    Method method = klass.getMethod("getAddresses", String.class);

                    Object client = constructor.newInstance(method.invoke(null, addresses));

                    context.setAttribute(CONTEXT_ATTR_SPYMEMCACHED, client);
                    loadedConfigPropKeys.add(CONTEXT_ATTR_SPYMEMCACHED);

                    Global.put(CONTEXT_ATTR_SPYMEMCACHED, client);

                    log.info("Spymemcached client created, connected to " + addresses);
                } catch (Exception e) {
                    log.error("Error creating memcached client [addresses=" + addresses + "]", e);
                }
            }
        } else {
            // If a spymemcached client was previous created
            Object client = Global.get(CONTEXT_ATTR_SPYMEMCACHED);
            if (client != null) {
                try {
                    // Reflect our way to the shutdown method
                    Class klass = Class.forName("net.spy.memcached.MemcachedClient");
                    Method method = klass.getMethod("shutdown");

                    method.invoke(null, client); // and shut it down

                    log.info("Spymemcached client shutdown");
                } catch (Exception e) {
                    log.error("Error shutting down spymemcached client", e);
                }

                // Then remove any references
                Global.remove(CONTEXT_ATTR_SPYMEMCACHED);
                client = null;
            }
        }
    } catch (ConfigurationException ce) {
        log.error("Error configuring app", ce);
    }
}

From source file:com.joliciel.jochre.Jochre.java

/**
 * Train a letter guessing model.//from   w w  w  .  j  a v a  2 s . c o  m
 * @param letterModelPath the path where the model should be saved
 * @param iterations the number of training iterations
 * @param cutoff the feature count cutoff
 * @param imageCount the maximum number of training corpus images to use when training - if <= 0 will use all.
 */
public void doCommandTrain(String letterModelPath, String letterFeatureFilePath, int iterations, int cutoff,
        CorpusSelectionCriteria criteria, boolean reconstructLetters) {
    try {
        if (letterModelPath.length() == 0)
            throw new RuntimeException("Missing argument: letterModel");
        if (!letterModelPath.endsWith(".zip"))
            throw new RuntimeException("letterModel must end with .zip");
        if (letterFeatureFilePath.length() == 0)
            throw new RuntimeException("Missing argument: letterFeatures");
        String modelDirPath = letterModelPath.substring(0, letterModelPath.lastIndexOf('/'));
        File modelDir = new File(modelDirPath);
        modelDir.mkdirs();

        File letterFeatureFile = new File(letterFeatureFilePath);
        Scanner scanner = new Scanner(letterFeatureFile);

        List<String> descriptors = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String descriptor = scanner.nextLine();
            descriptors.add(descriptor);
            LOG.debug(descriptor);
        }
        Set<LetterFeature<?>> features = letterFeatureService.getLetterFeatureSet(descriptors);

        BoundaryDetector boundaryDetector = null;
        if (reconstructLetters) {
            ShapeSplitter splitter = boundaryService.getTrainingCorpusShapeSplitter();
            ShapeMerger merger = boundaryService.getTrainingCorpusShapeMerger();
            boundaryDetector = boundaryService.getLetterByLetterBoundaryDetector(splitter, merger, 1);
        } else {
            boundaryDetector = boundaryService.getOriginalBoundaryDetector();
        }

        LetterValidator letterValidator = new ComponentCharacterValidator(locale);

        ClassificationEventStream corpusEventStream = letterGuesserService.getJochreLetterEventStream(criteria,
                features, boundaryDetector, letterValidator);

        File letterModelFile = new File(letterModelPath);

        DecisionFactory<Letter> letterDecisionFactory = letterGuesserService.getLetterDecisionFactory();

        Map<String, Object> trainParameters = new HashMap<String, Object>();
        trainParameters.put(MaxentModelTrainer.MaxentModelParameter.Iterations.name(), iterations);
        trainParameters.put(MaxentModelTrainer.MaxentModelParameter.Cutoff.name(), cutoff);

        ClassificationModelTrainer<Letter> trainer = machineLearningService
                .getClassificationModelTrainer(MachineLearningAlgorithm.MaxEnt, trainParameters);

        ClassificationModel<Letter> letterModel = trainer.trainModel(corpusEventStream, letterDecisionFactory,
                descriptors);
        letterModel.persist(letterModelFile);

    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.joliciel.jochre.Jochre.java

/**
 * Train the letter splitting model./*from w  ww.  java2  s  . com*/
 * @param splitModelPath the path where the model should be saved
 * @param iterations the number of training iterations
 * @param cutoff the feature count cutoff
 * @param imageCount the maximum number of training corpus images to use when training - if <= 0 will use all.
 */
public void doCommandTrainSplits(String splitModelPath, String splitFeatureFilePath, int iterations, int cutoff,
        CorpusSelectionCriteria criteria) {
    try {
        if (splitModelPath.length() == 0)
            throw new RuntimeException("Missing argument: splitModel");
        if (!splitModelPath.endsWith(".zip"))
            throw new RuntimeException("splitModel must end with .zip");
        if (splitFeatureFilePath.length() == 0)
            throw new RuntimeException("Missing argument: splitFeatures");
        String modelDirPath = splitModelPath.substring(0, splitModelPath.lastIndexOf('/'));
        File modelDir = new File(modelDirPath);
        modelDir.mkdirs();

        File splitFeatureFile = new File(splitFeatureFilePath);
        Scanner scanner = new Scanner(splitFeatureFile);

        List<String> splitFeatureDescriptors = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String descriptor = scanner.nextLine();
            splitFeatureDescriptors.add(descriptor);
            LOG.debug(descriptor);
        }

        Set<SplitFeature<?>> splitFeatures = this.boundaryFeatureService
                .getSplitFeatureSet(splitFeatureDescriptors);

        double minWidthRatio = 1.1;
        double minHeightRatio = 1.0;
        ClassificationEventStream corpusEventStream = boundaryService.getJochreSplitEventStream(criteria,
                splitFeatures, minWidthRatio, minHeightRatio);

        File splitModelFile = new File(splitModelPath);
        Map<String, Object> trainParameters = new HashMap<String, Object>();
        trainParameters.put(MaxentModelTrainer.MaxentModelParameter.Iterations.name(), iterations);
        trainParameters.put(MaxentModelTrainer.MaxentModelParameter.Cutoff.name(), cutoff);
        ClassificationModelTrainer<SplitOutcome> trainer = machineLearningService
                .getClassificationModelTrainer(MachineLearningAlgorithm.MaxEnt, trainParameters);

        DecisionFactory<SplitOutcome> splitDecisionFactory = boundaryService.getSplitDecisionFactory();
        ClassificationModel<SplitOutcome> splitModel = trainer.trainModel(corpusEventStream,
                splitDecisionFactory, splitFeatureDescriptors);
        splitModel.persist(splitModelFile);

    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.joliciel.jochre.Jochre.java

/**
 * Train the letter merging model./* ww w  . j  a  va  2s.  c om*/
 * @param mergeModelPath the path where the model should be saved
 * @param iterations the number of training iterations
 * @param cutoff the feature count cutoff
 * @param imageCount the maximum number of training corpus images to use when training - if <= 0 will use all.
 */
public void doCommandTrainMerge(String mergeModelPath, String mergeFeatureFilePath, int multiplier,
        int iterations, int cutoff, CorpusSelectionCriteria criteria) {
    try {
        if (mergeModelPath.length() == 0)
            throw new RuntimeException("Missing argument: mergeModel");
        if (!mergeModelPath.endsWith(".zip"))
            throw new RuntimeException("mergeModel must end with .zip");
        if (mergeFeatureFilePath.length() == 0)
            throw new RuntimeException("Missing argument: mergeFeatures");
        String modelDirPath = mergeModelPath.substring(0, mergeModelPath.lastIndexOf('/'));
        File modelDir = new File(modelDirPath);
        modelDir.mkdirs();

        File mergeFeatureFile = new File(mergeFeatureFilePath);
        Scanner scanner = new Scanner(mergeFeatureFile);

        List<String> mergeFeatureDescriptors = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String descriptor = scanner.nextLine();
            mergeFeatureDescriptors.add(descriptor);
            LOG.debug(descriptor);
        }

        Set<MergeFeature<?>> mergeFeatures = this.boundaryFeatureService
                .getMergeFeatureSet(mergeFeatureDescriptors);
        double maxWidthRatio = 1.2;
        double maxDistanceRatio = 0.15;
        ClassificationEventStream corpusEventStream = boundaryService.getJochreMergeEventStream(criteria,
                mergeFeatures, maxWidthRatio, maxDistanceRatio);
        if (multiplier > 0) {
            corpusEventStream = new OutcomeEqualiserEventStream(corpusEventStream, multiplier);
        }

        File file = new File(mergeModelPath);
        Map<String, Object> trainParameters = new HashMap<String, Object>();
        trainParameters.put(MaxentModelTrainer.MaxentModelParameter.Iterations.name(), iterations);
        trainParameters.put(MaxentModelTrainer.MaxentModelParameter.Cutoff.name(), cutoff);
        ClassificationModelTrainer<MergeOutcome> trainer = machineLearningService
                .getClassificationModelTrainer(MachineLearningAlgorithm.MaxEnt, trainParameters);

        DecisionFactory<MergeOutcome> mergeDecisionFactory = boundaryService.getMergeDecisionFactory();
        ClassificationModel<MergeOutcome> mergeModel = trainer.trainModel(corpusEventStream,
                mergeDecisionFactory, mergeFeatureDescriptors);
        mergeModel.persist(file);
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.houghtonassociates.bamboo.plugins.dao.GerritService.java

private void grantDatabaseAccess() throws RepositoryException {
    final String targetRevision = "refs/meta/config";
    String filePath = gc.getWorkingDirectoryPath() + File.separator + "MetaConfig";
    String projectConfig = filePath + File.separator + "project.config";
    String url = String.format("ssh://%s@%s:%d/%s", gc.getUsername(), gc.getHost(), gc.getPort(),
            "All-Projects.git");

    boolean accessDBFound = false;

    Scanner scanner = null;
    JGitRepository jgitRepo = new JGitRepository();

    if (dbAccessGranted)
        return;//from w w w . ja va 2  s  .c  om

    synchronized (GerritService.class) {
        try {
            jgitRepo.setAccessData(gc);

            jgitRepo.open(filePath, true);

            jgitRepo.openSSHTransport(url);

            jgitRepo.fetch(targetRevision);

            jgitRepo.checkout(targetRevision);

            StringBuilder content = new StringBuilder();
            File fConfig = new File(projectConfig);
            scanner = new Scanner(fConfig);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.contains("accessDatabase = group Administrators")) {
                    accessDBFound = true;
                    break;
                }

                content.append(line).append("\n");

                if (line.contains("[capability]")) {
                    content.append("\taccessDatabase = group Administrators\n");
                }
            }

            scanner.close();

            if (accessDBFound) {
                dbAccessGranted = true;
                return;
            }

            File fConfig2 = new File(projectConfig);

            FileUtils.writeStringToFile(fConfig2, content.toString());

            jgitRepo.add("project.config");

            PushResult r = jgitRepo.commitPush("Grant Database Access.", targetRevision);

            if (r.getMessages().contains("ERROR")) {
                throw new RepositoryException(r.getMessages());
            }

            dbAccessGranted = true;
        } catch (org.eclipse.jgit.errors.TransportException e) {
            throw new RepositoryException(e);
        } catch (FileNotFoundException e) {
            throw new RepositoryException(
                    "Could not locate the project.config! Your checkout must have failed.");
        } catch (IOException e) {
            throw new RepositoryException(e);
        } finally {
            jgitRepo.close();
            if (scanner != null)
                scanner.close();
        }
    }
}

From source file:com.houghtonassociates.bamboo.plugins.dao.GerritService.java

private void installVerificationLabel() throws RepositoryException {
    final String targetRevision = "refs/meta/config";
    String filePath = gc.getWorkingDirectoryPath() + File.separator + "MetaConfig";
    String projectConfig = filePath + File.separator + "project.config";
    String url = String.format("ssh://%s@%s:%d/%s", gc.getUsername(), gc.getHost(), gc.getPort(),
            "All-Projects.git");

    boolean verifiedSectionFound = false;

    Scanner scanner = null;
    JGitRepository jgitRepo = new JGitRepository();

    if (verifiedLabelAdded)
        return;//from  w  w w  . j a  v a 2 s.  c  om

    synchronized (GerritService.class) {
        try {
            jgitRepo.setAccessData(gc);

            jgitRepo.open(filePath, true);

            jgitRepo.openSSHTransport(url);

            jgitRepo.fetch(targetRevision);

            jgitRepo.checkout(targetRevision);

            StringBuilder content = new StringBuilder();
            File fConfig = new File(projectConfig);
            scanner = new Scanner(fConfig);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.contains("[label \"Verified\"]")) {
                    verifiedSectionFound = true;
                    break;
                }

                content.append(line).append("\n");

                if (line.contains("[access \"refs/heads/*\"]")) {
                    content.append("\tlabel-Verified = -1..+1 group Administrators\n");
                }
            }

            scanner.close();

            if (verifiedSectionFound) {
                verifiedLabelAdded = true;
                return;
            }

            content.append("[label \"Verified\"]\n");
            content.append("\tfunction = MaxWithBlock\n");
            content.append("\tvalue = -1 Fails\n");
            content.append("\tvalue =  0 No score\n");
            content.append("\tvalue = +1 Verified\n");

            File fConfig2 = new File(projectConfig);

            FileUtils.writeStringToFile(fConfig2, content.toString());

            jgitRepo.add("project.config");

            PushResult r = jgitRepo.commitPush("Enabled verification label.", targetRevision);

            if (r.getMessages().contains("ERROR")) {
                throw new RepositoryException(r.getMessages());
            }

            verifiedLabelAdded = true;
        } catch (org.eclipse.jgit.errors.TransportException e) {
            throw new RepositoryException(e);
        } catch (FileNotFoundException e) {
            throw new RepositoryException(
                    "Could not locate the project.config! Your checkout must have failed.");
        } catch (IOException e) {
            throw new RepositoryException(e);
        } finally {
            jgitRepo.close();
            if (scanner != null)
                scanner.close();
        }
    }
}

From source file:io.realm.RealmTest.java

private List<String> getCharacterArray() {
    List<String> chars_array = new ArrayList<String>();
    String file = "assets/unicode_codepoints.csv";
    Scanner scanner = new Scanner(getClass().getClassLoader().getResourceAsStream(file));
    int i = 0;/*w ww.j a  v a  2  s.com*/
    String currentUnicode = null;
    try {
        while (scanner.hasNextLine()) {
            currentUnicode = scanner.nextLine();
            char[] chars = Character.toChars(Integer.parseInt(currentUnicode, 16));
            String codePoint = new String(chars);
            chars_array.add(codePoint);
            i++;
        }
    } catch (Exception e) {
        fail("Failure, Codepoint: " + i + " / " + currentUnicode + " " + e.getMessage());
    }
    return chars_array;
}

From source file:cz.cuni.amis.planning4j.external.impl.itsimple.ItSimplePlanningProcess.java

/**
 * Runs the planner and returns the console output
 *//*from   ww w . ja  va2 s.c  om*/
protected UnprocessedPlanningResult runPlanner(File domain, File problem) {

    //1.Get main planner's parameters and arguments
    ItSimplePlannerSettings settings = chosenPlanner.getSettings();

    String plannerRelativeFile = settings.getExecutableFilePath();
    plannerExecutableFile = new File(plannerBinariesDirectory, plannerRelativeFile);

    if (!plannerExecutableFile.exists()) {
        String toolMessage = "Could not find selected planner '" + plannerRelativeFile + "' in directory "
                + plannerBinariesDirectory.getAbsolutePath();
        throw new PlanningException(toolMessage);
    }

    List<String> commandArguments = new ArrayList<String>();

    //1.0 Get planner execution file
    commandArguments.add(plannerExecutableFile.getAbsolutePath());

    //1.1 Get domain arguments
    if (!settings.getDomainArgumentName().trim().isEmpty()) {
        commandArguments.add(settings.getDomainArgumentName());
    }
    commandArguments.add(domain.getAbsolutePath()); //domain path

    //1.2 Get problem arguments
    if (!settings.getProblemArgumentName().trim().isEmpty()) {
        commandArguments.add(settings.getProblemArgumentName());
    }
    commandArguments.add(problem.getAbsolutePath()); //problem path

    //1.3 Get additional arguments
    for (PlannerArgument argument : settings.getAdditionalArguments()) {
        //System.out.println(argument.getChildText("name"));
        if (!argument.getName().trim().equals("")) {
            commandArguments.add(argument.getName());
        }
        //if there is a value for the argument then add to the command
        if (!argument.getValue().trim().equals("")) {
            commandArguments.add(argument.getValue());
        }

    }

    //1.4 Get output arguments
    if (settings.isHasOutputFile() && settings.isOutputFileNeedsArgument()) {
        commandArguments.add(settings.getOutputFileArgumentName());
        commandArguments.add(settings.getOutputFile()); //problem path            
    }

    synchronized (this) {
        if (cancelled) {
            return null;
        }
        logger.info("\n>> Calling planner " + chosenPlanner.getName() + " in directory: "
                + workingDirectory.getAbsolutePath());
        logger.debug("Planner arguments:" + commandArguments);
        //Call the planner
        try {
            ProcessBuilder builder = new ProcessBuilder(commandArguments);
            builder.directory(workingDirectory);
            process = spawnPlanner(builder, plannerRelativeFile);
        } catch (Exception e) {
            String message = "Error while running the planner " + chosenPlanner.getName() + ". ";
            throw new PlanningException(message, e);
        }
    }

    try {
        boolean plannerFoundNoSolution = false;

        Scanner sc = new Scanner(process.getInputStream());
        //Get the planner answer exposed in the console
        if (logger.isDebugEnabled()) {
            logger.debug("Planner console output:");
        }

        StringBuilder consoleOutputBuilder = new StringBuilder();
        List<String> unprocessedPlan = new ArrayList<String>();
        List<String> unprocessedStatistics = new ArrayList<String>();

        //Needed only when parsing plan from console, but need to be initialized here
        EConsoleParseState consoleParseState;
        int numLinesBeforePlan = settings.getConsoleOutputStartsAfterNLines();
        if (settings.getConsoleOutputPlanStartIdentifier() == null
                || settings.getConsoleOutputPlanStartIdentifier().isEmpty()) {
            consoleParseState = EConsoleParseState.COUNTING_TO_PLAN_START;
        } else {
            consoleParseState = EConsoleParseState.BEGIN;
        }

        while (sc.hasNextLine()) {

            String line = sc.nextLine();
            consoleOutputBuilder.append(line).append("\n");

            for (INoPlanFoundChecker checker : settings.getNoPlanFoundCheckers()) {
                if (checker.processOutputLine(line)) {
                    plannerFoundNoSolution = true;
                }
            }

            if (logger.isDebugEnabled()) {
                logger.debug(line);
            }

            if (!settings.isHasOutputFile()) {
                if (!line.trim().isEmpty()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug(consoleParseState + ":" + line);
                    }
                    //the plan is part of console output
                    switch (consoleParseState) {
                    case BEGIN: {
                        if (line.contains(settings.getConsoleOutputPlanStartIdentifier())) {
                            if (numLinesBeforePlan > 0) {
                                consoleParseState = EConsoleParseState.COUNTING_TO_PLAN_START;
                                numLinesBeforePlan--;
                            } else {
                                //the plan starts on the same line
                                int indexPlanStart = line
                                        .indexOf(settings.getConsoleOutputPlanStartIdentifier());
                                String firstLine = line.substring(indexPlanStart
                                        + settings.getConsoleOutputPlanStartIdentifier().length());
                                if (!isLineAction(firstLine)) {
                                    unprocessedStatistics.add(firstLine);
                                } else {
                                    unprocessedPlan.add(firstLine);
                                }
                                consoleParseState = EConsoleParseState.READING_PLAN;
                            }
                        }
                        break;
                    }
                    case COUNTING_TO_PLAN_START: {
                        if (numLinesBeforePlan > 0) {
                            numLinesBeforePlan--;
                            break;
                        } else {
                            consoleParseState = EConsoleParseState.READING_PLAN;
                        }
                        //intentional fallthrough!!!
                    }
                    case READING_PLAN: {
                        if (!settings.getConsoleOutputPlanEndIdentifier().isEmpty()
                                && line.contains(settings.getConsoleOutputPlanEndIdentifier())) {
                            consoleParseState = EConsoleParseState.END;
                        } else {
                            if (!isLineAction(line)) {
                                unprocessedStatistics.add(line);
                            } else {
                                unprocessedPlan.add(line);
                            }
                        }
                        break;
                    }
                    case END: {
                        if (isLineStatistics(line)) {
                            unprocessedStatistics.add(line);
                        }
                        break;
                    }

                    }
                }
            }

        }
        sc.close();

        if (cancelled) {
            return null;
        }

        //Need to clean the stream, otherwise, it would block the process from terminating
        String errorOuput;
        try {
            errorOuput = IOUtils.toString(process.getErrorStream());
        } catch (IOException ex) {
            errorOuput = "Could not get error stream: " + ex.getMessage();
        }

        try {
            if (cancelled) {
                return null;
            }

            process.waitFor();
            logger.info("\n>> Planner " + chosenPlanner.getName() + " finished execution\n ");
        } catch (InterruptedException ex) {
            if (cancelled) {
                return null;
            }
            logger.info("Waiting for planner execution interrupted", ex);
            destroyProcess();
            return null;
        }

        process.destroy();

        if (logger.isDebugEnabled()) {
            logger.debug("Planner console output end.");
        }

        if (cancelled) {
            return null;
        }

        int exitCode = process.exitValue();

        for (INoPlanFoundChecker checker : settings.getNoPlanFoundCheckers()) {
            if (checker.processExitCode(exitCode)) {
                plannerFoundNoSolution = true;
            }
        }

        if (exitCode != 0 && !plannerFoundNoSolution) {
            throw new PlanningException(
                    "Planner terminated with an error - exit code: " + exitCode + ". Planner output:\n "
                            + consoleOutputBuilder.toString() + "\nError output:\n" + errorOuput);
        }

        if (settings.isHasOutputFile()) { //The planner does provide an output file

            String solutionFile = "solution.soln";
            if (!settings.getOutputFile().trim().isEmpty()) {
                solutionFile = settings.getOutputFile();
            }

            if (settings.getOutputFileAutomaticIncrementSuffix() != null) {
                //Find the existing file with the highest increment index
                int i = 1;
                while (true) {
                    String candidateSolutionFileName = solutionFile + settings
                            .getOutputFileAutomaticIncrementSuffix().replace("#", Integer.toString(i));
                    File candidateSolutionFile = new File(workingDirectory, candidateSolutionFileName);
                    if (candidateSolutionFile.exists()) {
                        solutionFile = candidateSolutionFileName;
                        i++;
                    } else {
                        break;
                    }
                }
            }

            //Get the planner answer exposed in the solution Output File
            File outputFile = new File(workingDirectory, solutionFile);

            if (outputFile.exists()) {
                //Get output
                try {
                    for (String line : FileUtils.readLines(outputFile)) {
                        if (line.trim().isEmpty()) {
                            continue;
                        }
                        if (!isLineAction(line)) {
                            unprocessedStatistics.add(line);
                        } else {
                            unprocessedPlan.add(line);
                        }
                    }
                } catch (IOException ex) {
                    throw new PlanningException("Could not read planner output", ex);
                }

                //remove output solution file (only if the plan create it)
                outputFile.delete();
                //TODO check permission
            } else {
                //if the planner signalled before that it found nothing, the file may  not exits and it's OK
                if (!plannerFoundNoSolution) {
                    throw new PlanningException("Could not find the planner output solution file! \n");
                }
                //System.out.println(toolMessage);
            }

        }

        if (cancelled) {
            return null;
        }

        for (INoPlanFoundChecker checker : settings.getNoPlanFoundCheckers()) {
            if (checker.processUnprocessedPlan(unprocessedPlan)) {
                plannerFoundNoSolution = true;
            }
        }

        return new UnprocessedPlanningResult(unprocessedPlan, unprocessedStatistics,
                consoleOutputBuilder.toString(), !plannerFoundNoSolution);

    } finally {
        // delete additional generated files
        for (String generatedFileName : settings.getAdditionalGeneratedFiles()) {
            File file = new File(workingDirectory, generatedFileName);
            if (file.exists()) {
                // delete the file
                file.delete();
            }
        }
    }
}