The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)

8.1 Features of User -Defined Types

When a user-defined type is specified in an UML diagram, a number of features of that type are given. The features of a user-defined type include the following:

  • Attributes

  • Operations

  • Class attributes

  • Class operations

  • Association ends that are derived from associations and aggregations [1]

    [1] Note that inheritance relationships cannot be navigated, because they don't represent relationships between instances.

Each feature can be used in an OCL expression. This section explains how the first four features may be used. Section 8.2 explains how to use the information in an association.

8.1.1 Attributes and Operations

Attributes of user-defined types may be used in expressions by writing a dot followed by the attribute name . As with attributes, the operations of user-defined types can be used in OCL expressions. However, note one fundamental restriction: Because OCL is a side-effect-free language, operations that change the state of any object are not allowed. Only so-called query operations , which return a value but don't change anything, can be used. According to the UML specification, each operation has a boolean label called isQuery . If this label is true, the operation has no side effects and can be used in OCL expressions.

The dot notation used to reference attributes is also used to reference operations. The name of the operation, however, is always followed by two parentheses, which enclose the optional arguments of the operation. Even if an operation has no arguments, the parentheses are mandatory. This is necessary to distinguish between attributes and operations, because UML allows attributes and operations to have identical names .

The visibility of attributes and operations is ignoredby default in OCL. Optionally OCL can use the rules given in the UML specification. In this case, a private attribute of an associated object may not be used in an OCL expression, because it is not visible to the contextual instance.

8.1.2 Class Operations and Attributes

Class operations and class attributes may also be used in OCL expressions. The syntax for referencing a class attribute or operation is the class name followed by two colons, followed by the attribute or operation name (and parameters). For example, in the R&L example shown in Figure 2-1, the attribute now of Date is a class attribute. It can be used as shown in the following example:

context CustomerCard inv : goodThru.isAfter( Date::now )

Категории