Object Constraint Language
- Tags
- modelling
Is a sort of first-order logic for UML diagrams that simplifies defining [see page 6, constraints].
The general idea is to qualify path expressions in UML diagrams to constrain sets of objects reachable via associations.
Type | Ordered | Can contain duplicates? |
---|---|---|
Set | Unordered | No, unique |
Bag | Unordered | Yes |
OrderedSet | Ordered | No, unique |
Sequence | Ordered | Yes |
Path Expressions
Is a way of navigating from an object to a related object or collection of related objects. You may implicitly treat a single object as a singleton set (set containing only that object).
\begin{figure}
\centering
\begin{tikzpicture}
\umlclass{Tutor}{}{name: String \\ office: Integer \\ telNo: Integer}
\umlclass[x=6]{Tutee}{}{name: String \\ regNo: Integer}
\umlassoc[attr1=tutor|1, attr2=tutees|*]{Tutor}{Tutee}
\end{tikzpicture}
\label{fig:eg-ocl-objs}
\end{figure}
Paths always start from a given object self
that's commonly referred to as the
context.
Paths follow attribute names to attribute values and associations end-role names to
related objects. The .
operator accesses an attribute or follows an association, to
access a set/collection directly we use the ->
operator instead.
Path Expression | Type | Description |
---|---|---|
self.name | String | The name of self |
self.office | Integer | the office of self |
self.tutees | Set(Tutee) | The tutees of self |
self.tutees.name | Set(String) | The names of the tutees of self |
self.tutess->size() | Integer | The number of tutees self has |
self.tutees.name->size() | Integer | The size of the bag of names |
Path expressions also support some basic arithmetic operations based on the types.
These can be seen in tbl:path-ops. Note some can be called in infix meaning
int1.+(int2)
can also be written as int1 + int2
.
Basic Type | Built-in Operations |
---|---|
Integer | + , - , * , / , abs() , = , <> , max() , min() |
Real | + , - , * , / , floor() , = , <> , max() , min() |
Boolean | and , or , xor , not , implies , if-then-else (ternary) |
String | concat() , size() , substring() |
Collection | size() , isEmpty() , notEmpty() , count() , sum() , includes() , excludes() , select() , reject() , collect() , forAll() , exists() |
Set, Bag, OrderedSet | union() , intersection() , - , including() , excluding() , symmetricDifference() , count() , flatten()~ , asBag() , asSet() , asSequence() |
Sequence | first() , last() , append() , prepend() |
Describing Conditions
Constraints can be [see page 10, expressed] as invariants (always true) or pre/post-conditions. These can be attached both to class attributes or method invocations. Warn: Constraints can only describe the state of a system before or after an action, they can't express the action itself.
Keywrod | Description |
---|---|
context | The context of the declaration, either a class or a method |
inv: | An invariant must always be true for this class |
pre: | Must hold in order for the service to be invoked |
post: | Describes how the outcome relates to the inputs |
and , or , not , implies | Used to combine one or more logical expressions |
self | Refers to the current object in a context |
result | Refer to the result of a method in a context |
@pre (only in post-conditions) | Added onto any expression to refer to its value before the operation executed |
Metalanguage
The [see page 20, metalanguage] of OCL is used to refer to classes instead of objects.
OCL defines two distinguished class names:
OclAny
: The root class, I.E. the super-class of all user classes.OclType
: The meta-class, I.E. the class of all user classes.
The oclType()
method of an object retrieves its class, the oclIsTypeOf(type: OclType)
method assets that an object is an instance of a type, and oclIsKindOf(type: OclType)
asserts that an object is an instance of any subclass of type
.