Brain Dump

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.

Table 1: Listing of the available collection see page 15, [types] in OCL.
TypeOrderedCan contain duplicates?
SetUnorderedNo, unique
BagUnorderedYes
OrderedSetOrderedNo, unique
SequenceOrderedYes

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.

Table 2: Basic path expressions using the object model in fig:eg-ocl-objs and with context self:Tutor. tbl:basic-path
Path ExpressionTypeDescription
self.nameStringThe name of self
self.officeIntegerthe office of self
self.tuteesSet(Tutee)The tutees of self
self.tutees.nameSet(String)The names of the tutees of self
self.tutess->size()IntegerThe number of tutees self has
self.tutees.name->size()IntegerThe 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.

Table 3: Basic arithmetic operations with paths. tbl:path-ops
Basic TypeBuilt-in Operations
Integer+, -, *, /, abs(), =, <>, max(), min()
Real+, -, *, /, floor(), =, <>, max(), min()
Booleanand, or, xor, not, implies, if-then-else (ternary)
Stringconcat(), size(), substring()
Collectionsize(), isEmpty(), notEmpty(), count(), sum(), includes(), excludes(), select(), reject(), collect(), forAll(), exists()
Set, Bag, OrderedSetunion(), intersection(), -, including(), excluding(), symmetricDifference(), count(), flatten()~, asBag(), asSet(), asSequence()
Sequencefirst(), 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.

Table 4: OCL see page 13, [keywords] and syntactical constructs.
KeywrodDescription
contextThe 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, impliesUsed to combine one or more logical expressions
selfRefers to the current object in a context
resultRefer 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.