The OCL and UML metamodels are defined in the OMG standards, and are complete books on their own. The following sections describe only a simplified version of both. They also explain the relationship between them, which will clarify the context of an OCL expression in terms of metaclasses. 5.3.1 The UML MetamodelA simplified version of part of the UML metamodel is shown in Figure 5-3. It contains the concepts that can be used in a class diagram. Everything in a model is a ModelElement , so this is the superclass of all other metaclasses. Also included are types, which in the UML metamodel are called Classifiers. Classifier is an abstract superclass of the types one may encounter in a model: Class, DataType , and Interface (actually, Component should be in this list too). Every classifier has Features , either Attributes, Operations , or AssociationEnds . Every feature has a type. For attributes, this is simply the type of the attribute. For operations, this represents the return type. For association ends, the type refers to the class with which the holder of the association end is associated. To define associations, two or more AssociationEnds are coupled into one Association . Figure 5-3. UML metamodel (simplified)
For instance, in the R&L model, the class LoyaltyProgram has an association with the class ProgramPartner . Both ends of the association have multiplicity 1..*. The end at LoyaltyProgram is called programs , and the end at ProgramPartner is called partners . In terms of the metamodel, this means that there are two instances of the metaclass Class , called LoyaltyProgram and ProgramPartner . The LoyaltyProgram instance holds an AssociationEnd instance called partners . The type of this AssociationEnd is the instance of metaclass Class called ProgramPartner . 5.3.2 The OCL MetamodelA simplified version of part of the OCL metamodel is shown in Figure 5-4. It defines the various types of expressions that can be used. Superclass to all expressions is the metaclass OclExpression . The subclass ModelPropertyCallExp represents an expression that references a value within the model, an attribute, an operation, or an association end. The metaclass ModelPropertyCallExp has three subtypes to indicate the kind of feature of the source that has been called: AttributeCallExp, OperationCallExp , and AssociationEndCallExp . Because properties are always called from some object, a ModelPropertyCallExp has a source, which is another OclExpression . Figure 5-4. OCL metamodel (simplified) in relation to UML metamodel
For instance, in the following expression, monkey is the OclExpression, which is the source of the pealsBanana() part. The pealsBanana() part is an instance of ModelPropertyCallExp . In fact, it is an instance of its subclass, OperationCallExp : monkey.pealsBanana() Expressions that loop over collections are instances of the metaclass LoopExpression . Each loop expression has a source that is a collection or an instance treated as a collection. This source can be written as an OCL expression. The body of a loop expression is the part that indicates which elements of the collection should be considered . For instance, in the following expression, monkey.siblings is an OCL expression that represents the source of the select loop expression. The part between brackets, eyes.colour = Colour::blue , is the body of the loop expression: monkey.siblings->select( eyes.colour = Colour::blue ) Although not shown in Figure 5-4, the OCL metamodel adds a number of datatypes to the UML metamodel, such as the Set, OrderedSet, Bag , and Sequence types. Each is a subclass of Classifier ; in fact they are subclasses of the metaclass DataType . 5.3.3 The Relationship Between the UML and OCL MetamodelsThe relationship between the UML and OCL metamodels is twofold. First, an OCL expression may reference an element from the model. This element is an instance of a UML metaclass. This relation is shown in Figure 5-4. The metaclasses from the UML metamodel are shown in gray. The associations between the OCL metaclasses and the UML metaclasses define the relationship between the UML and OCL metamodel. For instance, every OCL expression results in a value. The type of this value is an instance of the UML metaclass Classifier . This relationship is represented in the metamodels by the association between the metaclasses OclExpression and Classifier . Another example is an OCL expression that references an attribute. The expression is an instance of AttributeCallExp , and the attribute is an instance of Attribute . Their relationship is represented by the association between both metaclasses. The second relationship between the UML and OCL metamodels is shown in Figure 5-5. Various elements from the UML metamodel may be adorned with information in the form of an OCL expression. For instance, invariants are OclExpression instances that are linked to a Classifier instance. This relationship represents the relation between an OCL expression and its context. Figure 5-5. OCL context in terms of the metamodels
For instance, an attribute A may have a derivation rule. In terms of the metamodels, the attribute is an instance of the metaclass Attribute , and the derivation rule is an instance of the metaclass OclExpression . The fact that the rule describes the derivation for attribute A is represented in the metamodel by the association with rolename derivationRule between the metaclasses Attribute and OclExpression . |