Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [TEXO] Enum values
[TEXO] Enum values [message #870678] Wed, 09 May 2012 06:19 Go to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi,

I have an enum defined in my XSD and want to automatically create the RuleType.java enum so that we get something closer to :
[size=2]public enum RuleType {
1,
2;
}[/size]


The XSD will contain many enumerations. I would prefer to set the default behaviour once for the whole model rather than adding an annotaion to each enum. Any ideas on how this can be implemented please?

The XSD:
<xs:simpleType name="RuleType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="1">
	<xs:annotation>
	  <xs:documentation>Aggregation</xs:documentation>
	</xs:annotation>
     </xs:enumeration>
     <xs:enumeration value="2">
	<xs:annotation>
	  <xs:documentation>Flash</xs:documentation>
	</xs:annotation>
     </xs:enumeration>
  </xs:restriction>
</xs:simpleType>[/size]


The system working with the generated Java model, is looking to use RuleType.1 etc.

The currently generated Java class:
public enum RuleType
{

	/**
	 * The enum: _1 <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	_1(0, "_1", "1")
	{

		/**
		 * @return always true for this instance
		 * @generated
		 */
		@Override
		public boolean is_1()
		{
			return true;
		}
	},
	/**
	 * The enum: _2 <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	_2(1, "_2", "2")
	{

		/**
		 * @return always true for this instance
		 * @generated
		 */
		@Override
		public boolean is_2()
		{
			return true;
		}
	};[/size]


Regards,
Gary
Re: [TEXO] Enum values [message #870689 is a reply to message #870678] Wed, 09 May 2012 07:06 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5329
Registered: July 2009
Senior Member
Hi Gary,
Afaik 1 or 2 invalid java identifiers, I tried your RuleType with 1 and 2, but Eclipse doesn't like it. Can you
post/attach/send a java file which compiles correctly in your ide?

gr. Martin

On 05/09/2012 12:19 PM, Gary Godfrey wrote:
> Hi,
>
> I have an enum defined in my XSD and want to automatically create the RuleType.java enum so that we get something closer
> to :
>
> public enum RuleType {
> 1,
> 2;
> }

>
>
> The XSD will contain many enumerations. I would prefer to set the default behaviour once for the whole model rather than
> adding an annotaion to each enum. Any ideas on how this can be implemented please?
>
> The XSD:
>
> <xs:simpleType name="RuleType">
> <xs:restriction base="xs:string">
> <xs:enumeration value="1">
> <xs:annotation>
> <xs:documentation>Aggregation</xs:documentation>
> </xs:annotation>
> </xs:enumeration>
> <xs:enumeration value="2">
> <xs:annotation>
> <xs:documentation>Flash</xs:documentation>
> </xs:annotation>
> </xs:enumeration>
> </xs:restriction>
> </xs:simpleType>[/size]
>
>
> The system working with the generated Java model, is looking to use RuleType.1 etc.
>
> The currently generated Java class:
>
> public enum RuleType
> {
>
> /**
> * The enum: _1 <!-- begin-user-doc --> <!-- end-user-doc -->
> * * @generated
> */
> _1(0, "_1", "1")
> {
>
> /**
> * @return always true for this instance
> * @generated
> */
> @Override
> public boolean is_1()
> {
> return true;
> }
> },
> /**
> * The enum: _2 <!-- begin-user-doc --> <!-- end-user-doc -->
> * * @generated
> */
> _2(1, "_2", "2")
> {
>
> /**
> * @return always true for this instance
> * @generated
> */
> @Override
> public boolean is_2()
> {
> return true;
> }
> };[/size]
>
>
> Regards,
> Gary
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [TEXO] Enum values [message #870709 is a reply to message #870689] Wed, 09 May 2012 08:23 Go to previous messageGo to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi Martin,

Thanks for your reply. Turns out the valid enum I had been given to match, was in fact junk !!

I have now altered the XSD restrictions to be non-Integer String values. The Enum values match the restrictions as expected.

Sorry to waste your time.

Regards,
Gary
Re: [TEXO] Enum values [message #871638 is a reply to message #870709] Mon, 14 May 2012 11:38 Go to previous messageGo to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi Martin,

I now been supplied with a valid XSD enum which needs to be mapped to a Java enum:
	<xs:simpleType name="CreditRating">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Excellent"/>
			<xs:enumeration value="Good"/>
			<xs:enumeration value="Fair"/>
			<xs:enumeration value="Poor"/>
			<xs:enumeration value="Doh!"/>
		</xs:restriction>
	</xs:simpleType>


This creates:
public enum CreditRating
{
	/**
	 * The enum: EXCELLENT <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	EXCELLENT(0, "Excellent", "Excellent")
	{

		/**
		 * @return always true for this instance
		 * @generated
		 */
		@Override
		public boolean isExcellent()
		{
			return true;
		}
	},
	/**
	 * The enum: GOOD <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	GOOD(1, "Good", "Good")
	{

		/**
		 * @return always true for this instance
		 * @generated
		 */
		@Override
		public boolean isGood()
		{
			return true;
		}
	},


Is it possible to generate a simple Enum as follows please?

public enum CreditRating
{
	EXCELLENT,
	GOOD,
	FAIR,
	POOR,
	BAD;
}


Regards,
Gary
Re: [TEXO] Enum values [message #871641 is a reply to message #871638] Mon, 14 May 2012 11:43 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5329
Registered: July 2009
Senior Member
Hi Gary,
It is possible with your own template, that's quite doable. See this wiki page:
http://wiki.eclipse.org/Texo/Template_Overriding

and use this template as a base (just get rid of most of its content):
http://git.eclipse.org/c/texo/org.eclipse.emf.texo.git/tree/generator/org.eclipse.emf.texo.modelgenerator/src/org/eclipse/emf/texo/modelgenerator/templates/enum.xpt

Note that I am not sure why the current enum does not work for you, the result is quite similar to what you want. Can
you let me know why the current Texo enum does not work for you?

gr. Martin

On 05/14/2012 05:38 PM, Gary Godfrey wrote:
> Hi Martin,
>
> I now been supplied with a valid XSD enum which needs to be mapped to a Java enum:
>
> <xs:simpleType name="CreditRating">
> <xs:restriction base="xs:string">
> <xs:enumeration value="Excellent"/>
> <xs:enumeration value="Good"/>
> <xs:enumeration value="Fair"/>
> <xs:enumeration value="Poor"/>
> <xs:enumeration value="Doh!"/>
> </xs:restriction>
> </xs:simpleType>
>
>
> This creates:
>
> public enum CreditRating
> {
> /**
> * The enum: EXCELLENT <!-- begin-user-doc --> <!-- end-user-doc -->
> * * @generated
> */
> EXCELLENT(0, "Excellent", "Excellent")
> {
>
> /**
> * @return always true for this instance
> * @generated
> */
> @Override
> public boolean isExcellent()
> {
> return true;
> }
> },
> /**
> * The enum: GOOD <!-- begin-user-doc --> <!-- end-user-doc -->
> * * @generated
> */
> GOOD(1, "Good", "Good")
> {
>
> /**
> * @return always true for this instance
> * @generated
> */
> @Override
> public boolean isGood()
> {
> return true;
> }
> },
>
> Is it possible to generate a simple Enum as follows please?
>
>
> public enum CreditRating
> {
> EXCELLENT,
> GOOD,
> FAIR,
> POOR,
> BAD;
> }
>
>
> Regards,
> Gary


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [TEXO] Enum values [message #871665 is a reply to message #871641] Mon, 14 May 2012 12:45 Go to previous messageGo to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi Martin,

Unfortunately the generated classes are being imported into a 3rd party system. This system is reading the entries as a List rather than an Enumeration.

I have managed to override/hack a template to return what we need - thank you.

Regards,
Gary
Re: [TEXO] Enum values [message #882954 is a reply to message #871665] Thu, 07 June 2012 10:17 Go to previous messageGo to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi Martin,

In a supplied XSD, some xs:enumerations contain an underscore.
	<xs:simpleType name="TelephoneType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="TETY_1">
				<xs:annotation>
					<xs:documentation>Mobile</xs:documentation>
				</xs:annotation>
			</xs:enumeration>
			<xs:enumeration value="TETY_2">
				<xs:annotation>
					<xs:documentation>LandLine</xs:documentation>
				</xs:annotation>
			</xs:enumeration>
		</xs:restriction>
	</xs:simpleType>


The generated .ecore file removes the underscore from the name attribute. Is it possible to maintain the exact value from the XSD?

  <eClassifiers xsi:type="ecore:EEnum" name="TelephoneType">
    <eAnnotations source="org/eclipse/emf/ecore/util/ExtendedMetaData">
      <details key="name" value="TelephoneType"/>
    </eAnnotations>
    <eLiterals name="TETY1" literal="TETY_1">
      <eAnnotations source="emf/2002/GenModel">
        <details key="documentation" value="Mobile"/>
      </eAnnotations>
    </eLiterals>
    <eLiterals name="TETY2" value="1" literal="TETY_2">
      <eAnnotations source="emf/2002/GenModel">
        <details key="documentation" value="LandLine"/>
      </eAnnotations>
    </eLiterals>
  </eClassifiers>


To ensure the XSD and generated Enum match I have worked around this by altered my custom enum.xpt to use the 'literal' attribute instead of the 'name' attribute when generating the Java Enum.

public enum «this.simpleClassName»
{

«EXPAND org::eclipse::emf::texo::modelgenerator::templates::enum_addition::root(modelController) FOR this»

«FOREACH eEnum.ELiterals AS e SEPARATOR ","-»
	«LET ((EEnumLiteral)e) AS el-»
	 	/**
	 	 * The enum: «toUpperCase(el.literal)»
	     * <!-- begin-user-doc -->
	     * <!-- end-user-doc -->
	 	 * @generated
	 	 */
		«modelController.getJavaAnnotations(el, "field")-»
		 «toUpperCase(el.literal)»
		 
	«ENDLET-»
«ENDFOREACH-»;


The Java Enum has the required values.
public enum TelephoneType
{

	/**
	 * The enum: TETY_1 <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	TETY_1

	, /**
	 * The enum: TETY_2 <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	TETY_2

	;


How though can I ensure entities which set a default value use the literal rather than the name of the feature annotation?

public class Telephone {
private TelephoneType telephoneType = TelephoneType.TETY1; // COMPILE ERROR
...
}


I have looked at the entity.xpt and see it uses «featureAnnotation.defaultValue», but I cannot see where the defaultValue is determined.
private «featureAnnotation.type» «featureAnnotation.validJavaMemberName» = «featureAnnotation.defaultValue»;


Regards,
Gary
Re: [TEXO] Enum values [message #882983 is a reply to message #882954] Thu, 07 June 2012 11:22 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5329
Registered: July 2009
Senior Member
Hi Gary,
Hmm, I don't have a good solution right away... I think the best you can do is write a short java program which reads
the ecore and sets all names to be the same as the literal and then saves the ecore.

But why do the generated code and the xsd need to match? Afaik the xml which gets generated from the code should be
correct and follow the xsd, right?

gr. Martin

On 06/07/2012 04:17 PM, Gary Godfrey wrote:
> Hi Martin,
>
> In a supplied XSD, some xs:enumerations contain an underscore.
>
> <xs:simpleType name="TelephoneType">
> <xs:restriction base="xs:string">
> <xs:enumeration value="TETY_1">
> <xs:annotation>
> <xs:documentation>Mobile</xs:documentation>
> </xs:annotation>
> </xs:enumeration>
> <xs:enumeration value="TETY_2">
> <xs:annotation>
> <xs:documentation>LandLine</xs:documentation>
> </xs:annotation>
> </xs:enumeration>
> </xs:restriction>
> </xs:simpleType>
>
>
> The generated .ecore file removes the underscore from the name attribute. Is it possible to maintain the exact value
> from the XSD?
>
>
> <eClassifiers xsi:type="ecore:EEnum" name="TelephoneType">
> <eAnnotations source="org/eclipse/emf/ecore/util/ExtendedMetaData">
> <details key="name" value="TelephoneType"/>
> </eAnnotations>
> <eLiterals name="TETY1" literal="TETY_1">
> <eAnnotations source="emf/2002/GenModel">
> <details key="documentation" value="Mobile"/>
> </eAnnotations>
> </eLiterals>
> <eLiterals name="TETY2" value="1" literal="TETY_2">
> <eAnnotations source="emf/2002/GenModel">
> <details key="documentation" value="LandLine"/>
> </eAnnotations>
> </eLiterals>
> </eClassifiers>
>
>
> To ensure the XSD and generated Enum match I have worked around this by altered my custom enum.xpt to use the 'literal'
> attribute instead of the 'name' attribute when generating the Java Enum.
>
>
> public enum «this.simpleClassName»
> {
>
> «EXPAND org::eclipse::emf::texo::modelgenerator::templates::enum_addition::root(modelController) FOR this»
>
> «FOREACH eEnum.ELiterals AS e SEPARATOR ","-»
> «LET ((EEnumLiteral)e) AS el-»
> /**
> * The enum: «toUpperCase(el.literal)»
> * <!-- begin-user-doc -->
> * <!-- end-user-doc -->
> * @generated
> */
> «modelController.getJavaAnnotations(el, "field")-»
> «toUpperCase(el.literal)»
> «ENDLET-»
> «ENDFOREACH-»;
>
>
> The Java Enum has the required values.
>
> public enum TelephoneType
> {
>
> /**
> * The enum: TETY_1 <!-- begin-user-doc --> <!-- end-user-doc -->
> * * @generated
> */
> TETY_1
>
> , /**
> * The enum: TETY_2 <!-- begin-user-doc --> <!-- end-user-doc -->
> * * @generated
> */
> TETY_2
>
> ;
>
>
> How though can I ensure entities which set a default value use the literal rather than the name of the feature annotation?
>
>
> public class Telephone {
> private TelephoneType telephoneType = TelephoneType.TETY1; // COMPILE ERROR
> ...
> }
>
>
> I have looked at the entity.xpt and see it uses «featureAnnotation.defaultValue», but I cannot see where the
> defaultValue is determined.
>
> private «featureAnnotation.type» «featureAnnotation.validJavaMemberName» = «featureAnnotation.defaultValue»;
>
>
> Regards,
> Gary


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:[Teneo] Hibernate 4.1 has Trouble with Booleans
Next Topic:TENEO problems on HOT redeploy
Goto Forum:
  


Current Time: Wed Aug 20 08:44:20 EDT 2014

Powered by FUDForum. Page generated in 0.01723 seconds