Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Is it possible to define and use "enum" in the ".dmodel" file?(xtext enum)
Is it possible to define and use "enum" in the ".dmodel" file? [message #1037094] Tue, 09 April 2013 06:58 Go to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
I am following the "domain-model" tutorial.I create the ".dmodel" file in the run-time eclipse. Is it possible to define and use an "enum" in the ".dmodel" file? For example, I would like to define an "enum" type of "sex", which can be either "female" or "male". Thank you in advance!
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037103 is a reply to message #1037094] Tue, 09 April 2013 07:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

i am not sure if you mean enum on xtext side or on jvm/java side.

for enums in xtext see docs.
for enums on jvm/xbase/javaside call toEnumerationType in the inferrer from whatever you have in the model.
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037116 is a reply to message #1037103] Tue, 09 April 2013 07:29 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you!
I would like to have "enum "in the ".dmodel" file (jvm/xbase/javaside ?). For example, I would like to have the following definition in the ".dmodel" file.

enum sex
{
female
male
}



Christian Dietrich wrote on Tue, 09 April 2013 03:11
Hi,

i am not sure if you mean enum on xtext side or on jvm/java side.

for enums in xtext see docs.
for enums on jvm/xbase/javaside call toEnumerationType in the inferrer from whatever you have in the model.

Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037126 is a reply to message #1037103] Tue, 09 April 2013 07:44 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Hi

I saw this link http://www.eclipse.org/forums/index.php/m/947690/?srch=toEnumerationType#msg_947690.
Should I define a speicial function for "EnumDef" or I only can have one def dispatch void infer() in the inferrer? For me, I would like to have the keyword of "enum" and then I can define many "enum" datatypes in the ".dmodel" file, just like the "int, double, String, etc". Thank you in advance!

def dispatch void infer(EnumDef enumDef, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
val enumName = enumDef.fullyQualifiedName.toString
acceptor.accept(enumDef.toEnumerationType(enumName) [
]).initializeLater [
documentation = enumDef.documentation
val intTypeRef = enumDef.newTypeRef(typeof(int))

for (const1 : enumDef.enumConsts) {
members += const1.toEnumerationLiteral(const1.name)
}

members += enumDef.toMethod("getValue", intTypeRef) [
body = [append('''
switch (this) {
«FOR const1 : enumDef.enumConsts»
case «const1.name»: return «const1.id»;
«ENDFOR»
default: throw new IllegalArgumentException("Constant " + name() + " is not an «enumName»");
}
''')]
]
]
}

Christian Dietrich wrote on Tue, 09 April 2013 03:11
Hi,

i am not sure if you mean enum on xtext side or on jvm/java side.

for enums in xtext see docs.
for enums on jvm/xbase/javaside call toEnumerationType in the inferrer from whatever you have in the model.

Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037144 is a reply to message #1037126] Tue, 09 April 2013 08:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

it shoub be 1:1 the same as if your infer a class, method, field etc
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037193 is a reply to message #1037144] Tue, 09 April 2013 09:12 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you very much for your so much help!
Currently, I have the "domain-model" example. In the "DomainmodelJvmModelInferrer.xtend" file, I have the definition of "DomainmodelJvmModelInferrer ". The contents are as follows. Could you show me how to change it in order to have the "enum" keyword in the ".dmodel" file? Sorry, I am really a very very bbeginner.


class DomainmodelJvmModelInferrer extends AbstractModelInferrer {

@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject XbaseCompiler xbaseCompiler

def dispatch infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
acceptor.accept(
entity.toClass( entity.fullyQualifiedName )
).initializeLater [
documentation = entity.documentation
if (entity.superType != null)
superTypes += entity.superType.cloneWithProxies
val procedure = entity.newTypeRef(typeof(Procedure1), it.newTypeRef())
members += entity.toConstructor() []
members += entity.toConstructor() [
parameters += entity.toParameter("initializer", procedure)
body = [it.append("initializer.apply(this);")]
]
val fields = <JvmField>newArrayList()
for ( f : entity.features ) {
switch f {

Property : {
val field = f.toField(f.name, f.type)
fields += field
members += field
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)
}

Operation : {
members += f.toMethod(f.name, f.type) [
documentation = f.documentation
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
//body = f.body
body = [
for (oneExp : f.body){

xbaseCompiler.compile(oneExp,it,f.type)
}
]
]
}
}
}
members += entity.toToStringMethod(it)
]
}

}


Christian Dietrich wrote on Tue, 09 April 2013 04:03
Hi,

it shoub be 1:1 the same as if your infer a class, method, field etc

Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037208 is a reply to message #1037193] Tue, 09 April 2013 09:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
def dispatch infer(MyEnumThing stuff, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
acceptor.accept(stuff.toEnumerationType(.....)).inititalizeLater[
....
]
}
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037243 is a reply to message #1037208] Tue, 09 April 2013 10:29 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you!
But I cannot write the "Domainmodel.xtext" and the "DomainmodelJvmModelInferrer.xtend" files corretly. Currently, part of the "Domainmodel.xtext" file are as follows:

DomainModel:
elements+=AbstractElement*;

AbstractElement:
PackageDeclaration | Entity | Import | MyEnum;

MyEnum:
'myEnum' name=ValidID '{'
enumConsts += ValidID*
'}'
;

Correspondingly, I have to write the "DomainmodelJvmModelInferrer.xtend" file. Currently, this file is as follows. I really do not know how to write them correctly, especially the ".xtext" part.

class DomainmodelJvmModelInferrer extends AbstractModelInferrer {

@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject XbaseCompiler xbaseCompiler

def dispatch infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
acceptor.accept(
entity.toClass( entity.fullyQualifiedName )
).initializeLater [
documentation = entity.documentation
if (entity.superType != null)
superTypes += entity.superType.cloneWithProxies
val procedure = entity.newTypeRef(typeof(Procedure1), it.newTypeRef())
members += entity.toConstructor() []
members += entity.toConstructor() [
parameters += entity.toParameter("initializer", procedure)
body = [it.append("initializer.apply(this);")]
]
val fields = <JvmField>newArrayList()
for ( f : entity.features ) {
switch f {

Property : {
val field = f.toField(f.name, f.type)
fields += field
members += field
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)
}

Operation : {
members += f.toMethod(f.name, f.type) [
documentation = f.documentation
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
//body = f.body
body = [
for (oneExp : f.body){

xbaseCompiler.compile(oneExp,it,f.type)
}
]
]
}
}
}
members += entity.toToStringMethod(it)
]
}


def dispatch infer(MyEnum myenum, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
val enumName = myenum.fullyQualifiedName.toString
acceptor.accept(myenum.toEnumerationType(enumName) [
]).initializeLater [

documentation = myenum.documentation
val intTypeRef = myenum.newTypeRef(typeof(int))

for (const1 : myenum.enumConsts) {
members += const1.toEnumerationLiteral(const1.name)
}

members += myenum.toMethod("getValue", intTypeRef) [
body = [append('''
switch (this) {
«FOR const1 : myenum.enumConsts»
case «const1.name»: return «const1.id»;
«ENDFOR»
default: throw new IllegalArgumentException("Constant " + name() + " is not an «enumName»");
}
''')]
]
]
}

}
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037265 is a reply to message #1037243] Tue, 09 April 2013 10:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

have a look at your metamodel. if your literals are no objects but simple string you have to do it different e.g.

for (const1 : myenum.enumConsts) {
members += myenum.toEnumerationLiteral(const1)
}
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037273 is a reply to message #1037265] Tue, 09 April 2013 11:11 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you very much!
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037999 is a reply to message #1037265] Wed, 10 April 2013 09:11 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
In the .dmodel file, for example, I defined "myEnum Gender", and then in the "entity Father ", I would like to initialize it, as in the "op initialization()". The error information is "Couldn't resolve reference to JvmIdentifiableElement 'Gender'". Could you know how to fix it? Thank you in advance!

myEnum Gender
{
female
male
}

entity Father {
id: String
name: String
gender:Gender

op initialization(): void{
name = "TestFather";
gender = Gender.male;
}


Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1038117 is a reply to message #1037999] Wed, 10 April 2013 12:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

(1) make sure Entity and Enum are inferred to the same package
(2) i guess it is Gender::male
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1038122 is a reply to message #1038117] Wed, 10 April 2013 12:34 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
The content of Gender.java is as follows:

package my.model;

public enum Gender {
female,

male
}


Part of the Father.java is as follows:

package my.model;

import java.util.Iterator;
import java.util.List;
import my.model.Daughter;
import my.model.Gender;
import my.model.Mother;
import my.model.Son;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.util.ToStringHelper;

public class Father {
public Father() {
}

public Father(final Procedure1<Father> initializer) {
initializer.apply(this);
}
......

Does this mean they are in the same package? Thank you!


Christian Dietrich wrote on Wed, 10 April 2013 08:28
Hi,

(1) make sure Entity and Enum are inferred to the same package
(2) i guess it is Gender::male

Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1038125 is a reply to message #1038122] Wed, 10 April 2013 12:36 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
I changed to gender = Gender::male;
The error information is "Static access to instance member male".
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038132 is a reply to message #1038122] Wed, 10 April 2013 12:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Yes

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038133 is a reply to message #1038125] Wed, 10 April 2013 12:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Will have a look in the evening

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038193 is a reply to message #1038133] Wed, 10 April 2013 14:26 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you very very much!

Christian Dietrich wrote on Wed, 10 April 2013 08:47
Will have a look in the evening

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de

Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038297 is a reply to message #1038193] Wed, 10 April 2013 17:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

this is a bug. please file a bugzilla. as a workaround
def dispatch infer(MyEnum e, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
	acceptor.accept(
			e.toEnumerationType( e.fullyQualifiedName.toString,[] )
		).initializeLater [
			for ( x : e.enumConsts) {
				val lit = e.toEnumerationLiteral(x)
				lit.static = true
				members+=lit
			}
		]
}

package test {
	
myEnum
Gender {
	A B
}


entity name {
	op xxxx() : Gender {
		Gender::A
	}
}
}
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038339 is a reply to message #1038297] Wed, 10 April 2013 19:00 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you very much again!
But what is "file a bugzilla. as a workaround"? Could you show me some example?
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038346 is a reply to message #1038339] Wed, 10 April 2013 19:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

since it is a bug please file a ticket into eclipse bugzilla https://bugs.eclipse.org/bugs/enter_bug.cgi?product=TMF&component=Xtext
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1038392 is a reply to message #1038346] Wed, 10 April 2013 20:31 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
I submitted the bug report just now. Thank you!

Christian Dietrich wrote on Wed, 10 April 2013 15:12
Hi,

since it is a bug please file a ticket into eclipse bugzilla https://bugs.eclipse.org/bugs/enter_bug.cgi?product=TMF&component=Xtext

Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1039431 is a reply to message #1038297] Fri, 12 April 2013 06:50 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
I copy the following code in the "class DomainmodelJvmModelInferrer extends AbstractModelInferrer". In the line of "lit.static = true", the error information is "This expression is not allowed in this context, since it doesn't cause any side effects.". Should I import some package?

Christian Dietrich wrote on Wed, 10 April 2013 13:40
Hi,

this is a bug. please file a bugzilla. as a workaround
def dispatch infer(MyEnum e, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
	acceptor.accept(
			e.toEnumerationType( e.fullyQualifiedName.toString,[] )
		).initializeLater [
			for ( x : e.enumConsts) {
				val lit = e.toEnumerationLiteral(x)
				lit.static = true
				members+=lit
			}
		]
}

package test {
	
myEnum
Gender {
	A B
}


entity name {
	op xxxx() : Gender {
		Gender::A
	}
}
}

Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1039443 is a reply to message #1039431] Fri, 12 April 2013 07:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

which Xtext version do you use? i cannot reproduce that use 2.4.0

~Christian
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1039444 is a reply to message #1039443] Fri, 12 April 2013 07:15 Go to previous messageGo to next message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
Thank you!
I am using the one "eclipse-SDK-4.2-Xtext-2.3.1-win32-x86_64.zip". Previously, I tried the xText 2.4.0, but some problems apprear. Should I change my xText to 2.4.0 now?
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1039458 is a reply to message #1039444] Fri, 12 April 2013 07:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
It is the current release so feel free to do so.
Re: Is it possible to define and use &quot;enum&quot; in the &quot;.dmodel&quot; fil [message #1039605 is a reply to message #1039458] Fri, 12 April 2013 10:50 Go to previous message
Hao Zhang is currently offline Hao Zhang
Messages: 107
Registered: April 2013
Senior Member
It does work in the xText 2.4.0.

Christian Dietrich wrote on Fri, 12 April 2013 03:34
It is the current release so feel free to do so.

Previous Topic:Different generated file directory organization for plugin project and for normal java project?
Next Topic:Highlight certain parts in comments
Goto Forum:
  


Current Time: Thu Oct 23 16:50:05 GMT 2014

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

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