Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Traversing all resources from XMI-file
Traversing all resources from XMI-file [message #1058598] Tue, 14 May 2013 14:17 Go to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Hi,

I wonder whether someone could please help on a small issue.

I have an Ecore model consisting of three classes: A, B and C. A references B (non-containment, whereas B references C (containment). When I load an XMI-model of the Ecore model I'm not able to access the objects of B and C. When I create the XMI-model starting with A, the a B object is correctly referenced in the editor(by loading resource and selecting the B object). I use .getAllContents() on the resource acquired by using resourceSet.getResource( "a.xmi" ). I can access the A object, but not the other objects. EcoreUtil.resolveAll() did not give me a complete list, but I may not access the resources in the proper way. Also, if I make a model with B as the root class, I can "access" C objects in the code.

Any tips?
Re: Traversing all resources from XMI-file [message #1058620 is a reply to message #1058598] Tue, 14 May 2013 14:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Peter,

Comments below.


On 14/05/2013 4:17 PM, Peter Johnsen wrote:
> Hi,
>
> I wonder whether someone could please help on a small issue.
>
> I have an Ecore model consisting of three classes: A, B and C. A
> references B (non-containment, whereas B references C (containment).
> When I load an XMI-model of the Ecore model I'm not able to access the
> objects of B and C.
How so?
> When I create the XMI-model starting with A, the a B object is
> correctly referenced in the editor(by loading resource and selecting
> the B object). I use .getAllContents() on the resource acquired by
> using resourceSet.getResource( "a.xmi" ).
Don't use a relative URI to load a resource; relative paths within the
resource will not resolve correctly. Use an absolute URI such as
platform:/resource/project/file.extension or file:/c:/path/file.xtension.
> I can access the A object, but not the other objects.
> EcoreUtil.resolveAll() did not give me a complete list, but I may not
> access the resources in the proper way. Also, if I make a model with B
> as the root class, I can "access" C objects in the code.
Also, Resource.getAllContents will only visit the contained objects of
that one resource, not objects contained in other resources. You can
use ResourceSet.getAllContents to visit all resources and all the
objects contained by those resources.
>
> Any tips?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traversing all resources from XMI-file [message #1058631 is a reply to message #1058620] Tue, 14 May 2013 15:05 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Thanks for your prompt reply!

A small follow-up, or actually it's a slightly different question. Is it possible to
set the value of an attribute reflectively, e.g. (pseudo wise):

EAttribute.set( object, value )
Re: Traversing all resources from XMI-file [message #1058644 is a reply to message #1058631] Tue, 14 May 2013 15:26 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
it's actually : object.eSet(eAttribute,value)
Re: Traversing all resources from XMI-file [message #1058649 is a reply to message #1058644] Tue, 14 May 2013 15:32 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Ah, I was close then, hehe. Thank you!
Re: Traversing all resources from XMI-file [message #1068002 is a reply to message #1058598] Thu, 11 July 2013 15:39 Go to previous messageGo to next message
bim boh is currently offline bim bohFriend
Messages: 11
Registered: July 2013
Junior Member
@ Peter,

Would it be possible for you to share any resources that helped you in reading the XMI and retrieving the data from it.

I am currently starting up on EMF and its bit tricky.I have found where the xyz.example java file which others posts often refer to , where the changes have to be made.

Really appreciate your help.

Thanks

Re: Traversing all resources from XMI-file [message #1068007 is a reply to message #1068002] Thu, 11 July 2013 16:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
For any model you can invoke Generate Test Code. In the *.tests bundle
there will be a class called XyzExample.java where Xyz depends on your
model's prefix. That shows how to load or save a file. Of course the
generated editor does that too.

On 11/07/2013 5:41 PM, bim boh wrote:
> @ Peter,
>
> Would it be possible for you to share any resources that helped you in
> reading the XMI and retrieving the data from it.
>
> I am currently starting up on EMF and its bit tricky.I have found
> where the xyz.example java file which others posts often refer to ,
> where the changes have to be made.
>
> Really appreciate your help.
>
> Thanks
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traversing all resources from XMI-file [message #1068027 is a reply to message #1068007] Thu, 11 July 2013 18:08 Go to previous messageGo to next message
bim boh is currently offline bim bohFriend
Messages: 11
Registered: July 2013
Junior Member
Thanks Ed,

Just to follow up, Actually what I was trying to do is read the contents of the file and display it on the console. what I am trying to read is a file that syntactically an XMI but its of an extension (.ecm),

Resource resource = resourceSet.createResource(URI.createURI("filepath:xyz.ecm\\"));

Do I need to mention the file name as .XMI or .ECM

Also any particular resource how to use load?

Thanks

Re: Traversing all resources from XMI-file [message #1068043 is a reply to message #1068027] Thu, 11 July 2013 19:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Do you have an Ecore model? Without that, nothing interesting will come
from reading XMI.

On 11/07/2013 8:08 PM, bim boh wrote:
> Thanks Ed,
>
> Just to follow up, Actually what I was trying to do is read the
> contents of the file and display it on the console.
You can just read the stream and spit it back out for that.
> what I am trying to read is a file that syntactically an XMI but its
> of an extension (.ecm),
It's just XML noise without a model
> Resource resource =
> resourceSet.createResource(URI.createURI("filepath:xyz.ecm\\"));
What's a URI like that supposed to mean?
>
> Do I need to mention the file name as .XMI or .ECM
You need to look at a concrete example.
>
> Also any particular resource how to use load?
There's an EMF book? Did you read the introductory material?
> Thanks
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traversing all resources from XMI-file [message #1068161 is a reply to message #1068043] Fri, 12 July 2013 13:26 Go to previous messageGo to next message
bim boh is currently offline bim bohFriend
Messages: 11
Registered: July 2013
Junior Member
@Ed,

Yes I do have ecore model.

Also after you suggested I grabbed the book on EMF and tried to use the load functionality.

At the moment I am stuck with this.

Caused by: org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'reports' not found. (file:/C:/Eclipse/report.ecm, 3, 49).

Thanks
Re: Traversing all resources from XMI-file [message #1068163 is a reply to message #1068161] Fri, 12 July 2013 13:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
It sounds like the serialization doesn't conform to the model, but since
I don't know the model and I can't see the serialization, there's not
much else I can say. Note that even when loading throws an exception,
the resource will still be populated to the extent that was possible, so
another call to resourceSet.getResource(<uri>, false) will yield the
resource and you can traverse starting with resource.getContents().


On 12/07/2013 3:26 PM, bim boh wrote:
> @Ed,
>
> Yes I do have ecore model.
>
> Also after you suggested I grabbed the book on EMF and tried to use
> the load functionality.
>
> At the moment I am stuck with this.
>
> Caused by: org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
> 'reports' not found. (file:/C:/Eclipse/report.ecm, 3, 49).
>
> Thanks


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traversing all resources from XMI-file [message #1068193 is a reply to message #1068163] Fri, 12 July 2013 15:32 Go to previous messageGo to next message
bim boh is currently offline bim bohFriend
Messages: 11
Registered: July 2013
Junior Member
Ed, Thanks you have been of great help. Previously there was some issue with serialization and the XMI was not conforming to Ecore.

I built a new ecore and generated an XMI and I want to traverse through the XMI.

This is my XMI:

<?xml version="1.0" encoding="ASCII"?>
<sampleModel:PurchaseOrder xmi:version="2.0" xmlns:xmi="httplink" xmlns:sampleModel="sampleModel" billTo="123 maple street">
<items productName="Apples"/>
</sampleModel:PurchaseOrder>

This is java file in test folder

ResourceSet resourceSet = new ResourceSetImpl();
// Register the appropriate resource factory to handle all file extensions.
//
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());

// Register the package to ensure it is available during loading.
//
resourceSet.getPackageRegistry().put
(SampleModelPackage.eNS_URI,
SampleModelPackage.eINSTANCE);

URI fileUri = URI.createFileURI(new File ("Model.ecm").getAbsolutePath());
Resource sampleModelResource = resourceSet.getResource(fileUri, true);
PurchaseOrder aPurchaseOrder= (PurchaseOrder)sampleModelResource.getContents().get(0);

Strangely , nothing actually happens. Am i doing this right?

Thanks
Re: Traversing all resources from XMI-file [message #1068195 is a reply to message #1068193] Fri, 12 July 2013 15:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Bim,

Comments below.

On 12/07/2013 5:32 PM, bim boh wrote:
> Ed, Thanks you have been of great help. Previously there was some
> issue with serialization and the XMI was not conforming to Ecore.
>
> I built a new ecore and generated an XMI and I want to traverse
> through the XMI.
>
> This is my XMI:
>
> <?xml version="1.0" encoding="ASCII"?>
> <sampleModel:PurchaseOrder xmi:version="2.0" xmlns:xmi="httplink"
> xmlns:sampleModel="sampleModel" billTo="123 maple street">
> <items productName="Apples"/>
> </sampleModel:PurchaseOrder>
>
> This is java file in test folder
>
> ResourceSet resourceSet = new ResourceSetImpl();
> // Register the appropriate resource factory to handle all
> file extensions.
> //
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
> (Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMIResourceFactoryImpl());
>
> // Register the package to ensure it is available during loading.
> //
> resourceSet.getPackageRegistry().put
> (SampleModelPackage.eNS_URI, SampleModelPackage.eINSTANCE);
> URI fileUri =
> URI.createFileURI(new File ("Model.ecm").getAbsolutePath());
> Resource sampleModelResource =
> resourceSet.getResource(fileUri, true);
> PurchaseOrder aPurchaseOrder=
> (PurchaseOrder)sampleModelResource.getContents().get(0);
> Strangely , nothing actually happens. Am i doing this right?
What are you expecting will happen? What do you mean by nothing? Now
you have your PurchaseOrder instance and can call the methods to access
the rest of the data. Either that or you're getting an exception in
which case you get a stack trace, not nothing.
>
> Thanks


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Traversing all resources from XMI-file [message #1068198 is a reply to message #1068195] Fri, 12 July 2013 16:04 Go to previous message
bim boh is currently offline bim bohFriend
Messages: 11
Registered: July 2013
Junior Member
Got it, Thank you so much.
Previous Topic:References in EMF
Next Topic:[CDO] Factory not found: org.eclipse.emf.cdo.server.queryHandlerFactories[ocl]
Goto Forum:
  


Current Time: Sat Apr 20 00:48:06 GMT 2024

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

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

Back to the top