Load EMF model instance with Xtend/Java [message #1846931] |
Sat, 09 October 2021 13:17 |
John Henbergs Messages: 239 Registered: October 2020 |
Senior Member |
|
|
Hi all,
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()
}
}
Quote:
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!
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01902 seconds