/** * Create a new port with the specified name in the container of this controller, which in turn * creates a port in this controller and all the refinements. This method is write-synchronized on * the workspace. * * @param name The name to assign to the newly created port. * @return The new port. * @exception NameDuplicationException If the entity already has a port with the specified name. */ public Port newPort(String name) throws NameDuplicationException { try { _workspace.getWriteAccess(); ModalModel container = (ModalModel) getContainer(); if (_mirrorDisable == 1 || container == null) { // We are mirroring a change above in the hierarchy // (or there is no above in the hierarchy), // so we should not mirror this change upwards. // But we should mirror it downwards. ModalRefinementPort port = new ModalRefinementPort(this, name); // Create the links on the outside of the new port. if (container != null) { String relationName = name + "Relation"; Relation relation = container.getRelation(relationName); if (relation == null) { relation = container.newRelation(relationName); Port containerPort = container.getPort(name); containerPort.link(relation); } port.link(relation); } // Mirror the change downwards in the hierarchy. Iterator entities = entityList().iterator(); while (entities.hasNext()) { Entity entity = (Entity) entities.next(); if (entity instanceof RefinementActor) { if (entity.getPort(name) == null) { try { ((RefinementActor) entity).setMirrorDisable(1); entity.newPort(name); } finally { ((RefinementActor) entity).setMirrorDisable(0); } } } } return port; } else { // We are originating the change, or it originated from // below us in the hierarchy, and hence should delegate // it upwards. This will cause this newPort() method // to be called again by the container after it has // created its own port. That will result in the code // above executing because it will set _mirrorPort to 1 // on this port before doing the call. ModalRefinementPort containerPort = container == null ? null : (ModalRefinementPort) container.getPort(name); if (containerPort == null) { // The container does not have a mirror port. // Delegate upwards. // Note that this will result in the container // creating the port here by calling this // newPort() method. It will set my // _mirrorDisable to 1 before doing that, // and set it back to 0 after. container.newPort(name); return getPort(name); } else { // The container already has a mirror port, so // we cannot use it to create our port. // This can happen if the container was created first // and populated with its ports before this refinement // was created. ModalRefinementPort port = new ModalRefinementPort(this, name); // Create the link on the outside of the port. String relationName = name + "Relation"; Relation relation = container.getRelation(relationName); // The container should already have this relation, // but in case not, we create it. if (relation == null) { relation = container.newRelation(relationName); containerPort.link(relation); } port.link(relation); // Mirror the change downwards in the hierarchy. Iterator entities = entityList().iterator(); while (entities.hasNext()) { Entity entity = (Entity) entities.next(); if (entity instanceof RefinementActor) { if (entity.getPort(name) == null) { try { ((RefinementActor) entity).setMirrorDisable(1); entity.newPort(name); } finally { ((RefinementActor) entity).setMirrorDisable(0); } } } } return port; } } } catch (IllegalActionException ex) { // This exception should not occur, so we throw a runtime // exception. throw new InternalErrorException( "ModalRefinement.newPort: Internal error: " + ex.getMessage()); } finally { _mirrorDisable = 0; _workspace.doneWriting(); } }