5

I have an Eclass Vehicle which has an enum attribute BreakType breakType.

BreakType is defined in the same Ecore model as:

BreakType{
    DRUM(0), DISC(1), BLADE(2)
}

I want to set attribute breakType default to null. for that I set following properties for breakType attribute->

DefaultLiteralValue: // it's blank
Unsettable: True

Properties of BreakType enum
Default Value : DRUM=0 // this is shown in editor UI even If i remove it from xml.

What I am getting after generating gen-model and code out of it is

BreakType breakType = DRUM // attribute set with default value

How can I set it to null, by default?

Lii
  • 11,553
  • 8
  • 64
  • 88
roul ze
  • 133
  • 1
  • 9

1 Answers1

1

I don't think you can. If you fail to provide a default value through the defaultValueLiteral property EMF automatically picks a value appropriate to the type of the attribute. For an enumerated type, it is the first literal value that it defines.

You can always modify the generated code yourself. Or maybe you should make use of the methods generated to provide the unsettable functionality:

void unsetAttribute();
boolean isSetAttribute();

and check for the unset state instead of the null value.

  • 4
    Mmm, look what I've come across in the EMF Recipes. I haven't tested it yet, but maybe this is what you were looking for: [Generating enumeration-based attributes that support null](http://wiki.eclipse.org/EMF/Recipes#Recipe:_Generating_enumeration-based_attributes_that_support_null) – José M. Benítez Sep 22 '12 at 22:35
  • As an alternative to the linked EMF recipe, you could define an extra enum instance, say UNDEFINED, and make sure that one is used as the default, i.e. in the ecorediag move it to the top of the literal list (the default value for that enum type will be adjusted automatically). With this there is at least an undefined default value in place, however it is not null. – Lukas Z. Feb 23 '15 at 12:07
  • @JoséM.Benítez: That's great! That's worth it's own answer, I'd upvote that. – Lii Mar 23 '17 at 14:00