Académique Documents
Professionnel Documents
Culture Documents
(OCL)
• The Object Constraint Language (OCL) is a declarative
language for describing rules that apply to UML.
• Initially, OCL was only a formal specification language
extension to UML.
• OCL may now be used with any Meta Object Facility
(MOF),Object Management Group (OMG) meta model
, including UML.
• The Object Constraint Language is a text language that
provides constraint and object query expressions on
any MOF model or meta-model.
• OCL is a key component of the new OMG standard
recommendation for transforming models, the
Queries/Views/Transformations (QVT) specification.
History of OCL
• Developed at IBM in 1995 originally as a business
• engineering language
• • Adopted as a formal specification language
within UML
• • Part of the official OMG standard for UML (from
• version 1.1 on)
• • Current version is OCL 2.0
• Constraint
Definition
– constraint is a restriction on one or more
values of (part of) an object-oriented model or
system.
– A constraint is formulated on the level of
classes, but its semantics is applied on the
level of objects.
– Originally formulated in the syntactic context
of a UML.
Types of expressions in OCL
• Expressions can be used in a number of places in a
UML model:
– To specify the initial value of an attribute or association
end.
– To specify the derivation rule for an attribute or
association end.
– To specify the body of an operation.
– To indicate an instance in a dynamic diagram.
– To indicate a condition in a dynamic diagram.
– To indicate actual parameter values in a dynamic diagram.
Types of constraints in OCL
• There are four types of constraints:
– An invariant
– A precondition
– A postcondition
– A guard is a constraint that must be true before a state
transition fires.
The Context of an OCL Expression
LoyaltyAccount
init: 0
points: Integer
number: Integer
earn(i: Integer)
pre: i > 0 burn(i: Integer)
isEmpty(): Boolean
1 Account
body: points = 0
Transactions 0..*
init: Set { }
Transaction
points: Integer
date: Date
program() :
LoyaltyProgram
Invariants on attributes
passengers 0..*
Person
name: String
passengers 0..*
Person
name: String
In this example, the correct way to specify the multiplicity is to add to the
diagram the following OCL constraint:
context Flight
inv: passengers->size() <= plane.numberOfSeats
Person
House 0..* 1 IDCardNo: Integer
value: Money
houses owner salary: Money
context Mortgage
inv: startDate < endDate
context Person
inv: Person::allInstances()->isUnique(socSecNr)
Student Teacher