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

In OCL, each value, whether it is an object, a component instance, or a datavalue, has a certain type, which defines the operations that can be applied to the object. Types in OCL are divided into the following groups:

  • Predefined types, as defined in the standard library, including the following:

    - Basic types

    - Collection types

  • User -defined types

The predefined basic types are Integer, Real, String , and Boolean , which are described in this chapter in some detail. Their definitions are similar to those in many known languages.

The predefined collection types are Collection, Set, Bag, OrderedSet, and Sequence . They are used to specify the exact results of a navigation through associations in a class diagram. You need to be familiar with these types to write more complex expressions. Collection types, and how to use them, are described in Chapter 9.

User-defined types, such as Customer or LoyaltyProgram , are defined by the user in the UML diagrams. Every instantiable model element ”that is, each class, interface, component, or datatype ”in a UML diagram is automatically a type in OCL. Chapter 8 explains how to work with user-defined types in OCL expressions.

Each OCL expression represents a value; therefore, it has a type as well ”either a user-defined type or a predefined OCL type. Each OCL expression has a result: the value that results from evaluating the expression. The type of the result value is equal to the type of the expression.

7.1.1 Value Types and Object Types

OCL distinguishes between value types and object types. Both are types, i.e., both specify instances, but there is one important difference: value types define instances that never change. The integer 1, for example, will never change its value and become an integer with a value of 2. Object types , or Classifiers, represent types that define instances that can change their value(s). An instance of the class Person can change the value of its attribute name and still remain the same instance. Other names have been used to indicate the same difference. For instance, Martin Fowler [Fowler97] calls object types reference objects, and value types value objects .

Another important characteristic of value types involves identity. For value types, the value identifies the instance, hence the name. Two occurrences of a value type that have the same value are by definition one and the same instance. Two occurrences of an object type are the same instance only if they have the same (object) identity. In other words, value types have value-based identity; object types have reference-based identity.

Both the predefined basic types and the predefined collection types of OCL are value types. The user-defined types can be either value types or object types. UML datatypes, including enumeration types, are value types. UML classes, components , and interfaces are object types.

Категории