Showing posts with label Ecore. Show all posts
Showing posts with label Ecore. Show all posts

Wednesday, June 12, 2013

Using Xcore in Xtext

With Xtext you can easily create your own textual language. The structure of the language is defined in an Ecore model. This model can be automatically derived from the grammar, which allows quick turnarounds in the beginning. But in the long run, this model becomes an increasingly important API to the language. You want to fix it and make it a first class artifact: You are switching from an inferred Ecore model to an imported one in the grammar.

Traditionally, you use EMF's tree editors to edit Ecore models. That can be cumbersome as the tooling is not the nicest to use. Furthermore, the Ecore model only describes the declarative part. Behavior, such as the body of EOperations or the code for derived EStructuralFeatures has to be woven in either from additional models or directly in the generated code. These and more issues are addressed by Xcore. Xcore allows to specify Ecore models textually, including the behavioral parts and the code generation parameters. Having a single source of information facilitates the maintanance of such a model drastically. Xcore is shipped with EMF.

In the upcoming Eclipse release 4.3 (Kepler) Xcore and Xtext can be used together. Here is a step-by-step example how to do this with Xtext's "Greetings" language:
  1. Create an Xtext project File > New > Project > Xtext Project. The defaults are fine. This will create the usual four projects for your Xtext language in your workspace.
  2. By default, Xcore generates its code to the folder src-gen. The same folder is used by Xtext and will be wiped each time you regenerate the language from the grammar. To avoid loosing data, choose a separate output folder for Xcore: Eclipse/Window > Preferences > Xcore > Compiler > Output Folder > Directory > ./emf-gen

  3. Create a new folder model in the root of org.xtext.example.mydsl and file Greetings.xcore inside that folder. Our Ecore model consists of an EPackage org.xtext.example.mydsl, two EClasses Model and Greetings, a containment EReference greetings and an EAttribute name. In Xcore, this looks like
:

  4. Open the grammar MyDsl.xtext. By default, the Ecore model myDsl is generated from the grammar rules. We want to replace this with the Ecore model specified in the Xcore file:

  5. We have to load the Xcore model in the workflow GenerateMyDsl.mwe2 of the language generator. Add an appropriate loadedResource entry to the language section:

    module org.xtext.example.mydsl.GenerateMyDsl
    ...
    Workflow {
      ...
      component = Generator {
        ...
        language = auto-inject {
          loadedResource =
    "platform:/resource/${projectName}/model/Greetings.xcore"
          uri = ...
    
  6. You have to add a dependency to org.eclipse.emf.xcore in the MANIFEST.MF of the project.
  7. Now generate the language infrastructure Run As > MWE2 Workflow. There should not be any error.
  8. Make sure that all folders will be correctly deployed. Your build.properties file should look like this
:
  9. Launch a new Eclipse workbench Run > Run Configurations > Eclipse Application > Launch Runtime Eclipse and enjoy your language in action.
For the geeky fun of it: As Xcore is itself implemented in Xtext with an imported Ecore model, this lays the foundation to replace Xcore.ecore with Xcore.xcore and thereby bootstraping it.
Sorry, I couldn't resist ;-)

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.