Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » Using EMF Resource to iterate over contents of model file
Using EMF Resource to iterate over contents of model file [message #1779124] Wed, 03 January 2018 05:22 Go to next message
Ruchika Kumar is currently offline Ruchika KumarFriend
Messages: 50
Registered: January 2017
Member
Hi,

I want to know if we can use EMF Resource to iterate over our model elements.
I have an ecore metamodel and a model that conforms to that metamodel. ResourceSet gives me the top most package but if I try to get the inner contents, it returns EClass instead of actual class.
I try below steps:

 Resource resource = resourceSet.getResource(
	        URI.createURI("path to model file"), true);
ModelPackage mp = (ModelPackage) resource.getContents().get(0);


But it does not let me do this:

ModelSpec ms= (ModelSpec) mp.getModelSpec();


as mp.getModelSpec() is an EClassImpl (with name ModelSpec) and not ModelSpec. I want to further iterate over the contents of ModelSpec in my model file.

Note: ModelPackage is an EPackage in my ecore file and contains ModelSpec EClass.
Re: Using EMF Resource to iterate over contents of model file [message #1779132 is a reply to message #1779124] Wed, 03 January 2018 06:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
I can't possibly know how you've defined your model. You say that ModelPackage is an EPackage, but an EPackage doesn't have a getModelSpec() method and in fact EPackage doesn't even generally have modeled subclassess, so I'm not sure how a ModelPackage can be an EPackage except of course if it's a generated package interface of some model, but in that case I'm not sure how you're loading it from a resource and what the point would be to do that instead of using ModelPackage.eINSTANCE. If that's the case, of course all the getters returns Ecore metadata. The question is, where is your instance model because you seem to be just processing the metamodel.
Re: Using EMF Resource to iterate over contents of model file [message #1779200 is a reply to message #1779132] Wed, 03 January 2018 18:31 Go to previous messageGo to next message
Ruchika Kumar is currently offline Ruchika KumarFriend
Messages: 50
Registered: January 2017
Member
Hi Ed,

Thanks for your reply.
I am using the path of my model while registering the package as well as creating/getting the resource.

 resourceSet.getPackageRegistry().put("path of model",ModelPackage.eINSTANCE); 


Resource resource = resourceSet.getResource(
	        URI.createURI("path of model"), true); 


If we want to save/load our model in xmi, we use
 resource.getContents().get(0) 
which in this case returns an instance of ModelPackage. Now i want to iterate through the contents of ModelPackage (one of which is ModelSpec).
Re: Using EMF Resource to iterate over contents of model file [message #1779211 is a reply to message #1779200] Thu, 04 January 2018 05:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
This looks kind of nonsensical. Firstly, I can only guess what "path of model" is, and that's the key detail. Secondly, unless you're running in a stand alone environment (not in an Eclipse/Equinox container), then resourceSet.getPackageRegistry().put("path of model",ModelPackage.eINSTANCE) is not needed, and if it were, you'd use the idiom ModelPackage.eINSTANCE.eClass() force the model to register itself in the global package registry, or you'd use resourceSet.getPackageRegistry().put(ModelPackage.eNSURI, ModelPackage.eINSTANCE) to register the model's namespace URI if there was some particular need to register it also in the resource set's package registry. Any call to resourceSet.getResource(<uri>, true), will return the resource of the generated model package if <uri> is equal to the eNSURI of any registered model, so no doubt what you're showing above will return the resource of the Ecore (meta) model not your instance model corresponding to that Ecore model. I imagine you want to be iterating over your instance model so you need to load that, (perhaps as you've shown, using a platform:/resource URI if you're running with an IWorkspace) and not map its URI to the Ecore model.
Re: Using EMF Resource to iterate over contents of model file [message #1779215 is a reply to message #1779211] Thu, 04 January 2018 06:10 Go to previous messageGo to next message
Ruchika Kumar is currently offline Ruchika KumarFriend
Messages: 50
Registered: January 2017
Member
Okay I can try to make things more clear.
"path of model" here is the path to my my model file (say test.project file) that conforms to my metamodel (say project.ecore). So the URI is something like: "/workspace/MyProject/test.project" which I use in getResource(). And it is definitely different from eNSURI of my metamodel.
Now, Is it correct? Should it still give me the resource for ecore metamodel? If yes, how should I get to iterate over my instance model?

Re: Using EMF Resource to iterate over contents of model file [message #1779225 is a reply to message #1779215] Thu, 04 January 2018 08:03 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
No, this is not correct. Read carefully what I wrote. Read these FAQ entries:

https://wiki.eclipse.org/EMF/FAQ#How_do_I_map_between_an_EMF_Resource_and_an_Eclipse_IFile.3F
https://wiki.eclipse.org/EMF/FAQ#Platform_URI_Scheme

In other words, remove your package registration code; it's not needed and is bogus in the form you've done it because you've effectively redirected the URI of your instance resource to the URI of your Ecore model so you will not be able to load that instance resource. Resources should always be loaded with an absolute URI, i.e., a URI with a scheme such as file:, platform:, http:, and so on. So while using a URI such as "/project/path/file.xmi" might work to load that initial resource, it will not be able to resolve the relative URI of any cross resource reference.

Please don't ask follow up questions until you've at least tried something new/different.
Previous Topic:Ecore to Emfatic conversion standalone application
Next Topic:[EMFStore][Edapt] Meta Model evolution
Goto Forum:
  


Current Time: Sun Sep 23 00:18:15 GMT 2018

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

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

Back to the top