Wednesday, March 31, 2010

Xtext For Your Ecore Models

The new M6 release of Xtext ships with a new component: The Ecore2Xtext Wizard.

Why should you use the
Ecore2Xtext Wizard?
  • You want your models to be in a syntax that humans can not only read but also understand.
  • You want a model editor that offers all the convenience of a modern IDE.
  • You already have an Ecore model but don't know how to start with Xtext.
  • Your Ecore model is huge and you want a quick start with Xtext. You can easily fine-tune the syntax later on.
How do you use it?
  1. Start the wizard by choosing New -> Xtext -> Xtext Project From Existing Ecore Models.
  2. Select the EMF generator models1 from your workspace for which you want a textual syntax and choose your root element's type.
  3. Fill in all the language metadata on the second page of the wizard. Remember the file extension.
  4. Click Finish and wait until Xtext has generated the two common Xtext plug-ins and the Xtext grammar for your language.
  5. Run the MWE2 workflow located in the same directory as the grammar. Now Xtext generates the language infrastructure (parser, editor, formatter etc) .
  6. Spawn a new Eclipse runtime workbench, create a sample Java Project, and open a new model file with the file extension you have chosen in the wizard. Play around and have fun with your new textual model editor.
1 the genmodel is needed because we need the fully qualified names of the generated Java classes as well as the location of the Ecore file. The genmodel offers both of them.

Watch this short screencast to see it in action:

What is the generated syntax like?

Names of EClasses and EStructuralFeatures become keywords, containment is marked with curly braces, elements in lists are separated by commas, etc... Here's an example of an entity model in the generated language:

What if it doesn't work?

The grammar is the primary artifact of every Xtext language, but there are a couple of further services you might have to configure:
  • A IQualifiedNameProvider to define how the fully qualified name of an element is derived.
  • A IScopeProvider to define which elements are candidates for a cross reference.
  • ...
Please consult the Xtext documentation for more information.

Another geekish meta-confusing example

All right geeks, Ecore itself is defined in Ecore, so let's generate a textual syntax for Ecore and see how Ecore looks in that syntax! Only two adaptions of the generated code where necessary to get this editor:

(The '^' chars are automatically added by Xtext to distinguish identifiers from keywords, which of course collide a lot in this example) It is certainly not as complete as EMFatic, and it has a quite verbose syntax, but it could be the starting point for a nice textual Ecore editor.


林守全 said...
This comment has been removed by a blog administrator.
RafaelLetso21555 said...
This comment has been removed by a blog administrator.
冠伶 said...
This comment has been removed by a blog administrator.
宛蓉 said...
This comment has been removed by a blog administrator.
Glennie9654 said...
This comment has been removed by a blog administrator.
韋于倫成 said...
This comment has been removed by a blog administrator.
健豪 said...
This comment has been removed by a blog administrator.
Simon Harrer said...

i used this for my language and it didnt work at first.

I use a second ecore file for which i do not have genmodel. It is the XMLNamespace.ecore. I removed it and it worked great.

Is it possible to use the XMLNamespace.ecore file somehow or do I have to generate a genmodel from this ecore file and only then can use it for my xtext grammar?

Ashwani said...


Thanks for the blog. Its very helpful.
For the example you shared about showing existing ecore metamodel using xText: Can i get access the source code for that ?

Ashwani Kr Sharma

Jan Köhnlein said...

Uff, that was two years ago. I guess I took the Ecore file from the GMF example, so it should be this one. But after all it's just a simple Ecore model.

Amine Benelallam said...

I would like to infer an ecore model from a giving textual code. This model is conformed to its metamodel and so is the code. It is conformed to its EBNF grammar. Is it possible to establish this task using Xtext ???

Jan Köhnlein said...

@Amine: Yes, that's what Xtext has been made for. There are a few restrictions in practice

1) Xtext's grammar language is not exactly EBNF but very similar. In addition to the syntax of the language it also describes how the model is inferred during parsing. The metamodel (^= structure of the model) can be inferred, too.
2) Your language's should be parseable by an LL* parser, as we use Antlr in the background. If your EBNF is left recursive, you have to refactor that.

Parisa Moosavi Nezhad said...


How can I make xtext grammar from Ecore.ecore. Actually I want to use it in Ecore2Xtext tool because I need a textual editor for Ecore.ecore.

Is xtext the only way to have textual editor or can I use another tools?


Jan Köhnlein said...

@Parisa Have a look at Xcore:

Parisa Moosavi Nezhad said...

Perfect, thanx a lot.