/**
   * Specify the container entity, adding the relation to the list of relations in the container. If
   * the container already contains a relation with the same name, then throw an exception and do
   * not make any changes. Similarly, if the container is not in the same workspace as this
   * relation, throw an exception. If the relation is a class element and the proposed container
   * does not match the current container, then also throw an exception. If the relation is already
   * contained by the container, do nothing. If this relation already has a container, remove it
   * from that container first. Otherwise, remove it from the workspace directory, if it is present.
   * If the argument is null, then unlink the ports from the relation and remove it from its
   * container. It is not added to the workspace directory, so this could result in this relation
   * being garbage collected. Derived classes may further constrain the class of the container to a
   * subclass of CompositeEntity. This method validates all deeply contained instances of Settable,
   * since they may no longer be valid in the new context. This method is write-synchronized on the
   * workspace and increments its version number.
   *
   * @param container The proposed container.
   * @exception IllegalActionException If this entity and the container are not in the same
   *     workspace, or if a contained Settable becomes invalid and the error handler throws it.
   * @exception NameDuplicationException If the name collides with a name already on the contents
   *     list of the container.
   * @see #getContainer()
   */
  public void setContainer(CompositeEntity container)
      throws IllegalActionException, NameDuplicationException {
    if ((container != null) && (_workspace != container.workspace())) {
      throw new IllegalActionException(
          this, container, "Cannot set container because workspaces are different.");
    }

    try {
      _workspace.getWriteAccess();
      _checkContainer(container);

      CompositeEntity previousContainer = (CompositeEntity) getContainer();

      if (previousContainer == container) {
        return;
      }

      // Do this first, because it may throw an exception.
      if (container != null) {
        container._addRelation(this);

        if (previousContainer == null) {
          _workspace.remove(this);
        }
      }

      _container = container;

      if (previousContainer != null) {
        previousContainer._removeRelation(this);
      }

      if (container == null) {
        unlinkAll();
      } else {
        // We have successfully set a new container for this
        // object. Mark it modified to ensure MoML export.
        // Transfer any queued change requests to the
        // new container.  There could be queued change
        // requests if this component is deferring change
        // requests.
        if (_changeRequests != null) {
          Iterator requests = _changeRequests.iterator();

          while (requests.hasNext()) {
            ChangeRequest request = (ChangeRequest) requests.next();
            container.requestChange(request);
          }

          _changeRequests = null;
        }
      }

      // Validate all deeply contained settables, since
      // they may no longer be valid in the new context.
      validateSettables();
    } finally {
      _workspace.doneWriting();
    }
  }
  /**
   * Set the name of the ComponentRelation. If there is already a ComponentRelation of the container
   * with the same name, throw an exception.
   *
   * @exception IllegalActionException If the name has a period.
   * @exception NameDuplicationException If there is already a relation with the same name in the
   *     container.
   */
  public void setName(String name) throws IllegalActionException, NameDuplicationException {
    if (name == null) {
      name = "";
    }

    CompositeEntity container = (CompositeEntity) getContainer();

    if ((container != null)) {
      ComponentRelation another = container.getRelation(name);

      if ((another != null) && (another != this)) {
        throw new NameDuplicationException(container, "Name duplication: " + name);
      }
    }

    super.setName(name);
  }
 /**
  * Construct a relation with the given name contained by the specified entity. The container
  * argument must not be null, or a NullPointerException will be thrown. This relation will use the
  * workspace of the container for synchronization and version counts. If the name argument is
  * null, then the name is set to the empty string. This constructor write-synchronizes on the
  * workspace.
  *
  * @param container The container.
  * @param name The name of the relation.
  * @exception IllegalActionException If the container is incompatible with this relation.
  * @exception NameDuplicationException If the name coincides with a relation already in the
  *     container.
  */
 public ComponentRelation(CompositeEntity container, String name)
     throws IllegalActionException, NameDuplicationException {
   super(container.workspace(), name);
   setContainer(container);
 }