/* A set of utilities for handling GTEntities.
@Copyright (c) 2008-2014 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the
above copyright notice and the following two paragraphs appear in all
copies of this software.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
PT_COPYRIGHT_VERSION_2
COPYRIGHTENDKEY
*/
package ptolemy.actor.gt;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import ptolemy.actor.CompositeActor;
import ptolemy.actor.TypedIOPort;
import ptolemy.actor.gt.ingredients.criteria.PortCriterion;
import ptolemy.actor.gt.ingredients.criteria.SubclassCriterion;
import ptolemy.kernel.ComponentEntity;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.Entity;
import ptolemy.kernel.Port;
import ptolemy.kernel.util.Attribute;
import ptolemy.kernel.util.ChangeRequest;
import ptolemy.kernel.util.ConfigurableAttribute;
import ptolemy.kernel.util.KernelException;
import ptolemy.kernel.util.KernelRuntimeException;
import ptolemy.kernel.util.NamedObj;
import ptolemy.kernel.util.Settable;
import ptolemy.kernel.util.SingletonConfigurableAttribute;
import ptolemy.kernel.util.Workspace;
import ptolemy.moml.MoMLChangeRequest;
import ptolemy.moml.MoMLParser;
import ptolemy.util.StringUtilities;
import ptolemy.vergil.icon.EditorIcon;
/**
A set of utilities for handling GTEntities (instances of {@link GTEntity}).
@author Thomas Huining Feng
@version $Id: GTEntityUtils.java 70398 2014-10-22 23:44:32Z cxh $
@since Ptolemy II 7.1
@Pt.ProposedRating Yellow (tfeng)
@Pt.AcceptedRating Red (tfeng)
*/
public class GTEntityUtils {
/** 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)
+ "\n");
outputStarted = true;
}
attribute.exportMoML(output, depth + 1);
}
}
if (outputStarted) {
output.write(StringUtilities.getIndentPrefix(depth)
+ "\n");
}
}
}
}
/** 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 preservedPortNames = new HashSet();
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, "");
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.") {
@Override
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