views:

124

answers:

3

I'm working up an analysis model in UML, and I'm a bit stuck when trying to represent a constraint on a couple of attributes in a class diagram. In the following class:

+-----------+
| SomeClass |
+-----------+
| isFoo     |
| isBar     |
| isBaz     |
| isQuux    |
+-----------+

all the listed attributes are Boolean types, and the final two, isBaz and isQuux, are mutually exclusive. How do I indicate that? I'd rather not use an ad-hoc note -- more clutter.

Everything I can find about constraints in UML seems to apply them to associations, not attributes. I could break the attributes out into an associated class, but they're simple Boolean types, so something like this seems like overkill:

+-----------+
| SomeClass |
+-----------+ 0..1   isBaz   1 +-------------------+
| isFoo     |------------------| ThereCanBeOnlyOne |
| isBar     |      |           +-------------------+
|           |      |{NAND}     | isBaz             |
|           |------------------| isQuux            |
+-----------+        isQuux    +-------------------+

What's the 'correct' way to model mutually exclusive attributes in UML?

A: 

Use an enumeration maybe? Not sure what you'd call your attribute - Bazquuxality?

You could still have isBaz and isQuux in the interface exposed by the class.

martin clayton
yes, that might work, but again overkill for what I'm doing. I'm really just using some simple flags; this question is mainly about the best way to communicate intent, not guide implementation. From an implementation point of view, I probably don't need an enum -- just some code in the property setter that unsets isQuux if setting isBaz, and vice-versa, should suffice.
Val
A: 

I would look into using Object Constraint Language to describe it.

I've never really used it beyond a couple lectures, but I'm fairly sure this is what you need.

Something like this may express the condition you want:

{context SomeClass
inv isBaz==True implies isQuux==False
inv isQuux==True implies isBaz==False}

This presentation may give you a good starting point.

chrisbunney
Yes -- the {NAND} in my 2nd diagram is shorthand for an OCL constraint; Scott Ambler uses that in an example. I scanned the preso you linked to. Mostly the constraints are applied against associations, but in a few places they just float around and reference attributes. I suppose I can put your proposed expression in an annotation that points to the class.
Val
A: 

There are only two options, model the attributes as associations with the boolean data type or use an OCL constraint.

A simplified and corrected (equals in OCL goes with just one "=") could be: context SomeClassinv not isBaz=isQuux

Btw, I'm not sure about the exact meaning for you of the concept "mutually exclusive". This usually imples an XOR (predefined constraint on associations in the standard) but then you use a NAND. Is it possible in your model that both attributes have a false value? (I'm assuming this is not possible in my OCL constraint)

Jordi Cabot
thanks, great clarification. Yes, in my model, isBaz and isQuux are really 'Operating Expense' and 'Major Operating Expense' The parent object, a Contract, can be classified as one, the other, or neither (both false), but not both.
Val