Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Anonymous Enumerations Always Get Default Value
Anonymous Enumerations Always Get Default Value [message #643009] Fri, 03 December 2010 16:13 Go to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi, I've noticed that if you have an anonymous enumeration like:

	<xs:complexType name="Plane">
		<xs:choice>
			<xs:element name="engineType" minOccurs="0">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:enumeration value="Jet"/>
						<xs:enumeration value="Propeller"/>
					</xs:restriction>
				</xs:simpleType>
			</xs:element>
			<xs:element name="engineName" type="xs:string"/>
		</xs:choice>
	</xs:complexType>


Then the engineType element will always get a default value of "Jet". This means that the whole usage of the choice in this case is invalid, and you will never be able to use EMF to generate XML for the choice where only the engineName is set.

I have had a look at the generated code and the Impl sets:

	protected static final EngineTypeType ENGINE_TYPE_EDEFAULT = EngineTypeType.JET;
	protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;


I'm assuming that it should really be:

	protected static final EngineTypeType ENGINE_TYPE_EDEFAULT = null;
	protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;


If enumerations are not anonymous then this case does not arrise. I'm using EMF 2.4.2

Is this a know issue, or am I doing some-thing wrong - is there an option I can set to stop this happening.

Any help would be greatly received.

Thanks, Rob
Re: Anonymous Enumerations Always Get Default Value [message #643044 is a reply to message #643009] Fri, 03 December 2010 17:53 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Rob,

Comments below.


Rob wrote:
> Hi, I've noticed that if you have an anonymous enumeration like:
>
> <xs:complexType name="Plane">
> <xs:choice>
> <xs:element name="engineType" minOccurs="0">
> <xs:simpleType>
> <xs:restriction base="xs:string">
> <xs:enumeration value="Jet"/>
> <xs:enumeration value="Propeller"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:element>
> <xs:element name="engineName" type="xs:string"/>
> </xs:choice>
> </xs:complexType>
>
>
> Then the engineType element will always get a default value of "Jet".
Yes. It's not unlike what would happen for xsd:int which maps to int in
Java, i.e., you'd always get 0...
> This means that the whole usage of the choice in this case is invalid,
I'm not sure how that follows...
> and you will never be able to use EMF to generate XML for the choice
> where only the engineName is set.
Did you try that case or are you making an assumption?
>
> I have had a look at the generated code and the Impl sets:
>
> protected static final EngineTypeType ENGINE_TYPE_EDEFAULT =
> EngineTypeType.JET;
> protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;
>
> I'm assuming that it should really be:
>
> protected static final EngineTypeType ENGINE_TYPE_EDEFAULT = null;
> protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;
>
> If enumerations are not anonymous then this case does not arrise.
EMF itself hasn't a clue about anonymous verses named, so I'm not sure
how you arrived at that conclusion.
> I'm using EMF 2.4.2
That's pretty old.
>
> Is this a know issue, or am I doing some-thing wrong - is there an
> option I can set to stop this happening.
Is the EMF attribute that's produced unsettable? I.e., is there a
generated isSetEngineType() method? I'd expect there to be one.
>
> Any help would be greatly received.
>
> Thanks, Rob
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Anonymous Enumerations Always Get Default Value [message #643381 is a reply to message #643044] Mon, 06 December 2010 17:07 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Ed, Thanks for taking the time to reply

Ed Merks wrote on Fri, 03 December 2010 17:53


> Then the engineType element will always get a default value of "Jet".
Yes. It's not unlike what would happen for xsd:int which maps to int in
Java, i.e., you'd always get 0...
> This means that the whole usage of the choice in this case is invalid,
I'm not sure how that follows...
> and you will never be able to use EMF to generate XML for the choice
> where only the engineName is set.
Did you try that case or are you making an assumption?



Sorry, I was having one of those Friday afternoon moments, the XML generated out of it will indeed be correct. The problem is with making a call to getEngineType(), it will always return a value and never null.

Ed Merks wrote on Fri, 03 December 2010 17:53

> I have had a look at the generated code and the Impl sets:
>
> protected static final EngineTypeType ENGINE_TYPE_EDEFAULT =
> EngineTypeType.JET;
> protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;
>
> I'm assuming that it should really be:
>
> protected static final EngineTypeType ENGINE_TYPE_EDEFAULT = null;
> protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;
>
> If enumerations are not anonymous then this case does not arrise.
EMF itself hasn't a clue about anonymous verses named, so I'm not sure
how you arrived at that conclusion.



I took a look through the generated code for the cases of enums verses anonymous enums. (The first code is from an anonymous case, the second is from the named enumeration case)

Ed Merks wrote on Fri, 03 December 2010 17:53

> I'm using EMF 2.4.2
That's pretty old.



Unfortunately we have to use this version due to other dependancies in our runtime system.

Ed Merks wrote on Fri, 03 December 2010 17:53

Is the EMF attribute that's produced unsettable? I.e., is there a
generated isSetEngineType() method? I'd expect there to be one.



Yes, the isSet method is being generated.

I was just wondering why the behaviour is different between anonymous and names enums.

For a named enum referenced in a complextype getEngineType() will return null, however for an anounymous enum it will always default to the first in the list if it is unset.

I understand that recommended usage is to first call the isSet method, but unfortunately I'm in an environment that I can use that (Long story)

Thanks

Rob
Re: Anonymous Enumerations Always Get Default Value [message #643395 is a reply to message #643381] Mon, 06 December 2010 17:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Rob,

I can't reproduce this issue of different behavior when a named type is
used (using EMF 2.7). I don't recall there ever being such a bug...



Rob wrote:
> Hi Ed, Thanks for taking the time to reply
>
> Ed Merks wrote on Fri, 03 December 2010 17:53
>> > Then the engineType element will always get a default value of
>> "Jet". Yes. It's not unlike what would happen for xsd:int which
>> maps to int in Java, i.e., you'd always get 0...
>> > This means that the whole usage of the choice in this case is
>> invalid, I'm not sure how that follows...
>> > and you will never be able to use EMF to generate XML for the
>> choice > where only the engineName is set.
>> Did you try that case or are you making an assumption?
>
>
> Sorry, I was having one of those Friday afternoon moments, the XML
> generated out of it will indeed be correct. The problem is with
> making a call to getEngineType(), it will always return a value and
> never null.
>
> Ed Merks wrote on Fri, 03 December 2010 17:53
>> > I have had a look at the generated code and the Impl sets:
>> >
>> > protected static final EngineTypeType ENGINE_TYPE_EDEFAULT = >
>> EngineTypeType.JET;
>> > protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;
>> >
>> > I'm assuming that it should really be:
>> >
>> > protected static final EngineTypeType ENGINE_TYPE_EDEFAULT = null;
>> > protected EngineTypeType engineType = ENGINE_TYPE_EDEFAULT;
>> >
>> > If enumerations are not anonymous then this case does not arrise.
>> EMF itself hasn't a clue about anonymous verses named, so I'm not
>> sure how you arrived at that conclusion.
>
>
> I took a look through the generated code for the cases of enums verses
> anonymous enums. (The first code is from an anonymous case, the second
> is from the named enumeration case)
>
> Ed Merks wrote on Fri, 03 December 2010 17:53
>> > I'm using EMF 2.4.2
>> That's pretty old.
>
>
> Unfortunately we have to use this version due to other dependancies in
> our runtime system.
>
> Ed Merks wrote on Fri, 03 December 2010 17:53
>> Is the EMF attribute that's produced unsettable? I.e., is there a
>> generated isSetEngineType() method? I'd expect there to be one.
>
>
> Yes, the isSet method is being generated.
>
> I was just wondering why the behaviour is different between anonymous
> and names enums.
>
> For a named enum referenced in a complextype getEngineType() will
> return null, however for an anounymous enum it will always default to
> the first in the list if it is unset.
>
> I understand that recommended usage is to first call the isSet method,
> but unfortunately I'm in an environment that I can use that (Long story)
>
> Thanks
>
> Rob
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Anonymous Enumerations Always Get Default Value [message #643555 is a reply to message #643395] Tue, 07 December 2010 11:59 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Thanks Ed, I think I'm going a little crazy. I have now tried the following:

	<xsd:element name="TopEnumElement" type="tns:TopEnumType"/>
	<xsd:simpleType name="TopEnumType">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="One"/>
			<xsd:enumeration value="Two"/>
			<xsd:enumeration value="Three"/>
		</xsd:restriction>
	</xsd:simpleType>


Which means I can write the code:

	DocumentRoot createDocumentRoot = EnumerationsFactory.eINSTANCE.createDocumentRoot();
	TopEnumType topEnumElement = createDocumentRoot.getTopEnumElement();


In this instance the topEnumElement will default to "One". However I can not see a way to test if it has actually been set, or just defaulted to that?

Any help would be greatly appreciated.

Thanks, Rob
Re: Anonymous Enumerations Always Get Default Value [message #643644 is a reply to message #643555] Tue, 07 December 2010 17:57 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Rob,

You'll have to paw through the feature map (getMixed()) to check if the
feature map entry is actually present. FeatureMap.isSet is useful.


Rob wrote:
> Thanks Ed, I think I'm going a little crazy. I have now tried the
> following:
>
> <xsd:element name="TopEnumElement" type="tns:TopEnumType"/>
> <xsd:simpleType name="TopEnumType">
> <xsd:restriction base="xsd:string">
> <xsd:enumeration value="One"/>
> <xsd:enumeration value="Two"/>
> <xsd:enumeration value="Three"/>
> </xsd:restriction>
> </xsd:simpleType>
>
>
> Which means I can write the code:
>
> DocumentRoot createDocumentRoot =
> EnumerationsFactory.eINSTANCE.createDocumentRoot();
> TopEnumType topEnumElement = createDocumentRoot.getTopEnumElement();
>
>
> In this instance the topEnumElement will default to "One". However I
> can not see a way to test if it has actually been set, or just
> defaulted to that?
>
> Any help would be greatly appreciated.
>
> Thanks, Rob


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Default Values of simpleContent
Next Topic:[Teneo] Illegal attempt to associate a collection with two open sessions
Goto Forum:
  


Current Time: Fri Apr 26 05:33:37 GMT 2024

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

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

Back to the top