I use a transformation engine to create an Ecore meta-model at runtime and I wonder how we can register that meta-model with EMF so that it can recognize the meta-model?
2 Answers
If you have the code generated by your metamodel:
resourceSet.getPackageRegistry()
.put(org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage.eINSTANCE.getNsURI()
, org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage.eINSTANCE);
(here for the "genmodel" metamodel)
If you only have the .ecore file:
// register globally the Ecore Resource Factory to the ".ecore" extension
// weird that we need to do this, but well...
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"ecore", new EcoreResourceFactoryImpl());
ResourceSet rs = new ResourceSetImpl();
// enable extended metadata
final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(rs.getPackageRegistry());
rs.getLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA,
extendedMetaData);
Resource r = rs.getResource(uriOfYourModel, true);
EObject eObject = r.getContents().get(0);
if (eObject instanceof EPackage) {
EPackage p = (EPackage)eObject;
rs.getPackageRegistry().put(p.getNsURI(), p);
}
You can find a bit more about this code here with the method named registerEcorePackages(), used to register .ecore file in the workspace (with their workspace fullpath) in our custom package registry. If you want to register your metamodel in EMF global package registry, replace resourceSet.getPackageRegistry() by EPackage.Registry.INSTANCE.
- 16,540
- 9
- 56
- 97
- 1,504
- 13
- 18
-
SUPERB ANSWER!!!!!! Thanks a lot Stephane ! Can't thank you enough..... The "ecore file" technique is what I'm looking for! – Hendy Irawan Jun 14 '12 at 19:38
-
Hi, awesome thank you! I had to modify the code a bit for it to work. Please see my answer for details. – Andrei Apr 30 '15 at 13:34
I had to modify the code from @sbegaudeau a bit for it to work:
Replace
rs.getPackageRegistry().put(p.getNsURI(), p);
with
EPackage.Registry.INSTANCE.put(p.getNsURI(), p);
Also, somehow I cannot register the .ecore type. Had to use "*": Resource.Factory.Registry.INSTANCE.
getExtensionToFactoryMap().put("*", new EcoreResourceFactoryImpl());
- 2,282
- 26
- 35