Question

I have a student class that has two associations: Graduate and Undergraduate. I am trying to create an invariant that makes sure no student is associated with both. I am very new to OCL and do not know the approach to this or syntax. I am thinking I need to have something like Student.allInstances->forAll( something here)

But I don't know.

Thanks!

Was it helpful?

Solution 2

Is the association a self-reference of student? Or does it have another class at the end of the association?

If there is another class "Study" (as an example) and the associations would be between them with the cardinality *, then a solution could be:

context Student s:
inv only_one_graduate_type:
  ( (s.graduate -> notEmpty() implies s.undergraduate -> isEmpty()) && 
    (s.undergraduate -> notEmpty() implies s.graduate -> isEmpty()) )

If you use something like Student.allInstances-> better rewrite your OCL expression using a context Student s

OTHER TIPS

UML even has a graphical xor dependency between two associations with this exact meaning so if you wish you could even skip this particular ocl constraint.

How about this?

context Student:
inv max_one_association:
    not (self.graduate -> notEmpty() && self.undergraduate -> notEmpty())

BTW, it would be good if you could post the class diagram, including the Student class and the affected associations. Just in case...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top