0

Let's say I have class A with associations to classes X, Y, and Z, respectively. I need to indicate that only one of these associations may be instantiated for any given instance of class A (so, an xor constraint). I know how to do this if the constraint is just across two associations. Obviously I can just make three seperate xor constraints (X-Y, X-Z, Y-Z) but I'm wondering if there is a better/cleaner/proper way to do it?

edit: The multiplicity constraints on the respective associations are not the same. Using an abstract class or interface will not work. Furthermore, a note is insufficient. I need to use some sort of formalized structure, preferably something standardized (e.g. by OMG) to express this because I am programmatically processing the model elements (i.e. it isn't just a picture). I understand how the underlying model for UML provides for this facility. It also specifies (though slightly vaguely) how it should be notated. I guess my main issue is, in fact, with finding a tool that allows me to make that notation. I don't think MagicDraw does so. I should have stated these things earlier.

FWIW, I'm using MagicDraw. It would be a nice bonus if the I could do this in a way that MagicDraw actually understood. I can live with it if that isn't possible.

Huliax
  • 1,489
  • 3
  • 15
  • 27

2 Answers2

2

If "X", "Y" and/or "Z" can be somehow generalized (I mean, if you not doing this puraly for a conditional flow control), you can make an interface (or and abstract class) "I" for example, and make "X", "Y" and "Z" implement this interface. Then, you put an association with multiplicity 1 between A and the interface I. See the diagram below:

enter image description here

Edit: The example above doesn't work in the case of A having different cardinalities between X, Y and Z. For this case, the only way that I can see is use an UML Constraint to restrict those relationships. You can define a Constraint in UML putting some OCL expression between curly braces. E. g.

enter image description here

Here, account owner is either Person or Corporation and this {xor} is predefined UML constraint.

I'm not sure about the details of your cardinalities requirements but, a combination between this {xor} and the interface example that I gave might be enough. At least it gives you a little bit more of options, like:

enter image description here

If you need to know more about the UML constraints subject, I got this example from uml-diagrams.org: http://www.uml-diagrams.org/constraint.html

Mauricio Reis
  • 319
  • 3
  • 9
  • This doesn't cover all of the cases. Please see edited question. – Huliax Aug 21 '17 at 19:09
  • An "interface" is a construct for making implementation models for platform/languages supporting interfaces (such as Java and C#), but not for logical design modeling, which is the type of model asked for. You should therefore better delete this part of your answer. – Gerd Wagner Aug 25 '17 at 19:36
  • I agree with you that specialization shall not be used purely for model a logical condition but, as the question doesn't specify what A, X, Z and Y are in the real world, I thought that would be useful to point this alternative for him as, maybe, X, Y and/or Z might be generalized. In any case, I'll update my answer to make that clear. – Mauricio Reis Aug 25 '17 at 21:00
2

The xor constraint is just a stylized and rather under-specified constraint for the 2-way case.

You can define an explicit constraint (in Complete OCL) as:

context A
inv OnlyAorBorC: A->size() + B->Size() + C->size() <= 1

MagicDraw may allow you to specify a similar contextual Constraint on A.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
Ed Willink
  • 1,205
  • 7
  • 8