ptolemy.actor.gt
Class GTTools

java.lang.Object
  extended by ptolemy.actor.gt.GTTools

public class GTTools
extends java.lang.Object

A set of tools for model transformation.

Since:
Ptolemy II 7.1
Version:
$Id: GTTools.java 57046 2010-01-27 23:35:53Z cxh $
Author:
Thomas Huining Feng
Accepted Rating:
Red (tfeng)
Proposed Rating:
Yellow (tfeng)

Constructor Summary
GTTools()
           
 
Method Summary
private static void _delete(Attribute attribute)
          Request a MoMLChangeRequest to delete this attribute from its container.
static void checkContainerClass(Attribute attribute, NamedObj container, java.lang.Class<? extends CompositeEntity> containerClass, boolean deep)
          Check the class of the container in which the attribute is to be placed.
static void checkUniqueness(Attribute attribute, NamedObj container)
          Check whether the attribute is unique in the given container.
static NamedObj cleanupModel(NamedObj model)
          Create a copy of the given model in a new workspace that is cleaned up with no execution state left in it.
static NamedObj cleanupModel(NamedObj model, MoMLParser parser)
          Create a copy of the given model with the given parser that is cleaned up with no execution state left in it.
static NamedObj cleanupModel(NamedObj model, Workspace workspace)
          Create a copy of the given model in the given workspace that is cleaned up with no execution state left in it.
static void deepAddAttributes(NamedObj container, java.lang.Class<? extends Attribute> attributeClass)
          Add an attribute to the given container and each of its children including ports, entities and relations, but not including attributes.
static void deepRemoveAttributes(NamedObj container, java.lang.Class<? extends Attribute> attributeClass)
          Remove all the attributes in the given class from the given container and all of its children including ports, entities and relations, but not including attributes.
static Attribute findMatchingAttribute(java.lang.Object object, java.lang.Class<? extends Attribute> attributeClass, boolean searchContainers)
          Find an attribute in the object or its container (if searchContainers is true) in the given class.
static NamedObj getChild(NamedObj object, java.lang.String name, boolean allowAttribute, boolean allowPort, boolean allowEntity, boolean allowRelation)
          Get the child of the given object with the given name.
static java.util.Collection<NamedObj> getChildren(NamedObj object, boolean includeAttributes, boolean includePorts, boolean includeEntities, boolean includeRelations)
          Get the children of the given object.
static java.lang.String getCodeFromObject(NamedObj object, NamedObj topContainer)
          Get the unique string description containing the type and name of the\ object within the given container.
static CompositeActorMatcher getContainingPatternOrReplacement(NamedObj object)
          Get the pattern or replacement of a transformation rule that contains the given object.
static NamedObj getCorrespondingPatternObject(NamedObj replacementObject)
          Given an object in the replacement, return the corresponding object in the pattern if any, or null otherwise.
static MoMLChangeRequest getDeletionChangeRequest(java.lang.Object originator, NamedObj object)
          Return the change request to delete the given object.
static NamedObj getObjectFromCode(java.lang.String code, NamedObj topContainer)
          Get the object within the given container whose type and name correspond to the code.
static java.lang.String getObjectTypeAbbreviation(NamedObj object)
          Get the abbreviation of the object's type.
static PatternObjectAttribute getPatternObjectAttribute(NamedObj object, boolean createNew)
          Get the PatternObjectAttribute associated with the object, and if it is not found, either return null if createNew is false, or create a new one and return it.
static boolean isCreated(java.lang.Object object)
          Return whether the object in the pattern is to be created.
static boolean isIgnored(java.lang.Object object)
          Return whether the object in the pattern is to be ignored.
static boolean isInPattern(NamedObj object)
          Return whether the object is in a pattern.
static boolean isInReplacement(NamedObj object)
          Return whether the object is in a replacement.
static boolean isNegated(java.lang.Object object)
          Return whether the object in the pattern is to be negated.
static boolean isOptional(java.lang.Object object)
          Return whether the object in the pattern is to be optional.
static boolean isPreserved(java.lang.Object object)
          Return whether the object in the pattern is to be preserved.
static void restoreValues(ComponentEntity root, java.util.Hashtable<ValueIterator,Token> records)
          Restore the values of the parameters that implement the ValueIterator interface within the root entity using the values recorded in the given table previously.
static void saveValues(ComponentEntity root, java.util.Hashtable<ValueIterator,Token> records)
          Save the values of parameters that implement the ValueIterator interface in the given records table, starting from the root entity.
static void setIconDescription(NamedObj object, java.lang.String iconDescription)
          Execute a MoMLChangeRequest to set the icon description of the attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GTTools

public GTTools()
Method Detail

checkContainerClass

public static void checkContainerClass(Attribute attribute,
                                       NamedObj container,
                                       java.lang.Class<? extends CompositeEntity> containerClass,
                                       boolean deep)
                                throws IllegalActionException
Check the class of the container in which the attribute is to be placed. If the container is not an intended one, throw an IllegalActionException.

Parameters:
attribute - The attribute to check.
container - The container.
containerClass - The intended class of container.
deep - Whether containers of the container should be checked instead, if the container does not qualify.
Throws:
IllegalActionException - If this attribute cannot be used with the given container.

checkUniqueness

public static void checkUniqueness(Attribute attribute,
                                   NamedObj container)
                            throws IllegalActionException
Check whether the attribute is unique in the given container.

Parameters:
attribute - The attribute to check.
container - The container.
Throws:
IllegalActionException - If the container already has an attribute in the same class.

cleanupModel

public static NamedObj cleanupModel(NamedObj model)
                             throws IllegalActionException
Create a copy of the given model in a new workspace that is cleaned up with no execution state left in it.

Parameters:
model - The model to be copied.
Returns:
A cleaned up copy of the given model.
Throws:
IllegalActionException - If the model cannot be copied.

cleanupModel

public static NamedObj cleanupModel(NamedObj model,
                                    MoMLParser parser)
                             throws IllegalActionException
Create a copy of the given model with the given parser that is cleaned up with no execution state left in it.

Parameters:
model - The model to be copied.
parser - The parser.
Returns:
A cleaned up copy of the given model.
Throws:
IllegalActionException - If the model cannot be copied.

cleanupModel

public static NamedObj cleanupModel(NamedObj model,
                                    Workspace workspace)
                             throws IllegalActionException
Create a copy of the given model in the given workspace that is cleaned up with no execution state left in it.

Parameters:
model - The model to be copied.
workspace - The workspace.
Returns:
A cleaned up copy of the given model.
Throws:
IllegalActionException - If the model cannot be copied.

deepAddAttributes

public static void deepAddAttributes(NamedObj container,
                                     java.lang.Class<? extends Attribute> attributeClass)
                              throws java.lang.InstantiationException,
                                     java.lang.IllegalAccessException,
                                     java.lang.reflect.InvocationTargetException
Add an attribute to the given container and each of its children including ports, entities and relations, but not including attributes. The name of the attribute is automatically generated by putting an "_" before the simple class name of the attribute to be created.

Parameters:
container - The container.
attributeClass - The attribute class.
Throws:
java.lang.InstantiationException - If an attribute cannot be instantiated.
java.lang.IllegalAccessException - If the constructor of the attribute class is not accessible.
java.lang.reflect.InvocationTargetException - If the arguments to constructor is not valid.

deepRemoveAttributes

public static void deepRemoveAttributes(NamedObj container,
                                        java.lang.Class<? extends Attribute> attributeClass)
                                 throws IllegalActionException
Remove all the attributes in the given class from the given container and all of its children including ports, entities and relations, but not including attributes.

Parameters:
container - The container.
attributeClass - The attribute class.
Throws:
IllegalActionException - If an attribute cannot be removed.

findMatchingAttribute

public static Attribute findMatchingAttribute(java.lang.Object object,
                                              java.lang.Class<? extends Attribute> attributeClass,
                                              boolean searchContainers)
Find an attribute in the object or its container (if searchContainers is true) in the given class.

Parameters:
object - The object to which the attribute longs.
attributeClass - The attribute class.
searchContainers - Whether containers of the object are searched.
Returns:
The attribute if found, or null otherwise.

getChild

public static NamedObj getChild(NamedObj object,
                                java.lang.String name,
                                boolean allowAttribute,
                                boolean allowPort,
                                boolean allowEntity,
                                boolean allowRelation)
Get the child of the given object with the given name. The child is either an attribute, a port, an entity, or a relation if permitted by the arguments.

Parameters:
object - The object.
name - The name of the child.
allowAttribute - Whether the child can be an attribute.
allowPort - Whether the child can be a port.
allowEntity - Whether the child can be an entity.
allowRelation - Whether the child can be a relation.
Returns:
The child if found, or null otherwise.

getChildren

public static java.util.Collection<NamedObj> getChildren(NamedObj object,
                                                         boolean includeAttributes,
                                                         boolean includePorts,
                                                         boolean includeEntities,
                                                         boolean includeRelations)
Get the children of the given object. The children can be either attributes, ports, entities, or relations if permitted by the arguments.

Parameters:
object - The object.
includeAttributes - Whether the children can be attributes.
includePorts - Whether the children can be ports.
includeEntities - Whether the children can be entities.
includeRelations - Whether the children can be relations.
Returns:
The collection of children.

getCodeFromObject

public static java.lang.String getCodeFromObject(NamedObj object,
                                                 NamedObj topContainer)
Get the unique string description containing the type and name of the\ object within the given container.

Parameters:
object - The object.
topContainer - The container used as the top level.
Returns:
The description.
See Also:
getObjectFromCode(String, NamedObj)

getContainingPatternOrReplacement

public static CompositeActorMatcher getContainingPatternOrReplacement(NamedObj object)
Get the pattern or replacement of a transformation rule that contains the given object.

Parameters:
object - The object.
Returns:
The pattern (of type Pattern) or replacement (of type Replacement.

getCorrespondingPatternObject

public static NamedObj getCorrespondingPatternObject(NamedObj replacementObject)
Given an object in the replacement, return the corresponding object in the pattern if any, or null otherwise.

Parameters:
replacementObject - The object in the replacement.
Returns:
The object in the pattern, or null if not found.

getDeletionChangeRequest

public static MoMLChangeRequest getDeletionChangeRequest(java.lang.Object originator,
                                                         NamedObj object)
Return the change request to delete the given object.

Parameters:
originator - The originator of the change request.
object - The object to be deleted.
Returns:
The change request.

getObjectFromCode

public static NamedObj getObjectFromCode(java.lang.String code,
                                         NamedObj topContainer)
Get the object within the given container whose type and name correspond to the code.

Parameters:
code - The code.
topContainer - The container used as the top level.
Returns:
The object, or null if not found.
See Also:
getCodeFromObject(NamedObj, NamedObj)

getObjectTypeAbbreviation

public static java.lang.String getObjectTypeAbbreviation(NamedObj object)
Get the abbreviation of the object's type.

Parameters:
object - The object.
Returns:
The abbreviation.

getPatternObjectAttribute

public static PatternObjectAttribute getPatternObjectAttribute(NamedObj object,
                                                               boolean createNew)
                                                        throws IllegalActionException,
                                                               NameDuplicationException
Get the PatternObjectAttribute associated with the object, and if it is not found, either return null if createNew is false, or create a new one and return it.

Parameters:
object - The object.
createNew - Whether a new attribute should be created if it is not found.
Returns:
The attribute.
Throws:
IllegalActionException - If the attribute is not of an acceptable class for the container, or if the name contains a period.
NameDuplicationException - If the name coincides with an attribute already in the container.

isCreated

public static boolean isCreated(java.lang.Object object)
Return whether the object in the pattern is to be created.

Parameters:
object - The object in the pattern.
Returns:
true if it is to be created; false otherwise.

isIgnored

public static boolean isIgnored(java.lang.Object object)
Return whether the object in the pattern is to be ignored.

Parameters:
object - The object in the pattern.
Returns:
true if it is to be ignored; false otherwise.

isInPattern

public static boolean isInPattern(NamedObj object)
Return whether the object is in a pattern.

Parameters:
object - The object.
Returns:
true if the object is in a pattern; false otherwise.
See Also:
isInReplacement(NamedObj)

isInReplacement

public static boolean isInReplacement(NamedObj object)
Return whether the object is in a replacement.

Parameters:
object - The object.
Returns:
true if the object is in a replacement; false otherwise.
See Also:
isInPattern(NamedObj)

isNegated

public static boolean isNegated(java.lang.Object object)
Return whether the object in the pattern is to be negated.

Parameters:
object - The object in the pattern.
Returns:
true if it is to be negated; false otherwise.

isOptional

public static boolean isOptional(java.lang.Object object)
Return whether the object in the pattern is to be optional.

Parameters:
object - The object in the pattern.
Returns:
true if it is to be optional; false otherwise.

isPreserved

public static boolean isPreserved(java.lang.Object object)
Return whether the object in the pattern is to be preserved.

Parameters:
object - The object in the pattern.
Returns:
true if it is to be preserved; false otherwise.

restoreValues

public static void restoreValues(ComponentEntity root,
                                 java.util.Hashtable<ValueIterator,Token> records)
                          throws IllegalActionException
Restore the values of the parameters that implement the ValueIterator interface within the root entity using the values recorded in the given table previously. The values are restored bottom-up.

Parameters:
root - The root.
records - The table with the previously stored values.
Throws:
IllegalActionException - If the values of those parameters cannot be set.

saveValues

public static void saveValues(ComponentEntity root,
                              java.util.Hashtable<ValueIterator,Token> records)
                       throws IllegalActionException
Save the values of parameters that implement the ValueIterator interface in the given records table, starting from the root entity.

Parameters:
root - The root.
records - The table to store the values.
Throws:
IllegalActionException - If the values of those parameters cannot be obtained.

setIconDescription

public static void setIconDescription(NamedObj object,
                                      java.lang.String iconDescription)
Execute a MoMLChangeRequest to set the icon description of the attribute.

Parameters:
object - The attribute.
iconDescription - The icon description.

_delete

private static void _delete(Attribute attribute)
Request a MoMLChangeRequest to delete this attribute from its container.

Parameters:
attribute - The attribute to be deleted.