Optional attributes



Date view Thread view Subject view Author view Attachment view

From: John Daniels (jd@syntropy.co.uk)
Date: Mon 17 May 2004 - 14:42:20 BST


Hi all,

I'm struggling to understand the relationship between optional
attributes (ie attributes with [0..1] multiplicity) and the use of
oclUndefined.

If I have a class X with an attribute p simply defined as:

p : Integer

can it ever be the case that p.oclIsUndefined()? In other words, can I
assume that p will always have a valid integer value?

I suspect the answer to that question is "no", in which case if I want
to ensure that p always has an Integer value I'd have to add the
invariant:

context X
inv: not p.oclIsUndefined()

Now, assuming I'm right about this, I don't like it much. It means
that all my attributes are effectively "optional" unless I add a
constraint to them. Which makes me wonder why I'd ever define p like
this:

p [0..1] : Integer

which is what all the textbooks tell you is the correct way of
defining an optional attribute. It would be good if

p->notEmpty() implies not p->any().oclIsUndefined()

but since, I suspect, it doesn't, I'm struggling to see why I'd ever
bother with [0..1] as the multiplicity for an attribute. Am I missing
something?

--John

John Daniels
www.syntropy.co.uk

Date view Thread view Subject view Author view Attachment view