Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Inferrer Enumeration type creation(Inferrer Enumeration type creation)
Inferrer Enumeration type creation [message #731855] Mon, 03 October 2011 10:33 Go to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

Can someone please post me an enumeration type creation sample - from within the Inferrer code - , how to setup the type itself and the literals?

Thank you,
Balázs
Re: Inferrer Enumeration type creation [message #731869 is a reply to message #731855] Mon, 03 October 2011 10:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

this should be straight forward. so what is the actual problem you have?
dont forget to adopt IQualifiedNameProvider too

def dispatch Iterable<JvmDeclaredType> transform(Entity entity) {
		val jvmClass = typesFactory.createJvmGenericType 
		jvmClass.simpleName = entity.name
		jvmClass.packageName = entity.packageName
		entity.associatePrimary(jvmClass)
		jvmClass.visibility = JvmVisibility::PUBLIC
		if (entity.superType != null)
			jvmClass.superTypes += cloneWithProxies(entity.superType)
		for(f : entity.features) {
			transform(f, jvmClass)
		} 
		val enum1 = typesFactory.createJvmEnumerationType
		enum1.simpleName = entity.name + "Type"
		val l1 = typesFactory.createJvmEnumerationLiteral
		l1.simpleName = "Deutschland"
		l1.visibility = JvmVisibility::PUBLIC
		l1.^static = true
		var t1 = typesFactory.createJvmParameterizedTypeReference
		t1.type = enum1
		l1.type = t1
		val l2 = typesFactory.createJvmEnumerationLiteral
		l2.simpleName = "Frankreich"
		l2.visibility = JvmVisibility::PUBLIC
		l2.^static = true
		var t2 = typesFactory.createJvmParameterizedTypeReference
		t2.type = enum1
		l2.type = t2
		enum1.members += l1
		enum1.members += l2
		entity.associatePrimary(enum1)
		newArrayList(jvmClass as JvmDeclaredType, enum1 as JvmDeclaredType) 	 
	}


note that you (may) have to add some other stuff (operations/constructors...) to the enum

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 03 October 2011 12:35]

Report message to a moderator

Re: Inferrer Enumeration type creation [message #731912 is a reply to message #731869] Mon, 03 October 2011 13:21 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

Thanks. I have a problem using such enums in Xbase logical expression. When testing for equality '==', I get this error:
Quote:

Couldn't resolve reference to JvmIdentifiableElement '=='.



Re: Inferrer Enumeration type creation [message #731913 is a reply to message #731912] Mon, 03 October 2011 13:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi this is correct since the == operation is not defined vom Enums
you have to make your enum be a subclass e.g. of java lang object.
normals enums have a superclasses

[java.lang.Enum, java.lang.Object, java.lang.Comparable, java.io.Serializable]

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 03 October 2011 13:37]

Report message to a moderator

Re: Inferrer Enumeration type creation [message #731918 is a reply to message #731913] Mon, 03 October 2011 13:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
So add something like

val enumTypeReference = typesFactory.createJvmParameterizedTypeReference
enumTypeReference.type = typeProviderFactory.findOrCreateTypeProvider(entity.eResource.resourceSet)
.findTypeByName("java.lang.Enum")
enum1.superTypes +=enumTypeReference

and it will work


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inferrer Enumeration type creation [message #731931 is a reply to message #731918] Mon, 03 October 2011 14:02 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Great Smile
thanks.
Re: Inferrer Enumeration type creation [message #731958 is a reply to message #731912] Mon, 03 October 2011 14:46 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Balazs,<br>
<br>
I was working on such a thing just yesterday for Xcore:<br>
<br>
<blockquote><small>  public JvmDeclaredType getDeclaredType(GenEnum
genEnum)</small><br>
<small>  {</small><br>
<small>    final XEnum xEnum = mapper.getXEnum(genEnum);</small><br>
<small>        final XDataTypeMapping mapping =
mapper.getMapping(xEnum);</small><br>
<small>    JvmDeclaredType jvmDeclaredType;</small><br>
<small>        if (genEnum.getGenModel().useGenerics())</small><br>
<small>        {</small><br>
<small>            JvmEnumerationType jvmEnumerationType =
TypesFactory.eINSTANCE.createJvmEnumerationType();</small><br>
<small>            jvmEnumerationType.setFinal(true);</small><br>
<small>            jvmDeclaredType = jvmEnumerationType;</small><br>
<small>            for (GenEnumLiteral genEnumLiteral :
genEnum.getGenEnumLiterals())</small><br>
<small>            {</small><br>
<small>                XEnumLiteral xEnumLiteral =
mapper.getXEnumLiteral(genEnumLiteral);</small><br>
<small>                JvmEnumerationLiteral jvmEnumerationLiteral
= TypesFactory.eINSTANCE.createJvmEnumerationLiteral();</small><br>
<small>                jvmEnumerationLiteral.setStatic(true);</small><br>
<small>                jvmEnumerationLiteral.setFinal(true);</small><br>
<small>               
jvmEnumerationLiteral.setSimpleName(genEnumLiteral.getEnumLiteralInstanceConstantName());</small><br>
<small>       
jvmEnumerationLiteral.setVisibility(JvmVisibility.PUBLIC);</small><br>
<small>        JvmParameterizedTypeReference
jvmParameterizedTypeReference =
TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();</small><br>
<small>       
jvmParameterizedTypeReference.setType(jvmDeclaredType);</small><br>
<small>       
jvmEnumerationLiteral.setType(jvmParameterizedTypeReference);</small><br>
<small>               
jvmEnumerationType.getMembers().add(jvmEnumerationLiteral);</small><br>
<small>         
mapper.getToXcoreMapping(jvmEnumerationLiteral).setXcoreElement(xEnumLiteral);</small><br>
<small>            }</small><br>
<small>        }</small><br>
<small>        else</small><br>
<small>        {</small><br>
<small>      JvmGenericType jvmGenericType =
TypesFactory.eINSTANCE.createJvmGenericType();</small><br>
<small>            jvmDeclaredType = jvmGenericType;</small><br>
<small>            // TODO</small><br>
<small>      return jvmGenericType;</small><br>
<small>        }</small><br>
<small>        jvmDeclaredType.setSimpleName(genEnum.getName());</small><br>
<small>   
jvmDeclaredType.setPackageName(genEnum.getGenPackage().getInterfacePackageName());</small><br>
<small>    jvmDeclaredType.setVisibility(JvmVisibility.PUBLIC);</small><br>
<small>    JvmParameterizedTypeReference
jvmParameterizedTypeReference =
TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();</small><br>
<small>    jvmParameterizedTypeReference.setType(jvmDeclaredType);</small><br>
<small>    mapping.setDataType(jvmParameterizedTypeReference);</small><br>
<small>    mapping.setEDataType(genEnum.getEcoreEnum());</small><br>
<small>     
mapper.getToXcoreMapping(jvmDeclaredType).setXcoreElement(xEnum);</small><br>
<small>    return jvmDeclaredType;</small><br>
<small>  }</small><br>
</blockquote>
The problem you describe though sounds more like an issue with
org.eclipse.xtext.xbase.lib not being on the classpath or the
XtextResourceSet not being configured with classpath information...<br>
<br>
<br>
On 03/10/2011 6:21 AM, Balazs Molnar wrote:
<blockquote cite="mid:j6cc67$s7h$1@news.eclipse.org" type="cite">Hello,
<br>
<br>
Thanks. I have a problem using such enums in Xbase logical
expression. When testing for equality '==', I get this error:
<br>
Quote:
<br>
<blockquote type="cite">Couldn't resolve reference to
JvmIdentifiableElement '=='.
<br>
</blockquote>
<br>
<br>
<br>
<br>
</blockquote>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Inferrer Enumeration type creation [message #731959 is a reply to message #731913] Mon, 03 October 2011 14:47 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
I thought == works on all java.lang.Objects...

On 03/10/2011 6:22 AM, Christian Dietrich wrote:
> Hi this is correct since the == operation is not defined vom Enums
>
> ~Christian


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Inferrer Enumeration type creation [message #731968 is a reply to message #731959] Mon, 03 October 2011 15:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Yes, but java.lang.Object has to get (directly or indirectly) a supertype of the JvmEnumerationType. Otherwise org.eclipse.xtext.xbase.scoping.featurecalls.StaticMethodsFeatureForTypeProvider.getVisibleTypesContainingStaticMethods(JvmTypeReference) would not find anything.

~Christianb


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inferrer Enumeration type creation [message #732108 is a reply to message #731968] Mon, 03 October 2011 22:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Christian,

You're right! I had to fix that in Xcore to get == to work for
enumerations. It looks like I need to be careful that java.lang.Object
is always explicitly in the hierarchy (which is a bit surprising)...


On 03/10/2011 8:04 AM, Christian Dietrich wrote:
> Yes, but java.lang.Object has to get (directly or indirectly) a
> supertype of the JvmEnumerationType. Otherwise
> org.eclipse.xtext.xbase.scoping.featurecalls.StaticMethodsFeatureForTypeProvider.getVisibleTypesContainingStaticMethods(JvmTypeReference)
> would not find anything.
>
> ~Christianb


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Inferrer Enumeration type creation [message #733332 is a reply to message #732108] Tue, 04 October 2011 15:34 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

I had the problem of adding org.eclipse.xtext.xbase.lib already fixed in an earlier post following Christian's advise Smile

regards,
Balázs
Re: Inferrer Enumeration type creation [message #733392 is a reply to message #733332] Tue, 04 October 2011 15:46 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Balázs,

I had to fix a problem with adding java.lang.Object as a base class for
root classes also following Christian's advice. He's very helpful!


On 04/10/2011 8:34 AM, Balazs Molnar wrote:
> Hello,
>
> I had the problem of adding org.eclipse.xtext.xbase.lib already fixed
> in an earlier post following Christian's advise :)
>
> regards,
> Balázs


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Reference a feature by another name
Next Topic:Xtext hangs on multiple parentheses
Goto Forum:
  


Current Time: Fri Apr 19 20:40:08 GMT 2024

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

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

Back to the top