Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » How to write OCL that tests an attribute value for null
How to write OCL that tests an attribute value for null [message #648593] Thu, 13 January 2011 15:33 Go to next message
Andreas Maier is currently offline Andreas MaierFriend
Messages: 32
Registered: July 2009
Member
Not knowing any other OCL newsgroup (see previous post), I am posting
this OCL language/spec question here.

I have difficulties figuring out from the OMG OCL 2.0 spec how to write
OCL that tests an attribute value for null.

The text that comes closest to saying something about testing for null
is the following definition of OclVoid in section 11.2.5:

OclVoid = (object : OclAny) : Boolean
Redefines the OclAny operation, returning true if object is null.
post: result = object.oclIsTypeOf(OclVoid)

That tells me that OclVoid is an operation that can be invoked on any
object and it returns true if the object on which it is invoked is null.

On the other hand, OclVoid seems to be a type, since it shown as an
argument to the oclIsType() operation in several places in the OCL spec,
and also there is wording to that extent in section 8.2 (VoidType).

To use a simple example, consider a class C1 that has an attribute a1
which is allowed to be null according to the UML model. I'd like to
understand which OCL expression to write for testing whether the value
of a1 on any instances of C1 is null.

I try to offer some options that came to mind, all formulated as
derivation constraints that are supposed to evaluate to true only if the
value of a1 is null:

(1) context C1 derive: self.a1.OclVoid

(2) context C1 derive: self.a1.OclVoid()

(3) context C1 derive: self.a1.oclIsType(OclVoid)

None of those seems right to me, for the following reasons:

(1) Operations usually have paranthesis, in addition OclVoid is used
like a type elsewhere.

(2) OclVoid is used like a type elsewhere.

(3) I'd like to test the value of a1, not its type.

Help!

Andy
Re: How to write OCL that tests an attribute value for null [message #648929 is a reply to message #648593] Sat, 15 January 2011 18:51 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Andreas

The not very obvious OCL idiom is

aVariable->notEmpty()

if aVariable is invalid result is invalid
else if aVariable is null result is false
else result is true
-- a real object

If you want to do it more obviously you can use oclIsUndefined() and
oclIsInvalid().

NB The OCL 2.3 specification that should be published very soon,
resolves numerous typos regarding OclInvalid, OclVoid, invalid, null
which may ease understanding.

Regards

Ed Willink

On 13/01/2011 15:33, Andreas Maier wrote:
> Not knowing any other OCL newsgroup (see previous post), I am posting
> this OCL language/spec question here.
>
> I have difficulties figuring out from the OMG OCL 2.0 spec how to
> write OCL that tests an attribute value for null.
>
> The text that comes closest to saying something about testing for null
> is the following definition of OclVoid in section 11.2.5:
>
> OclVoid = (object : OclAny) : Boolean
> Redefines the OclAny operation, returning true if object is null.
> post: result = object.oclIsTypeOf(OclVoid)
>
> That tells me that OclVoid is an operation that can be invoked on any
> object and it returns true if the object on which it is invoked is null.
>
> On the other hand, OclVoid seems to be a type, since it shown as an
> argument to the oclIsType() operation in several places in the OCL
> spec, and also there is wording to that extent in section 8.2 (VoidType).
>
> To use a simple example, consider a class C1 that has an attribute a1
> which is allowed to be null according to the UML model. I'd like to
> understand which OCL expression to write for testing whether the value
> of a1 on any instances of C1 is null.
>
> I try to offer some options that came to mind, all formulated as
> derivation constraints that are supposed to evaluate to true only if
> the value of a1 is null:
>
> (1) context C1 derive: self.a1.OclVoid
>
> (2) context C1 derive: self.a1.OclVoid()
>
> (3) context C1 derive: self.a1.oclIsType(OclVoid)
>
> None of those seems right to me, for the following reasons:
>
> (1) Operations usually have paranthesis, in addition OclVoid is used
> like a type elsewhere.
>
> (2) OclVoid is used like a type elsewhere.
>
> (3) I'd like to test the value of a1, not its type.
>
> Help!
>
> Andy
>
Previous Topic:Re: [MDT-OCL] Defining OCL constraints
Next Topic:How to write OCL for accessing operation parameters
Goto Forum:
  


Current Time: Sat Apr 20 00:17:17 GMT 2024

Powered by FUDForum. Page generated in 0.03042 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top