Object Constraint Language

from Wikipedia, the free encyclopedia

The Object Constraint Language (OCL) is a language that software architects can use to formally define the necessary boundary conditions when modeling computer programs . The OCL is part of the globally established modeling language Unified Modeling Language (UML) and is used, among other things, for the textual specification of invariants in class diagrams , of conditions in sequence diagrams or the formulation of pre- and post-conditions for methods. The OCL syntax is based on the Smalltalk programming language . OCL has been part of UML since UML version 1.1.

An essential area of ​​application of the OCL is also in the model transformation. Here OCL is a central component of many transformation languages, such as QVT or ATL .

The current version of the OCL is 2.4.

Constraints in the OCL

There are seven types of constraints (assurances):

  • Invariants must apply to an instance or association at all times.
  • Preconditions / postconditions must apply at the time when the execution of the associated operation begins / ends.
  • Initial & derived values represent conditions for initial and derived values.
  • Definition: attributes and operations that are not contained in the model can be defined.
  • Body definition of operations with isQuery = true.
  • Guards must apply when a state transition begins.

A constraint is always defined in the context of a context . This is any model entity, such as a class, a type, an interface or a component. A distinction is made between the context type and the context instance. The specifications of a constraint relate to the latter. For example, he can specify that the value of the Curvature attribute must not be greater than X for an instance of the Banana class.

Object here stands for a component of any system, this should be specified, defined or described more precisely.

Constraint stands for a limitation or restriction; this can assume maximum or minimum values, for example the maximum number of simultaneous accesses to a database, or the maximum height of a building object.

Language here does not mean a formal computer language, but rather a less formal language that can be used for every implementation.

OCL and UML

The OCL is designed as a supplement to UML and should make the modeling of software even more precise. While structures, processes and relationships between objects are modeled in UML, the boundary conditions of a model are also specified in OCL. This can be, for example, the restriction of an attribute to a range of values, or restrictions to be observed between objects. OCL expressions are free of contradictions and can be processed by programs. They thus contribute to the generation of the code, although they do not change the model, but monitor it.

UML is better suited to developing a model because diagrams are easier to understand than a textual representation of the model. OCL alone is unsuitable for modeling software.

example

The following UML class diagram illustrates the OCL and allows various conditions to be formulated in OCL: Class diagram

conditions

  1. A person's age is not negative.
  2. A person is younger than their parents.
  3. After a birthday, a person is exactly one year older.
  4. A person has a maximum of two parents.
  5. When someone has a child, the set of their children is not empty and the number of their children is greater than before.
  6. Only one adult can own a car.
  7. The first registration of a car is not before the year of manufacture.
  8. Every person who owns at least one car has at least one car that is younger than themselves.
  9. Nobody can be a parent of themselves.
  10. There is at least one person who owns a car. (Or of all people there is at least one person who owns at least one car.)

OCL constraints

  1. context Person inv: self.alter >=0
  2. context Person inv: self.eltern->forAll(e|e.alter>self.alter)
  3. context Person::hatGeburtstag() post: self.alter=self.alter@pre+1
  4. context Person inv: self.eltern->size()<=2
  5. context Person::bekommtKind() post: self.kinder->notEmpty() and self.kinder->size() > self.kinder@pre->size()
  6. context Person inv: self.alter<18 implies self.autos->isEmpty()
  7. context Auto inv: self.erstzulassung>=self.baujahr
  8. context Person inv: self.autos->notEmpty() implies self.autos->exists( a | Calendar.YEAR - a.baujahr < self.alter)
  9. context Person inv: self.eltern->excludes(self)
  10. context Person inv: Person.allInstances()->exists(p | p.autos->size() > 0)

literature

  • Jos Warmer, Anneke Kleppe: Object Constraint Language 2.0 . First edition. mitp-Verlag, Bonn 2004, ISBN 3-8266-1445-3 .

Web links

Individual evidence

  1. [1] OMG specification