Package ptolemy.data.type
Class RecordType
- java.lang.Object
-
- ptolemy.data.type.StructuredType
-
- ptolemy.data.type.AssociativeType
-
- ptolemy.data.type.RecordType
-
- All Implemented Interfaces:
java.lang.Cloneable
,Type
- Direct Known Subclasses:
LocationType
,MetroIIPtidesPlatform.PtidesNetworkType
,PtidesPlatform.PtidesNetworkModelTimeType
,PtidesPlatform.PtidesNetworkRealTimeType
public class RecordType extends AssociativeType implements java.lang.Cloneable
A class representing the type of a RecordToken. To set the type of a typeable object (such as a port or parameter) to a record with particular fields, create an instance of this class and call setTypeEquals() with that instance as an argument.Note that a record type with more fields is a subtype of a record type with a subset of the fields. For example, {x = double, y = int} is a subtype of {x = double}. When a record of type {x = double, y = int} is converted to one of type {x = double}, the extra field is preserved. The converted record, therefore, may have more fields than the type indicates, but not fewer.
A consequence of this is that all record types are subtypes of the empty record type. Hence, to require that a typeable object be a record type without specifying what the fields are, use
typeable.setTypeAtMost(BaseType.RECORD);
Note, however, that by itself this type constraint will not be useful because it does not, by itself, prevent the type from resolving to unknown (the unknown type is at the bottom of the type lattice, and hence satisfies this type constraint). To force a port to resolve to the empty record type, you can statetypeable.setTypeEquals(BaseType.RECORD);
Equivalently, in a port configurer dialog, you can set the type of a port to record, which references an empty record.- Since:
- Ptolemy II 3.0
- Version:
- $Id$
- Author:
- Yuhong Xiong, Elaine Cheong and Steve Neuendorffer; contributors: J. S. Senecal, Marten Lohstroh
- Pt.AcceptedRating:
- Red (cxh)
- Pt.ProposedRating:
- Red (neuendor)
-
-
Field Summary
Fields Modifier and Type Field Description static RecordType
EMPTY_RECORD
An empty record.-
Fields inherited from class ptolemy.data.type.StructuredType
MAXDEPTHBOUND
-
Fields inherited from interface ptolemy.data.type.Type
HASH_INVALID
-
-
Constructor Summary
Constructors Constructor Description RecordType(java.lang.String[] labels, Type[] types)
Construct a new RecordType with the specified labels and types.RecordType(java.util.Map<java.lang.String,ptolemy.data.type.RecordType.FieldType> fieldMap)
Construct a RecordType with the labels and values specified by a given Map object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
_compare(StructuredType type)
Compare this type with the specified type.protected StructuredType
_getRepresentative()
Return a static instance of RecordType.protected StructuredType
_greatestLowerBound(StructuredType type)
Return the greatest lower bound of this type with the specified type.protected StructuredType
_leastUpperBound(StructuredType type)
Return the least Upper bound of this type with the specified type.java.lang.Object
clone()
Return a deep copy of this RecordType if it is a variable, or itself if it is a constant.Token
convert(Token token)
Convert the argument token into a RecordToken having this type, if compatible.int
depth()
Return the depth of a record type.boolean
equals(java.lang.Object object)
Determine if the argument represents the same RecordType as this object.Type
get(java.lang.String label)
Return the type of the specified label.java.lang.Class<RecordToken>
getTokenClass()
Return the class for tokens that this type represents.InequalityTerm
getTypeTerm(java.lang.String label)
Return the InequalityTerm representing the type of the specified label.int
hashCode()
Return a hash code value for this object.void
initialize(Type type)
Set the elements that have declared type BaseType.UNKNOWN (the leaf type variable) to the specified type.boolean
isAbstract()
Return true if this type does not correspond to a single token class.boolean
isCompatible(Type type)
Test whether the argument type is compatible with this type (is less than or equal to this type in the type lattice).boolean
isConstant()
Test if this RecordType is a constant.boolean
isInstantiable()
Test if this type corresponds to an instantiable token class.boolean
isSubstitutionInstance(Type type)
Test whether the specified type is a substitution instance of this type.java.util.Set<java.lang.String>
labelSet()
Return the labels of this record type as a Set.java.lang.String
toString()
Return the string representation of this type.void
updateType(StructuredType newType)
Update this Type to the specified RecordType.-
Methods inherited from class ptolemy.data.type.StructuredType
add, divide, getTypeHash, modulo, multiply, one, subtract, zero
-
-
-
-
Field Detail
-
EMPTY_RECORD
public static RecordType EMPTY_RECORD
An empty record.
-
-
Constructor Detail
-
RecordType
public RecordType(java.lang.String[] labels, Type[] types)
Construct a new RecordType with the specified labels and types. To leave the types of some fields undeclared, use BaseType.UNKNOWN. The labels and the types are specified in two arrays. These two arrays must have the same length, and their elements have one to one correspondence. That is, the i'th entry in the types array is the type for the i'th label in the labels array. Record labels may be arbitrary strings. To construct the empty record type, set the length of the argument arrays to 0. *- Parameters:
labels
- An array of String.types
- An array of Type.- Throws:
java.lang.IllegalArgumentException
- If the two arrays do not have the same size.java.lang.NullPointerException
- If one of the arguments is null.
-
RecordType
public RecordType(java.util.Map<java.lang.String,ptolemy.data.type.RecordType.FieldType> fieldMap) throws IllegalActionException
Construct a RecordType with the labels and values specified by a given Map object. The object cannot contain any null keys or values.- Parameters:
fieldMap
- A Map that has keys of type String and values of type Token.- Throws:
IllegalActionException
- If the map contains null keys or values, or if it contains non-String keys or non-Type values
-
-
Method Detail
-
clone
public java.lang.Object clone()
Return a deep copy of this RecordType if it is a variable, or itself if it is a constant.- Specified by:
clone
in interfaceType
- Specified by:
clone
in classStructuredType
- Returns:
- A RecordType.
-
convert
public Token convert(Token token) throws IllegalActionException
Convert the argument token into a RecordToken having this type, if compatible. The argument must be a RecordToken, and its type must be a subtype of this record type. The argument token must have at least the fields of this type. Extra fields in the argument token that are not in this type are preserved.- Specified by:
convert
in interfaceType
- Parameters:
token
- A token.- Returns:
- An RecordToken.
- Throws:
IllegalActionException
- If token is incompatible with this type.
-
depth
public int depth()
Return the depth of a record type. The depth of a record type is the number of times it contains other structured types. For example, a record of arrays has depth 2.- Overrides:
depth
in classStructuredType
- Returns:
- the depth of a record type.
-
equals
public boolean equals(java.lang.Object object)
Determine if the argument represents the same RecordType as this object. Two record types are equal if they have the same field names and the type of each field is the same.
-
get
public Type get(java.lang.String label)
Return the type of the specified label. If this type does not contain the specified label, return null.- Specified by:
get
in classAssociativeType
- Parameters:
label
- The specified label.- Returns:
- a Type.
-
getTokenClass
public java.lang.Class<RecordToken> getTokenClass()
Return the class for tokens that this type represents.- Specified by:
getTokenClass
in interfaceType
- Returns:
- The class for tokens that this type represents.
-
getTypeTerm
public InequalityTerm getTypeTerm(java.lang.String label)
Return the InequalityTerm representing the type of the specified label.- Parameters:
label
- The specified label.- Returns:
- An InequalityTerm.
- See Also:
InequalityTerm
-
hashCode
public int hashCode()
Return a hash code value for this object.- Overrides:
hashCode
in classjava.lang.Object
-
isAbstract
public boolean isAbstract()
Return true if this type does not correspond to a single token class. This occurs if the type is not instantiable, or it represents either an abstract base class or an interface.- Specified by:
isAbstract
in interfaceType
- Overrides:
isAbstract
in classStructuredType
- Returns:
- true if the type of any field is abstract.
-
initialize
public void initialize(Type type)
Set the elements that have declared type BaseType.UNKNOWN (the leaf type variable) to the specified type.- Specified by:
initialize
in classStructuredType
- Parameters:
type
- the type to set the leaf type variable to.
-
isCompatible
public boolean isCompatible(Type type)
Test whether the argument type is compatible with this type (is less than or equal to this type in the type lattice). The given type will be compatible with this type if it is BaseType.UNKNOWN, or it is a RecordType that contains at least the fields of this record type.- Specified by:
isCompatible
in interfaceType
- Parameters:
type
- An instance of Type.- Returns:
- True if the argument is compatible with this type.
-
isConstant
public boolean isConstant()
Test if this RecordType is a constant. A RecordType is a constant if the declared type of all of its fields are constant.- Specified by:
isConstant
in interfaceType
- Returns:
- True if this type is a constant.
-
isInstantiable
public boolean isInstantiable()
Test if this type corresponds to an instantiable token class. A RecordType is instantiable if all of its fields are instantiable.- Specified by:
isInstantiable
in interfaceType
- Returns:
- True if this type is instantiable.
-
isSubstitutionInstance
public boolean isSubstitutionInstance(Type type)
Test whether the specified type is a substitution instance of this type. One record is a substitution instance of another if they have fields with the same names and each field of the given type is a substitution instance of the corresponding field in this type.- Specified by:
isSubstitutionInstance
in interfaceType
- Parameters:
type
- A Type.- Returns:
- True if the argument is a substitution instance of this type.
- See Also:
Type.isSubstitutionInstance(ptolemy.data.type.Type)
-
labelSet
public java.util.Set<java.lang.String> labelSet()
Return the labels of this record type as a Set.- Returns:
- A Set containing strings.
-
toString
public java.lang.String toString()
Return the string representation of this type. The format is {label = type, label = type, ...}. The record fields are listed in the lexicographical order of the labels determined by the java.lang.String.compareTo() method.
-
updateType
public void updateType(StructuredType newType) throws IllegalActionException
Update this Type to the specified RecordType. The specified type must be a RecordType and have the same structure as this one, and have depth less than the MAXDEPTHDOUND. This method will only update the component whose declared type is BaseType.UNKNOWN, and leave the constant part of this type intact.- Overrides:
updateType
in classStructuredType
- Parameters:
newType
- A StructuredType.- Throws:
IllegalActionException
- If the specified type is not a RecordType or it does not have the same structure as this one.
-
_compare
protected int _compare(StructuredType type)
Compare this type with the specified type. The specified type must be a RecordType, otherwise an exception will be thrown. This method returns one of ptolemy.graph.CPO.LOWER, ptolemy.graph.CPO.SAME, ptolemy.graph.CPO.HIGHER, ptolemy.graph.CPO.INCOMPARABLE, indicating this type is lower than, equal to, higher than, or incomparable with the specified type in the type hierarchy, respectively.- Specified by:
_compare
in classStructuredType
- Parameters:
type
- a RecordType.- Returns:
- An integer.
- Throws:
java.lang.IllegalArgumentException
- If the specified type is not a RecordType.
-
_getRepresentative
protected StructuredType _getRepresentative()
Return a static instance of RecordType.- Specified by:
_getRepresentative
in classStructuredType
- Returns:
- a RecordType.
-
_greatestLowerBound
protected StructuredType _greatestLowerBound(StructuredType type)
Return the greatest lower bound of this type with the specified type. The specified type must be a RecordType, otherwise an exception will be thrown.- Specified by:
_greatestLowerBound
in classStructuredType
- Parameters:
type
- a RecordType.- Returns:
- a RecordType.
- Throws:
java.lang.IllegalArgumentException
- If the specified type is not a RecordType.
-
_leastUpperBound
protected StructuredType _leastUpperBound(StructuredType type)
Return the least Upper bound of this type with the specified type. The specified type must be a RecordType, otherwise an exception will be thrown.- Specified by:
_leastUpperBound
in classStructuredType
- Parameters:
type
- a RecordType.- Returns:
- a RecordType.
- Throws:
java.lang.IllegalArgumentException
- If the specified type is not a RecordType.
-
-