Importing nested EPackages in Xtext

Aus SDQ-Wiki

Xtext supports building grammars for existing Ecore meta models via the import-statement ([1], [2]). However, this does currently (as of May 2016) not work for nested EPackages. The packages can be referenced in the Xtext grammar definition by their namespace URI and their elements are identified correctly. However, when generating the editor code with the MWE2 workflow, Xtext cannot correctly resolve the references to the EPackages, resulting in an error log similar to the following:

740  [main] INFO  clipse.emf.mwe.utils.GenModelHelper  - Registered GenModel 'http://nsuri.of.package/a' from 'platform:/resource/path/to/the/packageA.genmodel'
...
964  [main] ERROR xt.generator.XtextGeneratorLanguage  - [XtextLinkingDiagnostic: null:4 Couldn't resolve reference to EPackage 'http://nsuri.of.package/a'., 
970  [main] ERROR mf.mwe2.launch.runtime.Mwe2Launcher  - Problems running workflow org.xtext.example.mydsl1.GenerateMyDsl: Problem parsing 'file:/path/to/the/grammar.xtext':
XtextLinkingDiagnostic: null:4 Couldn't resolve reference to EPackage 'http://nsuri.of.package/a'.
...
XtextLinkingDiagnostic: null:13 Couldn't resolve reference to EClassifier 'ElementOfPackageA'.
...
TransformationDiagnostic: null:13 Cannot create type without declared package. (ErrorCode: UnknownMetaModelAlias)

Even though the use of platform URIs (as opposed to namespace URIs) for referencing the packages is deprecated, this seems to be a valid solution to this problem ([3]).

This means that the import statements must be altered as follows: Replace

import "http://my.metamo.del/..." as xyz

with

import "platform:/resource/path/to/the/metamodel.ecore" as xyz

For nested packages, the following notation can be used:

import "platform:/resource/path/to/the/metamodel.ecore#//subpackageName" as subpackageName
import "platform:/resource/path/to/the/metamodel.ecore#//subpackageName/subsubpackageName" as subsubpackageName

The generated editor creates instances of the meta models that reference the packages correctly by their namespace URI (at least this is the case when persisting the models in the XMI format).