From the wikipedia: The Object Constraint Language (OCL) is a declarative language describing rules applying to Unified Modeling Language (UML) models developed at IBM and is now part of the UML standard.

My goal is to provide a detailed and simplified guide on everything about OCL, from its data types, operators, invariants, pre- and postconditions, including some examples along the way. The information provided in this guide is from trusted documentation, which is mentioned in the Section Sources.

I will be using an older software called OCLE. There is a newer software like Papyrus or others, which can provide a better user interface, nevertheless this guide should still be usable with any other software supporting Object Constraint Language.

Let’s begin I will start with explanation of some keywords/variables, that are important in order to understand, how ocl works.

Self

Each OCL expression is written in the context of an instance of a specific type. In an OCL expression, the reserved word self is used to refer to the contextual instance. For example, if the context is Company, then self refers to an instance of Company.

Self can be used to introduce new properties to the contextual instance. If Company need to have have a property called name, we can assign or get it using keyword self, for example, self.name.

Invariants

An OCL expression is an invariant of the type and must be true for all instances of that type at any time. (Note that all OCL expressions that express invariants are of the type Boolean.) For example, if in the context of the Company type, the following expression would specify an invariant that the number of employees must always exceed 50:

self.numberOfEmployees > 50

where self is an instance of type Company. (We can view self as the object from where we start evaluating the expression.) This invariant holds for every instance of the Company type.

The type of the contextual instance of an OCL expression, which is part of an invariant, is written with the context keyword, followed by the name of the type as follows. The label inv: declares the constraint to be an «invariant» constraint.

context Company inv:
self.numberOfEmployees > 50

In most cases, the keyword self can be dropped because the context is clear, as in the above examples. As an alternative for self, a different name can be defined playing the part of self. For example:

context c : Company inv:
c.numberOfEmployees > 50

This invariant is equivalent to the previous one.
Optionally, the name of the constraint may be written after the inv keyword, allowing the constraint to be referenced by name. In the following example the name of the constraint is enoughEmployees.

context c : Company inv enoughEmployees:
c.numberOfEmployees > 50

Pre- and Postconditions

The OCL expression can be part of a Precondition or Postcondition, corresponding to «precondition» and «postcondition» stereotypes of Constraint associated with an Operation or other behavioral feature. The context declaration in OCL uses the context keyword, followed by the type and operation declaration. The stereotype of constraint is shown by putting the labels ‘pre:’ and ‘post:’ before the actual Preconditions and Postconditions. For example:

context Typename::operationName(param1 : Type1, … ): ReturnType
pre: param1 > …
post: result = …

The name self can be used in the expression referring to the object on which the operation was called. The reserved word result denotes the result of the operation, if there is one. The names of the parameters (param1) can also be used in the OCL expression. In the example diagram, we can write:

context Person::income(d : Date) : Integer
post: result = 5000

Optionally, the name of the precondition or postcondition may be written after the pre or post keyword, allowing the constraint to be referenced by name. In the following example the name of the precondition is parameterOk and the name of the postcondition is resultOk.

context Typename::operationName(param1 : Type1, … ): ReturnType
pre parameterOk: param1 > …
post resultOk : result = …

Package Context

The above context declaration is precise enough when the package in which the Classifier belongs is clear from the environment. To specify explicitly in which package invariant, pre or postcondition Constraints belong, these constraints can be enclosed between ‘package‘ and ‘endpackage‘ statements. The package statements have the syntax:

package Package::SubPackage
context X inv:
… some invariant …
context X::operationName(..)
pre: … some precondition …
endpackage

An OCL file (or stream) may contain any number package statements, thus allowing all invariant, preconditions, and postconditions to be written and stored in one file. This file may co-exist with a UML model as a separate entity.

Operation Body Expression

An OCL expression may be used to indicate the result of a query operation. This can be done using the following syntax:

context Typename::operationName(param1 : Type1, … ): ReturnType
body: — some expression

The expression must conform to the result type of the operation. Like in the pre- and postconditions, the parameters may be used in the expression. Pre-, and postconditions, and body expressions may be mixed together after one operation context. For example:

context Person::getCurrentSpouse() : Person
pre: self.isMarried = true
body: self.mariages->select( m | m.ended = false ).spouse

In this case, variable m is an item in a list of marriages and m.ended = false queries all the marriages, that have not been ended, retrieving the current marriage.

Initial and Derived Values

An OCL expression may be used to indicate the initial or derived value of an attribute or association end. This can be done using the following syntax:

context Typename::attributeName: Type
init: — some expression representing the initial value
context Typename::assocRoleName: Type
derive: — some expression representing the derivation rule

The expression must conform to the result type of the attribute. In the case the context is an association end the expression must conform to the classifier at that end when the multiplicity is at most one, or Set, or OrderedSet when the multiplicity may be more than one. Initial and derivation expressions may be mixed together after one context. For example:

The derivation constraint must be satisfied at any time, hence the derivation includes the initialization. Both are allowed on the same property but they must not be contradictory. For each property there should be at most one initialization constraint and at most one derivation constraint.

Values and Types

In OCL, a number of basic types are predefined and available to the modeler at all times. These predefined value types are independent of any object model and are part of the definition of OCL.
The most basic value in OCL is a value of one of the basic types. The basic types of OCL, with corresponding examples of their values, are shown in the following table

TypeExamplesAccording to specification
OclInvalidinvalid
OclVoidnull, invalid
Booleantrue, false (MOF) http://www.w3.org/TR/xmlschema-2/#boolean
Integer1, -5, 2, 34, 26524, … (MOF) http://www.w3.org/TR/xmlschema-2/#integer
Real1.5, 3.14, … http://www.w3.org/TR/xmlschema-2/#double
String‘To be or not to be…’(MOF) http://www.w3.org/TR/xmlschema-2/#string
UnlimitedNatural 0, 1, 2, 42, …, *http://www.w3.org/TR/xmlschema-2/#nonNegativeInteger
Table. Basic OCL types and their values

OCL defines a number of operations on the predefined types. Table 7.2 – gives some examples of the operations on the predefined types.

TypeExamples
Booleanand, or, xor, not, implies, if-then-else
Integer*, +, -, /, abs()
Real*, +, -, /, floor()
Stringconcat(), size(), substring()
UnlimitedNatural *, +, /
Table. Examples of operations on the predefined types

I also recommend visiting this site, which provides more examples and information on OCL.

Sources

  • Object Constraint Language. https://en.wikipedia.org/wiki/Object_Constraint_Language
  • Object Constraint Language. Version 2.4. Document Number: formal/2014-02-03. https://www.omg.org/spec/OCL/2.4/PDF
  • The Ultimate Object Constraint Language (OCL) tutorial. https://modeling-languages.com/ocl-tutorial/

By Owner

3 thought on “OCL: Ultimate Object Constraint Language using OCLE guide, cheatsheet”

Leave a Reply

Your email address will not be published. Required fields are marked *