Can you use dot notation when referring to classes in relation to other classes in OCL?

95 views Asked by At

For example, let's say I have a situation like this

enter image description here

Can I write an OCL expression that counts how many students a school has, such as:

School.students->count()

?

Or would it be incorrect? And in that case, how would I count the number of students?

1

There are 1 answers

5
Christophe On

Yes, the dot is the correct OCL syntax, here.

This is explained in the OCL specifications, section 7.4.10 on navigation operators:

The "." navigation operator supports navigation from an object using a property or operation.
The "->" navigation operator supports navigation from a collection using a property, operation or iteration.

Furthermore, section 7.5.3 on navigation of properties explain that:

Starting from a specific object, we can navigate an association on the class diagram to refer to other objects and their properties. To do so, we navigate the association by using the opposite association-end:
object.associationEndName
The value of this expression is the set of objects on the other side of the associationEndName association.

There is just one issue here: students is not formally defined. In principle you should use the role name indicated at the association end (next to the *), and if no such role is given in the class diagram, you should use the class name instead, so student.

Not related: count() is used with an argument regarding the objects that have to be counted in the set. If you mean the number of students associated with a given school, you should use ->size()