Esempio n. 1
0
 public void evaluate() {
   try {
     // clear problems and console messages
     problemsView.setText("");
     consoleView.setText("");
     // update status view
     statusView.setText(" Parsing ...");
     tabbedPane.setSelectedIndex(0);
     LispExpr root = Parser.parse(textView.getText());
     statusView.setText(" Running ...");
     tabbedPane.setSelectedIndex(1);
     // update run button
     runButton.setIcon(stopImage);
     runButton.setActionCommand("Stop");
     // start run thread
     runThread = new RunThread(root);
     runThread.start();
   } catch (SyntaxError e) {
     tabbedPane.setSelectedIndex(0);
     System.err.println(
         "Syntax Error at " + e.getLine() + ", " + e.getColumn() + " : " + e.getMessage());
   } catch (Error e) {
     // parsing error
     System.err.println(e.getMessage());
     statusView.setText(" Errors.");
   }
 }
Esempio n. 2
0
    public void run() {
      try {
        while (runThread == this) {
          interpreter.evaluate(program);
          break;
        }
      } catch (Error e) {
        tabbedPane.setSelectedIndex(0);
        System.err.println("Runtime Error: " + e.getMessage());
      }

      // notify GUI that program execution finished
      runFinished();
    }
Esempio n. 3
0
 private static KRun obtainKRun(Context context) {
   if (K.backend.equals("maude")) {
     return new MaudeKRun(context);
   } else if (K.backend.equals("java")) {
     try {
       return new JavaSymbolicKRun(context);
     } catch (KRunExecutionException e) {
       Error.report(e.getMessage());
       return null;
     }
   } else {
     Error.report("Currently supported backends are 'maude' and 'java'");
     return null;
   }
 }
  public void highlight(
      final Pattern searchText, final Pattern markingPattern, Color color, int pageNr) {
    if (textCache == null || document == null) {
      throw new IllegalArgumentException("TextCache was not initialized");
    }

    final List<PDPage> pages = document.getDocumentCatalog().getAllPages();

    try {
      boolean found = false;

      final PDPage page = pages.get(pageNr - 1);
      PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);

      PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
      graphicsState.setNonStrokingAlphaConstant(0.5f);
      PDResources resources = page.findResources();
      Map graphicsStateDictionary = resources.getGraphicsStates();
      if (graphicsStateDictionary == null) {
        // There is no graphics state dictionary in the resources dictionary, create one.
        graphicsStateDictionary = new TreeMap();
      }
      graphicsStateDictionary.put("highlights", graphicsState);
      resources.setGraphicsStates(graphicsStateDictionary);

      for (Match searchMatch : textCache.match(pageNr, searchText)) {
        if (textCache.match(searchMatch.positions, markingPattern).size() > 0) {
          for (Match markingMatch : textCache.match(searchMatch.positions, markingPattern)) {
            if (markupMatch(color, contentStream, markingMatch)) {
              found = true;
            }
          }
        } else {
          System.out.println(
              "Cannot highlight: " + markingPattern.pattern() + " on page " + (pageNr - 1));
        }
        if (found) {
          break;
        }
      }
      contentStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    } catch (Error e1) {
      e1.printStackTrace();
      throw e1;
    }
  }
Esempio n. 5
0
 public void prettyPrint() {
   try {
     // clear old problem messages
     problemsView.setText("");
     // update status view
     statusView.setText(" Parsing ...");
     LispExpr root = Parser.parse(textView.getText());
     statusView.setText(" Pretty Printing ...");
     String newText = PrettyPrinter.prettyPrint(root);
     textView.setText(newText);
     statusView.setText(" Done.");
   } catch (Error e) {
     System.err.println(e.getMessage());
     statusView.setText(" Errors.");
   }
 }
Esempio n. 6
0
 public static void assumeNotHeadless() {
   boolean headless = true;
   try {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     headless = ge.isHeadless();
   } catch (Exception e) {
     e.printStackTrace();
   } catch (Error e) {
     // Really not sure why this ever happens, maybe just jenkins issues
     e.printStackTrace();
   }
   if (headless) {
     System.out.println("You are trying to start a GUI in a headless environment. Aborting test");
   }
   org.junit.Assume.assumeTrue(!headless);
 }
  /*
   * The following methods are overwritten from the PDTextStripper
   */
  public void initialize(final PDDocument pdf) throws IOException {
    try {
      resetEngine();
      document = pdf;
      textCache = new TextCache();

      if (getAddMoreFormatting()) {
        setParagraphEnd(getLineSeparator());
        setPageStart(getLineSeparator());
        setArticleStart(getLineSeparator());
        setArticleEnd(getLineSeparator());
      }
      startDocument(pdf);
      processPages(pdf.getDocumentCatalog().getAllPages());
      endDocument(pdf);
    } catch (Exception e) {
      e.printStackTrace();
    } catch (Error e1) {
      e1.printStackTrace();
    }
  }
    public List<Match> match(List<TextPosition> textPositions, String text, final Pattern pattern) {
      try {
        final Matcher matcher = pattern.matcher(text);
        final List<Match> matches = new ArrayList<Match>();

        while (matcher.find()) {
          final List<TextPosition> elements = textPositions.subList(matcher.start(), matcher.end());
          matches.add(new Match(matcher.group(), elements));
        }
        return matches;
      } catch (Error e) {
        System.out.println("An error occurred while searching for: " + pattern.toString());
        e.printStackTrace();
        final List<Match> emptyList = new ArrayList<Match>();
        return emptyList;
      } catch (Exception e1) {
        System.out.println("An exception occurred while seraching for: " + pattern.toString());
        e1.printStackTrace();
        final List<Match> emptyList = new ArrayList<Match>();
        return emptyList;
      }
    }
Esempio n. 9
0
  /**
   * Load the applet into memory. Runs in a seperate (and interruptible) thread from the rest of the
   * applet event processing so that it can be gracefully interrupted from things like HotJava.
   */
  @SuppressWarnings("deprecation")
  private void runLoader() {
    if (status != APPLET_DISPOSE) {
      showAppletStatus("notdisposed");
      return;
    }

    dispatchAppletEvent(APPLET_LOADING, null);

    // REMIND -- might be cool to visually indicate loading here --
    // maybe do animation?
    status = APPLET_LOAD;

    // Create a class loader
    loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());

    // Load the archives if present.
    // REMIND - this probably should be done in a separate thread,
    // or at least the additional archives (epll).

    String code = getCode();

    // setup applet AppContext
    // this must be called before loadJarFiles
    setupAppletAppContext();

    try {
      loadJarFiles(loader);
      applet = createApplet(loader);
    } catch (ClassNotFoundException e) {
      status = APPLET_ERROR;
      showAppletStatus("notfound", code);
      showAppletLog("notfound", code);
      showAppletException(e);
      return;
    } catch (InstantiationException e) {
      status = APPLET_ERROR;
      showAppletStatus("nocreate", code);
      showAppletLog("nocreate", code);
      showAppletException(e);
      return;
    } catch (IllegalAccessException e) {
      status = APPLET_ERROR;
      showAppletStatus("noconstruct", code);
      showAppletLog("noconstruct", code);
      showAppletException(e);
      // sbb -- I added a return here
      return;
    } catch (Exception e) {
      status = APPLET_ERROR;
      showAppletStatus("exception", e.getMessage());
      showAppletException(e);
      return;
    } catch (ThreadDeath e) {
      status = APPLET_ERROR;
      showAppletStatus("death");
      return;
    } catch (Error e) {
      status = APPLET_ERROR;
      showAppletStatus("error", e.getMessage());
      showAppletException(e);
      return;
    } finally {
      // notify that loading is no longer going on
      dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
    }

    // Fixed #4508194: NullPointerException thrown during
    // quick page switch
    //
    if (applet != null) {
      // Stick it in the frame
      applet.setStub(this);
      applet.hide();
      add("Center", applet);
      showAppletStatus("loaded");
      validate();
    }
  }
Esempio n. 10
0
  /**
   * Execute applet events. Here is the state transition diagram
   *
   * <pre>{@literal
   *   Note: (XXX) is the action
   *         APPLET_XXX is the state
   *  (applet code loaded) --> APPLET_LOAD -- (applet init called)--> APPLET_INIT --
   *  (applet start called) --> APPLET_START -- (applet stop called) --> APPLET_STOP --
   *  (applet destroyed called) --> APPLET_DESTROY --> (applet gets disposed) -->
   *   APPLET_DISPOSE --> ...
   * }</pre>
   *
   * In the legacy lifecycle model. The applet gets loaded, inited and started. So it stays in the
   * APPLET_START state unless the applet goes away(refresh page or leave the page). So the applet
   * stop method called and the applet enters APPLET_STOP state. Then if the applet is revisited, it
   * will call applet start method and enter the APPLET_START state and stay there.
   *
   * <p>In the modern lifecycle model. When the applet first time visited, it is same as legacy
   * lifecycle model. However, when the applet page goes away. It calls applet stop method and
   * enters APPLET_STOP state and then applet destroyed method gets called and enters APPLET_DESTROY
   * state.
   *
   * <p>This code is also called by AppletViewer. In AppletViewer "Restart" menu, the applet is jump
   * from APPLET_STOP to APPLET_DESTROY and to APPLET_INIT .
   *
   * <p>Also, the applet can jump from APPLET_INIT state to APPLET_DESTROY (in Netscape/Mozilla
   * case). Same as APPLET_LOAD to APPLET_DISPOSE since all of this are triggered by browser.
   */
  @Override
  public void run() {

    Thread curThread = Thread.currentThread();
    if (curThread == loaderThread) {
      // if we are in the loader thread, cause
      // loading to occur.  We may exit this with
      // status being APPLET_DISPOSE, APPLET_ERROR,
      // or APPLET_LOAD
      runLoader();
      return;
    }

    boolean disposed = false;
    while (!disposed && !curThread.isInterrupted()) {
      AppletEvent evt;
      try {
        evt = getNextEvent();
      } catch (InterruptedException e) {
        showAppletStatus("bail");
        return;
      }

      // showAppletStatus("EVENT = " + evt.getID());
      try {
        switch (evt.getID()) {
          case APPLET_LOAD:
            if (!okToLoad()) {
              break;
            }
            // This complexity allows loading of applets to be
            // interruptable.  The actual thread loading runs
            // in a separate thread, so it can be interrupted
            // without harming the applet thread.
            // So that we don't have to worry about
            // concurrency issues, the main applet thread waits
            // until the loader thread terminates.
            // (one way or another).
            if (loaderThread == null) {
              setLoaderThread(new Thread(null, this, "AppletLoader", 0, false));
              loaderThread.start();
              // we get to go to sleep while this runs
              loaderThread.join();
              setLoaderThread(null);
            } else {
              // REMIND: issue an error -- this case should never
              // occur.
            }
            break;

          case APPLET_INIT:
            // AppletViewer "Restart" will jump from destroy method to
            // init, that is why we need to check status w/ APPLET_DESTROY
            if (status != APPLET_LOAD && status != APPLET_DESTROY) {
              showAppletStatus("notloaded");
              break;
            }
            applet.resize(defaultAppletSize);

            if (PerformanceLogger.loggingEnabled()) {
              PerformanceLogger.setTime("Applet Init");
              PerformanceLogger.outputLog();
            }
            applet.init();

            // Need the default(fallback) font to be created in this AppContext
            Font f = getFont();
            if (f == null
                || "dialog".equals(f.getFamily().toLowerCase(Locale.ENGLISH))
                    && f.getSize() == 12
                    && f.getStyle() == Font.PLAIN) {
              setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
            }

            // Validate the applet in event dispatch thread
            // to avoid deadlock.
            try {
              final AppletPanel p = this;
              Runnable r =
                  new Runnable() {
                    @Override
                    public void run() {
                      p.validate();
                    }
                  };
              AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
            } catch (InterruptedException ie) {
            } catch (InvocationTargetException ite) {
            }

            status = APPLET_INIT;
            showAppletStatus("inited");
            break;

          case APPLET_START:
            {
              if (status != APPLET_INIT && status != APPLET_STOP) {
                showAppletStatus("notinited");
                break;
              }
              applet.resize(currentAppletSize);
              applet.start();

              // Validate and show the applet in event dispatch thread
              // to avoid deadlock.
              try {
                final AppletPanel p = this;
                final Applet a = applet;
                Runnable r =
                    new Runnable() {
                      @Override
                      public void run() {
                        p.validate();
                        a.setVisible(true);

                        // Fix for BugTraq ID 4041703.
                        // Set the default focus for an applet.
                        if (hasInitialFocus()) {
                          setDefaultFocus();
                        }
                      }
                    };
                AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
              } catch (InterruptedException ie) {
              } catch (InvocationTargetException ite) {
              }

              status = APPLET_START;
              showAppletStatus("started");
              break;
            }

          case APPLET_STOP:
            if (status != APPLET_START) {
              showAppletStatus("notstarted");
              break;
            }
            status = APPLET_STOP;

            // Hide the applet in event dispatch thread
            // to avoid deadlock.
            try {
              final Applet a = applet;
              Runnable r =
                  new Runnable() {
                    @Override
                    public void run() {
                      a.setVisible(false);
                    }
                  };
              AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
            } catch (InterruptedException ie) {
            } catch (InvocationTargetException ite) {
            }

            // During Applet.stop(), any AccessControlException on an involved Class remains in
            // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is
            // reused, the same exception will occur during class loading.  Set the
            // AppletClassLoader's
            // exceptionStatusSet flag to allow recognition of what had happened
            // when reusing AppletClassLoader object.
            try {
              applet.stop();
            } catch (java.security.AccessControlException e) {
              setExceptionStatus(e);
              // rethrow exception to be handled as it normally would be.
              throw e;
            }
            showAppletStatus("stopped");
            break;

          case APPLET_DESTROY:
            if (status != APPLET_STOP && status != APPLET_INIT) {
              showAppletStatus("notstopped");
              break;
            }
            status = APPLET_DESTROY;

            // During Applet.destroy(), any AccessControlException on an involved Class remains in
            // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is
            // reused, the same exception will occur during class loading.  Set the
            // AppletClassLoader's
            // exceptionStatusSet flag to allow recognition of what had happened
            // when reusing AppletClassLoader object.
            try {
              applet.destroy();
            } catch (java.security.AccessControlException e) {
              setExceptionStatus(e);
              // rethrow exception to be handled as it normally would be.
              throw e;
            }
            showAppletStatus("destroyed");
            break;

          case APPLET_DISPOSE:
            if (status != APPLET_DESTROY && status != APPLET_LOAD) {
              showAppletStatus("notdestroyed");
              break;
            }
            status = APPLET_DISPOSE;

            try {
              final Applet a = applet;
              Runnable r =
                  new Runnable() {
                    @Override
                    public void run() {
                      remove(a);
                    }
                  };
              AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
            } catch (InterruptedException ie) {
            } catch (InvocationTargetException ite) {
            }
            applet = null;
            showAppletStatus("disposed");
            disposed = true;
            break;

          case APPLET_QUIT:
            return;
        }
      } catch (Exception e) {
        status = APPLET_ERROR;
        if (e.getMessage() != null) {
          showAppletStatus("exception2", e.getClass().getName(), e.getMessage());
        } else {
          showAppletStatus("exception", e.getClass().getName());
        }
        showAppletException(e);
      } catch (ThreadDeath e) {
        showAppletStatus("death");
        return;
      } catch (Error e) {
        status = APPLET_ERROR;
        if (e.getMessage() != null) {
          showAppletStatus("error2", e.getClass().getName(), e.getMessage());
        } else {
          showAppletStatus("error", e.getClass().getName());
        }
        showAppletException(e);
      }
      clearLoadAbortRequest();
    }
  }
Esempio n. 11
0
  /** @param cmds represents the arguments/options given to krun command.. */
  public static void execute_Krun(String cmds[]) {
    Context context = new Context();
    K.init(context);

    CommandlineOptions cmd_options = new CommandlineOptions();
    CommandLine cmd = cmd_options.parse(cmds);
    if (cmd == null) {
      printKRunUsageS(cmd_options);
      /* printKRunUsageE(cmd_options); */
      /* TODO: Switch to this when the user has tried to use an experimental option. */
      System.exit(1);
    }

    if (!cmd.hasOption("debug-info")) {
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread() {
                public void run() {
                  try {
                    deleteDirectory(new File(K.krunTempDir));
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }
              });
    }

    // set verbose
    if (cmd.hasOption("verbose")) {
      GlobalSettings.verbose = true;
    }

    // set fast-kast
    if (cmd.hasOption("fast-kast")) {
      GlobalSettings.fastKast = !GlobalSettings.fastKast;
    }

    if (GlobalSettings.verbose) sw.printIntermediate("Deleting temporary krun directory");

    try {

      // Parse the program arguments

      if (cmd.hasOption("search")
          || cmd.hasOption("search-final")
          || cmd.hasOption("search-all")
          || cmd.hasOption("search-one-step")
          || cmd.hasOption("search-one-or-more-steps")) {
        K.maude_cmd = "search";
        K.io = false;
        K.do_search = true;
        if (cmd.hasOption("search") && cmd.hasOption("depth")) {
          K.searchType = SearchType.STAR;
        }
      }
      if (cmd.hasOption("search-final")) {
        K.searchType = SearchType.FINAL;
      }
      if (cmd.hasOption("search-all")) {
        K.searchType = SearchType.STAR;
      }
      if (cmd.hasOption("search-one-step")) {
        K.searchType = SearchType.ONE;
      }
      if (cmd.hasOption("search-one-or-more-steps")) {
        K.searchType = SearchType.PLUS;
      }
      if (cmd.hasOption("help")) {
        K.help = true;
      }
      if (cmd.hasOption("help-experimental")) {
        K.helpExperimental = true;
      }
      if (cmd.hasOption("version")) {
        K.version = true;
      }
      // CLEANUP_OPTIONS: The option --directory was added, replacing --k-definition and
      // --compiled-def.
      if (cmd.hasOption("directory")) {
        K.directory = new File(cmd.getOptionValue("directory")).getCanonicalPath();
        org.kframework.utils.Error.checkIfInputDirectory(K.directory);
      }
      if (cmd.hasOption("main-module")) {
        K.main_module = cmd.getOptionValue("main-module");
      }
      if (cmd.hasOption("syntax-module")) {
        K.syntax_module = cmd.getOptionValue("syntax-module");
      }
      if (cmd.hasOption("parser")) {
        K.customParser = cmd.getOptionValue("parser");
      }
      if (cmd.hasOption("term")) {
        K.term = cmd.getOptionValue("term");
      }
      if (cmd.hasOption("io")) {
        String v = cmd.getOptionValue("io");
        if (v.equals("on")) K.io = true;
        else if (v.equals("off")) K.io = false;
        else Error.report("Unrecognized option: --io " + v + "\nUsage: krun --io [on|off]");
      }
      if (cmd.hasOption("statistics")) {
        String v = cmd.getOptionValue("statistics");
        if (v.equals("on")) K.statistics = true;
        else if (v.equals("off")) K.statistics = false;
        else
          Error.report(
              "Unrecognized option: --statistics " + v + "\nUsage: krun --statistics [on|off]");
      }
      if (cmd.hasOption("color")) {
        String v = cmd.getOptionValue("color");
        if (v.equals("on")) K.color = ColorSetting.ON;
        else if (v.equals("off")) K.color = ColorSetting.OFF;
        else if (v.equals("extended")) K.color = ColorSetting.EXTENDED;
        else
          Error.report(
              "Unrecognized option: --color " + v + "\nUsage: krun --color [on|off|extended]");
      }
      if (cmd.hasOption("terminal-color")) {
        String v = cmd.getOptionValue("terminal-color");
        Color terminalColor = ColorUtil.getColorByName(v);
        if (terminalColor != null) {
          K.terminalColor = terminalColor;
        } else {
          Error.report("Invalid terminal color: " + v);
        }
      }

      if (cmd.hasOption("parens")) {
        String v = cmd.getOptionValue("parens");
        if (v.equals("greedy")) {
          K.parens = true;
        } else if (v.equals("smart")) {
          K.parens = false;
        } else {
          Error.report(
              "Unrecognized option: --parens " + v + "\nUsage: krun --parens [greedy|smart]");
        }
      }

      // testcase generation
      if (cmd.hasOption("generate-tests")) {
        K.do_testgen = true;
        K.io = false;
        K.do_search = true;
      }
      if (cmd.hasOption("maude-cmd")) {
        K.maude_cmd = cmd.getOptionValue("maude-cmd");
      }
      /*
       * if (cmd.hasOption("xsearch-pattern")) { K.maude_cmd = "search";
       * K.do_search = true; K.xsearch_pattern =
       * cmd.getOptionValue("xsearch-pattern"); //
       * System.out.println("xsearch-pattern:" + K.xsearch_pattern); }
       */
      if (cmd.hasOption("pattern")) {
        K.pattern = cmd.getOptionValue("pattern");
      }
      if (cmd.hasOption("bound")) {
        K.bound = cmd.getOptionValue("bound");
      }
      if (cmd.hasOption("depth")) {
        K.depth = cmd.getOptionValue("depth");
      }
      if (cmd.hasOption("graph")) {
        K.showSearchGraph = true;
      }
      if (cmd.hasOption("output-mode")) {
        K.output_mode = cmd.getOptionValue("output-mode");
      }
      if (cmd.hasOption("log-io")) {
        String v = cmd.getOptionValue("log-io");
        if (v.equals("on")) K.log_io = true;
        else if (v.equals("off")) K.log_io = false;
        else Error.report("Unrecognized option: --log-io " + v + "\nUsage: krun --log-io [on|off]");
      }
      if (cmd.hasOption("debug")) {
        K.debug = true;
      }
      if (cmd.hasOption("debug-gui")) {
        K.guidebug = true;
      }
      if (cmd.hasOption("trace")) {
        K.trace = true;
      }
      if (cmd.hasOption("profile")) {
        K.profile = true;
      }
      if (cmd.hasOption("ltlmc")) {
        K.model_checking = cmd.getOptionValue("ltlmc");
      }
      if (cmd.hasOption("prove")) {
        K.prove = cmd.getOptionValue("prove");
      }
      if (cmd.hasOption("smt")) {
        K.smt = cmd.getOptionValue("smt");
      }
      if (cmd.hasOption("output")) {
        if (!cmd.hasOption("color")) {
          K.color = ColorSetting.OFF;
        }
        K.output = new File(cmd.getOptionValue("output")).getCanonicalPath();
      }
      if (cmd.hasOption("c")) {

        if (K.term != null) {
          Error.report("You cannot specify both the term and the configuration\nvariables.");
        }

        K.configuration_variables = cmd.getOptionProperties("c");
        String parser = null;
        for (Option opt : cmd.getOptions()) {
          if (opt.equals(cmd_options.getOptions().getOption("c")) && parser != null) {
            K.cfg_parsers.setProperty(opt.getValue(0), parser);
          }
          if (opt.equals(cmd_options.getOptions().getOption("cfg-parser"))) {
            parser = opt.getValue();
          }
        }
      }
      if (cmd.hasOption("backend")) {
        K.backend = cmd.getOptionValue("backend");
      }
      // printing the output according to the given options
      if (K.help) {
        printKRunUsageS(cmd_options);
        System.exit(0);
      }
      if (K.helpExperimental) {
        printKRunUsageE(cmd_options);
        System.exit(0);
      }
      if (K.version) {
        String msg = FileUtil.getFileContent(KPaths.getKBase(false) + KPaths.VERSION_FILE);
        System.out.println(msg);
        System.exit(0);
      }

      String[] remainingArguments = null;
      if (cmd_options.getCommandLine().getOptions().length > 0) {
        remainingArguments = cmd.getArgs();
      } else {
        remainingArguments = cmds;
      }
      String programArg = new String();
      if (remainingArguments.length > 0) {
        programArg = remainingArguments[0];
        K.pgm = programArg;
      }
      String lang = null;
      if (K.pgm != null) {
        File pgmFile = new File(K.pgm);
        if (pgmFile.exists()) {
          lang = FileUtil.getExtension(K.pgm, ".", K.fileSeparator);
        }
      }

      if (GlobalSettings.verbose) sw.printIntermediate("Parsing command line options");

      /* CLEANUP_OPTIONS */
      if (!cmd.hasOption("directory")) {
        K.directory = new File(K.userdir).getCanonicalPath();
      }
      {
        // search for the definition
        File[] dirs =
            new File(K.directory)
                .listFiles(
                    new FilenameFilter() {
                      @Override
                      public boolean accept(File current, String name) {
                        return new File(current, name).isDirectory();
                      }
                    });

        K.compiled_def = null;
        for (int i = 0; i < dirs.length; i++) {
          if (dirs[i].getAbsolutePath().endsWith("-kompiled")) {
            if (K.compiled_def != null) {
              String msg = "Multiple compiled definitions found.";
              GlobalSettings.kem.register(
                  new KException(
                      ExceptionType.ERROR,
                      KExceptionGroup.CRITICAL,
                      msg,
                      "command line",
                      new File(".").getAbsolutePath()));
            } else {
              K.compiled_def = dirs[i].getAbsolutePath();
            }
          }
        }

        if (K.compiled_def == null) {
          String msg = "Could not find a compiled definition.";
          GlobalSettings.kem.register(
              new KException(
                  ExceptionType.ERROR,
                  KExceptionGroup.CRITICAL,
                  msg,
                  "command line",
                  new File(".").getAbsolutePath()));
        }
      }

      if (K.compiled_def == null) {
        Error.report(
            "Could not find a compiled K definition. Please ensure that\neither a compiled K definition exists in the current directory with its default\nname, or that --directory has been specified.");
      }
      File compiledFile = new File(K.compiled_def);
      if (!compiledFile.exists()) {
        Error.report(
            "\nCould not find compiled definition: "
                + K.compiled_def
                + "\nPlease compile the definition by using `kompile'.");
      }

      context.dotk = new File(new File(K.compiled_def).getParent() + File.separator + ".k");
      if (!context.dotk.exists()) {
        context.dotk.mkdirs();
      }
      context.kompiled = new File(K.compiled_def);
      K.kdir = context.dotk.getCanonicalPath();
      K.setKDir();
      /*
       * System.out.println("K.k_definition=" + K.k_definition);
       * System.out.println("K.syntax_module=" + K.syntax_module);
       * System.out.println("K.main_module=" + K.main_module);
       * System.out.println("K.compiled_def=" + K.compiled_def);
       */

      if (GlobalSettings.verbose) sw.printIntermediate("Checking compiled definition");

      // in KAST variable we obtain the output from running kast process
      // on a program defined in K
      Term KAST = null;
      RunProcess rp = new RunProcess();

      if (!context.initialized) {
        String path = K.compiled_def + "/defx-" + K.backend + ".bin";
        Definition javaDef;
        if (new File(path).exists()) {
          javaDef = (Definition) BinaryLoader.load(K.compiled_def + "/defx-" + K.backend + ".bin");
        } else {
          GlobalSettings.kem.register(
              new KException(
                  ExceptionType.ERROR,
                  KExceptionGroup.CRITICAL,
                  "Could not find compiled definition for backend '"
                      + K.backend
                      + "'.\n"
                      + "Please ensure this backend has been kompiled."));
          throw new AssertionError("unreachable");
        }

        if (GlobalSettings.verbose) sw.printIntermediate("Reading definition from binary");

        // This is essential for generating maude
        javaDef = new FlattenModules(context).compile(javaDef, null);

        if (GlobalSettings.verbose) sw.printIntermediate("Flattening modules");

        try {
          javaDef = (Definition) javaDef.accept(new AddTopCellConfig(context));
        } catch (TransformerException e) {
          e.report();
        }

        if (GlobalSettings.verbose) sw.printIntermediate("Adding top cell to configuration");

        javaDef.preprocess(context);

        if (GlobalSettings.verbose) sw.printIntermediate("Preprocessing definition");

        K.definition = javaDef;

        if (GlobalSettings.verbose) sw.printIntermediate("Importing tables");

        K.kompiled_cfg =
            (org.kframework.kil.Configuration)
                BinaryLoader.load(K.compiled_def + "/configuration.bin");

        CommandLine compileOptions =
            (CommandLine) BinaryLoader.load(K.compiled_def + "/compile-options.bin");
        if (compileOptions.hasOption("sortCells")) GlobalSettings.sortedCells = true;

        if (GlobalSettings.verbose) sw.printIntermediate("Reading configuration from binary");
      }

      if (!cmd.hasOption("main-module")) {
        K.main_module = K.definition.getMainModule();
      }
      if (!cmd.hasOption("syntax-module")) {
        K.syntax_module = K.definition.getMainSyntaxModule();
      }

      if (GlobalSettings.verbose) sw.printIntermediate("Resolving main and syntax modules");

      if (K.pgm != null) {
        KAST = rp.runParserOrDie(K.getProgramParser(), K.pgm, false, null, context);
      } else {
        KAST = null;
      }

      if (GlobalSettings.verbose) sw.printIntermediate("Kast process");

      if (K.term != null) {
        if (K.parser.equals("kast") && !cmd.hasOption("parser")) {
          if (K.backend.equals("java")) {
            K.parser = "kast -ruleParser";
          } else {
            K.parser = "kast -groundParser";
          }
        }
      }

      GlobalSettings.kem.print();

      if (!K.debug && !K.guidebug) {
        normalExecution(KAST, lang, rp, cmd_options, context);
      } else {
        if (K.do_search) {
          Error.report(
              "Cannot specify --search with --debug. In order to search\nnside the debugger, use the step-all command.");
        }
        if (K.guidebug) guiDebugExecution(KAST, lang, null, context);
        else debugExecution(KAST, lang, null, context);
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Esempio n. 12
0
  // execute krun in debug mode (i.e. step by step execution)
  // isSwitch variable is true if we enter in debug execution from normal
  // execution (we use the search command with --graph option)
  @SuppressWarnings("unchecked")
  public static void debugExecution(
      Term kast,
      String lang,
      KRunResult<SearchResults> state,
      org.kframework.kil.loader.Context context) {
    try {
      // adding autocompletion and history feature to the stepper internal
      // commandline by using the JLine library
      ConsoleReader reader = new ConsoleReader();
      reader.setBellEnabled(false);

      List<Completor> argCompletor = new LinkedList<Completor>();
      argCompletor.add(
          new SimpleCompletor(
              new String[] {
                "help",
                "abort",
                "resume",
                "step",
                "step-all",
                "select",
                "show-search-graph",
                "show-node",
                "show-edge",
                "save",
                "load",
                "read"
              }));
      argCompletor.add(new FileNameCompletor());
      List<Completor> completors = new LinkedList<Completor>();
      completors.add(new ArgumentCompletor(argCompletor));
      reader.addCompletor(new MultiCompletor(completors));

      new File(K.compiled_def + K.fileSeparator + "main.maude").getCanonicalPath();
      RunProcess rp = new RunProcess();
      KRun krun = obtainKRun(context);
      krun.setBackendOption("io", false);
      KRunDebugger debugger;
      try {
        if (state == null) {
          Term t = makeConfiguration(kast, "", rp, (K.term != null), context);
          debugger = krun.debug(t);
          System.out.println("After running one step of execution the result is:");
          AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
        } else {
          debugger = krun.debug(state.getResult().getGraph());
        }
      } catch (UnsupportedBackendOptionException e) {
        Error.report("Backend \"" + K.backend + "\" does not support option " + e.getMessage());
        throw e; // unreachable
      }
      while (true) {
        System.out.println();
        String input = reader.readLine("Command > ");
        if (input == null) {
          // probably pressed ^D
          System.out.println();
          System.exit(0);
        }

        // construct the right command line input when we specify the
        // "step" option with an argument (i.e. step=3)
        input = input.trim();
        String[] tokens = input.split(" ");
        // store the tokens that are not a blank space
        List<String> items = new ArrayList<String>();
        for (int i = 0; i < tokens.length; i++) {
          if ((!(tokens[i].equals(" ")) && (!tokens[i].equals("")))) {
            items.add(tokens[i]);
          }
        }
        StringBuilder aux = new StringBuilder();
        // excepting the case of a command like: help
        if (items.size() > 1) {
          for (int i = 0; i < items.size(); i++) {
            if (i > 0) {
              aux.append("=");
              aux.append(items.get(i));
            } else if (i == 0) {
              aux.append(items.get(i));
            }
          }
          input = aux.toString();
        }
        // apply trim to remove possible blank spaces from the inserted
        // command
        String[] cmds = new String[] {"--" + input};
        CommandlineOptions cmd_options = new CommandlineOptions();
        CommandLine cmd = cmd_options.parse(cmds);
        // when an error occurred during parsing the commandline
        // continue the execution of the stepper
        if (cmd == null) {
          continue;
        } else {
          if (cmd.hasOption("help")) {
            printDebugUsage(cmd_options);
          }
          if (cmd.hasOption("abort")) {
            System.exit(0);
          }
          if (cmd.hasOption("resume")) {
            try {
              debugger.resume();
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (IllegalStateException e) {
              Error.silentReport(
                  "Wrong command: If you previously used the step-all command you must"
                      + K.lineSeparator
                      + "seletc first a solution with step command before executing steps of rewrites!");
            }
          }
          // one step execution (by default) or more if you specify an
          // argument
          if (cmd.hasOption("step")) {
            // by default execute only one step at a time
            String arg = new String("1");
            String[] remainingArguments = null;
            remainingArguments = cmd.getArgs();
            if (remainingArguments.length > 0) {
              arg = remainingArguments[0];
            }
            try {
              int steps = Integer.parseInt(arg);
              debugger.step(steps);
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (NumberFormatException e) {
              Error.silentReport("Argument to step must be an integer.");
            } catch (IllegalStateException e) {
              Error.silentReport(
                  "Wrong command: If you previously used the step-all command you must"
                      + K.lineSeparator
                      + "select first a solution with step command before executing steps of rewrites!");
            }
          }
          if (cmd.hasOption("step-all")) {
            // by default compute all successors in one transition
            String arg = new String("1");
            String[] remainingArguments = null;
            remainingArguments = cmd.getArgs();
            if (remainingArguments.length > 0) {
              arg = remainingArguments[0];
            }
            try {
              int steps = Integer.parseInt(arg);
              SearchResults states = debugger.stepAll(steps);
              AnsiConsole.out.println(states);

            } catch (NumberFormatException e) {
              Error.silentReport("Argument to step-all must be an integer.");
            } catch (IllegalStateException e) {
              Error.silentReport(
                  "Wrong command: If you previously used the step-all command you must"
                      + K.lineSeparator
                      + "select first a solution with step command before executing steps of rewrites!");
            }
          }
          // "select n7" or "select 3" are both valid commands
          if (cmd.hasOption("select")) {
            String arg = new String();
            arg = cmd.getOptionValue("select").trim();
            try {
              int stateNum = Integer.parseInt(arg);
              debugger.setCurrentState(stateNum);
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (NumberFormatException e) {
              Error.silentReport("Argument to select must bean integer.");
            } catch (IllegalArgumentException e) {
              System.out.println(
                  "A node with the specified state number could not be found in the"
                      + K.lineSeparator
                      + "search graph");
            }
          }
          if (cmd.hasOption("show-search-graph")) {
            System.out.println(K.lineSeparator + "The search graph is:" + K.lineSeparator);
            System.out.println(debugger.getGraph());
          }
          if (cmd.hasOption("show-node")) {
            String nodeId = cmd.getOptionValue("show-node").trim();
            try {
              int stateNum = Integer.parseInt(nodeId);
              AnsiConsole.out.println(debugger.printState(stateNum));
            } catch (NumberFormatException e) {
              Error.silentReport("Argument to select node to show must be an integer.");
            } catch (IllegalArgumentException e) {
              System.out.println(
                  "A node with the specified state number could not be found in the"
                      + K.lineSeparator
                      + "search graph");
            }
          }
          if (cmd.hasOption("show-edge")) {
            String[] vals = cmd.getOptionValues("show-edge");
            vals = vals[0].split("=", 2);
            try {
              int state1 = Integer.parseInt(vals[0].trim());
              int state2 = Integer.parseInt(vals[1].trim());
              AnsiConsole.out.println(debugger.printEdge(state1, state2));
            } catch (ArrayIndexOutOfBoundsException e) {
              Error.silentReport("Must specify two nodes with an edge between them.");
            } catch (NumberFormatException e) {
              Error.silentReport("Arguments to select edge to show must be integers.");
            } catch (IllegalArgumentException e) {
              System.out.println(
                  "An edge with the specified endpoints could not be found in the"
                      + K.lineSeparator
                      + "search graph");
            }
          }

          DirectedGraph<KRunState, Transition> savedGraph = null;
          if (cmd.hasOption("save")) {
            BinaryLoader.save(
                new File(cmd.getOptionValue("save")).getCanonicalPath(), debugger.getGraph());
            System.out.println("File successfully saved.");
          }
          if (cmd.hasOption("load")) {
            savedGraph =
                (DirectedGraph<KRunState, Transition>)
                    BinaryLoader.load(cmd.getOptionValue("load"));
            krun = new MaudeKRun(context);
            debugger = krun.debug(savedGraph);
            debugger.setCurrentState(1);
            System.out.println("File successfully loaded.");
          }
          if (cmd.hasOption("read")) {
            try {
              debugger.readFromStdin(
                  StringBuiltin.valueOf("\"" + cmd.getOptionValue("read") + "\"").stringValue());
              AnsiConsole.out.println(debugger.printState(debugger.getCurrentState()));
            } catch (IllegalStateException e) {
              Error.silentReport(e.getMessage());
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Esempio n. 13
0
  // execute krun in normal mode (i.e. not in debug mode)
  public static void normalExecution(
      Term KAST, String lang, RunProcess rp, CommandlineOptions cmd_options, Context context) {
    try {
      CommandLine cmd = cmd_options.getCommandLine();

      KRun krun = obtainKRun(context);
      KRunResult<?> result = null;
      // Set<String> varNames = null;
      Rule patternRule = null;
      RuleCompilerSteps steps;
      steps = new RuleCompilerSteps(K.definition, context);
      try {
        if (cmd.hasOption("pattern") || "search".equals(K.maude_cmd)) {
          org.kframework.parser.concrete.KParser.ImportTbl(K.compiled_def + "/def/Concrete.tbl");
          ASTNode pattern =
              DefinitionLoader.parsePattern(K.pattern, "Command line pattern", context);
          CollectVariablesVisitor vars = new CollectVariablesVisitor(context);
          pattern.accept(vars);
          // varNames = vars.getVars().keySet();

          try {
            pattern = steps.compile(new Rule((Sentence) pattern), null);
          } catch (CompilerStepDone e) {
            pattern = (ASTNode) e.getResult();
          }
          patternRule = new Rule((Sentence) pattern);
          if (GlobalSettings.verbose) sw.printIntermediate("Parsing search pattern");
        }

        if (K.do_search) {
          if ("search".equals(K.maude_cmd)) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String buffer = "";
            // detect if the input comes from console or redirected
            // from a pipeline
            Console c = System.console();
            if (c == null && br.ready()) {
              try {
                buffer = br.readLine();
              } catch (IOException ioe) {
                ioe.printStackTrace();
              } finally {
                if (br != null) {
                  try {
                    br.close();
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }
              }
            }
            Integer depth = null;
            Integer bound = null;
            if (cmd.hasOption("bound") && cmd.hasOption("depth")) {
              bound = Integer.parseInt(K.bound);
              depth = Integer.parseInt(K.depth);
            } else if (cmd.hasOption("bound")) {
              bound = Integer.parseInt(K.bound);
            } else if (cmd.hasOption("depth")) {
              depth = Integer.parseInt(K.depth);
            }
            if (K.do_testgen) {
              result =
                  krun.generate(
                      bound,
                      depth,
                      K.searchType,
                      patternRule,
                      makeConfiguration(KAST, buffer, rp, (K.term != null), context),
                      steps);
            } else {
              result =
                  krun.search(
                      bound,
                      depth,
                      K.searchType,
                      patternRule,
                      makeConfiguration(KAST, buffer, rp, (K.term != null), context),
                      steps);
            }

            if (GlobalSettings.verbose) sw.printTotal("Search total");
          } else {
            Error.report("For the search option you must specify that --maude-cmd=search");
          }
        } else if (K.model_checking.length() > 0) {
          // run kast for the formula to be verified
          File formulaFile = new File(K.model_checking);
          Term KAST1 = null;
          if (!formulaFile.exists()) {
            // Error.silentReport("\nThe specified argument does not exist as a file on the disc; it
            // may represent a direct formula: "
            // + K.model_checking);
            // assume that the specified argument is not a file and
            // maybe represents a formula
            KAST1 = rp.runParserOrDie("kast -e", K.model_checking, false, "LtlFormula", context);
          } else {
            // the specified argument represents a file
            KAST1 = rp.runParserOrDie("kast", K.model_checking, false, "LtlFormula", context);
          }

          result =
              krun.modelCheck(KAST1, makeConfiguration(KAST, null, rp, (K.term != null), context));

          if (GlobalSettings.verbose) sw.printTotal("Model checking total");
        } else if (K.prove.length() > 0) {
          File proofFile = new File(K.prove);
          if (!proofFile.exists()) {
            Error.report("Cannot find the file containing rules to prove");
          }
          String content = FileUtil.getFileContent(proofFile.getAbsoluteFile().toString());
          Definition parsed =
              DefinitionLoader.parseString(content, proofFile.getAbsolutePath(), context);
          Module mod = parsed.getSingletonModule();
          try {
            mod = new SpecificationCompilerSteps(context).compile(mod, null);
          } catch (CompilerStepDone e) {
            assert false : "dead code";
          }
          result = krun.prove(mod);
        } else if (cmd.hasOption("depth")) {
          int depth = Integer.parseInt(K.depth);
          result = krun.step(makeConfiguration(KAST, null, rp, (K.term != null), context), depth);

          if (GlobalSettings.verbose) sw.printTotal("Bounded execution total");
        } else {
          result = krun.run(makeConfiguration(KAST, null, rp, (K.term != null), context));

          if (GlobalSettings.verbose) sw.printTotal("Normal execution total");
        }

        if (cmd.hasOption("pattern") && !K.do_search) {
          Object krs = result.getResult();
          if (krs instanceof KRunState) {
            Term res = ((KRunState) krs).getRawResult();
            krun.setBackendOption("io", false);
            result = krun.search(null, null, K.searchType, patternRule, res, steps);
          } else {
            Error.report(
                "Pattern matching after execution is not supported by search\nand model checking");
          }
        }

      } catch (KRunExecutionException e) {
        rp.printError(e.getMessage(), lang, context);
        System.exit(1);
      } catch (TransformerException e) {
        e.report();
      } catch (UnsupportedBackendOptionException e) {
        Error.report("Backend \"" + K.backend + "\" does not support option " + e.getMessage());
      }

      if ("pretty".equals(K.output_mode)) {
        String output = result.toString();
        if (!cmd.hasOption("output")) {
          AnsiConsole.out.println(output);
        } else {
          writeStringToFile(new File(K.output), output);
        }
        // print search graph
        if ("search".equals(K.maude_cmd) && K.do_search && K.showSearchGraph) {
          System.out.println(K.lineSeparator + "The search graph is:" + K.lineSeparator);
          @SuppressWarnings("unchecked")
          KRunResult<SearchResults> searchResult = (KRunResult<SearchResults>) result;
          AnsiConsole.out.println(searchResult.getResult().getGraph());
          // offer the user the possibility to turn execution into
          // debug mode
          while (true) {
            System.out.print(K.lineSeparator + "Do you want to enter in debug mode? (y/n):");
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            String input = stdin.readLine();
            if (input == null) {
              K.debug = false;
              K.guidebug = false;
              System.out.println();
              break;
            }
            if (input.equals("y")) {
              K.debug = true;
              debugExecution(KAST, lang, searchResult, context);
            } else if (input.equals("n")) {
              K.debug = false;
              K.guidebug = false;
              break;
            } else {
              System.out.println("You should specify one of the possible answers:y or n");
            }
          }
        }
      } else if ("raw".equals(K.output_mode)) {
        String output = result.getRawOutput();
        ;
        if (!cmd.hasOption("output")) {
          System.out.println(output);
        } else {
          writeStringToFile(new File(K.output), output);
        }
      } else if ("none".equals(K.output_mode)) {
        System.out.print("");
      } else if ("binary".equals(K.output_mode)) {
        Object krs = result.getResult();
        if (krs instanceof KRunState) {
          Term res = ((KRunState) krs).getRawResult();

          if (!cmd.hasOption("output")) {
            Error.silentReport(
                "Did not specify an output file. Cannot print output-mode binary to\nstandard out. Saving to .k/krun/krun_output");
            BinaryLoader.save(K.krun_output, res);
          } else {
            BinaryLoader.save(K.output, res);
          }
        } else {
          Error.report("binary output mode is not supported by search and model\nchecking");
        }

      } else {
        Error.report(K.output_mode + " is not a valid value for output-mode option");
      }

      System.exit(0);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Esempio n. 14
0
  public static Term makeConfiguration(
      Term kast, String stdin, RunProcess rp, boolean hasTerm, Context context)
      throws TransformerException, IOException {

    if (hasTerm) {
      if (kast == null) {
        return rp.runParserOrDie(K.getProgramParser(), K.term, false, null, context);
      } else {
        Error.report("You cannot specify both the term and the configuration\nvariables.");
      }
    }

    HashMap<String, Term> output = new HashMap<String, Term>();
    boolean hasPGM = false;
    Enumeration<Object> en = K.configuration_variables.keys();
    while (en.hasMoreElements()) {
      String name = (String) en.nextElement();
      String value = K.configuration_variables.getProperty(name);
      String parser = K.cfg_parsers.getProperty(name);
      // TODO: get sort from configuration term in definition and pass it
      // here
      Term parsed = null;
      if (parser == null) {
        parser = "kast -groundParser -e";
      }
      parsed = rp.runParserOrDie(parser, value, false, null, context);
      output.put("$" + name, parsed);
      hasPGM = hasPGM || name.equals("$PGM");
    }
    if (!hasPGM && kast != null) {
      output.put("$PGM", kast);
    }
    if (!K.io && stdin == null) {
      stdin = "";
    }
    if (stdin != null) {
      KApp noIO = KApp.of(KLabelConstant.of("'#noIO", context));
      if (K.backend.equals("java")) {
        DataStructureSort myList =
            context.dataStructureListSortOf(DataStructureSort.DEFAULT_LIST_SORT);
        if (myList != null) {
          output.put("$noIO", DataStructureBuiltin.element(myList, noIO));
        }
      } else {
        output.put("$noIO", new ListItem(noIO));
      }
      output.put("$stdin", StringBuiltin.kAppOf(stdin + "\n"));
    } else {
      if (K.backend.equals("java")) {
        DataStructureSort myList =
            context.dataStructureListSortOf(DataStructureSort.DEFAULT_LIST_SORT);
        if (myList != null) {
          output.put("$noIO", DataStructureBuiltin.empty(myList));
        }
      } else {
        output.put("$noIO", org.kframework.kil.List.EMPTY);
      }
      output.put("$stdin", StringBuiltin.EMPTY);
    }

    if (GlobalSettings.verbose) sw.printIntermediate("Make configuration");

    return plug(output, context);
  }