Search code examples
javaeclipseemfmetamodelxmi

Loading multiple xmi files with EMF


I've been using EMF for three weeks. I already created my meta-model and an instance using the running Eclipse application. Now my problem is that I have multiple XMI files saved by the running instance of Eclipse and I want to parse them in order to create another text file processing the data in the xmi files. To do so I'm using XMIResource API of EMF and when I load a file without cross-references to other xmi documents everything goes well. However when I load a xmi document with cross-references to other xmi documents all the cross-references are not saved properly in my Java model.

I used this piece of code to load the xmi files:

XMIResource resourceInTy = new XMIResourceImpl(URI.createURI("file:/runtime-New_configuration/ApplicationInstance/instancesTypes.model"));
    resourceInTy.load(null);
    Environment env1 = (Environment) resourceInTy.getContents().get(0);

    XMIResource resourceContRel = new XMIResourceImpl(URI.createURI("file:/runtime-New_configuration/ApplicationInstance/ContextRelations.model"));
    resourceContRel.load(null);
    Environment env2 = (Environment) resourceContRel.getContents().get(0);

    env.setTypes(env1.getTypes());
    env.setInstances(env1.getInstances());
    env.setContextRelations(env2.getContextRelations());

As you can see the object "Environment" is my root object of all the meta-model that should contain all the EList objects properly set after I parse the xmi files.

The problem here is that inside the EList "ContextRelations" there should be a reference to another object called "Parameter" that has a reference to the object Type. If I try to retrieve type a null reference comes out! I deeply searched for another question about this problem but the closest one I found was a no-answered question available at this link: Loading Multiple files using EMF resource load?

Here there are the two xmi files: ContextRelations.model

<parameters number="5">
  <type href="instancesTypes.model#//@types.0"/>
</parameters>
<parameters number="6">
  <type href="instancesTypes.model#//@types.4"/>
</parameters>
<contextRelations name="in"parameters="//@parameters.0//@parameters.1">
  <initialComplexEvent href="Events.model#//@events.0"/>
  <endingComplexEvent href="Events.model#//@events.1"/>
</contextRelations>

InstancesTypes.model

<types name="Emp"/>
<types name="Cam"/>
<types name="Comp"/>
<types name="Fi"/>
<types name="Loc"/>
<types name="St"/>
<types name="Read"/>
<instances name="Alice" type="//@types.0"/>
<instances name="r01" type="//@types.4"/>
<instances name="Bob" type="//@types.0"/>
<instances name="cctv1" type="//@types.1"/>
<instances name="doc" type="//@types.3"/>
<instances name="m1" type="//@types.2"/>
<instances name="m2" type="//@types.2"/>
<instances name="m3" type="//@types.2"/>
<instances name="nfc01" type="//@types.6"/>
<instances name="usb1" type="//@types.5"/>

Secondary question:

As you can see I use three root objects: env1 and env2 are used as temporary variables that put their contents in the main root object (env). I do that because I don't know how to parse all the xmi files simultaneously creating just one root object.

Anybody can help me please?


Solution

  • I solved the problem thanks to this web page: http://www.bar54.de/2012/04/emf-load-model-with-eproxyuri-references/

    Basically I had to create a resource set putting all the resources inside the set in order to solve the proxies (cross-references).