Ejemplo n.º 1
0
  /**
   * Apply the current scaling transformation to the figure.
   *
   * @param figure The figure the transformation is to be applied to.
   * @exception IllegalActionException If the getToken() method throws such an exception.
   */
  protected void _applyTransform(Figure figure) throws IllegalActionException {
    double scaleFactorXValue = 1.0;
    double scaleFactorYValue = 1.0;

    boolean needsTransform = false;

    if ((scaleFactorX.isOutsideConnected()) && scaleFactorX.hasToken(0)) {
      scaleFactorXValue = ((DoubleToken) scaleFactorX.get(0)).doubleValue();
      needsTransform = true;
    }

    if ((scaleFactorY.isOutsideConnected()) && scaleFactorY.hasToken(0)) {
      scaleFactorYValue = ((DoubleToken) scaleFactorY.get(0)).doubleValue();
      needsTransform = true;
    }

    if (needsTransform) {
      if (_isAccumulating()) {
        scaleFactorXValue *= _oldScaleFactorX;
        scaleFactorYValue *= _oldScaleFactorY;
      }

      _oldScaleFactorX = scaleFactorXValue;
      _oldScaleFactorY = scaleFactorYValue;

      AffineTransform inputTransform =
          AffineTransform.getScaleInstance(scaleFactorXValue, scaleFactorYValue);

      figure.transform(inputTransform);
    }
  }
Ejemplo n.º 2
0
  /**
   * If the <i>enable</i> input is connected, then if it has a true token, produce the next output.
   * If it is not connected, produce the next output unconditionally. Whether it is connected is
   * determined by checking the width of the port.
   *
   * @exception IllegalActionException If there is no director.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    if ((!enable.isOutsideConnected())
        || (enable.hasToken(0) && ((BooleanToken) enable.get(0)).booleanValue())) {
      ArrayToken valuesArray = (ArrayToken) values.getToken();

      if (_currentIndex < valuesArray.length()) {
        output.send(0, valuesArray.getElement(_currentIndex));
        _outputProduced = true;
      }
    }
  }
Ejemplo n.º 3
0
  /**
   * Perform a multiway rendezvous with all input channels, collect one input token from each
   * channel, and then perform a multiway rendezvous with the output channels, providing that data.
   *
   * @exception IllegalActionException If the input width is zero.
   * @exception TerminateProcessException If the process termination is requested by the director.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    if (_debugging && _VERBOSE_DEBUGGING) {
      if (!_listeningToBranchController) {
        _branchController.addDebugListener(this);
        _listeningToBranchController = true;
      }
    } else {
      _branchController.removeDebugListener(this);
      _listeningToBranchController = false;
    }
    if (!input.isOutsideConnected()) {
      throw new IllegalActionException(this, "Barrier requires at least one input.");
    }
    ConditionalBranch[] branches = new ConditionalBranch[input.getWidth()];
    for (int i = 0; i < input.getWidth(); i++) {
      // The branch has channel i and ID i.
      branches[i] = new ConditionalReceive(input, i, i);
      if (_debugging && _VERBOSE_DEBUGGING) {
        branches[i].addDebugListener(this);
      }
    }
    if (_debugging) {
      _debug("Performing multiway rendezvous on the input channels.");
    }
    if (!_branchController.executeBranches(branches)) {
      if (_debugging) {
        _debug("At least one input rendezvous was terminated.");
      }
      _terminate = true;
      return;
    }
    if (_debugging) {
      _debug("Input channels completed.");
      if (_VERBOSE_DEBUGGING) {
        for (ConditionalBranch branche : branches) {
          branche.removeDebugListener(this);
        }
      }
    }
    Token[] data = new Token[input.getWidth()];
    for (int i = 0; i < input.getWidth(); i++) {
      data[i] = branches[i].getToken();
      if (_debugging) {
        _debug("Completed read input from channel " + i + ": " + data[i]);
      }
      if (data[i] == null) {
        throw new InternalErrorException("Input data is null!");
      }
    }

    if (output.isOutsideConnected()) {
      branches = new ConditionalBranch[output.getWidth()];
      Token token = null;
      for (int i = 0; i < output.getWidth(); i++) {
        if (i < input.getWidth()) {
          token = data[i];
        }
        if (_debugging) {
          _debug("Sending output to channel " + i + ": " + token);
        }
        branches[i] = new ConditionalSend(output, i, i, token);
        if (_debugging && _VERBOSE_DEBUGGING) {
          branches[i].addDebugListener(this);
        }
      }
      if (_debugging) {
        _debug("Performing multiway rendezvous on the output channels.");
      }
      if (_branchController.executeBranches(branches)) {
        if (_debugging) {
          _debug("Output channels completed.");
        }
      } else {
        if (_debugging) {
          _debug("Output channels failed.");
        }
      }
      if (_debugging && _VERBOSE_DEBUGGING) {
        for (ConditionalBranch branche : branches) {
          branche.removeDebugListener(this);
        }
      }
    }
  }
Ejemplo n.º 4
0
  /**
   * If the input width is greater than zero and it has not already been done, start a thread to
   * read a token from the <i>release</i> input port and store it in the pool. Then, in the calling
   * thread, if there is at least one resource in the pool, write the first resource in the pool to
   * any <i>grant</i> output channel.
   *
   * @exception IllegalActionException If an error occurs during executing the process.
   * @exception TerminateProcessException If the process termination is requested by the director.
   */
  public void fire() throws IllegalActionException {
    super.fire();
    final RendezvousDirector director = (RendezvousDirector) getDirector();
    final Thread writeThread = Thread.currentThread();

    if (!(getDirector() instanceof RendezvousDirector)) {
      throw new IllegalActionException(
          this, "ResourcePool actor can only be used with RendezvousDirector.");
    }
    _postfireReturns = true;
    if (release.isOutsideConnected() && _readThread == null) {
      _readThread =
          new Thread(getFullName() + "_readThread") {
            public void run() {
              try {
                while (!_stopRequested) {
                  // Synchronize on the director since all read/write
                  // operations do.
                  synchronized (director) {
                    if (_debugging) {
                      _debug("Resources available: " + _pool);
                    }
                    Token resource =
                        RendezvousReceiver.getFromAny(release.getReceivers(), director);
                    _pool.add(resource);
                    director.threadUnblocked(writeThread, null);
                    director.notifyAll();
                  }
                }
              } catch (TerminateProcessException ex) {
                // OK, just exit
                _postfireReturns = false;
              } finally {
                director.removeThread(_readThread);
              }
            }
          };
      director.addThread(_readThread);
      _readThread.start();
    } else if (!release.isOutsideConnected() && _readThread != null) {
      // A mutation has eliminated the sources.
      _readThread.interrupt();
    }
    // Synchronize on the director since all read/write
    // operations do.
    synchronized (director) {
      while (_pool.size() == 0) {
        if (_stopRequested || !_postfireReturns) {
          _postfireReturns = false;
          return;
        }
        try {
          director.threadBlocked(writeThread, null);
          RendezvousReceiver.waitForChange(director);
        } catch (TerminateProcessException ex) {
          _postfireReturns = false;
          return;
        } finally {
          director.threadUnblocked(writeThread, null);
        }
      }
      // There is a token.
      Token token = (Token) _pool.get(0);
      // If this put blocks for any reason, it will block on
      // a director.wait(), so the lock will not be held.
      try {
        RendezvousReceiver.putToAny(token, grant.getRemoteReceivers(), director);
      } catch (TerminateProcessException e) {
        _postfireReturns = false;
        return;
      }
      _pool.remove(0);
    }
  }