Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Loading models/ metamodels from classpath resources.
Loading models/ metamodels from classpath resources. [message #1804278] Thu, 21 March 2019 17:12 Go to next message
Tobias Fox is currently offline Tobias FoxFriend
Messages: 10
Registered: February 2019
Junior Member
Hi,

I am looking to load models from .ecore and .model files within a resources package that will be bundled into a JAR along with other classes.

ResourceSet.createResource() takes a URI, and URI.createURI() takes the string of the path of a file, which will not apply to these resources. I notice URI.createPlatformResourceURI() exists, but I'm not sure if this is what I'm looking for and cannot find usage to suggest it is.

Do you have any suggestions?

My project structure (where class.java is where I try loading the metamodel/model) is:

Project
----src
--------package
------------class.java
--------resources
------------metamodel.ecore
------------model.model

Thanks again,
Toby
Re: Loading models/ metamodels from classpath resources. [message #1804299 is a reply to message #1804278] Fri, 22 March 2019 05:30 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Is this supposed to work in a stand alone runtime environment (plain old classpath) or within an Eclipse application (bundle classpath managed by Equinox)? A platform:/plugin URI only works in the latter case.

Furthermore, do the *.ecore resource(s) and the *.model resource(s) have any across references to other resources in other projects? I suppose that the model.model has metamodel.ecore as its model and has an xsi:schemaLocation reference to it?

I ask all these questions because any cross references will need to resolve. Not only that, they need to resolve at development time when you are editing these resources and at runtime when you are loading and using them.

To ensure that both work properly, we generally do not store the *.ecore models in a src folder, but rather in the model folder. At development time, all the resources are loaded using absolute platform:/resource/<project-name>/model/*.* URIs and as a result, all saved cross references are relative. At runtime in an Equinox environment, we can load them using platform:/plugin/<plugin-id>/model/*.* URIs and then all relative references resolve to platform:/plugin URIs. We ensure that the model folder is in the binary *.jar via the build.properties and we ensure that <project.name> == <plugin-id> so that the mapping is trivial.

In the simplest case, if you don't have cross references other than than one from model.model to metamodel.ecore then you can use <your-class>.getClass(). getResource("/resources/model.model") to create a URI that you can use with URI.createURI to load that resource from the classpath.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Loading models/ metamodels from classpath resources. [message #1804308 is a reply to message #1804299] Fri, 22 March 2019 08:31 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

As Ed Merks points out, do not point model resources in src/... Typically this means they get copied to bin/... and your packaged plugin ends up with two copies, neither of which is accessible sensibly as platform:/resource. Emulate org.eclipse.emf.ecore's layout, but perhaps with a distinct "emf-gen" src path to distinguish auto-generated code more clearly.

If your metamodel is evolving then keeping them both in the same plugin may make sense but it requires you to take care to use platform:/plugin or platform:/resource consistently and initialize your ResourceSets appropriately.

More commonly, as with org.eclipse.emf.ecore, the metamodel is a (very) slowly changing development used by many different consumers. The metamodel is therefore registered and accessed by its nsURI avoiding anyone needing to know where it comes from. It should only be necessary to use platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore rather than http://www.eclipse.org/emf/2002/Ecore when you need a local version of Ecore.ecore that differs from the installed version; not recommended.

Regards

Ed Willink


Re: Loading models/ metamodels from classpath resources. [message #1804353 is a reply to message #1804308] Sat, 23 March 2019 23:03 Go to previous message
Tobias Fox is currently offline Tobias FoxFriend
Messages: 10
Registered: February 2019
Junior Member
Hi,
Thank you again for your help.

Regards,
Tobias
Previous Topic:Package.eINSTANCE.getElement not equals reference.etype - ResourceFactory or Registry Problem?
Next Topic:CSV Resource implementation for EMF
Goto Forum:
  


Current Time: Tue Apr 23 11:23:28 GMT 2024

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

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

Back to the top