Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Load EMF model instance with Xtend/Java
Load EMF model instance with Xtend/Java [message #1846931] Sat, 09 October 2021 13:17 Go to next message
John Henbergs is currently offline John HenbergsFriend
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!
Re: Load EMF model instance with Xtend/Java [message #1846941 is a reply to message #1846931] Sat, 09 October 2021 15:20 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

This appears to be an Xtend bug. It is doing what is 'obviously correct', but is actually wrong. It's certainly surprising.

Much of the EMF API predates generics and so that factory argument is actually Object and when I look at my models they extend EFactory rather than Resource.Factory. Your MyExtFactoryImpl probably does so too. Hence the cast to Resource.Factory fails, but it should never have been attempted.

Personally, I am not fond of Xtend and so have never seen similar code. I much prefer to layer my Xtend as a minimal M2T layer on top of a pure Java layer which debugs better, and, as this example appears to demonstrate, does not have an erroneous Xtend2Java conversion.

If you use some intermediate variables and casts you can probably make the spurious error go away. Maybe just calling the true Java function getExtensionToFactoryMap() rather than navigating the Xtend pseudo-field extensionToFactoryMap will be enough.

Regards

Ed Willink
Re: Load EMF model instance with Xtend/Java [message #1846942 is a reply to message #1846931] Sat, 09 October 2021 15:22 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
After going through some other things, I believe that the issue is that instead of MyExtResourceFactoryImpl, I should have MyExtResourceFactoryImpl. However, that is not generated anywhere. When I check the util folder I only see MyExtAdapterFactory.java and MyExtSwitch.java . Any help on how I can generate that?

Thank you!
Re: Load EMF model instance with Xtend/Java [message #1846943 is a reply to message #1846931] Sat, 09 October 2021 15:23 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
Try new XMIResourceFactoryImpl instead of MyExtFactoryImpl. The MyExtFactoryImpl looks like your EFactory for creating instances of your model not a resource factory for creating resource. But then if you have a MyExtFactoryImpl I expect you have MyExtPackage.eINSTANCE too, i.e., a generated model. If you have generated the model, try invoking Generate Test Code and look at the generated *Example.java for how to read and write instances of your model.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Load EMF model instance with Xtend/Java [message #1846946 is a reply to message #1846943] Sat, 09 October 2021 18:29 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Ed,

When I try to use new XMIResourceFactoryImpl, I cannot because I cannot even import it.
I use the following line on top of the file in order to import it but not luck.
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl


However, I can import and use the XMLResourceFactory, and that is kinda weird because both of them are in the same plugin that I have added in the plugin dependencies

Re: Load EMF model instance with Xtend/Java [message #1846951 is a reply to message #1846946] Sun, 10 October 2021 04:41 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

This is obviously impossible, so you are being deceived in what you observe.

Xtend auto-translates to Java and reverse presents the Java errors in the Xtend editor. If you open the underlying Java file you may get to see something that explains the problem. For instance a bad import can block a correct import.

I suspect that the auto-translate has been disabled in some way. The easiest way to fix it is to refresh then clean the project and if that doesn't work manually delete the xtend-gen folder. If that doesn't work clean all projects and restart Eclipse a couple of times. If that doesn't work start closing projects and you may find a copy and paste duplicate experiment that has polluted the index with confusion.

Regards

Ed Willink
Previous Topic:[CDO] Container reference db mapping- referential integrity problem
Next Topic:Object name instead of eProxyURI
Goto Forum:
  


Current Time: Thu May 02 03:09:23 GMT 2024

Powered by FUDForum. Page generated in 0.03613 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top