Exemplo n.º 1
0
  /**
   * 系统程序调用接口,调试使用
   *
   * @param args
   * @return
   */
  public int exec(String[] args) {

    int exitVal = 0;
    if (args.length < 1) {
      return 1;
    }
    try {
      String osName = System.getProperty("os.name");
      String[] cmd = new String[2 + args.length];

      // "Windows NT","Windows XP", e.g
      cmd[0] = "cmd.exe";
      cmd[1] = "/C";
      for (int i = 0; i < args.length; i++) {
        cmd[2 + i] = args[i];
      }

      Runtime rt = Runtime.getRuntime();
      Process proc = rt.exec(cmd);

      StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", false);
      outputGobbler.start();

      exitVal = proc.waitFor();
      System.out.println("exec exit value: " + exitVal);

    } catch (Throwable t) {
      t.printStackTrace();
    }
    return exitVal;
  }
Exemplo n.º 2
0
  private void callCMD() throws Throwable {
    StreamGobbler errorGobbler;
    StreamGobbler outputGobbler;

    // Execute args command...
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(this.cmd);

    // Check Error...
    errorGobbler = run(proc, "ERR", this.delay);

    // Check Output...
    outputGobbler = run(proc, "ICC", this.delay);

    // Any error???
    proc.waitFor();

    // Set Error List...
    List<String> tmp_linese = new ArrayList<String>();
    tmp_linese = errorGobbler.getOs_lines();

    // Set Output List...
    List<String> tmp_linesi = new ArrayList<String>();
    tmp_linesi = outputGobbler.getOs_lines();

    if (tmp_linesi.size() > 0) this.setGetOutList(tmp_linesi);
    else this.setGetOutList(tmp_linese);
  }
  private int execProcess(List<String> cmds, ShellCallback sc)
      throws IOException, InterruptedException {

    ProcessBuilder pb = new ProcessBuilder(cmds);
    pb.directory(fileBinDir);

    StringBuffer cmdlog = new StringBuffer();

    for (String cmd : cmds) {
      cmdlog.append(cmd);
      cmdlog.append(' ');
    }

    Log.v(TAG, cmdlog.toString());

    //	pb.redirectErrorStream(true);
    Process process = pb.start();

    // any error message?
    StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR", sc);

    // any output?
    StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT", sc);

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    int exitVal = process.waitFor();

    sc.processComplete(exitVal);

    return exitVal;
  }
Exemplo n.º 4
0
  public void run() {
    Throwable tr = null;
    try {
      List<Object> cmdArray = ((List<Object>) (new ArrayList<Object>(20)));
      cmdArray.add(((Object) (SettingsAdapter.getInstance().getMPlayerCommand())));
      if (MPlayer.isChangingProcessPrioritySupported()) {
        cmdArray.add("-priority");
        cmdArray.add(((Object) (SettingsAdapter.getInstance().getMPlayerProcessPriority().name())));
      }
      cmdArray.add("-ss");
      cmdArray.add(((Object) (Long.toString(mOffset))));
      cmdArray.add(((Object) (player.mFile.getPath())));
      if (logger.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        String s;
        for (Iterator<Object> iterator = cmdArray.iterator();
            iterator.hasNext();
            sb.append(s).append(' ')) s = (String) iterator.next();

        logger.debug(
            ((Object)
                ((new StringBuilder("Executing ["))
                    .append(((Object) (sb)))
                    .append("]")
                    .toString())));
      }
      mProc =
          Runtime.getRuntime()
              .exec((String[]) cmdArray.toArray(((Object[]) (new String[cmdArray.size()]))));
      logger.debug(
          ((Object) ((new StringBuilder("  as process ")).append(((Object) (mProc))).toString())));
      threadStartupDone = true;
      StreamGobbler errorGobbler = new StreamGobbler("stderr", mProc.getErrorStream());
      errorGobbler.start();
      StreamGobbler outputGobbler = new StreamGobbler("stdout", mProc.getInputStream());
      outputGobbler.start();
      int exitCode = mProc.waitFor();
      logger.debug(
          ((Object)
              ((new StringBuilder())
                  .append(((Object) (mProc)))
                  .append(" finished with exit code ")
                  .append(exitCode)
                  .toString())));
    } catch (Exception e) {
      logger.error("", ((Throwable) (e)));
      tr = ((Throwable) (e));
    } finally {
      threadStartupDone = true;
      terminated = true;
    }
    if (tr != null) player.fireExceptionEvent(tr);
  }
Exemplo n.º 5
0
  /** Executes the command. */
  public Process execute() throws IOException {

    // Execute the command using the specified environment
    currentProcess = super.execute();

    String prefix = (getExecutable() != null) ? getExecutable() + " - " : "";
    stdOutGobbler = new StreamGobbler(currentProcess.getInputStream(), prefix + "StdOut");
    stdErrGobbler = new StreamGobbler(currentProcess.getErrorStream(), prefix + "StdErr");
    stdOutGobbler.start();
    stdErrGobbler.start();

    // Just to be compatible
    return currentProcess;
  }
Exemplo n.º 6
0
  private static StreamGobbler run(Process proc, String typ, int delay) {
    StreamGobbler tmpGobbler;

    // Any error message?
    if (typ.equals("ERR")) {
      if (delay > 0) tmpGobbler = new StreamGobbler(proc.getErrorStream(), typ, delay);
      else tmpGobbler = new StreamGobbler(proc.getErrorStream(), typ);
    } else {
      if (delay > 0) tmpGobbler = new StreamGobbler(proc.getInputStream(), typ, delay);
      else tmpGobbler = new StreamGobbler(proc.getInputStream(), typ);
    }

    // Start process...
    tmpGobbler.start();

    return tmpGobbler;
  }
Exemplo n.º 7
0
  public Process waitForProcessFinished() throws IOException, IllegalStateException {
    if (currentProcess == null) throw new IllegalStateException("No running process");

    // Wait for the command to complete
    try {
      currentProcess.waitFor();
    } catch (InterruptedException e) {
      logger.error(
          "Thread was interrupted while executing command \"" + this.toString() + "\".", e);
    }

    // make sure last outputs are also captured, even if no new-line was at the
    // end...
    stdOutGobbler.readRemainder();
    stdErrGobbler.readRemainder();

    // Just to be compatible
    return currentProcess;
  }
  public void convert2SWF(String inputFile, String swfFile, String extend) {
    File pdfFile = new File(inputFile);
    File outFile = new File(swfFile);

    if (!pdfFile.exists()) {
      org.core.util.LogUtil.info("PDF文件不存在!");
      return;
    }

    if (outFile.exists()) {
      org.core.util.LogUtil.info("SWF文件已存在!");
      return;
    }
    String command =
        ConStant.getSWFToolsPath(extend)
            + " \""
            + inputFile
            // ---update-for--文件预览无效------ scott@2013/05/22 ADD START
            + "\" -o "
            + " \""
            + swfFile
            + " \""
            + " -s languagedir=D:\\xpdf-chinese-simplified -T 9 -f";
    //				+ "\" -o " + swfFile + " -s languagedir=D:\\xpdf-chinese-simplified -T 9 -f";
    //		---update-for--文件预览无效----- scott@2013/05/22 ADD END
    try {
      // 开始转换文档
      Process process = Runtime.getRuntime().exec(command);
      StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "Error");
      StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "Output");
      errorGobbler.start();
      outputGobbler.start();
      try {
        process.waitFor();
        org.core.util.LogUtil.info("时间-------" + process.waitFor());
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  private void launchRenderer() {
    try {
      String myCommand = PATH_TO_RENDERER + " " + mCurrentXMLFileName;
      Process mProcess = Runtime.getRuntime().exec(myCommand);
      System.out.println("### EXECUTE: " + myCommand);

      StreamGobbler mErrorGobbler = new StreamGobbler(mProcess.getErrorStream(), "ERR");
      StreamGobbler mOutputGobbler = new StreamGobbler(mProcess.getInputStream(), "OUT");
      mErrorGobbler.start();
      mOutputGobbler.start();

      if (BLOCK_PROCESS) {
        try {
          mProcess.waitFor();
        } catch (InterruptedException ex) {
          ex.printStackTrace();
        }
      }

      System.out.println("### DONE EXECUTING PROCESS.");
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
Exemplo n.º 10
0
 public void setStdoutAlert(String alert) {
   stdOutGobbler.setAlert(alert);
 }
Exemplo n.º 11
0
 public boolean isAlertDone() {
   return stdOutGobbler.isAlertDone();
 }
Exemplo n.º 12
0
 public void resetBuffers() {
   stdOutGobbler.resetBuffer();
   stdErrGobbler.resetBuffer();
 }
Exemplo n.º 13
0
 public String getStderr() {
   return stdErrGobbler == null ? "" : stdErrGobbler.getAllLines();
 }
Exemplo n.º 14
0
 public String getStdout() {
   return stdOutGobbler == null ? "" : stdOutGobbler.getAllLines();
 }
Exemplo n.º 15
0
  public void run() {
    try {
      long startTime = System.currentTimeMillis();

      logger.debug(id + "> Creating " + descFile.getName());
      createDescFile();

      logger.debug(id + "> Dumping features to " + featFile.getName());
      dumpFeatureVectors();

      logger.debug(id + "> Dumping distance matrix to " + distFile.getName());
      binarySaveDistanceMatrix();

      logger.debug(id + "> Calling wagon as follows:");
      logger.debug(systemCall);
      Process p = Runtime.getRuntime().exec(systemCall);
      // collect the output
      // read from error stream
      StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), id + " err");

      // read from output stream
      StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), id + " out");
      // start reading from the streams
      errorGobbler.start();
      outputGobbler.start();
      p.waitFor();
      if (p.exitValue() != 0) {
        finished = true;
        success = false;
      } else {
        success = true;
        logger.debug(id + "> Wagon call took " + (System.currentTimeMillis() - startTime) + " ms");

        // read in the resulting CART
        logger.debug(id + "> Reading CART");
        BufferedReader buf = new BufferedReader(new FileReader(cartFile));
        WagonCARTReader wagonReader = new WagonCARTReader(LeafType.IntAndFloatArrayLeafNode);
        cart = new CART(wagonReader.load(buf, featureDefinition), featureDefinition);
        buf.close();

        // Fix the new cart's leaves:
        // They are currently the index numbers in featureVectors;
        // but what we need is the unit index numbers!
        for (LeafNode leaf : cart.getLeafNodes()) {
          int[] data = (int[]) leaf.getAllData();
          for (int i = 0; i < data.length; i++) {
            data[i] = fv[data[i]].getUnitIndex();
          }
        }

        logger.debug(id + "> completed in " + (System.currentTimeMillis() - startTime) + " ms");
        finished = true;
      }
      if (!Boolean.getBoolean("wagon.keepfiles")) {
        featFile.delete();
        distFile.delete();
      }

    } catch (Exception e) {
      e.printStackTrace();
      finished = true;
      success = false;
      throw new RuntimeException("Exception running wagon");
    }
  }
Exemplo n.º 16
0
 /**
  * Returns the stderr from the command as a List of Strings where each String is one line of the
  * error.
  *
  * @return The standard error of the command as a <code>List</code> of output lines.
  */
 public List<String> getStderrAsList() {
   return stdErrGobbler != null ? stdErrGobbler.getStreamDataAsList() : null;
 }
Exemplo n.º 17
0
  public void execute() throws MojoExecutionException {
    getLog().debug("Started maven-jlint-plugin: execute() method");
    getLog().info("Running Maven-Jlint-Plugin : ");

    // Setup ResourceManager
    locator.addSearchPath(
        FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath());
    locator.addSearchPath("url", "");
    locator.setOutputDirectory(new File(project.getBuild().getDirectory()));

    getLog().info("Configuration File used: " + configLocation);

    // File f = outputDirectory;
    JlintViolationHandler violationHandler = new JlintViolationHandler(getLog());
    violationHandler.setTargetDir(outputDirectory.toString());
    violationHandler.setClassesDir(
        System.getProperty("file.separator") + Constants.JLINT_CLASSES_DIR);

    JlintConfiguration jlintConfig = new JlintConfiguration(locator, configLocation, getLog());

    String cmdDisableCategories =
        jlintConfig.getCategoriesToDisable(violationHandler.getDefaultMessageList());

    String cmd =
        Constants.JLINT_CMD
            + cmdDisableCategories
            + outputDirectory.toString()
            + System.getProperty("file.separator")
            + Constants.JLINT_CLASSES_DIR;

    getLog()
        .info(
            "Classes Directory : "
                + outputDirectory.toString()
                + System.getProperty("file.separator")
                + Constants.JLINT_CLASSES_DIR);
    getLog().debug("Disabled Categories: " + cmdDisableCategories);
    getLog().info("Executing Jlint with command : [" + cmd + "]");

    // Open output files for writing
    File xmlFile = new File(outputDirectory, Constants.JLINT_XML_OUTPUT_FILENAME);
    File txtFile = new File(outputDirectory, Constants.JLINT_TXT_OUTPUT_FILENAME);
    File errFile = new File(outputDirectory, Constants.JLINT_ERROR_FILENAME);

    String s = null;

    try {

      // run the Jlint command on the "classes" directory

      getLog().debug("execute: About to execute jlint command");

      Process p = Runtime.getRuntime().exec(cmd);

      /*
      BufferedReader stdInput = new BufferedReader(new
           InputStreamReader(p.getInputStream()));

      BufferedReader stdError = new BufferedReader(new
           InputStreamReader(p.getErrorStream()));


      outputXmlFile = new BufferedWriter(new FileWriter(xmlFile));
      outputTxtFile = new BufferedWriter(new FileWriter(txtFile));
      errorFile = new BufferedWriter(new FileWriter(errFile));
      */

      FileOutputStream outputXmlFile = new FileOutputStream(xmlFile);
      FileOutputStream outputTxtFile = new FileOutputStream(txtFile);
      FileOutputStream errorFile = new FileOutputStream(errFile);

      // Create the output file
      /*
      outputXmlFile.write(Constants.XML_HEADER);
      outputXmlFile.newLine();
      outputXmlFile.newLine();
      outputXmlFile.write(Constants.ROOT_START_TAG);
      */

      StreamGobbler inputGobbler =
          new StreamGobbler(p.getInputStream(), outputXmlFile, outputTxtFile, "INPUT");

      inputGobbler.setViolationHandler(violationHandler);

      StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), errorFile, "ERROR");

      getLog().info("Reading the standard input and error for Jlint");
      inputGobbler.start();
      errorGobbler.start();

      int exitVal = p.waitFor();

      getLog().info("Jlint exited with status value: " + String.valueOf(exitVal));

      if (exitVal != 0) {
        getLog()
            .error(
                "Jlint did not execute properly. Check "
                    + Constants.JLINT_ERROR_FILENAME
                    + " file.");
      }

      inputGobbler.join();
      errorGobbler.join();

      getLog().info("** Jlint Report: **");
      getLog().info("-------------------");
      getLog()
          .info(
              "JLint Violations (if any): "
                  + String.valueOf(inputGobbler.getNofLines())
                  + " violations reported.");
      getLog().info("Jlint Error Messages (if any): ");
      for (String msg : errorGobbler.getErrorMsgList()) {
        getLog().error(msg);
      }
      getLog().info("----End Report-----");

    } catch (IOException e) {
      System.out.println("JLint: Exception Occured: ");
      e.printStackTrace();
      // System.exit(-1);
    } catch (InterruptedException e) {
      System.out.println("JLint: Exception Occured: ");
      e.printStackTrace();
    }
  }
Exemplo n.º 18
0
  public static void main(String[] args) throws Exception {
    boolean external = false; // should each MERT iteration be launched externally?

    if (args.length == 1) {
      if (args[0].equals("-h")) {
        printZMERTUsage(args.length, true);
        System.exit(2);
      } else {
        external = false;
      }
    } else if (args.length == 3) {
      external = true;
    } else {
      printZMERTUsage(args.length, false);
      System.exit(1);
    }

    if (!external) {
      MertCore myMert = new MertCore(args[0]);
      myMert.run_MERT(); // optimize lambda[]!!!
      myMert.finish();
    } else {

      int maxMem = Integer.parseInt(args[1]);
      String configFileName = args[2];
      String stateFileName = "ZMERT.temp.state";
      String cp = System.getProperty("java.class.path");
      boolean done = false;
      int iteration = 0;

      while (!done) {
        ++iteration;
        Runtime rt = Runtime.getRuntime();
        Process p =
            rt.exec(
                "java -Xmx"
                    + maxMem
                    + "m -cp "
                    + cp
                    + " joshua.zmert.MertCore "
                    + configFileName
                    + " "
                    + stateFileName
                    + " "
                    + iteration);
        /*
                BufferedReader br_i = new BufferedReader(new InputStreamReader(p.getInputStream()));
                BufferedReader br_e = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                String dummy_line = null;
                while ((dummy_line = br_i.readLine()) != null) { System.out.println(dummy_line); }
                while ((dummy_line = br_e.readLine()) != null) { System.out.println(dummy_line); }
        */
        StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), 1);
        StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), 1);

        errorGobbler.start();
        outputGobbler.start();

        int status = p.waitFor();

        if (status == 90) {
          done = true;
        } else if (status == 91) {
          done = false;
        } else {
          System.out.println("Z-MERT exiting prematurely (MertCore returned " + status + ")...");
          break;
        }
      }
    }

    System.exit(0);
  } // main(String[] args)
Exemplo n.º 19
0
  /**
   * This method run a partition and creates the report when all partitions of the experiment are
   * finished </p<
   */
  public synchronized void doIterate() {
    while (interrupted == true && stop == false) {
      try {
        wait();
      } catch (InterruptedException e) {
      }
    }
    // interrumpido = false;

    if (i < sentences.size() && stop == false) // else: All partitions have been finished
    {
      command = "";
      line = ((Element) sentences.get(i)).getChildren();
      // Caught the complet command in a String, except  algorithmType and seed (line.size()  -2)
      for (int j = 0; j < line.size() - 2; j++) {
        command += ((Element) line.get(j)).getText() + " ";
      }

      problemType = ((Element) line.get(line.size() - 2)).getText();

      Date now = Calendar.getInstance().getTime();
      System.out.println("*** BEGIN OF EXPERIMENT " + now + "\n");
      System.out.print("\nExecuting: " + command);

      StreamGobbler errorGobbler;
      StreamGobbler outputGobbler;

      // Initial time, in milliseconds
      timePartition = System.currentTimeMillis();
      try {
        rt = Runtime.getRuntime();
        proc = rt.exec(command);
        errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
        outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
        errorGobbler.start();
        outputGobbler.start();
        // wait to finish the extern process
        int exitVal = proc.waitFor();
        execExternalFinished = true;
        now = Calendar.getInstance().getTime();
        // Final time, in milliseconds
        timePartition = System.currentTimeMillis() - timePartition;
        System.out.println("ExitValue: " + exitVal);
        if (nJobFinished == true)
          // Inicializacion de las sentencias del nuevo trabajo
          sentencesAux = new ArrayList<Element>();

        if (exitVal != 0) // Error
        {
          System.out.println("\n*** ERROR - END OF EXPERIMENT!! " + now + "\n");
        } else // Experiment correct.Increase i
        {
          sentencesAux.add(sentences.get(i));
          System.out.println("\n*** END OF EXPERIMENT " + now + "\n");
          i++;
          if (contPart == 0) contPartReal = 0;
          contPart++;
          contPartReal = contPart;
          nJobFinished = false;

          // Job completed
          if (contPart == (int) partitionList.get(countJobs)) // sentencias.size())
          {
            if (problemType.equals("Method") == true) {
              EducationalMethodReport inf =
                  new EducationalMethodReport((ArrayList<Element>) sentencesAux, tipoExp);
              inf.running();
            } else if (problemType.equals("Preprocess-D") == true) {
              EducationalDiscretizerReport inf =
                  new EducationalDiscretizerReport((ArrayList<Element>) sentencesAux, tipoExp);
              inf.running();
            } else if (problemType.equals("Preprocess-TSS") == true) {
              EducationalISReport inf =
                  new EducationalISReport((ArrayList<Element>) sentencesAux, tipoExp);
              inf.running();
            } else if (problemType.equals("Preprocess-FS") == true) {
              EducationalFSReport inf =
                  new EducationalFSReport((ArrayList<Element>) sentencesAux, tipoExp);
              inf.running();
            }
            nJobFinished = true;
            NumberofJobFinished = countJobs;
            // It may be put in "if". Now all are methods
            contPart = 0;
            // Se pasa el siguiente trabajo que haya en RunKeel.xml
            countJobs++;
            // Se acabaron todos los trabajos
            if (i == sentences.size()) {
              stop = true;
            }
          }
        }
      } catch (Throwable t) {
        t.printStackTrace();
      }
    } else // All partitions have finished
    {
      stop = true;
    }
    execExternalFinished = false;
  }
Exemplo n.º 20
0
  protected static void runBuild(
      CommandLineProject project,
      PropertyTree config,
      File dir,
      File output,
      Build build,
      long buildId) {
    int result = -1;
    Writer buildOut = null;
    Process process = null;
    StreamGobbler serr = null, sout = null;
    try {
      buildOut = new FileWriter(output);

      String command = config.getProperty(CIApplication.CONFIGURATION_COMMAND_LINE.getKey());
      if (command == null) {
        command = (String) CIApplication.CONFIGURATION_COMMAND_LINE.getDefault();
      }

      // wrap it in a shell call so we can do things like "make && make install"
      String[] commands = new String[3];
      commands[0] = "sh";
      commands[1] = "-c";
      commands[2] = command;
      process = Runtime.getRuntime().exec(commands, null, dir);

      serr = new StreamGobbler(new InputStreamReader(process.getErrorStream()), buildOut);
      sout = new StreamGobbler(new InputStreamReader(process.getInputStream()), buildOut);
      serr.start();
      sout.start();

      result = process.waitFor();
    } catch (InterruptedException e) {
      // TODO use this hook when we cancel the process
    } catch (IOException e) {
      e.printStackTrace(new PrintWriter(buildOut));
      log.error("Unable to write to build output file - reported in build log", e);
    } finally {
      if (process != null) {
        // defensively try to close the gobblers
        if (serr != null && sout != null) {
          // check that our gobblers are finished...
          while (!serr.isComplete() || !sout.isComplete()) {
            log.debug("waiting 1s to close gobbler");
            try {
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              // we were just trying to tidy up...
            }
          }
        }

        IOUtil.close(process.getOutputStream());
        IOUtil.close(process.getErrorStream());
        IOUtil.close(process.getInputStream());
        process.destroy();
      }

      IOUtil.close(buildOut);
    }

    build.setEndTime(new Date());
    if (result != 0) {
      build.setStatus(Build.BUILD_FAILED);
    } else {
      build.setStatus(Build.BUILD_SUCCEEDED);
    }
  }
Exemplo n.º 21
0
 /**
  * Returns the stdout from the command as a String
  *
  * @return The standard output of the command as a <code>String</code>
  */
 public String getStdoutAsString() {
   return stdOutGobbler != null ? stdOutGobbler.getStreamDataAsString() : null;
 }
  public String runCmd(String command, String[] parms) throws ServiceException {
    String result = "";
    try {
      String osName = System.getProperty("os.name");
      LOG.debug("OS name: " + osName);
      String[] cmd;
      int nextParm;
      if (osName.equals("Windows NT") || osName.equals("Windows XP")) {
        cmd = new String[3 + parms.length];
        cmd[0] = "cmd.exe";
        cmd[1] = "/C";
        nextParm = 2;
      } else if (osName.equals("Windows 95")) {
        cmd = new String[3 + parms.length];
        cmd[0] = "command.com";
        cmd[1] = "/C";
        nextParm = 2;
      } else // Assuming that else is unix or unix-like in this regard of not needing an explicit
             // shell program.
      {
        cmd = new String[1 + parms.length];
        nextParm = 0;
      }

      // Example values to use for 'command' here:
      //   If using cygwin on a Windows OS:
      //     "c:\\cygwin\\bin\\telnet.exe 192.168.1.101"
      //     "c:\\cygwin\\bin\\expect.exe /tmp/telnettojbdesktop.exp"
      //   If using a Unix (I'm guessing here):
      //     "telnet 192.168.1.101"
      //     "expect /tmp/telnettojbdesktop.exp"
      cmd[nextParm++] = command;

      for (String p : parms) {
        cmd[nextParm++] = p;
      }

      if (LOG.isDebugEnabled()) {
        StringBuffer buffer = new StringBuffer();
        for (String a : cmd) {
          buffer.append(a).append(" ");
        }
        LOG.debug("Executing " + buffer.toString());
      }

      Runtime rt = Runtime.getRuntime();
      Process proc = rt.exec(cmd);

      StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");

      StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

      errorGobbler.start();
      outputGobbler.start();

      // This is the stream used to pass INPUT to the process. (It's called the "output stream"
      // because
      // it's an *output* stream from the perspective of this class.)
      OutputStream outputStream = proc.getOutputStream();

      int exitVal = proc.waitFor();
      if (exitVal != 0) {
        LOG.error("Exit value from " + cmd[2] + ": " + exitVal);
      }

      String stdErr = errorGobbler.getBuffer();
      if (stdErr.length() > 0) {
        result = stdErr;
      }

      String stdOut = outputGobbler.getBuffer();
      if (stdOut.length() > 0) {
        // It's intentional that stdOut replaces what was in stdErr if there *is* anything in
        // stdOut.
        result = stdOut;
      }
    } catch (Throwable t) {
      throw new ServiceException(ServiceError.RUNTIME_ERROR, t);
    }
    return result;
  }
Exemplo n.º 23
0
  @Override
  public double[] runClassifier(Vector<String> samples, double[] initialLambda, int featDim) {
    System.out.println("------- SVM training starts ------");

    double[] lambda = new double[featDim + 1];
    for (int i = 1; i <= featDim; i++) lambda[i] = 0;

    // String root_dir =
    // "/media/Data/JHU/Research/MT discriminative LM training/joshua_expbleu/PRO_test/";
    // String root_dir = "/home/ycao/WS11/nist_zh_en_percep/pro_forward/pro_libsvm/";

    try {
      // prepare training file for MegaM
      PrintWriter prt = new PrintWriter(new FileOutputStream(trainingFilePath));

      for (String line : samples) {
        String[] feat = line.split("\\s+");

        if (feat[feat.length - 1].equals("1")) prt.print("+1 ");
        else prt.print("-1 ");

        for (int i = 0; i < feat.length - 1; i++)
          prt.print((i + 1) + ":" + feat[i] + " "); // feat id starts from 1!

        prt.println();
      }
      prt.close();

      // start running SVM
      Runtime rt = Runtime.getRuntime();
      // String cmd = "/home/yuan/tmp_libsvm_command";

      Process p = rt.exec(commandFilePath); // only linear kernel is used

      StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), 1);
      StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), 1);

      errorGobbler.start();
      outputGobbler.start();

      int decStatus = p.waitFor();
      if (decStatus != 0) {
        System.out.println("Call to decoder returned " + decStatus + "; was expecting " + 0 + ".");
        System.exit(30);
      }

      // read the model file
      BufferedReader wt = new BufferedReader(new FileReader(modelFilePath));
      String line;
      boolean sv_start = false;
      double coef;

      while ((line = wt.readLine()) != null) {
        if (sv_start) // start reading support vectors and coefs
        {
          String[] val = line.split("\\s+");
          coef = Double.parseDouble(val[0]);

          // System.out.print(coef+" ");

          for (int i = 1; i < val.length; i++) // only valid for linear kernel
          // W = \sum_{i=1}^{l} y_i alpha_i phi(x_i)
          // = \sum_{i=1}^{l} coef_i x_i
          {
            String[] sv = val[i].split(":"); // feat id
            lambda[Integer.parseInt(sv[0])] += coef * Double.parseDouble(sv[1]); // index starts
            // from 1
            // System.out.print(Integer.parseInt(sv[0])+" "+Double.parseDouble(sv[1])+" ");
          }

          // System.out.println();
        }

        if (line.equals("SV")) sv_start = true;
      }

      File file = new File(trainingFilePath);
      file.delete();
      file = new File(modelFilePath);
      file.delete();
    } catch (IOException exception) {
      exception.getStackTrace();
    } catch (InterruptedException e) {
      System.err.println("InterruptedException in MertCore.run_decoder(int): " + e.getMessage());
      System.exit(99903);
      ;
    }

    System.out.println("------- SVM training ends ------");

    return lambda;
  }
Exemplo n.º 24
0
 public void setStdoutAlertNotify(Object toBeNotified) {
   stdOutGobbler.setAlertNotify(toBeNotified);
 }
Exemplo n.º 25
0
 /** First lines of stdout */
 public String getHead() {
   if (stdOutGobbler != null) return stdOutGobbler.getHead();
   return "";
 }
Exemplo n.º 26
0
  public int exec() {
    try {
      executing = true;
      ProcessBuilder pb = new ProcessBuilder(commandArgs);
      if (pwd != null) pb.directory(new File(pwd));

      if (debug) {
        Gpr.debug("PWD: " + pwd);
        for (String arg : commandArgs) Gpr.debug("ARGS: " + arg);
      }

      pr = pb.start();

      // ---
      // Prepare & start stdout/sdterr reader processes
      // ---
      stdErrGobbler = new StreamGobbler(pr.getErrorStream(), true); // StdErr
      stdOutGobbler = new StreamGobbler(pr.getInputStream(), false); // StdOut

      // Quiet? => Do not show
      if (quietStderr) stdErrGobbler.setQuietMode();
      if (quietStdout) stdOutGobbler.setQuietMode();

      // Keep a copy in memory?
      stdErrGobbler.setSaveLinesInMemory(saveStd);
      stdOutGobbler.setSaveLinesInMemory(saveStd);

      // Binary?
      stdErrGobbler.setBinary(binaryStderr);
      stdOutGobbler.setBinary(binaryStdout);

      // Redirect?
      if (redirectStderr != null) stdErrGobbler.setRedirectTo(redirectStderr);
      if (redirectStdout != null) stdOutGobbler.setRedirectTo(redirectStdout);

      // Filter stdout
      stdOutGobbler.setLineFilter(stdOutFilter);

      // Set this object as the progress monitor
      stdErrGobbler.setProgress(this);
      stdOutGobbler.setProgress(this);

      // Start gobblers
      stdErrGobbler.start();
      stdOutGobbler.start();

      // Assign StdIn
      stdin = pr.getOutputStream();

      // ---
      // Start process & wait until completion
      // ---
      started = true;

      // Wait for the process to finish and store exit value
      exitValue = pr.waitFor();

      // Wait for gobblers to finish processing the remaining of STDIN & STDERR
      while (stdOutGobbler.isRunning() || stdErrGobbler.isRunning()) {
        Thread.sleep(100);
      }

      if (debug && (exitValue != 0)) Gpr.debug("Exit value: " + exitValue);
    } catch (Exception e) {
      error = e.getMessage();
      exitValue = -1;
      if (showExceptions) e.printStackTrace();
    } finally {
      // We are done. Either process finished or an exception was raised.
      started = true;
      executing = false;
      if (objetcToNotify != null) {
        synchronized (objetcToNotify) {
          objetcToNotify.notify();
        }
      }
    }

    return exitValue;
  }
Exemplo n.º 27
0
 /** First lines of stderr */
 public String getHeadStderr() {
   if (stdErrGobbler != null) return stdErrGobbler.getHead();
   return "";
 }
Exemplo n.º 28
0
 /**
  * Returns the stdout from the command as a List of Strings where each String is one line of the
  * output.
  *
  * @return The standard output of the command as a <code>List</code> of output lines.
  */
 public List<String> getStdoutAsList() {
   return stdOutGobbler != null ? stdOutGobbler.getStreamDataAsList() : null;
 }
Exemplo n.º 29
0
 /**
  * Returns the stderr from the command as a String
  *
  * @return The standard error of the command as a <code>String</code>
  */
 public String getStderrAsString() {
   return stdErrGobbler != null ? stdErrGobbler.getStreamDataAsString() : null;
 }