You can define a class's DefaultStringRepresentation to control the text returned when that object is used with asString; use it to give objects a meaningful, consistent presentation in OCL expressions, messages, and views.
How DefaultStringRepresentation works
asString is available on objects and converts the value to a string in the result of an OCL expression. For an object instance, the operation uses the class's DefaultStringRepresentation expression as its result.
For example, if a Car class has a registration-number attribute and its DefaultStringRepresentation is based on that attribute, then an expression such as:
self.asStringreturns the car's registration number. This makes a constraint or message identify the actual car rather than an unspecified object representation.
Set a DefaultStringRepresentation that identifies the object to a user. A registration number is suitable for Car; a name or a business identifier may be suitable for another class. The expression must remain valid while the object is incomplete. For example, a newly created car may not yet have a brand, but it can still have a registration number for display.
Use a derived presentation attribute
Do not place a complex expression directly in DefaultStringRepresentation when the representation must update as model values change. Instead, define a derived attribute that contains the presentation logic, then use that attribute from DefaultStringRepresentation.
For example, on a Fruit class, you can define a derived attribute named Presentation that combines the fruit name and weight. Configure the class's DefaultStringRepresentation to use:
self.PresentationWhen the fruit's weight changes, the derived Presentation value changes with it. The same presentation definition can also be used elsewhere in the model, rather than duplicating the expression.
Recommended steps
- Identify the short text that users need to recognize an instance. For example, use a car's registration number.
- Add a derived attribute, such as
Presentation, when the text requires more than a single stable attribute or will be reused. - Put the OCL expression for the display text on the derived attribute.
- Set the class's DefaultStringRepresentation to
self.Presentation. - Use
self.asStringwhere an expression needs that object as text. - Change an input value used by the presentation and verify that the derived value and displayed representation update.
Performance and change tracking
DefaultStringRepresentation and asString are not subscribed. Each use of asString must be reevaluated. This matters when an expression is evaluated often or when it is part of an expression tree that needs efficient change tracking.
Avoid using asString directly in expressions that have high performance requirements. It can prevent the expression tree from subscribing properly to the values on which the text depends.
Use a derived attribute as the subscribed boundary instead. Put the dependencies in the derived attribute's OCL expression and refer to that attribute from DefaultStringRepresentation. For example, use self.Presentation as the default representation rather than repeating an expression that reads several attributes each time asString is evaluated.
| Situation | Recommended approach |
|---|---|
| You need a readable object name in a message or expression. | Define a meaningful DefaultStringRepresentation and use object.asString.
|
| The representation combines values or is used in several places. | Create a derived Presentation attribute and use self.Presentation in DefaultStringRepresentation.
|
| The surrounding expression is performance-sensitive. | Avoid direct asString in the expression tree; use a derived attribute whose dependencies can be tracked.
|
Do not confuse similar string features
| Feature | Use it for | Example |
|---|---|---|
asString
|
Converting an object or value to its string representation in OCL. | self.asString returns the text defined by the object's DefaultStringRepresentation.
|
toString
|
Formatting a number as text, optionally with a format string. | 5.toString('00') returns '05'.
|
| State value as a string | Converting a state-machine state value to text. | (#New).asString.
|
<AsString> in ViewModels
|
The case-sensitive ViewModel pattern used in action names or constraint messages. | This is not the same feature as the object asString operation; for non-action ViewModel presentation, use Databind labels.
|
ClassFromString
|
Converting a string that names a model type back to an actionable OclType when working with model metadata. | Use it as the inverse direction when a type has first been treated as a string. |
