OCL
OCL
OCL
CONSTRAINT LANGUAGE IN
UML
OCL website:
http://www.omg.org/uml/
ENOUGH!
• We need a language to help with the spec.
• We look for some “add-on” instead of a brand new language
with full specification capability.
• Why not first order logic? – Not OO.
• OCL is used to specify constraints on OO systems.
• OCL is not the only one.
• But OCL is the only one that is standardized.
OCL – fills the missing
gap:5
Better documentation
Constraints add information about the model elements and their
relationships to the visual models used in UML
It is way of documenting the model
More precision
OCL constraints have formal semantics, hence, can be used to
reduce the ambiguity in the UML models
SYSTEM
THE CONSTRAINTS:
EXAMPLES
Player
• Allcontext
players
Player must be over 18.
invariant: self. age >=18 age: Integer
• The number
Roomof guests roomin each
guest room
Guest
*
doesn’t exceed the
numberOfBeds: Integer number of beds
in the room.
context Room
invariant: guests -> size <= numberOfBeds
CONSTRAINTS (INVARIANTS), CONTEXTS
AND11 SELF
• A constraint (invariant) is a boolean OCL expression
– evaluates to true/false.
• Every constraint is bound to a specific type (class,
association class, interface) in the UML model – its
context.
• The context objects may be denoted within the
expression using the keyword ‘self’.
• The context can be specified by:
• Context <context name>
• A dashed note line connecting to the context figure in the
UML models
• A constraint might have a name following the
keyword invariant.
EXAMPLE OF A STATIC
12
Problem story:
UML MODEL
A company handles loyalty programs (class LoyaltyProgram) for
companies (class ProgramPartner) that offer their customers various
kinds of bonuses. Often, the extras take the form of bonus points or air
miles, but other bonuses are possible. Anything a company is willing
to offer can be a service (class Service) rendered in a loyalty program.
Every customer can enter the loyalty program by obtaining a
membership card (class CustomerCard). The objects of class Customer
represent the persons who have entered the program. A membership
card is issued to one person, but can be used for an entire family or
business. Loyalty programs can allow customers to save bonus points
(class loyaltyAccount) , with which they can “buy” services from
program partners. A loyalty account is issued per customer
membership in a loyalty program (association class Membership).
Transactions (class Transaction) on loyalty accounts involve various
services provided by the program partners and are performed per
single card. There are two kinds of transactions: Earning and burning.
Membership durations determine various levels of services (class
serviceLevel).
LoyaltyProgram Customer
1..*
0..* 0..* name: String
1..*
13 partners enroll(c:Customer) program title:String
ProgramPartner isMale: Boolean
1 dateOfBirth: Date
numberOfCustomers: Integer 0..* Membership
age(): Integer
partner 1 {ordered} 1..* 1 actualLeve 1 1 owner
0..1
ServiceLevel l 0..* card
LoyaltyAccount card
name: String CustomerCard
deliveredServices 0..* points: Integer 1
1 level valid: Boolean
Service earn(i: Integer)
0..* validForm: Date
burn(i: Integer)
condition: Boolean goodThru: Date
availableServices isEmpty(): Boolean
pointsEarned: Integer color: enum{silver,
pointsBurned: Integer
1 account gold}
transactions 0..*
description: String printedName: String
Transaction
generatedBy 1 card
0..* points: Integer 1
date:Date 0..*
transactions
Date program(): LoyaltyProgram transactions
$now: Date
isBefore(t:Date): Boolean
isAfter(t:Date): Boolean
=(t:Date): Boolean Burning Earning
USING OCL IN CLASS
14
DIAGRAMS
LoyaltyAccount class invariant
points: Integer { points >= 0 }
earn(i: Integer)
<<postcondition>>
points = points@pre + i
<<postcondition>>
points = points@pre - i
<<postcondition>>
result = (points=0) postcondition for burn operation
Invariants on Attributes
15
Invariants on attributes:
context Customer
invariant agerestriction: age >= 18
context CustomerCard
invariant correctDates: validFrom.isBefore(goodThru)
If the role name is missing – use the class name at the other
end of the association, starting with a lowercase letter.
Preferred: Always give role names.
Navigation over
Association Ends – Roles
17
printedName a String.
owner a Customer instance.
owner.title a String.
owner.name a String.
String is a recognized OCL type.
concat is a String operation, with the
signature concat(String): String.
Invariants using
18 Navigation from
Association Classes
Navigation from an association class can use the classes at the
association class end, or the role names. The context object is
the association class instance – a tuple.
“Many” Multiplicity
Navigation over associations roles with multiplicity greater than
1 yields a Collection type. Operations on collections are
accessed using an arrow ->, followed by the operation name.
COLLECTIONS
Customer 0..* Account 0..* Transaction
context Customer
account produces a set of Accounts
context Customer
account.transaction produces a bag of transactions
If we want to use this as a set we have to do the following
account.transaction -> asSet
Navigation to Collections
22
“When a loyalty program does not offer the possibility to earn or burn
points, the members of the loyalty program do not have loyalty accounts.
That is, the loyalty accounts associated with the Memberships must be
empty”:
context LoyaltyProgram
invariant noAccounts:
partners.deliveredservices->
forAll(pointsEarned = 0 and pointsBurned = 0)
implies Membership.account->isEmpty()
<collection> size
isEmpty
notEmpty
sum ( )
count ( object )
includes ( object )
includesAll ( collection )
To distinguish between attributes in classes from collections, OCL uses the dot
notation for accessing attributes and the -> operator for accessing collections.
26 COLLECTION OPERATIONS
COLLECTIONS CONT.
27
Customer StoreCard
name:String 1..* printName:String
title: String owner cards points: Integer
golduser: Boolean
earn(p:Integer)
age( ):Integer
context StoreCard
invariant: printName = owner.title.concat(owner.name)
submits Assessment
1..* weight: Integer
Exam Coursework
hours: Integer date: String
CONSTRAINTS
31
context Module
invariant: taken_bysize > 7
CONSTRAINT (B)
33
context Module
invariant:
set_work.weightsum( ) = 100
CONSTRAINT (C)
34
context Student
invariant: takes.creditsum( ) = 120
CONSTRAINT (D)
35
context Student
invariant:
takes
select(code.substring(1,2) = ‘CS’).creditsum( )
>= 90
CONSTRAINT (E)
36
context Module
invariant: set_workexists(weight > 50)
CONSTRAINT (F)
37
context Student
invariant:
takesincludesAll(submits.for_module)
INVARIANTS USING NAVIGATION
THROUGH CYCLIC ASSOCIATION CLASSES
38
context Person
invariant:
employeeRanking[employees]->exists(score = 10)
INVARIANTS USING NAVIGATION
THROUGH QUALIFIED ASSOCIATION
39
0..1
• Example
ServiceLevel
name: String
context LoyaltyProgram
serviceLevel[1].name = ‘basic’
context LoyaltyProgram
serviceLevel->exists(name = ‘basic’)
CLASSES AND SUBCLASSES
40
context LoyaltyProgram
invariant:
partners.deliveredServices.transaction.points->sum() < 10,000
Dependency is both
a ModelElement and an Association class.
context Dependency
invariant: self.oclAsType(Dependency).source <> self
invariant:
self.oclAsType(ModelElement).source -> isEmpty()
OCL CONSTRAINTS
42
• A constraint is a restriction on one or more values of (part of)
an object model/system.
• Constraints come in different forms:
• invariant
• constraint on a class or type that must always hold
• pre-condition
• constraint that must hold before the execution of an op.
• post-condition
• constraint that must hold after the execution of an op.
• guard
• constraint on the transition from one state to another.