Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generate Xtext grammar from Ecore file without the project wizard
Generate Xtext grammar from Ecore file without the project wizard [message #1859464] Fri, 09 June 2023 10:52 Go to next message
Kaan B. Y. is currently offline Kaan B. Y.Friend
Messages: 15
Registered: June 2023
Junior Member
Hello,

I have an Ecore metamodel I would like to use for automatically generating code. For this, I have written Xtend functions to process model members and output code; effectively I am using Xtext as a bridge between Ecore and Xtend.

I was wondering if it there is a way to generate a Xtext grammar from an Ecore file without using the "New Xtext Project From Ecore" wizard. Right now, every time I change my Ecore model, I have to generate a dummy project, copy over the (new) generated Xtext file over to the Xtext project with my Xtend functions and delete the dummy project. It would be especially practical if there was a way to automatically re-generate the Xtext grammar when the underlying Ecore model is changed.

Thanks in advance.

Regards
K.
Re: Generate Xtext grammar from Ecore file without the project wizard [message #1859473 is a reply to message #1859464] Fri, 09 June 2023 16:06 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 162
Registered: February 2016
Senior Member
Hi Kaan,

you can take a look at the presentation of Karsten Thoms (itemis AG): Recipes to build Code Generators for Non-Xtext Models with Xtend:
https://youtu.be/teOULtQ81-U

Hope that helps,
Tamás
Re: Generate Xtext grammar from Ecore file without the project wizard [message #1859478 is a reply to message #1859473] Fri, 09 June 2023 17:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14723
Registered: July 2009
Senior Member
Can you please give some more details what you are exactly doing with Xtext
Besides that you should be able to look into the wizards code
And call something similar programmatically


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Generate Xtext grammar from Ecore file without the project wizard [message #1859482 is a reply to message #1859478] Fri, 09 June 2023 19:46 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7677
Registered: July 2009
Senior Member
An Xtext grammar is much richer than an Ecore metamodel; it provides all sorts of syntax options that may be pleasing to a user.

(Conversely, in a small number of areas, an Ecore metamodel is richer than Xtext can synthesize.)

Using Ecore to Xtext you must be using some rather formulaic syntax that is no doubt adequate and predictable but not pleasant. If you want a formulaic syntax that is better than XML you might care to look at HUTN that is supported as part of the Eclipse Epsilon project.

Personally, I recommend maintaining both Xtext grammar and Ecore metamodel as primary source artefacts. The Ecore to Xtext generator, when it works, is deceptive; it seems to generate something but it is not pleasing and you find yourself improving it all at once and it falls apart. I prefer to steadily grow the Xtext grammar to cover more and more of the Ecore metamodel. (I also like to use an Xtext 2 LPG converter, available as part of the OCL project, to help ensure that my grammar is free of ambiguities.)

Regards

Ed Willink
Re: Generate Xtext grammar from Ecore file without the project wizard [message #1859557 is a reply to message #1859473] Thu, 15 June 2023 08:23 Go to previous messageGo to next message
Kaan B. Y. is currently offline Kaan B. Y.Friend
Messages: 15
Registered: June 2023
Junior Member
Hello Tamás,

the title of the presentation describes my use case perfectly: I am trying to create a code generator for a non-Xtext Ecore metamodel with Xtend. Thanks for sharing. That should help me out.

Regards
K.
Re: Generate Xtext grammar from Ecore file without the project wizard [message #1859576 is a reply to message #1859478] Fri, 16 June 2023 11:03 Go to previous messageGo to next message
Kaan B. Y. is currently offline Kaan B. Y.Friend
Messages: 15
Registered: June 2023
Junior Member
Hello,

I have a metamodel created in EMF. I would like to generate code using models of this metamodel. The metamodel contains Rust templates, for example there is an EClass named ConstructorImplementation that contains an EList of Fields. My code generator is supposed to "fill out" Xtend templates with the relevant values from the model (e.g. name of a Field) and then write the generated code in a Rust file.

At least what I've understood from the video that Tamas, is that no Xtext grammar is required to write Xtend methods, if the Ecore model already exists on its own. I deleted the automatically generated Xtext file (I used the project wizard "New Xtext Project from Existing Ecore Model...") and the Xtend file was still valid, as the types were being imported from the respective EMF project, and not from the Ecore model implied through the Xtext grammar (which, in return, was generated using the Rust template metamodel).

Now I am not sure how am I supposed to approach this. Can I just load some serialized model file and invoke the root generate method in my Xtend code generation class?

Is Xtend even meant for such an use case?

Thanks in advance.

Regards
K.

[Updated on: Fri, 16 June 2023 11:07]

Report message to a moderator

Re: Generate Xtext grammar from Ecore file without the project wizard [message #1859580 is a reply to message #1859576] Fri, 16 June 2023 15:09 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14723
Registered: July 2009
Senior Member
i still can follow you

- if you use an external ecore in xtext you also have to have a genmodel and the javaclasses that the genmodel produces.
- no xtext involved so far
- you can use these java classes for the eclasses in your xtend code.

now you do something with xtext, what is the something?

if you have use ecore based model instances with emf means e.g. from xmi or xml,
you dont need xtext to serialize and parse the model?

is this what you are asking for?

who produces your model instance?

xtend is just "java" with ability to write template expressions
=> all you can do in java with emf you can do with xtend with emf.

https://www.vogella.com/tutorials/EclipseEMF/article.html


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Fri, 16 June 2023 15:10]

Report message to a moderator

Previous Topic:xtext - Eclipse Application launch issue
Next Topic:Encountering EProxies after building projects in new workspace
Goto Forum:
  


Current Time: Sun Oct 13 01:38:48 GMT 2024

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

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

Back to the top