A formal specification language used with MOF meta-models (including UML) to express queries or constraints that cannot otherwise be expressed in diagramic notation.
The object constraint language (OCL) can be used to define invariants, queries, pre- and postconditions for MOF meta-models (including UML models). It allows a user to specify constraints on models, which can not be specified by the graphical notation of the UML.
OCL itself is a side-effect free, typed language based on first-order logic.
Invariants are defined for a context, typically a class. The following invariant states, that all person names should be unique:
context Person inv namesUnique:
Person.allInstances->forAll(p|self.name=p2.name implies self=p2)
Note, that OCL provides the built in operation allInstances()
to access all instances of a class.
Pre- and postconditions are specified in the context of an operation. In a post condition the special operator @pre
can be used to access the values of variables, ettributes, etc. before an operation was called. Also, the special variable result
can be used to access the result of the operation call.
The following conditions could be specified for an operation pop():OclAny
of a Stack:
pop():OclAny
pre self.size > 0
post self.size = self.size@pre - 1
post result = self.elements@pre->last()
Beside constraint definition, OCL is used as a query language in model transformation approaches like QVT.
Further information about OCL can be found on the websites of the OCL workshop or the OCL Portal.