0

First of all : thank for the support !

My problem : I want to simply test if an "enumerated" attribute is defined or not.

Let's say I have :

  • an EnumValues enumeration with 2 values VALEUR1 and VALEUR
  • an EObject object with an optional attribute value

I would have expected to test the value existence thanks to this expression :

object.value.oclIsUndefined()

But this expression always retuens true as the value attribute seems to be initialized with the first value of the enumeration. The only bypass way I've found is to not declare EnumValues as an Enum but as an EObject. Then the oclIsUndefined() method returns false when the attribute is not set in the model. BTW my model is generated thanks to Xtext.

It seems to be a stupid question but I'm not able to find the answer on Google, in the OCL specification or in the Acceleo resources.

The only material I've found here says that the only way to achieve this is to use the isUndefined or the more general isUndefined('value') OCL helper methods. But I'm not able to decline this in the Acceleo environnement.

Do you have any idea ?

Thanks in advance for your reply !

Stéphane

Stéphane
  • 1
  • 1

2 Answers2

0

In EMF, Enumerations have a default value (by default, the first enum literal of your enumeration), if you really want to create an enumeration with a default value, use this. Keep in mind that it's not the "regular" behavior for enumeration in EMF.

Acceleo is just reporting you what EMF tells: "your enumeration has a value".

You can test in Acceleo your enumeration with the following expression:

[myObject.myEnumValue = MyEnum::MyEnumLiteral/]

As explained in the wiki.

Regards,

Stephane Begaudeau, leader of Acceleo

sbegaudeau
  • 1,504
  • 13
  • 18
  • Hi Stéphane ! What an honnor ! The leader of Acceleo ! Thanks for your reply and the link. Effectively I use the notation you mentionned to test the enum value. One workaround pointed by Xtext documentation is introducing a dedicated none-value in first position of the enum... What I didn't find very elegant but I want to let Xtext generating my ecore model. So I'm gonna use this tip... Thanks again ! Stéphane – Stéphane May 08 '12 at 06:06
0

I would do the following:

  • use a manually maintained metamodel in Xtext
  • but do NOT add a Dummy Literal to the EEnum
  • A Cleaner solution is imho to declare the EAttribute that contains the Enum value as unsettable and thus EMF will tell you if the value was explicitely set or is default.
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • Hi Christian ! You are everywhere ! Thanks for your second reply. Effectively, your solution is a better one : I'm certainly gonna use a manually metamodel... But in a second time. At the moment, I just want to quickly show what it is possible to do with all these wonderful tools !... And maintaining the metamodel is... more work:) Thanks again. Stéphane – Stéphane May 11 '12 at 06:51