Example usage for java.lang Process exitValue

List of usage examples for java.lang Process exitValue

Introduction

In this page you can find the example usage for java.lang Process exitValue.

Prototype

public abstract int exitValue();

Source Link

Document

Returns the exit value for the process.

Usage

From source file:org.auraframework.archetype.AuraArchetypeSimpleTestMANUAL.java

public void testProjectCreation() throws Throwable {
    Process jettyProcess = null;
    workspace = new File(
            System.getProperty("java.io.tmpdir") + File.separator + getName() + System.currentTimeMillis());
    try {/*from w  w w.  j  av  a2 s.c  o m*/
        // create a workspace to place the project files in
        workspace.mkdirs();

        // generate a project from the archetype
        Process genProcess = startProcess(workspace,
                ImmutableList.of("mvn", "archetype:generate", "-DarchetypeRepository=" + archRepo,
                        "-DarchetypeCatalog=" + archCatalog, "-DarchetypeGroupId=" + archetype.groupId,
                        "-DarchetypeArtifactId=" + archetype.artifactId,
                        "-DarchetypeVersion=" + archetype.version, "-DgroupId=" + project.groupId,
                        "-DartifactId=" + project.artifactId, "-Dversion=" + project.version,
                        "-Dpackage=" + projectPackage, "-DinteractiveMode=false"));
        goldMavenOutput(genProcess, "-creation.txt", "Failed to generate artifact!");

        File projectDir = new File(workspace, project.artifactId);
        assertDirectory(projectDir);
        verifyGeneratedResources(projectDir);

        // build the new project
        Process buildProcess = startProcess(projectDir, ImmutableList.of("mvn", "install"));
        goldMavenOutput(buildProcess, "-install.txt", "Failed to build new project!");

        // get a free port for jetty
        ServerSocket socket = new ServerSocket(0);
        int jettyPort = socket.getLocalPort();
        socket.close();

        // start up jetty
        jettyProcess = startProcess(projectDir,
                ImmutableList.of("mvn", "jetty:run", "-Djetty.port=" + jettyPort));

        int status = 0;
        for (int i = 0; i < 30; i++) {
            try {
                HttpGet get = obtainGetMethod("/");
                HttpResponse response = perform(get);
                status = getStatusCode(response);
                get.releaseConnection();
                break;
            } catch (ConnectException ce) {
                // expected, before server is listening
                Thread.sleep(1000);
            }
        }
        assertEquals("Failed to connect to server", HttpStatus.SC_OK, status);

        verifyDefaultDocument();
        verifySampleComponents();
    } catch (Throwable t) {
        // if any errors in Jetty requests, let's print out the Jetty
        // console output for diag before killing the
        // test
        if (jettyProcess != null) {
            InputStream is = jettyProcess.getInputStream();
            int len = is.available();
            byte[] buf = new byte[len];
            is.read(buf);
            System.err.println(new String(buf));
        }
        throw t;
    } finally {
        // kill Jetty
        if (jettyProcess != null) {
            try {
                jettyProcess.exitValue();
            } catch (IllegalThreadStateException e) {
                jettyProcess.destroy();
            }
        }
        // cleanup generated workspace
        IOUtil.delete(workspace);
    }
}

From source file:org.jsweet.transpiler.JSweetTranspiler.java

/**
 * Evaluates the given source files with the given evaluation engine.
 * <p>/*from w ww . java 2  s.  c o  m*/
 * If given engine name is "Java", this function looks up for the classes in
 * the classpath and run the main methods when found.
 * 
 * @param engineName
 *            the engine name: either "Java" or any valid and installed
 *            JavaScript engine.
 * @param transpilationHandler
 *            the log handler
 * @param sourceFiles
 *            the source files to be evaluated (transpiled first if needed)
 * @return the evaluation result
 * @throws Exception
 *             when an internal error occurs
 */
public EvaluationResult eval(String engineName, TranspilationHandler transpilationHandler,
        SourceFile... sourceFiles) throws Exception {
    logger.info("[" + engineName + " engine] eval files: " + Arrays.asList(sourceFiles));
    if ("Java".equals(engineName)) {
        // search for main functions
        JSweetContext context = new JSweetContext(this);
        Options options = Options.instance(context);
        if (classPath != null) {
            options.put(Option.CLASSPATH, classPath);
        }
        options.put(Option.XLINT, "path");
        JavacFileManager.preRegister(context);
        JavaFileManager fileManager = context.get(JavaFileManager.class);

        List<JavaFileObject> fileObjects = toJavaFileObjects(fileManager,
                Arrays.asList(SourceFile.toFiles(sourceFiles)));

        JavaCompiler compiler = JavaCompiler.instance(context);
        compiler.attrParseOnly = true;
        compiler.verbose = true;
        compiler.genEndPos = false;

        logger.info("parsing: " + fileObjects);
        List<JCCompilationUnit> compilationUnits = compiler.enterTrees(compiler.parseFiles(fileObjects));
        MainMethodFinder mainMethodFinder = new MainMethodFinder();
        try {
            for (JCCompilationUnit cu : compilationUnits) {
                cu.accept(mainMethodFinder);
            }
        } catch (Exception e) {
            // swallow on purpose
        }
        if (mainMethodFinder.mainMethod != null) {
            try {
                initExportedVarMap();
                Class<?> c = Class.forName(
                        mainMethodFinder.mainMethod.getEnclosingElement().getQualifiedName().toString());
                c.getMethod("main", String[].class).invoke(null, (Object) null);
            } catch (Exception e) {
                throw new Exception("evalution error", e);
            }
        }

        final Map<String, Object> map = getExportedVarMap();
        return new EvaluationResult() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T get(String variableName) {
                return (T) map.get("_exportedVar_" + variableName);
            }

            @Override
            public String toString() {
                return map.toString();
            }

            @Override
            public String getExecutionTrace() {
                return "<not available>";
            }
        };
    } else {
        if (!areAllTranspiled(sourceFiles)) {
            ErrorCountTranspilationHandler errorHandler = new ErrorCountTranspilationHandler(
                    transpilationHandler);
            transpile(errorHandler, sourceFiles);
            if (errorHandler.getErrorCount() > 0) {
                throw new Exception("unable to evaluate: transpilation errors remain");
            }
        }

        StringWriter trace = new StringWriter();

        Process runProcess;
        if (context.useModules) {
            File f = null;
            if (!context.entryFiles.isEmpty()) {
                f = context.entryFiles.get(0);
                for (SourceFile sf : sourceFiles) {
                    if (sf.getJavaFile().equals(f)) {
                        f = sf.getJsFile();
                    }
                }
            }
            if (f == null) {
                f = sourceFiles[sourceFiles.length - 1].getJsFile();
            }
            logger.info("[modules] eval file: " + f);
            runProcess = ProcessUtil.runCommand(ProcessUtil.NODE_COMMAND, line -> trace.append(line + "\n"),
                    null, f.getPath());
        } else {
            File tmpFile = new File(new File(TMP_WORKING_DIR_NAME), "eval.tmp.js");
            FileUtils.deleteQuietly(tmpFile);
            if (jsLibFiles != null) {
                for (File jsLibFile : jsLibFiles) {
                    String script = FileUtils.readFileToString(jsLibFile);
                    FileUtils.write(tmpFile, script + "\n", true);
                }
            }
            for (SourceFile sourceFile : sourceFiles) {
                String script = FileUtils.readFileToString(sourceFile.getJsFile());
                FileUtils.write(tmpFile, script + "\n", true);
            }
            logger.info("[no modules] eval file: " + tmpFile);
            runProcess = ProcessUtil.runCommand(ProcessUtil.NODE_COMMAND, line -> trace.append(line + "\n"),
                    null, tmpFile.getPath());
        }

        int returnCode = runProcess.exitValue();
        logger.info("return code=" + returnCode);
        if (returnCode != 0) {
            throw new Exception("evaluation error (code=" + returnCode + ") - trace=" + trace);
        }
        return new TraceBasedEvaluationResult(trace.getBuffer().toString());
    }
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Force Align database for Automatic labels 
 * @throws Exception/*from  w w w. ja v a 2  s. c  om*/
 */
private void hviteAligning() throws Exception {

    String hvite = getProp(HTKDIR) + File.separator + "HVite"; // -A -D -V -T 1 "; // to add -A -D -V -T 1 in every function
    File htkFile = new File(hvite);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";

    // Virtual sp change_ phoneList should be a member? 
    // Without sp: 
    /*String phoneList = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phone2.list";*/

    // Whit sp:

    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

    String hmmDef = getProp(HTDIR) + File.separator + "hmm" + File.separator + "hmm-final" + File.separator
            + "hmmdefs";
    String macros = getProp(HTDIR) + File.separator + "hmm" + File.separator + "hmm-final" + File.separator
            + "macros";

    // Virtual sp change_ phoneMlf should be a member?

    // Without sp: 
    /*String phoneMlf = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phones2.mlf";*/
    // Whit sp:
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones3.mlf";

    String alignedMlf = getProp(HTDIR) + File.separator + "aligned.mlf";
    String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.dict";
    String labDir = getProp(HTDIR) + File.separator + "lab";

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell

    //when no sp use (-m)!

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println("( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir
            + " -o W -C " + configFile + " -a -H " + macros + " -H " + hmmDef + " -i " + alignedMlf
            + " -t 250.0 -y lab" + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList
            + " > log_hviteAligning.txt" + "; exit )\n");

    pw.println("( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -o W -C "
            + configFile + " -a -H " + macros + " -H " + hmmDef + " -i " + alignedMlf + " -t 250.0 -y lab"
            + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList
            + " > log_hviteAligning.txt" + "; exit )\n");

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Initialize HTK Training process/* w  w w  . j av  a 2s .  co  m*/
 * @throws Exception
 */
private void initialiseHTKTrain() throws Exception {

    String hcompv = getProp(HTKDIR) + File.separator + "HCompV";
    File htkFile = new File(hcompv);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";
    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

    System.out.println("( cd " + getProp(HTDIR) + " ; mkdir hmm/hmm-dummy ; " + " mkdir hmm/hmm-final ; "
            + hcompv + " " + HTK_SO + " -C " + configFile + " -f 0.01 -m -S " + listFile + " -M "
            + getProp(HTDIR) + File.separator + "hmm/hmm-dummy " + getProp(HTDIR) + File.separator + "config"
            + File.separator + "htk.proto" + " > log_initialiseHTKTrain.txt" + "; exit )\n");
    pw.print("( cd " + getProp(HTDIR) + " ; mkdir hmm/hmm-dummy ; " + " mkdir hmm/hmm-final ; " + hcompv + " "
            + HTK_SO + " -C " + configFile + " -f 0.01 -m -S " + listFile + " -M " + getProp(HTDIR)
            + File.separator + "hmm/hmm-dummy " + getProp(HTDIR) + File.separator + "config" + File.separator
            + "htk.proto" + " > log_initialiseHTKTrain.txt" + "; exit )\n");
    pw.flush();
    //shut down
    pw.close();

    process.waitFor();

    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:org.opencastproject.composer.impl.AbstractCmdlineEncoderEngine.java

/**
 * Executes the command line encoder with the given set of files and properties and using the provided encoding
 * profile.//from w w  w  . j  a  va2 s  . c  o m
 * 
 * @param audioSource
 *          the audio file (used when muxing)
 * @param videoSource
 *          the video file
 * @param profile
 *          the profile identifier
 * @param properties
 *          the encoding properties to be interpreted by the actual encoder implementation
 * @return the processed file
 * @throws EncoderException
 *           if processing fails
 */
protected Option<File> process(File audioSource, File videoSource, EncodingProfile profile,
        Map<String, String> properties) throws EncoderException {
    // Fist, update the parameters
    if (properties != null)
        params.putAll(properties);
    // build command
    BufferedReader in = null;
    Process encoderProcess = null;
    if (videoSource == null && audioSource == null) {
        throw new IllegalArgumentException("At least one track must be specified.");
    }
    try {
        // Set encoding parameters
        String audioInput = null;
        if (audioSource != null) {
            audioInput = FilenameUtils.normalize(audioSource.getAbsolutePath());
            params.put("in.audio.path", audioInput);
            params.put("in.audio.name", FilenameUtils.getBaseName(audioInput));
            params.put("in.audio.suffix", FilenameUtils.getExtension(audioInput));
            params.put("in.audio.filename", FilenameUtils.getName(audioInput));
            params.put("in.audio.mimetype",
                    MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(audioInput));
        }
        if (videoSource != null) {
            String videoInput = FilenameUtils.normalize(videoSource.getAbsolutePath());
            params.put("in.video.path", videoInput);
            params.put("in.video.name", FilenameUtils.getBaseName(videoInput));
            params.put("in.video.suffix", FilenameUtils.getExtension(videoInput));
            params.put("in.video.filename", FilenameUtils.getName(videoInput));
            params.put("in.video.mimetype",
                    MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(videoInput));
        }
        File parentFile;
        if (videoSource == null) {
            parentFile = audioSource;
        } else {
            parentFile = videoSource;
        }
        String outDir = parentFile.getAbsoluteFile().getParent();
        String outFileName = FilenameUtils.getBaseName(parentFile.getName());
        String outSuffix = processParameters(profile.getSuffix());

        if (params.containsKey("time")) {
            outFileName += "_" + properties.get("time");
        }

        // generate random name if multiple jobs are producing file with identical name (MH-7673)
        outFileName += "_" + UUID.randomUUID().toString();

        params.put("out.dir", outDir);
        params.put("out.name", outFileName);
        params.put("out.suffix", outSuffix);

        // create encoder process.
        // no special working dir is set which means the working dir of the
        // current java process is used.
        // TODO: Parallelisation (threading)
        List<String> command = buildCommand(profile);
        StringBuilder sb = new StringBuilder();
        for (String cmd : command) {
            sb.append(cmd);
            sb.append(" ");
        }
        logger.info("Executing encoding command: {}", sb);
        ProcessBuilder pbuilder = new ProcessBuilder(command);
        pbuilder.redirectErrorStream(REDIRECT_ERROR_STREAM);
        encoderProcess = pbuilder.start();

        // tell encoder listeners about output
        in = new BufferedReader(new InputStreamReader(encoderProcess.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            handleEncoderOutput(profile, line, audioSource, videoSource);
        }

        // wait until the task is finished
        encoderProcess.waitFor();
        int exitCode = encoderProcess.exitValue();
        if (exitCode != 0) {
            throw new EncoderException(this, "Encoder exited abnormally with status " + exitCode);
        }

        if (audioSource != null) {
            logger.info("Audio track {} and video track {} successfully encoded using profile '{}'",
                    new String[] { (audioSource == null ? "N/A" : audioSource.getName()),
                            (videoSource == null ? "N/A" : videoSource.getName()), profile.getIdentifier() });
        } else {
            logger.info("Video track {} successfully encoded using profile '{}'",
                    new String[] { videoSource.getName(), profile.getIdentifier() });
        }
        fireEncoded(this, profile, audioSource, videoSource);
        if (profile.getOutputType() != EncodingProfile.MediaType.Nothing)
            return some(new File(parentFile.getParent(), outFileName + outSuffix));
        else
            return none();
    } catch (EncoderException e) {
        if (audioSource != null) {
            logger.warn("Error while encoding audio track {} and video track {} using '{}': {}",
                    new String[] { (audioSource == null ? "N/A" : audioSource.getName()),
                            (videoSource == null ? "N/A" : videoSource.getName()), profile.getIdentifier(),
                            e.getMessage() });
        } else {
            logger.warn("Error while encoding video track {} using '{}': {}",
                    new String[] { (videoSource == null ? "N/A" : videoSource.getName()),
                            profile.getIdentifier(), e.getMessage() });
        }
        fireEncodingFailed(this, profile, e, audioSource, videoSource);
        throw e;
    } catch (Exception e) {
        logger.warn("Error while encoding audio {} and video {} to {}:{}, {}",
                new Object[] { (audioSource == null ? "N/A" : audioSource.getName()),
                        (videoSource == null ? "N/A" : videoSource.getName()), profile.getName(),
                        e.getMessage() });
        fireEncodingFailed(this, profile, e, audioSource, videoSource);
        throw new EncoderException(this, e.getMessage(), e);
    } finally {
        IoSupport.closeQuietly(in);
        IoSupport.closeQuietly(encoderProcess);
    }
}

From source file:org.oxymores.chronix.engine.RunnerShell.java

public static RunResult run(RunDescription rd, String logFilePath, boolean storeLogFile,
        boolean returnFullerLog) {
    RunResult res = new RunResult();
    Process p;
    String nl = System.getProperty("line.separator");
    Pattern pat = Pattern.compile("^set ([a-zA-Z]+[a-zA-Z0-9]*)=(.+)");
    Matcher matcher = pat.matcher("Testing123Testing");
    String encoding = getEncoding(rd);
    log.debug("Encoding is " + encoding);

    // ///////////////////////////
    // Build command
    List<String> argsStrings = buildCommand(rd);

    // /////////////////////////////////////////////////////////////////////////
    // Create a process builder with the command line contained in the array
    ProcessBuilder pb = new ProcessBuilder(argsStrings);

    // Mix stdout and stderr (easier to put errors in context this way)
    pb.redirectErrorStream(true);/*from w  w  w . j  av a 2s .  co  m*/

    // Create array containing environment
    Map<String, String> env = pb.environment();
    for (int i = 0; i < rd.getEnvNames().size(); i++) {
        env.put(rd.getEnvNames().get(i), rd.getEnvValues().get(i));
    }

    BufferedReader br = null;
    Writer output = null;
    try {
        // Start!
        log.debug("GO (" + rd.getSubMethod() + ")");
        p = pb.start();

        // Read output (err & out), write it to file
        InputStreamReader isr = new InputStreamReader(p.getInputStream(), encoding);
        br = new BufferedReader(isr);

        String line = null;
        int i = 0;
        LinkedHashMap<Integer, String> endBuffer = new LinkedHashMap<Integer, String>() {
            private static final long serialVersionUID = -6773540176968046737L;

            @Override
            protected boolean removeEldestEntry(java.util.Map.Entry<Integer, String> eldest) {
                return this.size() > Constants.MAX_RETURNED_BIG_LOG_END_LINES;
            }
        };

        if (storeLogFile) {
            output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFilePath), "UTF-8"));
        }
        line = br.readLine();
        while (line != null) {
            i++;

            // Local log file gets all lines
            if (storeLogFile) {
                output.write(line + nl);
            }

            // Small log gets first 500 lines or 10000 characters (the smaller of the two)
            if (i < Constants.MAX_RETURNED_SMALL_LOG_LINES
                    && res.logStart.length() < Constants.MAX_RETURNED_SMALL_LOG_CHARACTERS) {
                res.logStart += nl + line;
            }

            // Scheduler internal log gets first line only
            if (i == 1) {
                log.debug(String.format("Job running. First line of output is: %s", line));
            }

            // Fuller log gets first 10k lines, then last 1k lines.
            if (returnFullerLog) {
                if (i < Constants.MAX_RETURNED_BIG_LOG_LINES) {
                    res.fullerLog += line;
                } else {
                    endBuffer.put(i, line);
                }
            }

            // Analysis: there may be a new variable definition in the line
            matcher.reset(line);
            if (matcher.find()) {
                log.debug("Key detected :" + matcher.group(1));
                log.debug("Value detected :" + matcher.group(2));
                res.newEnvVars.put(matcher.group(1), matcher.group(2));
            }
            line = br.readLine();
        }
        IOUtils.closeQuietly(br);

        if (i > Constants.MAX_RETURNED_BIG_LOG_LINES
                && i < Constants.MAX_RETURNED_BIG_LOG_LINES + Constants.MAX_RETURNED_BIG_LOG_END_LINES
                && returnFullerLog) {
            res.fullerLog += Arrays.toString(endBuffer.entrySet().toArray());
        }
        if (i >= Constants.MAX_RETURNED_BIG_LOG_LINES + Constants.MAX_RETURNED_BIG_LOG_END_LINES
                && returnFullerLog) {
            res.fullerLog += "\n\n\n*******\n LOG TRUNCATED - See full log on server\n********\n\n\n"
                    + Arrays.toString(endBuffer.entrySet().toArray());
        }

        // Done: close log file
        if (storeLogFile) {
            IOUtils.closeQuietly(output);
            File f = new File(logFilePath);
            res.logSizeBytes = f.length();
        }
    } catch (IOException e) {
        log.error("error occurred while running job", e);
        res.logStart = e.getMessage();
        res.returnCode = -1;
        IOUtils.closeQuietly(br);
        IOUtils.closeQuietly(output);
        return res;
    }

    // Return
    res.returnCode = p.exitValue();
    res.logPath = logFilePath;
    res.envtUser = System.getProperty("user.name");
    try {
        res.envtServer = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        res.envtServer = "unknown";
    }
    log.info(String.format("Job ended, RC is %s", res.returnCode));
    return res;
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Force Align database for Automatic labels 
 * @throws Exception/*www.ja  v a 2s. c o m*/
 */
private void hviteMultiplePronunciationAligning(String hmmNumber, String alignedMlf, boolean labOutput,
        String labDir, boolean full, String logfile, boolean cmp) throws Exception {

    String hvite = getProp(HTKDIR) + File.separator + "HVite"; // -A -D -V -T 1 "; // to add -A -D -V -T 1 in every function
    File htkFile = new File(hvite);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";

    // Virtual sp change_ phoneList should be a member? 
    // Without sp: 
    /*String phoneList = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phone2.list";*/

    // Whit sp:

    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

    String hmmDef = getProp(HTDIR) + File.separator + "hmm" + File.separator + hmmNumber + File.separator
            + "hmmdefs";
    String macros = getProp(HTDIR) + File.separator + "hmm" + File.separator + hmmNumber + File.separator
            + "macros";

    // Virtual sp change_ phoneMlf should be a member?

    // Without sp: 
    /*String phoneMlf = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phones2.mlf";*/
    // Whit sp:
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.words3.mlf";

    String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.words.dict";

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell

    //when no sp use (-m)!
    String cmd;

    String alignout, mOptioon, oOption;

    if (labOutput)
        alignout = "";
    else
        alignout = " -i " + alignedMlf;

    if (full) {
        if (labOutput)
            mOptioon = "";
        else
            mOptioon = " -m";

        cmd = "( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -C "
                + configFile + mOptioon + " -a -H " + macros + " -H " + hmmDef + alignout + " -t 250.0 -y lab"
                + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > " + logfile
                + "; exit )\n";
    } else {
        if (cmp)
            oOption = " -o TS";
        else
            oOption = " -o W";

        cmd = "( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + oOption + " -C "
                + configFile + " -m -a -H " + macros + " -H " + hmmDef + alignout + " -t 250.0 -y lab" + " -I "
                + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > " + logfile
                + "; exit )\n";
    }

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println(cmd);
    pw.println(cmd);

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:io.hops.hopsworks.common.dao.tensorflow.config.TensorBoardProcessMgr.java

/**
 * Start the TensorBoard process/*  ww  w.j  a  v  a 2 s.c o m*/
 * @param project
 * @param user
 * @param hdfsUser
 * @param hdfsLogdir
 * @return
 * @throws IOException
 */
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public TensorBoardDTO startTensorBoard(Project project, Users user, HdfsUsers hdfsUser, String hdfsLogdir)
        throws IOException {

    String prog = settings.getHopsworksDomainDir() + "/bin/tensorboard.sh";
    Process process = null;
    Integer port = 0;
    BigInteger pid = null;
    String tbBasePath = settings.getStagingDir() + Settings.TENSORBOARD_DIRS + File.separator;
    String projectUserUniquePath = project.getName() + "_" + hdfsUser.getName();
    String tbPath = tbBasePath + DigestUtils.sha256Hex(projectUserUniquePath);
    String certsPath = "\"\"";

    File tbDir = new File(tbPath);
    if (tbDir.exists()) {
        for (File file : tbDir.listFiles()) {
            if (file.getName().endsWith(".pid")) {
                String pidContents = com.google.common.io.Files.readFirstLine(file, Charset.defaultCharset());
                try {
                    pid = BigInteger.valueOf(Long.parseLong(pidContents));
                    if (pid != null && ping(pid) == 0) {
                        killTensorBoard(pid);
                    }
                } catch (NumberFormatException nfe) {
                    LOGGER.log(Level.WARNING,
                            "Expected number in pidfile " + file.getAbsolutePath() + " got " + pidContents);
                }
            }
        }
        FileUtils.deleteDirectory(tbDir);
    }
    tbDir.mkdirs();

    DistributedFileSystemOps dfso = dfsService.getDfsOps();
    try {
        certsPath = tbBasePath + DigestUtils.sha256Hex(projectUserUniquePath + "_certs");
        File certsDir = new File(certsPath);
        certsDir.mkdirs();
        HopsUtils.materializeCertificatesForUserCustomDir(project.getName(), user.getUsername(),
                settings.getHdfsTmpCertDir(), dfso, certificateMaterializer, settings, certsPath);
    } catch (IOException ioe) {
        LOGGER.log(Level.SEVERE,
                "Failed in materializing certificates for " + hdfsUser + " in directory " + certsPath, ioe);
        HopsUtils.cleanupCertificatesForUserCustomDir(user.getUsername(), project.getName(),
                settings.getHdfsTmpCertDir(), certificateMaterializer, certsPath, settings);
    } finally {
        if (dfso != null) {
            dfsService.closeDfsClient(dfso);
        }
    }

    String anacondaEnvironmentPath = settings.getAnacondaProjectDir(project.getName());
    int retries = 3;

    while (retries > 0) {

        if (retries == 0) {
            throw new IOException(
                    "Failed to start TensorBoard for project=" + project.getName() + ", user=" + user.getUid());
        }

        // use pidfile to kill any running servers
        port = ThreadLocalRandom.current().nextInt(40000, 59999);

        String[] command = new String[] { "/usr/bin/sudo", prog, "start", hdfsUser.getName(), hdfsLogdir,
                tbPath, port.toString(), anacondaEnvironmentPath, settings.getHadoopVersion(), certsPath,
                settings.getJavaHome() };

        LOGGER.log(Level.INFO, Arrays.toString(command));
        ProcessBuilder pb = new ProcessBuilder(command);

        try {
            // Send both stdout and stderr to the same stream
            pb.redirectErrorStream(true);

            process = pb.start();

            synchronized (pb) {
                try {
                    // Wait until the launcher bash script has finished
                    process.waitFor(20l, TimeUnit.SECONDS);
                } catch (InterruptedException ex) {
                    LOGGER.log(Level.SEVERE, "Woken while waiting for the TensorBoard to start: {0}",
                            ex.getMessage());
                }
            }

            int exitValue = process.exitValue();
            String pidPath = tbPath + File.separator + port + ".pid";
            File pidFile = new File(pidPath);
            // Read the pid for TensorBoard server
            if (pidFile.exists()) {
                String pidContents = com.google.common.io.Files.readFirstLine(pidFile,
                        Charset.defaultCharset());
                pid = BigInteger.valueOf(Long.parseLong(pidContents));
            }
            if (exitValue == 0 && pid != null) {
                int maxWait = 10;
                String logFilePath = tbPath + File.separator + port + ".log";
                File logFile = new File(logFilePath);
                while (maxWait > 0) {
                    String logFileContents = com.google.common.io.Files.readFirstLine(logFile,
                            Charset.defaultCharset());
                    // It is not possible to have a fixed wait time before showing the TB, we need to be sure it has started
                    if (logFile.length() > 0
                            && (logFileContents.contains("Loaded") | logFileContents.contains("Reloader")
                                    | logFileContents.contains("event")) | maxWait == 1) {
                        Thread.currentThread().sleep(5000);
                        TensorBoardDTO tensorBoardDTO = new TensorBoardDTO();
                        String host = null;
                        try {
                            host = InetAddress.getLocalHost().getHostAddress();
                        } catch (UnknownHostException ex) {
                            Logger.getLogger(TensorBoardProcessMgr.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        tensorBoardDTO.setEndpoint(host + ":" + port);
                        tensorBoardDTO.setPid(pid);
                        return tensorBoardDTO;
                    } else {
                        Thread.currentThread().sleep(1000);
                        maxWait--;
                    }
                }
                TensorBoardDTO tensorBoardDTO = new TensorBoardDTO();
                tensorBoardDTO.setPid(pid);
                String host = null;
                try {
                    host = InetAddress.getLocalHost().getHostAddress();
                } catch (UnknownHostException ex) {
                    Logger.getLogger(TensorBoardProcessMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
                tensorBoardDTO.setEndpoint(host + ":" + port);
                return tensorBoardDTO;
            } else {
                LOGGER.log(Level.SEVERE,
                        "Failed starting TensorBoard got exitcode " + exitValue + " retrying on new port");
                if (pid != null) {
                    this.killTensorBoard(pid);
                }
                pid = null;
            }

        } catch (Exception ex) {
            LOGGER.log(Level.SEVERE, "Problem starting TensorBoard: {0}", ex);
            if (process != null) {
                process.destroyForcibly();
            }
        } finally {
            retries--;
        }
    }

    //Failed to start TensorBoard, make sure there is no process running for it! (This should not be needed)
    if (pid != null && this.ping(pid) == 0) {
        this.killTensorBoard(pid);
    }

    //Certificates cleanup in case they were materialized but no TB started successfully

    dfso = dfsService.getDfsOps();
    certsPath = tbBasePath + DigestUtils.sha256Hex(projectUserUniquePath + "_certs");
    File certsDir = new File(certsPath);
    certsDir.mkdirs();
    try {
        HopsUtils.cleanupCertificatesForUserCustomDir(user.getUsername(), project.getName(),
                settings.getHdfsTmpCertDir(), certificateMaterializer, certsPath, settings);
    } finally {
        if (dfso != null) {
            dfsService.closeDfsClient(dfso);
        }
    }

    return null;
}

From source file:com.anthemengineering.mojo.infer.InferMojo.java

/**
 * Executes infer once for each source file and writes the output to {@code inferOutputDir}.
 *
 * @param classpath classpath used as an argument to the javac command given to Infer.
 * @param inferOutputDir directory where Infer will write its output
 * @param sourceFiles collection of files for Infer to analyze
 * @param numSourceFiles number of source files to analyze; used to make sure every Infer execution finishes
 * before moving on.//from w w w .  j a v  a 2s  .c o m
 */
private void completeInferExecutions(final String classpath, final File inferOutputDir,
        Collection<File> sourceFiles, int numSourceFiles) throws MojoExecutionException {
    // temporary directory for storing .class files created by {@code javac}; placed in build directory
    final File buildTmpDir = new File(project.getBuild().getDirectory(), JAVAC_OUTPUT_DIRECTORY_NAME);
    try {
        FileUtils.forceMkdir(buildTmpDir);
    } catch (final IOException e) {
        final String errMsg = String.format("Unable to make temp directory %s!", buildTmpDir.getAbsolutePath());
        getLog().error(errMsg, e);
        throw new MojoExecutionException(errMsg, e);
    }
    buildTmpDir.deleteOnExit();

    // used to wait for all processes running infer to complete
    final CountDownLatch doneSignal = new CountDownLatch(numSourceFiles);

    // TODO: optionally allow debugging info? Output directory?

    // TODO: a better way to do this may be to determine if there is an entry point that takes a set of source
    //  files and the classpath and use this. @See mvn, inferj and inferlib in the infer repository.

    ExecutorService pool = null;
    try {
        pool = Executors.newFixedThreadPool(4);

        for (final File sourceFile : sourceFiles) {
            final Runnable r = new Runnable() {
                @Override
                public void run() {
                    Process proc = null;

                    try {
                        // infer
                        final List<String> command = new ArrayList<String>();
                        command.add(inferPath);
                        command.add("-i");
                        command.add("-o");
                        command.add(inferOutputDir.getAbsolutePath());

                        command.add("--");

                        // javac
                        command.add("javac");
                        command.add(sourceFile.getAbsolutePath());
                        command.add("-d");
                        command.add(buildTmpDir.getAbsolutePath());
                        command.add("-classpath");
                        command.add(classpath);

                        final ProcessBuilder builder = new ProcessBuilder(command);
                        builder.environment().putAll(System.getenv());

                        if (consoleOut) {
                            builder.redirectErrorStream(true);
                            proc = builder.start();

                            InputStreamReader isr = null;
                            BufferedReader br = null;
                            InputStream pis = null;

                            try {
                                pis = proc.getInputStream();

                                isr = new InputStreamReader(pis);
                                br = new BufferedReader(isr);
                                String line = null;
                                while ((line = br.readLine()) != null) {
                                    getLog().info(line);
                                }
                            } catch (final IOException e) {
                                getLog().error(String.format("Error writing process output for file: %s.",
                                        sourceFile.getAbsolutePath()), e);
                            } finally {
                                if (isr != null) {
                                    isr.close();
                                }

                                if (br != null) {
                                    br.close();
                                }

                                if (pis != null) {
                                    pis.close();
                                }
                            }
                        } else {
                            // no logging.
                            proc = builder.start();
                        }

                        // NOTE: most/all executions end in failure during analysis, however,
                        // supported java bugs are still reported
                        proc.waitFor();
                    } catch (final IOException e) {
                        getLog().error(
                                "Exception occurred while trying to perform Infer execution; output not complete"
                                        + "",
                                e);
                    } catch (final InterruptedException e) {
                        getLog().error(EARLY_EXECUTION_TERMINATION_EXCEPTION_MSG, e);
                    } finally {
                        try {
                            // currently they all fail, although java bugs are still reported
                            if (proc != null && proc.exitValue() != 0) {
                                FAILED_CHECKS.put(sourceFile, proc.exitValue());
                            }
                        } catch (final Exception e) {
                            FAILED_CHECKS.put(sourceFile, -1);
                        }
                        doneSignal.countDown();
                    }
                }
            };

            pool.submit(r);
        }
    } finally {
        if (pool != null) {
            pool.shutdown();
        }
    }
    try {
        doneSignal.await();
    } catch (final InterruptedException e) {
        getLog().error(EARLY_EXECUTION_TERMINATION_EXCEPTION_MSG, e);
    }
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Flat-start initialization for automatic labeling
 * @throws Exception//from ww w  . j a  v a2  s  . c  om
 */
private void herestTraining() throws Exception {

    String herest = getProp(HTKDIR) + File.separator + "HERest";
    String hhed = getProp(HTKDIR) + File.separator + "HHEd";

    File htkFile = new File(herest);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }

    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String hhedconf = getProp(HTDIR) + File.separator + "config" + File.separator + "sil.hed";

    String hhedconf_vp = getProp(HTDIR) + File.separator + "config" + File.separator + "sil_vp.hed";

    String trainList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";
    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.list";

    String hmmDir = getProp(HTDIR) + File.separator + "hmm" + File.separator;
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones.mlf";

    int BEST_ITERATION = MAX_ITERATIONS;
    int SP_ITERATION = -1;
    int VP_ITERATION = -1;
    int FA_ITERATION = -1;
    int change_mix_iteration = -1;
    for (int iteration = 1; iteration <= MAX_ITERATIONS; iteration++) {

        System.out.println("Iteration number: " + iteration);

        File hmmItDir = new File(hmmDir + "hmm" + iteration);
        if (!hmmItDir.exists())
            hmmItDir.mkdir();

        Runtime rtime = Runtime.getRuntime();
        //get a shell
        Process process = rtime.exec("/bin/bash");
        //get an output stream to write to the shell
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

        if (PHASE_NUMBER == 0) {

            if (iteration == (SP_ITERATION + 1)) {

                phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones2.mlf";
                phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone2.list";

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);

                //now we enter in PHASE 1 
                PHASE_NUMBER = 1;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                continue;
            }

            // check epsilon_array  
            if (iteration > 2) {
                if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                        || iteration == MAX_SP_ITERATION) {
                    SP_ITERATION = iteration;
                    insertShortPause(iteration);
                    String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                    String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                    FileUtils.copy(oldMacro, newMacro);

                    // copy of logProbFrame_array in current iteration 
                    logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                    epsilon_array.add(100000000.0);
                    continue;
                }
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 1) {
            if (iteration == (VP_ITERATION + 1)) {
                phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones3.mlf";
                phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_vp + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_vp + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);

                //now we enter in PHASE 2
                PHASE_NUMBER = 2;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                continue;
            }

            // check epsilon_array  
            if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                    || iteration == MAX_VP_ITERATION) {
                VP_ITERATION = iteration;
                insertVirtualPauseThreeStates(iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 2) {
            if (iteration == (FA_ITERATION + 1)) {
                String logfile = "log_hviteMultiplePronunciationAligning_" + iteration + ".txt";
                String labDir = "'*'";//getProp(HTDIR)+File.separator+"lab";
                String alignedMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";
                hviteMultiplePronunciationAligning("hmm" + (iteration - 1), alignedMlf, false, labDir, true,
                        logfile, false);
                phoneMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";

                System.out.println("Copy hmm" + (iteration - 1) + " in " + "hmm" + iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);
                String oldHmmdefs = hmmDir + "hmm" + (iteration - 1) + File.separator + "hmmdefs";
                String newHmmdefs = hmmDir + "hmm" + iteration + File.separator + "hmmdefs";
                FileUtils.copy(oldHmmdefs, newHmmdefs);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                //now we enter in PHASE 3
                PHASE_NUMBER = 3;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);

                continue;
            }

            // check epsilon_array  
            if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                    || iteration == MAX_FA_ITERATION) {
                FA_ITERATION = iteration;

                System.out.println("Copy hmm" + (iteration - 1) + " in " + "hmm" + iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);
                String oldHmmdefs = hmmDir + "hmm" + (iteration - 1) + File.separator + "hmmdefs";
                String newHmmdefs = hmmDir + "hmm" + iteration + File.separator + "hmmdefs";
                FileUtils.copy(oldHmmdefs, newHmmdefs);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 3) {
            // check epsilon_array  
            // the following change_mix_iteration + 2 is used to allow more than one re-estimation after insertion of new mixture
            // Because just after the insertion the delta can be negative 

            if (((iteration != change_mix_iteration + 2)
                    && (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]))
                    || iteration == MAX_MIX_ITERATION) {

                System.out.println("Condition = true: " + "iteration=" + iteration + " change_mix_iteration="
                        + change_mix_iteration + " epsilon_array.get(iteration-2)="
                        + epsilon_array.get(iteration - 2) + " epsilon_PHASE[PHASE_NUMBER]="
                        + epsilon_PHASE[PHASE_NUMBER] + " MAX_MIX_ITERATION" + MAX_MIX_ITERATION);

                change_mix_iteration = iteration;
                MAX_MIX_ITERATION = -1;

                // Creating Increasing mixture config file dynamic iteration
                String hhedconf_mix = getProp(HTDIR) + File.separator + "config" + File.separator + "sil_mix_"
                        + iteration + ".hed";
                File file = new File(hhedconf_mix);
                PrintWriter hhed_conf_pw = new PrintWriter(new FileWriter(file));

                //MU 3 {*.state[2].mix}
                Boolean need_other_updates = false;
                for (int state = 0; state < num_mixtures_for_state.length; state++) {
                    if (current_number_of_mixtures[state] < num_mixtures_for_state[state]) {
                        int wanted_mix = current_number_of_mixtures[state] + 1;
                        int state_to_print = state + 2;
                        hhed_conf_pw.println("MU " + wanted_mix + "{*.state[" + state_to_print + "].mix}");

                        current_number_of_mixtures[state] = wanted_mix;

                        if (current_number_of_mixtures[state] < num_mixtures_for_state[state]) {
                            need_other_updates = true;
                        }
                    }
                }

                if (!need_other_updates) {
                    // copy of logProbFrame_array in current iteration 
                    //logProbFrame_array.add(logProbFrame_array.get(iteration-2));
                    //epsilon_array.add(100000000.0);
                    //now we enter in PHASE 3
                    PHASE_NUMBER = PHASE_NUMBER + 1;
                    System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                    String logfile = "log_hviteMultiplePronunciationAligning_" + iteration + ".txt";
                    String labDir = "'*'";//getProp(HTDIR)+File.separator+"lab";
                    String alignedMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";
                    hviteMultiplePronunciationAligning("hmm" + (iteration - 1), alignedMlf, false, labDir, true,
                            logfile, false);
                    phoneMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";

                    //continue;
                }

                hhed_conf_pw.flush();
                hhed_conf_pw.close();

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_mix + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_mix + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 4) {
            // check epsilon_array
            if (((iteration != change_mix_iteration + 2)
                    && (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]))
                    || iteration == MAX_ITERATIONS) {
                int last = iteration - 1;
                int previus_last = iteration - 2;

                System.out.println(
                        "Average log prob per frame has not beeen increased too much respect the previus iteration:");
                System.out.println("Average log prob per frame at last HREST iteration (" + last + ")-> "
                        + logProbFrame_array.get(iteration - 2));
                System.out.println("Average log prob per frame at previus HREST iteration (" + previus_last
                        + ")-> " + logProbFrame_array.get(iteration - 3));
                System.out.println("Delta -> " + epsilon_array.get(iteration - 2));
                System.out.println("Suggested Action -> stop the iterations.");

                if (logProbFrame_array.get(iteration - 3) > logProbFrame_array.get(iteration - 2)) {
                    BEST_ITERATION = iteration - 2;
                } else {
                    BEST_ITERATION = iteration - 1;
                }
                break;
            }
        }

        //Normal HEREST:
        System.out.println("( cd " + getProp(HTDIR) + "; " + herest + " " + HTK_SO + " -C " + configFile
                + " -I " + phoneMlf + " -t 250.0 150.0 1000.0" + " -S " + trainList + " -H " + hmmDir + "hmm"
                + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm" + (iteration - 1)
                + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration + " " + phoneList
                + " >> log_herestTraining_" + iteration + ".txt" + "; exit )\n");

        pw.println("( cd " + getProp(HTDIR) + "; " + herest + " " + HTK_SO + " -C " + configFile + " -I "
                + phoneMlf + " -t 250.0 150.0 1000.0" + " -S " + trainList + " -H " + hmmDir + "hmm"
                + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm" + (iteration - 1)
                + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration + " " + phoneList
                + " >> log_herestTraining_" + iteration + ".txt" + "; exit )\n");
        pw.flush();
        //shut down
        pw.close();
        process.waitFor();
        // check exit value
        if (process.exitValue() != 0) {
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            throw new MaryConfigurationException(errorReader.readLine());
        }

        // update average_log_prob_per_frame and deltas
        check_average_log_prob_per_frame(iteration);

        System.out.println("Delta average log prob per frame to respect previus iteration-> "
                + epsilon_array.get(iteration - 1));
        System.out.println("Current PHASE: " + PHASE_NUMBER);
        System.out.println("Current state and number of mixtures (for each phoneme): "
                + Arrays.toString(current_number_of_mixtures));

        System.out.println("---------------------------------------");
    }

    System.out.println("***********\n");
    System.out.println("BEST ITERATION: " + BEST_ITERATION);
    System.out.println("COPYNING BEST ITERATION FILES IN hmm-final directory");
    System.out.println("logProbFrame_array:" + logProbFrame_array.toString());

    System.out.println("epsilon_array:" + epsilon_array.toString());

    System.out.println("***********\n");

    String oldMacro = hmmDir + "hmm" + BEST_ITERATION + File.separator + "macros";
    String newMacro = hmmDir + "hmm-final" + File.separator + "macros";
    FileUtils.copy(oldMacro, newMacro);

    String oldHmmdefs = hmmDir + "hmm" + BEST_ITERATION + File.separator + "hmmdefs";
    String newHmmdefs = hmmDir + "hmm-final" + File.separator + "hmmdefs";
    FileUtils.copy(oldHmmdefs, newHmmdefs);

}