Xtext and existing .ecore: Making use of the Atlan Model zoo

Xtext is a great tool for defining your own languages and generating editors for that. A lot of the work involved is in the definition of the language. So obviously, if there is some available similar language or meta-model you might want to reuse that. With Xtext, you can use existing .ecore files and create a grammar and editor out of those.

The AtlanMod Meta-Model Zoos

One source to start looking for existing meta-models is the AtlanMod Meta-Model Zoo. It provides a variety of meta-models. The meta-models are originally in KM3 format, but they have an automatic conversion into other meta-meta-models, so you can download meta-models in UML or .ecore format. As an example, we will use the FiniteStateMachine.ecore from the AtlandMod Meta-Model Zoo.

The EMF Project

Adding the .ecore to your project

Adding the .ecore to your project

To start, simply download your .ecore from the website. Create an empty EMF Project in your Eclipse workspace and copy the .ecore into your “model” sub-directory. The Xtext project wizard does actually require a .genmodel, so we need to create this first. If you open your .ecore and have a look at the properties of the FSM and PrimitiveTypes package, you will see that the properties “Ns Prefix” and “Ns URI” are unset. We need to set these values to be able to create a genmodel.

In addition, the PrimitiveTypes have unset Instance Type Names and we need those for the genmodel too, so we set them (we will get rid of most of the Primitive Types package later, but at this stage we still need to keep it).

Setting the model properties
Setting the model properties

At this point in time, make sure that the ecore is saved and then create a genmodel for the .ecore. After the genmodel is created, create all necessary emf-projects.

Creating the Xtext Project

Use the Create New Project wizard and select “Xtext from existing ecore”. Add the FSM genmodel by clicking “add” and you are almost ready to go.

A small but important selection

There is a very important but small detail: In the dialog-box you have to select the model element that should be the root rule of your grammar. Usually this is not set to a good default, so you need to pick it by yourself (State Machine in our case). Create the project and find the default grammar.

Cleaning the Xtext Project

You will find some errors in the project. The first is a duplicate import. Since the AtlanMod .ecores have more than one root package, we need to do some tweaking here by adding the fragment here. If you are familiar with the URI syntax, you might have tried something like appending “#/FSM”.

Problem with import

However, this does not work for root packages. As Sebastian Zarnekow points out, the names don’t work on the root level, you have to use the numeric reference to the packages.

Import fixed

In addition there is a number of optimizations that you can do:

  • References in the grammar often use EString as the parser rule, you might want to replace the EStrings with the ID
  • The model zoo uses String for names etc., you might want to really check if you want Strings, replace them with the terminal STRING, or if you want IDs (replace them with ID)
  • Similar with Integers (replace by INT) and other datatypes. At this point, you might have replaced all data types by built-ins so that you might want to remove the PrimitiveTypes import and rules completely
  • The generated grammar is verbose. Often you would want the “name” rule of an element to just be an ID, not preceded by the keyword “name”. So you might want to remove some of the keywords (Have a look at the generated editor to see where the keywords are too annoying for you).

At this point, you are ready to go and try your new editor.

2 thoughts on “Xtext and existing .ecore: Making use of the Atlan Model zoo

  1. Pingback: Combining Xtext and the AtlanMod zoo of metamodels | MOdeling LAnguages

  2. Pingback: Combinando Xtext y el AtlanMod zoo de metamodelos | MOdeling LAnguages

Leave a Reply

Your email address will not be published. Required fields are marked *