Skip to content.

The AMMA home page

Personal tools

Interactive OCL Tutorial

Introduction

This in an interactive tutorial on OCL, the Object Constraint Language. You may test your own OCL expressions with the Simple OCL Web Tester .

This tutorial is based on UML 2.0 OCL Specification (in particular see chapter 7)

Please note that there may be a displaying problem with Microsoft explorer when executing the interactive OCL Web Tester since there is a problem representing apostrophes.

OCL primitive types

OCL has four basic primitive datatypes

  • Boolean (true, false)
  • Integer (1, -5, 2, 34, 26524, ...)
  • Real (with the values 1.5, 3.14, ...)
  • String (`To be or not to be...`)

Furthermore, OCL has the following comparators: , =, =>, =

Operations on primitive types

  • Integer has the operations: *, +, -, /, div(), abs(), mod(), max(), min(), sum(), sin(), cos()
  • Real has the operations: *, +, -, /, floor(), sum(), sin(), cos()
  • Boolean has the operations: and, or, xor, not, implies, if-then-else
  • String has the operations: concat(), size(), substring(), toInteger() and toReal()

Differences between OCL and ATL

  • In ATL you can use the operator + for the concatenation of Strings
  • Early versions of ATL use div instead of / or div() for division

Examples of operation on primitive datatypes

Let expressions

OCL collections

Collection is the abstract superclass of Set, OrderedSet, Bag and Sequence.

  • Set is a collection without duplicates. Set has no order.
  • OrderedSet is a collection without duplicates. OrderedSet has an order.
  • Bag is a collection in which duplicates are allowed. Bag has no order.
  • Sequence is a collection in which duplicates are allowed. Sequence has an order.

Collection operations

  • The number of elements in the collection self: size()
  • The information of whether an object is part of a collection: includes()
  • The information of whether an object isn't part of a collection: excludes()
  • The number of times that object occurs in the collection self. count()
  • The information of whether all objects of a given collection are part of a specific collection: includesAll()
  • The information of whether none of the objects of a given collection are part of a specific collection: excludesAll()
  • The information if a collection is empty: isEmpty()
  • The information if a collection is not empty: notEmpty()

Iterators over collections

  • The selection of a sub-collection: select()
  • When specifying a collection which is derived from some other collection, but which contains different objects from the original collection (i.e., it is not a sub-collection) use: collect()
  • The information of whether an expression is true for all objects of a given collection: forAll()
  • The addition of all elements of a collection: sum() Elements must be of a type supporting the + operation.

OCL collection operation examples

OCL conditional expression: "if"

You can formulate an if-clause in OCL: if-then-else-endif. Else cannot be dropped.

OCL classes

In ATL the notation "Metamodel!Class" is used to be able to differentiate classes of different metamodels. This means in ATL it is possible to use several metamodels in the same file. In OCL this is not the case. There are different operations to treat and analyze classes.

  • The operation oclIsTypeOf() checks if a given instance is an instance of a certain type (and not of one of its subtypes or of other types).
  • The operation oclIsKindOf() checks if a given instance is an instance of a certain type or of one of its subtypes.
  • The operation allInstances() returns you all instances of a given Type.
  • The operation oclIsUndefined() tests if the value of an expression is undefined (e.g. if an attribute with the multiciplicity zero to one is void or not. Please note: attributes with the multiplicity n are often represented with collections, which may be empty and not void).

Examples on OCL class operations

OCL queries on MOF 1.4

The following examples are based on the metamodel MOF 1.4 which you may use to test OCL expressions with. You may also have a look at the MOF XMI file or at the Java JMI implementation (JMI overview with frames) to learn about MOF. Start with MofClass.

OCL comments

OCL comments start with two consecutive hyphens "--" and end at the end of the line.

Last modified 2009-07-15 08:01 PM
 

Plone powered, XHTML valid and CSS valid