0

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!

Koziołek
  • 2,791
  • 1
  • 28
  • 48

2 Answers2

0

For reference, use Vogella's tutorials: https://www.vogella.com/tutorials/EclipseEMF/article.html#load-an-existing-model

The error message you are getting is a bit weird, I would check whether MyExtFactoryImpl (which should be the generated factory implementation for your metamodel) works (e.g. MyExtFactoryImpl.eINSTANCE.createXXX()) as intended. Also the error looks like it may be related to Java modules so check up on that (is EMF Resource on your classpath etc?)

user1292456
  • 778
  • 4
  • 12
0

If your .myext models are persisted as XMI files you need an XMIResourceFactoryImpl to load the file. In your example this would give:

resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("myext", new XMIResourceFactoryImpl())

MyExtFactoryImpl is a factory to create instances of the MyExt metamodel, it doesn't deal with loading the actual file.

zelus
  • 143
  • 8