Search code examples
umlconstraintsmodeling

Does anyone know good Object Constraint Language (OCL) tutorial?


I came across couple of questions about OCL expressions. After reading some university slides and googling it I still cannot properly understand it.

I wonder if any of you guys know any good resources that I should read to understand this stuff.


Constraints that bother me:

  1. Everybody working in the department has the same manager.
  2. Nobody in the company is the manager of him/herself.
  3. Nobody in the company earns more than his manager.

For the 1st one I have:

context Department

inv self.stuff -> forAll(manager = self.staff.manager)

2nd one:

context Company

inv self.employee -> select(manager = manager.manager) -> isEmpty()

3rd one:

context Company

inv self.employee -> select(salary > manager.salary) -> isEmpty()

but I dont think these are right. What I'm most unsure of is whether in example 2 and 3 I actually compare individual employees with theirs actual manager / manager salary.


Solution

  • A quick solution on the constraints:

    context Department inv: self.staff -> forAll(s1,s2| s1.manager = s2.manager)

    context Company inv: self.employee->forAll(e| e.manager<>e)

    context Company inv: self.employee->forAll(e| e.salary<=e.manager.salary)

    Btw, I don't really see the need for the Company class (how many objects of type company do you have in the system?). If constraints two and three are true for all companies then they could be expressed using Person as context in this way (e.g. with number 2): context Person inv: self.manager<>self)

    We can also add checks to see if the employee has a manager before doing the comparison