예제 #1
0
 /**
  * Test for ordering of the values of this Token and the argument Token. It is assumed that the
  * type of the argument is PetiteToken. The argument token is then adjusted to the range of a
  * PetiteToken.
  *
  * @param rightArgument The token to compare this token with.
  * @exception IllegalActionException If this method is not supported by the derived class.
  * @return A new Token containing the result.
  */
 protected BooleanToken _isLessThan(ScalarToken rightArgument) throws IllegalActionException {
   PetiteToken convertedArgument = (PetiteToken) rightArgument;
   return BooleanToken.getInstance(_value < convertedArgument.doubleValue());
 }
예제 #2
0
 /**
  * Test for equality of the values of this Token and the argument Token. Two function tokens are
  * equal if they correspond to the same expression, under renaming of any bound variables.
  *
  * @param rightArgument The token to compare to this token.
  * @return A token containing true if the value element of the first argument is equal to the
  *     value of this token.
  */
 public BooleanToken isEqualTo(Token rightArgument) {
   FunctionToken convertedArgument = (FunctionToken) rightArgument;
   return BooleanToken.getInstance(convertedArgument._function.isCongruent(_function));
 }
예제 #3
0
  /**
   * Evaluate a logical AND or OR on the children of the specified node.
   *
   * @param node The specified node.
   * @exception IllegalActionException If an evaluation error occurs.
   */
  public void visitLogicalNode(ASTPtLogicalNode node) throws IllegalActionException {
    if (node.isConstant() && node.isEvaluated()) {
      _evaluatedChildToken = node.getToken();
      return;
    }

    // Note that we do not always evaluate all of the children...
    // We perform short-circuit evaluation instead and evaluate the
    // children in order until the final value is determined, after
    // which point no more children are evaluated.
    int numChildren = node.jjtGetNumChildren();
    _assert(numChildren > 0, node, "The number of child nodes must be greater than zero");

    _evaluateChild(node, 0);

    ptolemy.data.Token result = _evaluatedChildToken;

    if (!(result instanceof BooleanToken)) {
      throw new IllegalActionException(
          "Cannot perform logical "
              + "operation on "
              + result
              + " which is a "
              + result.getClass().getName());
    }

    // Make sure that exactly one of AND or OR is set.
    _assert(node.isLogicalAnd() ^ node.isLogicalOr(), node, "Invalid operation");

    // Perform both the short-circuit AND and short-circuit OR in
    // one piece of code.
    // FIXME: I dislike that this is not done in the token classes...
    boolean flag = node.isLogicalAnd();

    for (int i = 0; i < numChildren; i++) {
      ASTPtRootNode child = (ASTPtRootNode) node.jjtGetChild(i);

      // Evaluate the child
      child.visit(this);

      // Get its value.
      ptolemy.data.Token nextToken = _evaluatedChildToken;

      if (!(nextToken instanceof BooleanToken)) {
        throw new IllegalActionException(
            "Cannot perform logical "
                + "operation on "
                + nextToken
                + " which is a "
                + result.getClass().getName());
      }

      if (flag != ((BooleanToken) nextToken).booleanValue()) {
        _evaluatedChildToken = (BooleanToken.getInstance(!flag));

        // Note short-circuit eval.
        return;
      }
    }

    _evaluatedChildToken = (BooleanToken.getInstance(flag));

    if (node.isConstant()) {
      node.setToken(_evaluatedChildToken);
    }
  }
예제 #4
0
파일: Report.java 프로젝트: ptII/ptII
  /**
   * Process this event. If the mode is {@link Mode#ERROR}, a message is shown in an error dialog.
   * If the mode is {@link Mode#EXCEPTION}, a message is shown in the form of an exception. If the
   * mode is {@link Mode#MESSAGE}, a message is shown in a message dialog. If the mode is {@link
   * Mode#TABLEAU}, a tableau is opened to show the message. The default tableau is the one defined
   * in the {@link #tableau} parameter. However, if {@link #referredTableau} is not an empty string,
   * its value is interpreted as the name of the tableau parameter in the model, whose tableau
   * should be used instead of the default one. If the mode is {@link Mode#WARNING}, a message is
   * shown in a warning dialog. If the mode is {@link Mode#YES_OR_NO}, a query dialog is shown with
   * the message, which allows the user to answer with yes or no. The answer is stored in {@link
   * #response}.
   *
   * @param arguments The arguments used to process this event, which must be either an ArrayToken
   *     or a RecordToken.
   * @return A refiring data structure that contains a non-negative double number if refire() should
   *     be called after that amount of model time, or null if refire() need not be called.
   * @exception IllegalActionException If the tableau cannot be used, or if thrown by the
   *     superclass.
   */
  public RefiringData fire(Token arguments) throws IllegalActionException {
    RefiringData data = super.fire(arguments);

    Mode choice = (Mode) mode.getChosenValue();
    String text = message.stringValue();
    MessageHandler oldHandler;
    switch (choice) {
      case ERROR:
        oldHandler = MessageHandler.getMessageHandler();
        try {
          MessageHandler.setMessageHandler(_MESSAGE_HANDLER);
          MessageHandler.error(text);
        } finally {
          MessageHandler.setMessageHandler(oldHandler);
        }
        break;
      case MESSAGE:
        oldHandler = MessageHandler.getMessageHandler();
        try {
          MessageHandler.setMessageHandler(_MESSAGE_HANDLER);
          MessageHandler.message(text);
        } finally {
          MessageHandler.setMessageHandler(oldHandler);
        }
        break;
      case EXCEPTION:
        throw new RuntimeException(text);
      case TABLEAU:
        Effigy effigy = EventUtils.findToplevelEffigy(this);
        if (effigy == null) {
          // The effigy may be null if the model is closed.
          return data;
        }

        Tableau tableau = EventUtils.getTableau(this, referredTableau, this.tableau);
        if (tableau != null && !(tableau.getFrame() instanceof TextEditor)) {
          EventUtils.setTableau(this, referredTableau, this.tableau, null);
          EventUtils.closeTableau(tableau);
          tableau = null;
        }

        boolean openNewWindow = true;
        String previousText = null;
        if (tableau != null) {
          JFrame frame = tableau.getFrame();
          if (frame instanceof TextEditor) {
            TextEditor editor = (TextEditor) frame;
            if (editor.getEffigy() == null) {
              previousText = editor.text.getText();
            } else {
              openNewWindow = false;
            }
          }
        }

        TextEditor frame;
        if (openNewWindow) {
          TextEffigy textEffigy;
          try {
            textEffigy = TextEffigy.newTextEffigy(effigy, "");
          } catch (Exception e) {
            throw new IllegalActionException(this, e, "Unable to " + "create effigy.");
          }
          try {
            tableau = new Tableau(textEffigy, "tableau");
          } catch (NameDuplicationException e) {
            throw new IllegalActionException(this, e, "Unable to " + "create tableau.");
          }
          frame = new TextEditor(tableau.getTitle(), textEffigy.getDocument());
          frame.text.setColumns(((IntToken) columnsDisplayed.getToken()).intValue());
          frame.text.setRows(((IntToken) rowsDisplayed.getToken()).intValue());
          tableau.setFrame(frame);
          frame.setTableau(tableau);
          EventUtils.setTableau(this, referredTableau, this.tableau, tableau);
          frame.pack();
          frame.setVisible(true);
          if (previousText != null) {
            frame.text.setText(previousText);
          }
        } else {
          frame = (TextEditor) tableau.getFrame();
        }
        frame.text.append(text + "\n");
        try {
          int lineOffset = frame.text.getLineStartOffset(frame.text.getLineCount() - 1);
          frame.text.setCaretPosition(lineOffset);
        } catch (BadLocationException ex) {
          // Ignore ... worst case is that the scrollbar
          // doesn't move.
        }
        break;
      case WARNING:
        try {
          oldHandler = MessageHandler.getMessageHandler();
          try {
            MessageHandler.setMessageHandler(_MESSAGE_HANDLER);
            MessageHandler.warning(text);
          } finally {
            MessageHandler.setMessageHandler(oldHandler);
          }
          response.setToken(BooleanToken.TRUE);
        } catch (CancelException e) {
          response.setToken(BooleanToken.FALSE);
        }
        break;
      case YES_OR_NO:
        oldHandler = MessageHandler.getMessageHandler();
        boolean success = false;
        boolean answer;
        try {
          MessageHandler.setMessageHandler(_MESSAGE_HANDLER);
          answer = MessageHandler.yesNoQuestion(text);
          success = true;
        } finally {
          MessageHandler.setMessageHandler(oldHandler);
        }
        if (success) {
          response.setToken(BooleanToken.getInstance(answer));
        }
        break;
      default:
        throw new IllegalActionException(
            "Unrecognized mode choice \"" + mode.getExpression() + "\".");
    }

    return data;
  }