/** * For each port of the given entity, if the port's derived level is greater than 0 (i.e., it is * created automatically by the entity), store the persistent attributes of the port in a "port" * XML element. * * @param entity The entity whose ports are looked at. * @param output The output writer. * @param depth The depth for the MoML output. * @exception IOException If the output writer cannot be written to. */ public static void exportPortProperties(GTEntity entity, Writer output, int depth) throws IOException { if (entity instanceof Entity) { Entity ptEntity = (Entity) entity; for (Object portObject : ptEntity.portList()) { Port port = (Port) portObject; if (port.getDerivedLevel() == 0) { continue; } boolean outputStarted = false; for (Object attributeObject : port.attributeList()) { Attribute attribute = (Attribute) attributeObject; if (attribute.isPersistent()) { if (!outputStarted) { output.write( StringUtilities.getIndentPrefix(depth) + "<port name=\"" + port.getName() + "\">\n"); outputStarted = true; } attribute.exportMoML(output, depth + 1); } } if (outputStarted) { output.write(StringUtilities.getIndentPrefix(depth) + "</port>\n"); } } } }
/** * Remove all the annotations from the graph. Actors, their ports, and relations are inspected to * see if they either a _color and/or an _explanation attribute. If so, then the attribute is * removed via a MoMl changeRequest. */ public void deAnnotateGraph() { StringBuffer moml = new StringBuffer(); Iterator entities = _model.entityList(ComponentEntity.class).iterator(); while (entities.hasNext()) { ComponentEntity entity = (ComponentEntity) (entities.next()); String entityDeletes = _deletesIfNecessary(entity); moml.append("<entity name=\"" + entity.getName() + "\">"); if (entityDeletes != null) { moml.append(entityDeletes); } Iterator ports = entity.portList().iterator(); while (ports.hasNext()) { Port port = (Port) (ports.next()); String portDeletes = _deletesIfNecessary(port); if (portDeletes != null) { moml.append("<port name=\"" + port.getName() + "\">" + portDeletes + "</port>"); } } moml.append("</entity>"); } Iterator relations = _model.relationList().iterator(); while (relations.hasNext()) { Relation relation = (Relation) (relations.next()); String relationDeletes = _deletesIfNecessary(relation); if (relationDeletes != null) { moml.append( "<relation name=\"" + relation.getName() + "\">" + relationDeletes + "\"/></relation>"); } } if (moml.length() > 0) { String momlUpdate = "<group>" + moml.toString() + "</group>"; MoMLChangeRequest request = new MoMLChangeRequest(this, _model, momlUpdate); request.setUndoable(true); request.setPersistent(false); _model.requestChange(request); } }
/** * Remove all ports by setting their container to null. As a side effect, the ports will be * unlinked from all relations. This method is write-synchronized on the workspace, and increments * its version number. */ public void removeAllPorts() { try { _workspace.getWriteAccess(); // Have to copy _portList to avoid corrupting the iterator. List portListCopy = new LinkedList(portList()); Iterator ports = portListCopy.iterator(); while (ports.hasNext()) { Port port = (Port) ports.next(); try { MoMLChangeRequest request = new MoMLChangeRequest(this, this, "<deletePort name=\"" + port.getName() + "\"/>"); request.setUndoable(true); requestChange(request); } catch (Exception ex) { // Ignore. } } } finally { _workspace.doneWriting(); } }
/** * Update the appearance (icons and ports) of the entity with the change in a * GTIngredientAttribute, such as a criterion or an operation. * * @param entity The entity. * @param attribute The attribute whose recent change leads to an update of the entity. */ public static void updateAppearance(final GTEntity entity, GTIngredientsAttribute attribute) { NamedObj object = (NamedObj) entity; Workspace workspace = object.workspace(); try { workspace.getWriteAccess(); List<?> icons = object.attributeList(EditorIcon.class); boolean foundPersistentIcon = false; for (Object iconObject : icons) { EditorIcon icon = (EditorIcon) iconObject; if (icon.isPersistent()) { foundPersistentIcon = true; break; } } Set<String> preservedPortNames = new HashSet<String>(); boolean isIconSet = false; int i = 1; GTIngredientList list = attribute.getIngredientList(); for (GTIngredient ingredient : list) { if (ingredient instanceof PortCriterion) { PortCriterion criterion = (PortCriterion) ingredient; String portID = criterion.getPortID(list); preservedPortNames.add(portID); TypedIOPort port = (TypedIOPort) ((ComponentEntity) entity).getPort(portID); boolean isInput = criterion.isInput(); boolean isOutput = criterion.isOutput(); boolean isMultiport = !criterion.isMultiportEnabled() || criterion.isMultiport(); if (port != null) { if (port instanceof PortMatcher) { port.setInput(isInput); port.setOutput(isOutput); } else { MoMLChangeRequest request = new MoMLChangeRequest( entity, object, "<deletePort name=\"" + port.getName() + "\"/>"); request.setUndoable(true); request.setMergeWithPreviousUndo(true); request.execute(); port = new PortMatcher(criterion, (ComponentEntity) entity, portID, isInput, isOutput); port.setDerivedLevel(1); } } else { port = new PortMatcher(criterion, (ComponentEntity) entity, portID, isInput, isOutput); port.setDerivedLevel(1); } port.setMultiport(isMultiport); } else if (ingredient instanceof SubclassCriterion && !isIconSet && !foundPersistentIcon) { SubclassCriterion criterion = (SubclassCriterion) ingredient; final String superclass = criterion.getSuperclass(); object.requestChange( new ChangeRequest(entity, "Deferred load actor icon action.") { protected void _execute() throws Exception { _loadActorIcon(entity, superclass); } }); isIconSet = true; } i++; } if (!isIconSet && !foundPersistentIcon) { object.requestChange(new RestoreAppearanceChangeRequest(entity)); } ComponentEntity component = (ComponentEntity) entity; try { component.workspace().getReadAccess(); List<?> portList = new LinkedList<Object>(component.portList()); for (i = 0; i < portList.size(); i++) { Port port = (Port) portList.get(i); if (port instanceof PortMatcher && !preservedPortNames.contains(port.getName())) { ((PortMatcher) port)._setPortCriterion(null); port.setContainer(null); } } } finally { component.workspace().doneReading(); } } catch (KernelException e) { throw new KernelRuntimeException( e, "Cannot update appearance for " + "actor " + entity.getName() + "."); } finally { workspace.doneWriting(); } }