You can use attributes to obtain the names of attributes owned by a class when you need class metadata in an OCL expression.
Purpose
attributes returns metadata about a class: a collection containing the names of the attributes that the class owns. It does not return the values held by an object.
For example, if a class owns the attributes Name, Age, and IsActive, the result contains the strings 'Name', 'Age', and 'IsActive'.
Return type
| Item | Value |
|---|---|
| Return type | Collection(String)
|
| Each collection item | The name of one attribute owned by the class |
Example
Assume that the class Person owns these attributes:
| Attribute | Example value on a Person object |
|---|---|
Name
|
'Ada'
|
Age
|
37
|
IsActive
|
true
|
Using attributes for Person returns a Collection(String) with the attribute names:
Name
Age
IsActiveThe result describes the class definition. It does not return 'Ada', 37, or true.
| OCL Operation | Result |
|---|---|
| Product.attributes | ['Price', 'DateAdded', 'Description', 'Name'] |
Use it as metadata
Use this operator when an expression needs to discover which attributes are defined on a class. Treat the result as a collection of strings: each item is an attribute name that you can inspect, compare, or present as metadata.
Do not use attributes when you need a particular object's data. In the example above, Name is an attribute name, while 'Ada' is that attribute's value.
