Do you wonder how to add expressions and constraints to your UML and OO models? Well, Object Constraint Language is the answer!

The Object Constraint Language (OCL) is a language that enables you to describe expressions and constraints on object-oriented models and not only. An expression is an indication or specification of a value. A constraint is a restriction on one or more values of an object-oriented model or system.

For example, expressions usage in your UML model might be for:

- the specification of an initial value of an attribute
- the specification of th derivation rule for an attribute
- the specification of the body of an operation.

On the other hand constraints are distinguished in four types:

- invariant is a constraint that states a condition that must always be met by all instances of the class, type, or interface. An invariant is described using an expression that evaluates to true if the invariant is met. Invariants must be true all the time. For example,
*for all instances of object woman, sex is female* - A precondition to an operation is a restriction that must be true at the moment that the operation is going to be executed. e.g.
*Person.age > 18 to vote()* - A postcondition to an operation is a restriction that must be true at the moment that the operation has just ended its execution. e.g.
*After person.marriage() the person.status=3* - A guard is a constraint that must be true before a state transition event. e.g.
*[Person.age > 18 ] then Person.vote()*

**An example**

The above Class diagram can be encanced with some OCL expressions. For example:

__Circle__

self.radius > 0

which contraints all the objects of Circle on their radius attribute to be always a positive value.

Another example is the following:

__Polygon__

self.vertices->forAll(p1, p2 |

(p1.x = p2.x and p1.y = p2.y) implies p1 = p2)

A constraint of the Polygon class where two vertex points of a polygon must be in different positions. It means that for all instances of polygons if any two vertex points have same coordinates it is actually the same instance.

**The power of OCL**

The real power of OCL usage to your applications, comes when you use it with a CASE tool, while you design your UML diagrams. When the model is not read by humans, but instead is used as input to an automated system, the use of OCL becomes even more important. Tools can be used for generating simulations and tests, for checking consistency, for generating derived models in other languages using MDA transformations, for generating code, and so on. This is something we did in CORFU.

**Probe further:**

The authors of the famous book *The Object Constraint Language: Precise Modelling with UML*. http://www.klasse.nl/ocl/

Check http://www.omg.org for the latest specification of OCL

See how to use it with Eclipse

and of course wikipedia

## Recent comments

11 years 7 weeks ago

11 years 9 weeks ago

11 years 10 weeks ago

11 years 12 weeks ago

11 years 12 weeks ago

11 years 13 weeks ago

11 years 13 weeks ago

11 years 13 weeks ago

11 years 14 weeks ago

11 years 14 weeks ago