V. small inconsistency of Enum default literal between Dynamic and Genereated EMF [message #543657] |
Wed, 30 June 2010 08:47 |
Aiden Grandfield 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
[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 #543714 is a reply to message #543657] |
Wed, 30 June 2010 11:43 |
Aiden Grandfield 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 #543855 is a reply to message #543779] |
Wed, 30 June 2010 19:59 |
Ed Merks 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()
<em>Sex</em>}' attribute.<br>
* <!-- begin-user-doc --><br>
* <!-- end-user-doc --><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>
<?xml version="1.0" encoding="UTF-8"?>
<br>
<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">
<br>
<eClassifiers xsi:type="ecore:EClass" name="Person">
<br>
<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>"/>
<br>
<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"/>
<br>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="sex"
eType="#//Sex" defaultValueLiteral="FemaleLit"/>
<br>
</eClassifiers>
<br>
<eClassifiers xsi:type="ecore:EEnum" name="Sex">
<br>
<eLiterals name="MALE" literal="MaleLit"/>
<br>
<eLiterals name="FEMALE" value="1" literal="FemaleLit"/>
<br>
</eClassifiers>
<br>
</ecore:EPackage>
<br>
<br>
Thanks in advance .
<br>
A.g.
<br>
</blockquote>
</body>
</html>
--------------050706020501090406020807--
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03567 seconds