Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » V. small inconsistency of Enum default literal between Dynamic and Genereated EMF(<Not that serious so no need to worry>)
V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543657] Wed, 30 June 2010 08:47 Go to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Hi Folks,

This is now a more serious issue... see second post

I have noticed a small difference between the default literal for an Enum attribute in the dynamic vs generated emf code.

If I have a Person eClass, with an enum attribute Sex.... and the enum is as follow

Sex:

Name=MALE
Literal=MaleLit
Value=0

Name=FEMALE
Literal=FemaleLit
Value=1

Now, if in the person class I set the default literal to the sex to the "name" of the enumeration (i.e. "MALE"), then the .eGet for the autogenerated code returns the Male enumeration and the dynamic version returns null.

If on the other hand I set the default literal of sex to the "literal" value of the enumeration (i.e. "MaleLit"), the .eGet works for both dynamic and autogenerated code.

I know this is not a bug, as the default literal of the sex attribute should have been set the the literal value of the enum, but the fact that you can also set the default to the name of the enum without causing problems will result in people using the ecore defaults for enums incorrectly.

I have tested this on v2.6

Thanks for listening... yawwwwwwwn Laughing

[Updated on: Wed, 30 June 2010 10:02]

Report message to a moderator

Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543684 is a reply to message #543657] Wed, 30 June 2010 09:56 Go to previous messageGo to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Ok, The problem is actually a little more serious than i though because entering the literal values does not appear to be consistent.

So, for the person.sex, if I set the default to to "FEMALE" i.e. I use the name of the enum... the .eGet returns:

(as expceted.. the auto gen worked with the name of the enum)
AutoGen EMF = FemaleLit
Dynamic EMF = null

If i set the person.sex default to "FemaleLit"...

(The literal worked for the dynamic emf and not for the AutoGen)
AutoGen EMF = MaleLit
Dynamic EMF = FemaleLit

Sooo.... Unlike my assumption in my previous post, I have no way of specifiing a default that it consistent between dynamic and autogen EMF.

Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543714 is a reply to message #543657] Wed, 30 June 2010 11:43 Go to previous messageGo to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Ok, just so ye know...... In order to allow the dynamic emf to accept both the enum name (as the autogen only accpets) and the enum literal I had to make local modification to the EFactoryImpl.createFromString(EDataType eDataType, String stringValue)

I will log this as a bug in the next 24 hours unless someone can tell me If i am doing something wrong

Original code
if (eDataType instanceof EEnum)
{
Object result = ((EEnum)eDataType).getEEnumLiteralByLiteral(stringValue);>
if (result == null)
{
throw new IllegalArgumentException("The value '" + stringValue + "' is not a valid enumerator of '" + eDataType.getName() + "'");
}
return result;
}


My new code
if (eDataType instanceof EEnum)
{
Object result = ((EEnum)eDataType).getEEnumLiteralByLiteral(stringValue);
if (result == null)
{
// Lets try get the literal by the Enum name
result = ((EEnum)eDataType).getEEnumLiteral(stringValue);
}
if (result == null)
{
throw new IllegalArgumentException("The value '" + stringValue + "' is not a valid enumerator of '" + eDataType.getName() + "'");
}
return result;
}

[Updated on: Wed, 30 June 2010 11:45]

Report message to a moderator

Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543747 is a reply to message #543714] Wed, 30 June 2010 12:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Aiden,

Only the literal value should be used, not the name. It's "difficult"
to validate the default value of a feature because in the generated
code, the generated factory can be specialized to make literals that
appear incorrect actually work correctly; it sounds like you're doing
exactly that. If there is a lurking bug, it's the fact that somewhere
the name of the literal is working when only the literal value should be
working; that wouldn't be too surprising given that the literal value
feature was added a few releases after only the name was supported.


Aiden Grandfield wrote:
> Ok, just so ye know...... In order to allow the dynamic emf to accept
> both the enum name (as the autogen only accpets) and the enum literal
> I had to make local modification to the createFromString(EDataType
> eDataType, String stringValue)
> I will log this as a bug in the next 24 hours unless someone can tell
> me If i am doing something wrong
>
> Original code
> if (eDataType instanceof EEnum)
> {
> Object result =
> ((EEnum)eDataType).getEEnumLiteralByLiteral(stringValue);>
> if (result == null)
> {
> throw new IllegalArgumentException("The value '" + stringValue
> + "' is not a valid enumerator of '" + eDataType.getName() + "'");
> }
> return result;
> }
>
>
> My new code
> if (eDataType instanceof EEnum)
> {
> Object result =
> ((EEnum)eDataType).getEEnumLiteralByLiteral(stringValue);
> if (result == null)
> {
> // Lets try get the literal by the Enum name
> result = ((EEnum)eDataType).getEEnumLiteral(stringValue);
> }
> if (result == null)
> {
> throw new IllegalArgumentException("The value '" + stringValue
> + "' is not a valid enumerator of '" + eDataType.getName() + "'");
> }
> return result;
> }


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543779 is a reply to message #543747] Wed, 30 June 2010 14:31 Go to previous messageGo to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
As usual Ed, thanks for your speedy reply.

So if I understand correctly, when specifiying a default value literal for an attribute, we should use the literal of the EEnum and not the name. This works fine in dynamic EMF .....the problem now is that the code generation does not work when we used the literal value of an enum. It only works if we used the Name of the enum (which is incorrect)

If i do a codegen on the following ecore, because I am using the "FemaleLit" as the default value literal for person.sex, It will generate the code so the default = Sex.MALE.


<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="mypackage"
nsURI="com.pilz.mypackage.nsure" nsPrefix="com.pilz.mypackage">
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="age" lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
defaultValueLiteral="23"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="sex" eType="#//Sex" defaultValueLiteral="FemaleLit"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Sex">
<eLiterals name="MALE" literal="MaleLit"/>
<eLiterals name="FEMALE" value="1" literal="FemaleLit"/>
</eClassifiers>
</ecore:EPackage>

Thanks in advance .
A.g.
Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543855 is a reply to message #543779] Wed, 30 June 2010 19:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------050706020501090406020807
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Aiden,

I'm seeing this as the result.

/**
* The default value of the '{@link #getSex() <em>Sex</em>}'
attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getSex()
* @generated
* @ordered
*/
protected static final Sex SEX_EDEFAULT = Sex.FEMALE;

Are you using some older release?

Aiden Grandfield wrote:
> As usual Ed, thanks for your speedy reply.
>
> So if I understand correctly, when specifiying a default value literal
> for an attribute, we should use the literal of the EEnum and not the
> name. This works fine in dynamic EMF .....the problem now is that the
> code generation does not work when we used the literal value of an
> enum. It only works if we used the Name of the enum (which is incorrect)
>
> If i do a codegen on the following ecore, because I am using the
> "FemaleLit" as the default value literal for person.sex, It will
> generate the code so the default = Sex.MALE.
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <ecore:EPackage xmi:version="2.0"
> xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="mypackage"
> nsURI="com.pilz.mypackage.nsure" nsPrefix="com.pilz.mypackage">
> <eClassifiers xsi:type="ecore:EClass" name="Person">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="age"
> lowerBound="1" eType="ecore:EDataType
> http://www.eclipse.org/emf/2002/Ecore#//EInt"
> defaultValueLiteral="23"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="sex"
> eType="#//Sex" defaultValueLiteral="FemaleLit"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EEnum" name="Sex">
> <eLiterals name="MALE" literal="MaleLit"/>
> <eLiterals name="FEMALE" value="1" literal="FemaleLit"/>
> </eClassifiers>
> </ecore:EPackage>
>
> Thanks in advance .
> A.g.

--------------050706020501090406020807
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Aiden,<br>
<br>
I'm seeing this as the result.<br>
<blockquote>  /**<br>
   * The default value of the '{@link #getSex()
&lt;em&gt;Sex&lt;/em&gt;}' attribute.<br>
   * &lt;!-- begin-user-doc --&gt;<br>
   * &lt;!-- end-user-doc --&gt;<br>
   * @see #getSex()<br>
   * @generated<br>
   * @ordered<br>
   */<br>
  protected static final Sex SEX_EDEFAULT = Sex.FEMALE;<br>
</blockquote>
Are you using some older release?<br>
<br>
Aiden Grandfield wrote:
<blockquote cite="mid:i0fkfo$fuo$1@build.eclipse.org" type="cite">As
usual Ed, thanks for your speedy reply.
<br>
<br>
So if I understand correctly, when specifiying a default value literal
for an attribute, we should use the literal of the EEnum and not the
name. This works fine in dynamic EMF .....the problem now is that the
code generation does not work when we used the literal value of an
enum. It only works if we used the Name of the enum (which is
incorrect)
<br>
<br>
If i do a codegen on the following ecore, because I am using the
"FemaleLit" as the default value literal for person.sex, It will
generate the code so the default = Sex.MALE.
<br>
<br>
<br>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<br>
&lt;ecore:EPackage xmi:version="2.0"
<br>
   xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://www.omg.org/XMI">"http://www.omg.org/XMI"</a>
xmlns:xsi=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
<br>
   xmlns:ecore=<a class="moz-txt-link-rfc2396E" href="http://www.eclipse.org/emf/2002/Ecore">"http://www.eclipse.org/emf/2002/Ecore"</a> name="mypackage"
<br>
   nsURI="com.pilz.mypackage.nsure" nsPrefix="com.pilz.mypackage"&gt;
<br>
 &lt;eClassifiers xsi:type="ecore:EClass" name="Person"&gt;
<br>
   &lt;eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
eType="ecore:EDataType
<a class="moz-txt-link-freetext" href="http://www.eclipse.org/emf/2002/Ecore#//EString">http://www.eclipse.org/emf/2002/Ecore#//EString</a>"/&gt;
<br>
   &lt;eStructuralFeatures xsi:type="ecore:EAttribute" name="age"
lowerBound="1" eType="ecore:EDataType
<a class="moz-txt-link-freetext" href="http://www.eclipse.org/emf/2002/Ecore#//EInt">http://www.eclipse.org/emf/2002/Ecore#//EInt</a>"
<br>
       defaultValueLiteral="23"/&gt;
<br>
   &lt;eStructuralFeatures xsi:type="ecore:EAttribute" name="sex"
eType="#//Sex" defaultValueLiteral="FemaleLit"/&gt;
<br>
 &lt;/eClassifiers&gt;
<br>
 &lt;eClassifiers xsi:type="ecore:EEnum" name="Sex"&gt;
<br>
   &lt;eLiterals name="MALE" literal="MaleLit"/&gt;
<br>
   &lt;eLiterals name="FEMALE" value="1" literal="FemaleLit"/&gt;
<br>
 &lt;/eClassifiers&gt;
<br>
&lt;/ecore:EPackage&gt;
<br>
<br>
Thanks in advance .
<br>
A.g.
<br>
</blockquote>
</body>
</html>

--------------050706020501090406020807--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543973 is a reply to message #543855] Thu, 01 July 2010 08:53 Go to previous messageGo to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Thanks for your reply Ed,

Honestly, I dont understand what I am doing wrong .. (I use dynamic emf more than autogen so Im not an expert with the gen stuff) Im using eclipse 3.6.0 with EMF 2.5 v20090615043 (the latest stable release on the update site) .

I use the EMF Project wizard which generates a project with a .gen model from the person.ecore, then, when I have the .genmodel open, I select the menu "Generator" & click "Generate model Code" ... Is there some other workflow you are using to create the generated code ?

Here is the Gen model
<?xml version="1.0" encoding="UTF-8"?>
<genmodel:GenModel xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:genmodel="http://www.eclipse.org/emf/2002/GenModel" modelDirectory="/EMFeSetCheck/src"
modelPluginID="EMFeSetCheck" modelName="Person" importerID="org.eclipse.emf.importer.ecore"
complianceLevel="6.0" copyrightFields="false">
<foreignModel>person.ecore</foreignModel>
<genPackages prefix="Mypackage" disposableProviderFactory="true" ecorePackage="person.ecore#/">
<genEnums typeSafeEnumCompatible="false" ecoreEnum="person.ecore#//Sex">
<genEnumLiterals ecoreEnumLiteral="person.ecore#//Sex/MALE"/>
<genEnumLiterals ecoreEnumLiteral="person.ecore#//Sex/FEMALE"/>
</genEnums>
<genClasses ecoreClass="person.ecore#//Person">
<genFeatures createChild="false" ecoreFeature="ecore:EAttribute person.ecore#//Person/name"/>
<genFeatures createChild="false" ecoreFeature="ecore:EAttribute person.ecore#//Person/age"/>
<genFeatures createChild="false" ecoreFeature="ecore:EAttribute person.ecore#//Person/sex"/>
</genClasses>
</genPackages>
</genmodel:GenModel>


I really hope I am making some obvious mistake with this.

Thanks again
A.g.
Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543981 is a reply to message #543973] Thu, 01 July 2010 09:12 Go to previous messageGo to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Ok, just tried EMF 2.6 http://download.eclipse.org/modeling/emf/updates/milestones/ update site and it works. Case closed. Didnt think this bug would have existed in the gen code up to v2.5

We use 2.4 in our project because we use RCP 3.4. Sad Do you think it is safe to use the 2.6 code gen to generated code for a 2.4 runtime ?

Thanks for all your help ed.

[Updated on: Thu, 01 July 2010 09:15]

Report message to a moderator

Re: V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #544035 is a reply to message #543855] Thu, 01 July 2010 12:35 Go to previous message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Hi Ed, in EMF 2.6, there is another small difference between the autogen EMF and dynamic EMF

if the default value literal for an attribute is incorrect (i.e. it could be an empty " " ), the auto gen will assigne the EEnum's default value as the default value of the attribute (E.g MALE), where as the .get for the dynamic EMF will return null.

Anyways, this is not much of a problem for me.
Thanks again for all your help
A.g.
Previous Topic:[Databinding] ObservableListTreeContentProvider for multiList of 'single' references
Next Topic:XSD to Ecore to XSD
Goto Forum:
  


Current Time: Fri Apr 26 01:36:31 GMT 2024

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

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

Back to the top