Object Constraint Language
About OCL
-
- side-effect free
- An Query Language
- To describe Guards
- To specify invariants, pre- & post-conditions
Constraint examples
-
- context of the expression (attached to the UML model or specified explicitly)
- stereotype:inv, pre, post
- more examples:
- context Account
inv: Account.allInstances()->forAll(a,b | a<>b implies a.accName<>b.accName)
inv: Account.allInstances()->forAll(a | self<>a implies a.accName<>self.accName)
- invarient with let expressions:
context Person inv:
let income : Integer = self.job.salary->sum()
let hasTitle(t: String) : Boolean = self.job->exists(title = t) in
if isUnemployed then income < 100
else income >= 100 and hasTitle('manager') endif
- Pre- and Post-conditions:
context Person::income(d :Date):Integer
pre : d.value >= self.job.startDate.value
post :result =5000
- Attributes
-
- Operations
- Association Ends and Navigation
context Company inv :self.manager.isUnemployed =false inv :self.employee->notEmpty()
context Person inv : self.employer->size()< 3
Note:By default, navigation will result in a Set. When the association on the Class
Diagram is adorned with {ordered}, the navigation results in a Sequence.
- Navigation over Association with Multiplicity zero or one
context Person inv : self.wife->notEmpty()implies self.wife.sex =Sex::female
- Combining Properties
[1] Married people are of age >= 18
context Person inv :
self.wife->notEmpty()implies self.wife.age >=18 and
self.husband->notEmpty()implies self.husband.age >=18
- Navigation to Association Classes
- Navigation from Association Classes
context Job
inv :self.employer.numberOfEmployees >=1
inv :self.employee.age >21
- Accessing overridden properties of supertypes
context B inv :
self.oclAsType(A).p1 --accesses the p1 property defined in A
self.p1 --accesses the p1 property defined in B
- Navigating to collections:
-
-
-
-
- The OCL Standard Library
- References:
- OCL-specificatin-v1.3a
- OCL Tutorial