I am writing a code to get all the contents of an EMF model instance. I tried to follow this tutorial that I found here: Create and modify ecore files and their instances from code in order to load the model.
In this tutorial, the model instance is an XMI file. In my case, I have a .myExt
file (the extension I decided upon when I created the metamodel to which this model instance conforms to in ecore
). I wrote the following code, but I get the following error:
class GetModelObjects {
ResourceSet resourceSet = new ResourceSetImpl()
Resource metamodel
EPackage mp
Resource model
EFactory mappinginstance
//EList <EObject> modelobjects = new BasicEList<EObject>()
def doTransform() {
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("ecore", new EcoreResourceFactoryImpl)
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("myext", new MyExtFactoryImpl)
metamodel = resourceSet.getResource(URI.createFileURI("myext.ecore"), true)
mp = metamodel.contents.get(0) as EPackage
resourceSet.packageRegistry.put("http://www.example.org/myExt", metamodel)
model = resourceSet.getResource(URI.createURI("model.myext"), true)
mappinginstance = mp.getEFactoryInstance
System.out.println("Model:" + model.contents)
}
def static void main(String[] args) {
new GetModelObjects().doTransform()
}
}
class myExt.impl.MyExtFactoryImpl cannot be cast to class org.eclipse.emf.ecore.resource.Resource$Factory (myExt.impl.MyExtFactoryImpl
and org.eclipse.emf.ecore.resource.Resource$Factory are in unnamed module of loader 'app')
One thing I am not quite sure about, and I think might be the reason for this error, is this line:
resourceSet. resourceFactoryRegistry.extensionToFactoryMap.put("myext", new MyExtFactoryImpl)
Because I am not sure if this is the factory that should be placed here. I would really appreciate any input on this.
Thank you in advance!