Ejemplo n.º 1
0
  /**
   * Override the base class to set type constraints between the output ports and parameters of this
   * actor whose name matches the output port. If there is no such parameter, then create an
   * instance of Variable with a matching name and set up the type constraints to that instance. The
   * type of the output port is constrained to be at least that of the parameter or variable.
   *
   * @exception IllegalActionException If there is no director, or if the director's preinitialize()
   *     method throws it, or if this actor is not opaque.
   */
  public void preinitialize() throws IllegalActionException {
    super.preinitialize();

    Iterator ports = outputPortList().iterator();

    while (ports.hasNext()) {
      TypedIOPort port = (TypedIOPort) ports.next();

      // Ensure that the production rate is one.
      // FIXME: This may not be right if there is no
      // actual source of data for this port (e.g. no
      // SetVariable actor).
      Variable rate = (Variable) port.getAttribute("tokenProductionRate");

      if (rate == null) {
        try {
          rate = new Variable(port, "tokenProductionRate");
        } catch (NameDuplicationException e) {
          throw new InternalErrorException(e);
        }
      }

      rate.setToken(new IntToken(1));

      String portName = port.getName();
      Attribute attribute = getAttribute(portName);

      if (attribute == null) {
        try {
          workspace().getWriteAccess();
          attribute = new Variable(this, portName);
        } catch (NameDuplicationException ex) {
          throw new InternalErrorException(ex);
        } finally {
          workspace().doneWriting();
        }
      }

      // attribute is now assured to be non-null.
      if (attribute instanceof Variable) {
        port.setTypeAtLeast((Variable) attribute);
      } else {
        // Assume the port type must be a string.
        port.setTypeEquals(BaseType.STRING);
      }
    }
  }