1

I have two Eclasses(call them X and Y) in my metamodel and a containment(X contains Y) relation between them which lower bound is 0 and upper bound is 1.

I need to know in my .mtl file if the user has added this only instance of Y in order to add some code. Tries like this have failed:

[if (X.relationname.oclIsUndefined() = false)]
[if (X.relationname <> null)]

Thanks in advance and let me know if you need any extra information.

omniyo
  • 330
  • 2
  • 6
  • 19

2 Answers2

1

This will depend on your containment relationship; whether it is multivalued (its "upperBound" is set to "-1" i.e. it can hold as many Y as you want) or monovalued (its "upperBound" is "1" or unchanged, it can only hold a single Y).

If multivalued, the reference will never be "null" (or "oclIsUndefined"). When it does not hold a single Y, it will be an empty list and you thus need to check for the size :

[if (not X.relationname.isEmpty())]

Otherwise, for monovalued references, you can check for null (what you have done in your answer seems to indicate that it is the case for you here) :

[if (not X.relationname.oclIsUndefined())]

On the contrary, what you have done in your answer is a little different :

[if((X.relationname.attributename->size()).oclIsUndefined() <> true)]

This will actually retrieve the Y associated with your X and access its attribute values. This will not be null if there is no "Y" : it will be "invalid" i.e. it will fail. Of course, "oclInvalid" (the "failure" object) is different from "true" so your "<>" works... even though it is clunky (you'd usually use the "not" operation instead of testing against a boolean value).

Kellindil
  • 4,523
  • 21
  • 20
0

SOLUTION:

I finally solved by:

[if((X.relationname.attributename->size()).oclIsUndefined() <> true)]

It isn't the best solution but it did the trick. The attribute is an EString.

omniyo
  • 330
  • 2
  • 6
  • 19