Tuesday, November 24, 2009

Xtext - Using existing Ecore models

Maybe it's because it was not possible in the old oAW Xtext or because the wizard suggests a grammar which makes use of the "generated Ecore model"-feature. But somehow some people tend to think that you cannot use existing Ecore models with Xtext. YOU CAN!

The feature is described in the documentation, but maybe it got too few words (Which is not because we don't use it and don't want you to use it, but because it's so simple).

In the following I want to show you how to do this. You'll see that it is relatively straight forward.
Let's assume you have an existing Ecore model, describing a library of books (sounds familiar?;-)):

In order to create a textual syntax with Xtext you only need to create an Xtext project and change the proposed grammar to something like this :

So if you want to reuse an existing ecore model, it is just a matter of switching from a "generated Ecore"-statement to an "import"-statement. Note that as soon as you do this, the grammar is statically analyzed and you'll get all kinds of compiler errors and warnings for missing attributes and classes in the imported Ecore models (you can import multiple ecore models, of course).

Next, you run Xtext's generator and you'll get an EMF Resource writing and reading instances of your Ecore model to the textual syntax defined in aboves Xtext grammar.
In addition you'll get an editor for your new language :

You can, of course, still use any EMF-based editor, since it's just an EMF model:

Note, that you cannot only import multiple Ecore models but can even mix generated and imported Ecore models in the same grammar.

To sum it up: The feature we have in Xtext to derive an Ecore model from the grammar description, is a very convenient option to get you started. If you have one or more existing Ecore models, you won't want and need to use it. But if you start developing a new language this feature speeds up your turn-arounds dramatically.


  1. hi, just a fast question:
    don;t you need to edit the mwe file? like this
    !-- generates Java API for the generated EPackages -->
    fragment class="org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment" genModels="platform:/resource/gr.softwareagility.mylib/model/mylib.genmodel"/>

    as this link:


  2. No, you only need to tell the EcoreGenerator about genmodels, if you mix imported and generated metamodels and your generated metamodel refers to an imported.

    We have to correct that FAQ. Thanks for the pointer.

  3. Sven,

    how about generating a default textual syntax for existing meta-models? Like for example EMF-Text does based on HUTN, although one should be able to come up with an even more readable notation :) Why not ship an Xpand-template for this purpose?


  4. Jan promised to contribute such a generator, but he hasn't yet found the time.

  5. Nice article.
    What about scopes? I see they require generated Classes. Should I generate code from my existing ecore model and import that in the ScopeProvider?



  6. It is in generally preferable to generate the Java classes for an ecore model, because it provides a nice Java programming model.

  7. Sven,

    Nice example! Any hints why my example does not like the UML.ecore model:

    grammar org.xtext.example.MyDsl with org.eclipse.xtext.common.Terminals

    import "platform:/plugin/org.eclipse.uml2.uml/model/UML.ecore"

    Comment: 'Comment' body = ID

    [XtextLinkingDiagnostic: null:4 Cannot resolve reference to '"platform:/plugin/org.eclipse.uml2.uml/model/UML.ecore"', TransformationDiagnostic: null:8 Cannot create type for Comment because its MetaModel is unknown. (ErrorCode: UnknownMetaModelAlias)]

    Thank you and best regards

  8. platform:/plugin is not supported by the code generator. For UML it's best to use the nsURI. Make sure that you've initialized the UMLPackage in the mwe file.

  9. Hi Sven,
    i have another question about that.

    I have created a ecore meta-model for a XML-based (shxml) language and generated a EMF-Editor and GMF-Editor.

    Now, my goals are:
    1. Open an .shxml File and generate the GMF-Model(Is this possible with XText?).

    2. Create my Model and generate the Shxml Code out of it.( should i use XPand or could this be done with the XText Editor?)

    3. The three Editors (EMF-Tree, GMF-Model, Texteditor) should update eachother by saving. Is this possible?

    I would be very grateful if you could help me, because I am very confused, about what's possible and what's not.

  10. Hi Sui,
    if shxml is XML, I would use an XML parser instead of an Xtext generated parser. Is a schema available? EMF supports derivation of ecore models from schema. The XMLResource will automatically read and write corresponding xml and GMF should be able to work on those resources out of the box.

  11. Hi Sven

    Thanks for the quick reply.

    Yes, there are XSD files. I created the ecore model from it.
    Although I was already reasonably incorporated in EMF and GMF, but unfortunately still do not know exactly what you mean.

    I would be eternally grateful if you could give me a push in the direction in which I have to look.
    It's about my diploma thesis and i wasted a lot of time by learning the basic functions of EMF, GMF, XText, Xpand, MWE, etc.

    You can also send me an e-mail to: suithedoc@arcor.de, also like in German, which do not fall so hard on me :-).

  12. hi sven,

    is it possible to refer to an XML Schema from my grammar definition, same for ecore files?

    And if so, could I then "import" and refer to some XML files, based on this schema, from my xtext based editor?


  13. Hi Steffen,
    no not directly. You'll have to convert the Schema to ecore using the EMF'S XSD support.

  14. hi,

    ok, i've converted it to ecore and genmodel and also generated emf classes. No I generated the editor and it works so far.

    But now in my model i will import an xml file based on this, to use code completion from the contents from the xml file. It doesnt work. Could you explain, what I can do to bring it working?


  15. Hi

    I have a quetsion about laoding non dsl models like:

    grammar com.ubs.hpt.automatedfunctionaltester.language.AutomatedFunctionalTest with org.eclipse.xtext.common.Terminals

    generate automatedFunctionalTest "http://AutomatedFunctionalTest.language.automatedfunctionaltester.hpt.ubs.com"
    import "platform:/resource/com.ubs.hpt.automatedfunctionaltester.model/model/UserInterfaceDiagram.ecore" as UID
    import "http://www.eclipse.org/emf/2002/Ecore" as ecore


    /* terminals */
    terminal PackageName:
    ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '-')+ '.' ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '-')+ ('.' ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' |

    terminal QualifiedName:
    ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '-' | '.')* ':' ('a'..'z' | 'A'..'Z') ('a'..'z' |
    'A'..'Z' | '0'..'9' | '_' | '-' | '.')*;

    /* test structure */
    'package' name=PackageName

    Import returns UID::UserInterfaceDiagramType :
    'import' QualifiedName {Import};

    how would i actually load real instances of UID models?

  16. I don't understand the question. Bt it is generally better to ask such detailed questions in the forum / neswgroup at eclipse.org : http://www.eclipse.org/forums/index.php?t=thread&frm_id=27&

  17. hi
    I have a question about writing hutn file for inheritance and many to many relationship would you please answer me?

    1. please ask such questions in the official Xtext forum : http://www.eclipse.org/forums/index.php?t=thread&frm_id=27&

  18. If you just want a simple DSL for an existing ecore model, without your own Xtext grammar (and therefore more limited, of course), you may be interested in EFactory https://github.com/vorburger/efactory. Contributions would be very welcome!

  19. How we can compile the particular language file generated by xtext with some external program and show the results back in Xtext.

    1. I don't really understand your question. Could you ask it at the official Xtext forum, please?


  20. Hello,

    Can the grammar be generated from an .ecore or a .genmodel file like in :http://sistemas.uniandes.edu.co/~isis4712/dokuwiki/doku.php?id=xtext-petrinets-tutorial ?
    because i do not find any EMF generator model to be chosen in the second step...
    Or do we still have to write it ourselves ?

    And can we work on the .ecore that is in the generated file "model" ton update the entities of our DSL ?

    PS: i am a beginner
    Thanks in advance


    1. Yes, there's a wizard in Eclipse that let's you create a fresh Xtext project from an existing ecore file.

  21. I've just stumbled upon this post and got really enthusiastic about it. Tried with the Ecore.genmodel. Unfortunately, it failed silently. Added four types to the grammar and was done. Is this an error? Should I file a bug? Or the Ecore metamodel contains some constructs that are not designed to be handled by Xtext?

    1. Yes, there are some limitations, but it should work fine for ordinary ecore models. Would still be helpful if you could file a bugzilla.

  22. Is this a UML diagram created from creately ?