Object Constraint Language (OCL) is a formal language used to describe expressions on UML models. These expressions typically specify invariant conditions that must hold for the system being modeled or queries over objects described in a model.

A UML diagram, such as a class diagram, is typically not refined enough to provide all the relevant aspects of a specification. There is, among other things, a need to describe additional constraints about the objects in the model. Such constraints are often described in natural language. Practice has shown that this will always result in ambiguities. In order to write unambiguous constraints, so-called formal languages have been developed. The disadvantage of traditional formal languages is that they are usable to persons with a strong mathematical background, but difficult for the average business or system modeler to use. OCL has been developed to fill this gap. It is a formal language that remains easy to read and write. It has been developed as a business modeling language.

Gary Larson’s cartoon can be used to illustrate the misunderstandings thay may occur when using informal modeling languages: there may be a huge difference between what we say and what others understand.[1]

picture alt

OCL is a pure specification language; therefore, an OCL expression is guaranteed to be without side effects. This means that the state of the system will never change because of the evaluation of an OCL expression, even though an OCL expression can be used to specify a state change.

OCL is not a programming language; therefore, it is not possible to write program logic or flow control in OCL. You cannot invoke processes or activate non-query operations within OCL. Because OCL is a modeling language in the first place, OCL expressions are not by definition directly executable.

OCL is a typed language so that each OCL expression has a type. To be well formed, an OCL expression must conform to the type conformance rules of the language. For example, you cannot compare an Integer with a String.

As a specification language, all implementation issues are out of scope and cannot be expressed in OCL.

OCL can be used for a number of different purposes:

  • as a query language,
  • to specify invariants on classes and types in the class model,
  • to describe pre- and post conditions on Operations and Methods,
  • to specify target (sets) for messages and actions,
  • to specify constraints on operations, and
  • to specify derivation rules for attributes for any expression over a UML model.

This article is issued from OCL 2.0 Specification Document. [2]

For more detailed information you can “Object constraint language (OCL): a definitive guide” [3] article and “OCL Tutorial” [4] slides as reference.

References:

[1] Carbot, Jordi. “Why You Need to Learn OCL.” Modeling Languages, 14 May 2013, modeling-languages.com/why-you-need-to-learn-ocl/.

[2] OMG, OCL. “2.0 Specification.” Object Management Group, Final Adopted Specification (2005).

[3] Cabot, Jordi, and Martin Gogolla. “Object constraint language (OCL): a definitive guide.” Formal methods for model-driven engineering. Springer Berlin Heidelberg, 2012. 58-90. Full-text

[4] Cabot, Jordi. “OCL Tutorial.” LinkedIn SlideShare, 28 June 2012, www.slideshare.net/jcabot/ocl-tutorial.