Home Learn more about OCL Rational Rose OCL-AddIn product description Downloads Buy our products Support information Sitemap Contact Privacy policy  
Examples
Example 4

Example 4: Expressing UML Wellformedness Rules

The UML specification itself uses OCL to specify the semantics of the UML metamodel. In addition, OCL expressions are used to tailor the UML to specific needs by creating a UML profile.
Let's look at an example from the UML specification.
Class Foundation::Core::Association is used to describe an association between two or more involved classes. Here is a fragment of the class diagram that defines class Association and its relation to other classes. The diagram states, that an Association aggregates two or more AssociationEnds. Since it would lead to ambiguities, if two or more association ends have the same role names, this should be forbidden. The authors of the UML specification have specified this like this.

For readability, a new attribute was introduced as a definition expression:

allConnections : Set(AssociationEnd);
allConnections = self.connection
Then the following wellformedness rule was specified:

[1] The AssociationEnds must have a unique name within the Association.

self.allConnections->forAll(r1, r2 |
                            r1.name = r2.name implies r1 = r2 )
The UML specification uses a variation of the OCL syntax, which expresses the expression type in the chapters heading (for example, invariants are listed in a chapter named "Well-formedness Rules"). The correct OCL syntax is:
package Foundation::Core
context Association
def: allConnections : Sequence(AssociationEnd) = self.connection
inv: self.allConnections->forAll(r1, r2 | 
                                 r1.name = r2.name implies r1 = r2 )
endpackage
Please note, that this version defines the type of allConnections as Sequence and not as Set. The OCL specification defines that the result of navigating an ordered association is a Sequence and a Sequence does not conform to a Set. This error in the UML specification was detected by our OCL-AddIn and caused this error message:

Initialization expression (of type 'Sequence(AssociationEnd)') does not conform to declared type ('Set(AssociationEnd)').

Page last modified: 2009-12-01 22:13:18