Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Software Engineering (OCL)Object Constraint Language
Lina YE
https://www.lri.fr/~linaye/GL.html
[email protected] 3, 2017-2018
1/45
Software Engineering (OCL) Forward
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Plan1 Introduction2 Constraints
Context and SelfInvariantPre- and Post-conditionConstraints on Attributes
3 LanguageAccess to characteristicsTypesVariableCollectionsOCL function
4 Example
2/452/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
MotivationWhy OCL
Cannot represent all the relevant aspects of aspecification (e.g., class diagram)
Need to describe additional constraints withoutambiguities
Formal languages requires a strong mathematicalbackground
What is OCLFormal language to express constraints, that remainseasy to read and write
Developed by IBM and standardized by OMG
Integrated into the UML standard
3/453/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Objet Constraint Language
Where to useSpecify invariants on classes and types in the class model
Describe pre- and post-conditions on operations andmethods
Describe guards
As a navigation language
etc.
ExampleHow to represent the constraint that the age of anemployee cannot be smaller than 18?
4/454/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Objet Constraint Language
Pure specification language: do not have side effetevaluation of an OCL expression returns a valuethis evaluation do not alter the system state and isinstantaneous
Not a programming languagecannot write program logic in OCLcannot invoke processes or activate non-queryoperations
Typed language: each expression has a typeexpression must obey the type conformance rules ofOCLeach classifier defined in a UML model represents adistinct OCL typeincludes a set of supplementary predefined types
5/455/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Objet Constraint Language
Pure specification language: do not have side effetevaluation of an OCL expression returns a valuethis evaluation do not alter the system state and isinstantaneous
Not a programming languagecannot write program logic in OCLcannot invoke processes or activate non-queryoperations
Typed language: each expression has a typeexpression must obey the type conformance rules ofOCLeach classifier defined in a UML model represents adistinct OCL typeincludes a set of supplementary predefined types
5/455/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Objet Constraint Language
Pure specification language: do not have side effetevaluation of an OCL expression returns a valuethis evaluation do not alter the system state and isinstantaneous
Not a programming languagecannot write program logic in OCLcannot invoke processes or activate non-queryoperations
Typed language: each expression has a typeexpression must obey the type conformance rules ofOCLeach classifier defined in a UML model represents adistinct OCL typeincludes a set of supplementary predefined types
5/455/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Context
Each constraint must be associated to one model element
Such an element constitutes the context of the constraint
Syntax: keyword context
exampleclass: nameClasscontext Person
operation: nameClass::nameOperation(param1:Type1,...):TypeReturnedcontext Account::getSolde(): Real
attribute: nameClass::nameAtt: TypeAttcontext Person::age : Integer
6/456/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Self
In an OCL expression, reserved word self is used torefer to the contextual instance
If the context is Person, then self refers to an instance ofPerson
This keyword can be omitted when the context is clear
examplecontext Personself.name
context Personname
7/457/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
InvariantDetermine a constraint that should be always true for allinstances of a typeSyntaxinv: <logic expression>
exampleValue of attribute nbEmployees in instances of Companymust be less than or equal to 50context Companyinv: self.nbEmployees≤50
The stock price of each company is greater than 0(stockPrice() is a operation defined in the classCompany)context Companyinv: self.stockPrice()>0
8/458/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Pre-condition
Constraints associated with an operation or otherbehavioral feature
Constraint assumed to be true before the execution ofthe operation
Syntaxpre: <logic expression>
exampleThe age of a person who has an income must be olderthan or equal to 18 (income() is an operation defined inthe class Person)context Person:: income(): Integerpre: self.age≥18
9/459/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Post-conditionConstraints associated with an operation or otherbehavioral featureConstraint satisfied after the execution of the operationKeyword result denotes the value returned by theoperation, whose type is the returned typeKeyword @pre denotes the attribute value before theoperationSyntaxpost: <logic expression>
exampleThe age of a person who has an income cannot besmaller than 18, and the income must be less than 5000context Person:: income(): Integerpre: self.age≥18post: result<5000
10/4510/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Init
Reserved word init is used to represent the initial value
Possibility to precise the initial value of an attribute or anassociation end when the object is created
Syntaxinit: expression
exampleAttribute isMarried in Person is initialized to falsecontext Person:: isMarried: Booleaninit: false
11/4511/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Derive
Reserved word derive is used to represent the derivedvalue
Precise how to obtain the derived value of an attributebased on the value of other attributes, such a constraintshould always be respected
Syntaxderive: expression
exampleThe age of one person is obtained by subtracting theirbirth date from the current datecontext Person:: age: Integerderive: currentDate-dateOfBirth
12/4512/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Access to characteristics of anobject
The access of attributes and operations of an object isspecified by a dot followed by their name
Syntaxself.nameAttributeself.nameOperation(arg1, ..., argn )
examplecontext Personself.ageself.income()
13/4513/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Navigation
From an object, an association is navigated by a dotfollowed by the opposite role name
Value of expression depends on maximal multiplicity ofthe association end1: value is an object (”.“) or can also be used as a setcontaining a single object (”→“)∗: value is a set of objects (”→“)
For optional associations, it is useful to check whetherthere is an object or not when navigating the association
14/4514/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Navigation: example
Example
context Companyinv: self.manager.isUnemployed=falseinv: self.employee→notEmpty()
self.manager is an object of type Personcontext Companyinv: self.manager.age> 40
self.manager as a setcontext Companyinv: self.manager→ size()=1
15/4515/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Navigation: example
Example
context Companyinv: self.manager.isUnemployed=falseinv: self.employee→notEmpty()self.manager is an object of type Personcontext Companyinv: self.manager.age> 40
self.manager as a setcontext Companyinv: self.manager→ size()=1
15/4515/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Enumeration: example
Example
context Personinv: self.wife→notEmpty() impliesself.gender=Gender::Male andself.husband→ notEmpty() impliesself.gender=Gender::Female
16/4516/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
List of TypesPredefined types with their operators
Types OperatorsBoolean and; or; xor; not; implies; if-then-else-endif;...Integer ∗; +;−; /; abs(); ...Real ∗; +;−; /; abs(); floor (); ...String concat(s: String); size(); substring(lower: Integer, upper: Integer);...
ImpliesP1 P2 P1 implies P2True True TrueTrue False FalseFalse True TrueFalse False True
17/4517/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operations on Types
One person who is married must be more than 18 yearsold
One person is either male or female but cannot be both
context Personinv: self.isMarried implies self.age>18
context Personinv: self.gender=Gender::male xorself.gender=Gender::female
18/4518/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operations on Types
One person who is married must be more than 18 yearsold
One person is either male or female but cannot be both
context Personinv: self.isMarried implies self.age>18
context Personinv: self.gender=Gender::male xorself.gender=Gender::female
18/4518/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operations on Types
One person who is married must be more than 18 yearsold
One person is either male or female but cannot be both
context Personinv: self.isMarried implies self.age>18
context Personinv: self.gender=Gender::male xorself.gender=Gender::female
18/4518/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Create a Variable
If the same expression is used more than one time
For the better readability of the constraint
SyntaxWith keywords let...inlet < variable >: TypeVar=< request > in< expression >
With keyword defdef: < variable >: TypeVar=< request >< expression >
19/4519/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Create a Variableexample
An unemployed person has no job. Otherwise, he has atleast one job.
The name of one person is the concatenation of firstname and last name
context Personinv: let numberJobs: Integer=self.job→size() inif isUnemployed then numberJobs=0else numberJobs > 0endif
context Persondef: name: String=self.firstName.concat(’ ’).concat(lastName)
20/4520/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Create a Variableexample
An unemployed person has no job. Otherwise, he has atleast one job.
The name of one person is the concatenation of firstname and last name
context Personinv: let numberJobs: Integer=self.job→size() inif isUnemployed then numberJobs=0else numberJobs > 0endif
context Persondef: name: String=self.firstName.concat(’ ’).concat(lastName)
20/4520/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Collection
Conversion of an association to a type within OCL
21/4521/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on CollectionSyntax
For a collection,→ is used to apply some operation on it:nameCollection→ operation()Recall: the dot is used for the access of a property of anobject
Some examplessize(): Integer (return the number of elements in thecollection)
includes(object: T): Boolean (return true if object isincluded in the collection)
excludes(object: T): Boolean (return true if object is notin the collection)
22/4522/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Collection
Some examplescount(object: T): Integer (return the number of object)
isEmpty(): Boolean (return true if the collection is empty)
notEmpty(): Boolean (return true if the collection is notempty)
includesAll(c: Collection(T)): Boolean (return true if thecollection contains all elements of c)
excludesAll(c: Collection(T)): Boolean (return true if thecollection does not contains any element of c)
sum(): T (return the sum of all elements in the collection)
union(set: Set(T)): Set(T) (return the union of self withset)
23/4523/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Collection
Some examplesany(exp: OclExpression): Type (return any element inself validating exp)
=(set: Set(T)): Boolean (return true if self and setcontain exactly the same elements)
including(object: T): Set(T) (return a collection thatcontains all elements of self plus object)
24/4524/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Collection
One company has at least one employee
The manager of a company is also an employee
context Companyinv: self.employee→ size() > 0inv: self.employee→ notEmpty()
context Companyinv: self.employee→ includes(self.manager)
25/4525/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Collection
One company has at least one employee
The manager of a company is also an employee
context Companyinv: self.employee→ size() > 0inv: self.employee→ notEmpty()context Companyinv: self.employee→ includes(self.manager)
25/4525/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elements
Syntaxcollection→ operation (expression)collection→ operation (v | expression-with-v)collection→ operation (v: Type | expression-with-v)
The expression is applied on each element of the collection
26/4526/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elementsselect: generate a sub-collection that contains only theelements satisfying expression
reject: generate a sub-collection that contains only theelements that does not satisfy expression
Each company must have at least one employee that is morethan 50 years old
context Company:inv: self.employee→ select(p: Person | p.age> 50)→notEmpty()
context Company:inv: self.employee→ reject(p: Person | p.age<= 50)→ notEmpty()
27/4527/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elementsselect: generate a sub-collection that contains only theelements satisfying expression
reject: generate a sub-collection that contains only theelements that does not satisfy expression
Each company must have at least one employee that is morethan 50 years old
context Company:inv: self.employee→ select(p: Person | p.age> 50)→notEmpty()
context Company:inv: self.employee→ reject(p: Person | p.age<= 50)→ notEmpty()
27/4527/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elements
forAll: return true if the expression is true for eachelement
exists: return true if the expression is true for at leastone element
Each company must have at least one employee that is morethan 50 years old
context Company:inv: self.employee→ not (forAll(p: Person |p.age<= 50))
context Company:inv: self.employee→ exists(p: Person | p.age> 50)
28/4528/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elements
forAll: return true if the expression is true for eachelement
exists: return true if the expression is true for at leastone element
Each company must have at least one employee that is morethan 50 years old
context Company:inv: self.employee→ not (forAll(p: Person |p.age<= 50))
context Company:inv: self.employee→ exists(p: Person | p.age> 50)
28/4528/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elements
The employer of an employee that participates a team projectis the organisation that possesses this project
context Person:inv: (self.employer→size()=1 and self.team→size()=1)implies self.employer=self.team.project.organisation
29/4529/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Operation on Elements
collect: create a new collection, for which each elementis the result of the expression
It is required to obtain the set of birthday dates for allemployees
context Companyself.employee→ collect (p: Person | p.birthdayDate)
Shorthand for collect
context Companyself.employee.birthdayDate
30/4530/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Iterate Operation
iterate: calculate an accumulator whose value is built upduring the iteration of the collection.
Collection→ iterate (e: Type; acc: Type=initialexpression | expression with e and acc)
The sum of ages of all children for a person
context Personself.children→ iterate (p: Person; acc: Integer=0 |acc=acc+p.age)
31/4531/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Re-typing or Casting
With o.oclAsType(T2), one re-types an object o of typeT1 into another type T2
Let type Super be a super type of type Sub
Allows one to use a property of an object defined on asubtype of the currently known type of the objectcontext Superinv: self.oclAsType(Sub).p (accesses the p propertydefined in Sub, valid when actual type of self is Sub,otherwise, invalid)
Can be used to access a property of a superclasscontext Subinv: self.oclAsType(Super).p (accesses the p propertydefined in Super)
32/4532/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Other functions
oclIsTypeOf(t: Type): return true if the type of self and tare the same
oclIsKindOf(t: Type): return true if t is a direct/indirect(supertype) type of self
oclIsNew: used in a post-condition, return true if theobject has been created during the operation
Examplecontext Personinv: self.oclIsTypeOf(Person) –trueinv: self.oclIsTypeOf(Company) –false
33/4533/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Class features
Features of a class, not of its instances
Either predefined or user-defined
Predefined: allInstances holds on all types and returnsthe set of class instances
There are at most 100 personscontext Personinv: Person.allInstances()→ size() ≤ 100A user-defined feature averageAge of class Personcontext PersonPerson.averageAge=(Person.allInstances()→ collect(age)→ sum()) / (Person.allInstances()→ size())
34/4534/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Example 1
35/4535/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
A hotel never has a floor 13, because of superstition.
context Roominv: self.floor <> 13
context BathRoominv: self.floor <> 13
36/4536/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
A hotel never has a floor 13, because of superstition.
context Roominv: self.floor <> 13
context BathRoominv: self.floor <> 13
36/4536/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
The number of clients for each room must be smaller or equalto the number of beds in the rented room. The children under4 are not ”taken into account“ in this calculation rule (condition: maximum of one child under 4 per room).
context Roominv: clients→size()≤nbBeds or(clients→ size()=nbBeds+1 and clients→exists(p:Person| p.age<4))
37/4537/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
The number of clients for each room must be smaller or equalto the number of beds in the rented room. The children under4 are not ”taken into account“ in this calculation rule (condition: maximum of one child under 4 per room).
context Roominv: clients→size()≤nbBeds or(clients→ size()=nbBeds+1 and clients→exists(p:Person| p.age<4))
37/4537/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
Each floor owns at least one room except for floor 13.
context Hotelinv: Sequence{floorMin, ..., floorMax}→forAll (f:Integer | f<>13 implies self.room→ select(r:Room|r.floor=f)→ notEmpty())
38/4538/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
Each floor owns at least one room except for floor 13.
context Hotelinv: Sequence{floorMin, ..., floorMax}→forAll (f:Integer | f<>13 implies self.room→ select(r:Room|r.floor=f)→ notEmpty())
38/4538/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
Rooms are on the first to the last floor.
context Hotelinv: self.room→ forAll(r: Room | r.floor≤ self.floorMaxand r.floor≥ self.floorMin)
39/4539/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
Rooms are on the first to the last floor.
context Hotelinv: self.room→ forAll(r: Room | r.floor≤ self.floorMaxand r.floor≥ self.floorMin)
39/4539/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
A room can be repainted when it is not occupied. Oncerepainted, the cost of a room is 10% more.
context Room::repaint(c: Color)pre: clients→ isEmpty()post: price=price@pre*1.1
40/4540/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
A room can be repainted when it is not occupied. Oncerepainted, the cost of a room is 10% more.
context Room::repaint(c: Color)pre: clients→ isEmpty()post: price=price@pre*1.1
40/4540/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
The hotel income is equal to the sum of prices for all rentedrooms.
context Hotel::income():Realpost: result=self.room→ select(r: Room | r.clients→notEmpty())→ collect(r:Room | r.price)→ sum()
41/4541/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 1
The hotel income is equal to the sum of prices for all rentedrooms.
context Hotel::income():Realpost: result=self.room→ select(r: Room | r.clients→notEmpty())→ collect(r:Room | r.price)→ sum()
41/4541/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
Example 2
42/4542/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 2
The salary of an employee cannot be greater than thesalary of his/her supervisercontext Employeeinv: self.supervisor→ notEmpty() impliesself.salary < self.supervisor.salary
The condition notEmpty must be tested since themultiplicity of the role is not mandatory
43/4543/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 2
The salary of an employee cannot be greater than thesalary of his/her supervisercontext Employeeinv: self.supervisor→ notEmpty() impliesself.salary < self.supervisor.salary
The condition notEmpty must be tested since themultiplicity of the role is not mandatory
43/4543/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 2
The SSN of employees is an identifier (or a key)context Employeeinv: Employee.allInstances()→forAll(e1, e2 | e1 <> e2implies e1.SSN <> e2.SSN)
The name and relationship of dependents is a partialidentifier: they are unique among all dependents of anemployeecontext Employeeinv: self.dependents→ notEmpty() impliesself.dependents→ forAll(e1, e2 | e1 <> e2 impliese1.name <> e2.name or e1.relationship <>e2.relationship)
44/4544/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 2
The SSN of employees is an identifier (or a key)context Employeeinv: Employee.allInstances()→forAll(e1, e2 | e1 <> e2implies e1.SSN <> e2.SSN)
The name and relationship of dependents is a partialidentifier: they are unique among all dependents of anemployeecontext Employeeinv: self.dependents→ notEmpty() impliesself.dependents→ forAll(e1, e2 | e1 <> e2 impliese1.name <> e2.name or e1.relationship <>e2.relationship)
44/4544/45
Software Engineering (OCL) ForwardBack
Software Engineering(OCL)
Lina YE
Introduction
ConstraintsContext and Self
Invariant
Pre- and Post-condition
Constraints on Attributes
LanguageAccess to characteristics
Types
Variable
Collections
OCL function
Example
OCL for example 2
An employee cannot supervise him/herselfcontext Employeeinv: self.subordinates→excludes(self)
45/4545/45
Software Engineering (OCL) Back