umlocl

UML constraint {xor} between two associations


I am currently struggeling with contraints in UML. Considering the following example:

The class A have an association to the class B and an association to the class C. Both associations are bidirectional. The Class A can have exactly one instance of B or one instance of C, but not B and C at the same time. In the other direction, B can have only one instance of A, and C can have 0..* instances of A.

I tried to solve this problem with an {XOR} constraint, which is described here https://www.uml-diagrams.org/constraint.html. Unfortunately, there are few examples that can be used as a guidance. Here an example of my solution: UML Example Solution

Does the solution correctly represents the described example?


Solution

  • UML graphics are good for many things, but there comes a point when textual expressions are needed; hence OCL constraints. IMHO the "xor" is an attempt to push graphics beyond what is sensible. Even if you manage to use it correctly, will your users or tools do likewise? Use an OCL constraint. I'm far from convinced that the exactly 1 multiplicity for a B is redefined by the "xor". I think you need 0..1 and then you really do need OCL.

    context A
    inv AxorB: self.B->size() + self.C->size() = 1