0

I want to get the value of the EReference from a Java program

I use this library: https://github.com/tesis-dynaware/graph-editor

Here is an example of xmi file generated :

<?xml version="1.0" encoding="ASCII"?>
<graph:GModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:graph="http://de.tesis.dynaware.grapheditor.model/1.0">
  <nodes type="tree-node" x="1217.0" y="556.0">
    <connectors type="tree-output" connections="//@connections.0 //@connections.1" connectionDetachedOnDrag="false"/>
  </nodes>
  <nodes type="tree-node" x="1020.0" y="756.0">
    <connectors type="tree-input" connections="//@connections.0"/>
    <connectors type="tree-output" connectionDetachedOnDrag="false"/>
  </nodes>
  <nodes type="tree-node" x="1260.0" y="775.0">
    <connectors type="tree-input" connections="//@connections.1"/>
    <connectors type="tree-output" connectionDetachedOnDrag="false"/>
  </nodes>
  <connections type="tree-connection" source="//@nodes.0/@connectors.0" target="//@nodes.1/@connectors.0"/>
  <connections type="tree-connection" source="//@nodes.0/@connectors.0" target="//@nodes.2/@connectors.0"/>
</graph:GModel>

What i want is to get the value of the EReference source, and i must get for exemple : /@nodes.0/@connectors.0

I tried the code below

GModel gModel=model;
      EList<GConnection> connections=gModel.getConnections();
      for(GConnection connection : connections)
      {
          GConnector source=connection.getSource();
          System.out.println(" valeur source =" +source);
      }

but i obtain the result below :

valeur source=de.tesis.dynaware.grapheditor.model.impl.GConnectorImpl@21dd34be (id: null, type: tree-output, x: 0.0, y: 0.0, connectionDetachedOnDrag: false)

  • I don't fully understand what you're trying to accomplish. `/@nodes.0/@connectors.0` is the cross reference to the first connector Object in the first node Object in the tree, therefore the `source` reference should retrieve it correctly.... Or do you want to actually retrieve the String value `/@nodes.0/@connectors.0` ? Can you be a bit more specific? – Mad Matts Dec 07 '16 at 13:33

1 Answers1

1

After I read it again I suppose I understood your question. You're trying to get the actual EObject at a specific location eg: //@nodes.0/@connectors.0? This path is called the URIfragment. With an URIfragment you can get the EObject which is stored at this position from the root of the resource tree. You need to put your GModelin an emf org.eclipse.emf.ecore.resource.Resource, if does not have one yet (you can check gModel.eResource())

Suppose gModel.eResource() == null:

//create a new Resource
Resource resource = new ResourceImpl();
//add the gModel
resource.getContents().add(gModel);
//get the EObject from the URIfragment
GConnector connector = (GConnector)resource.getEObject("//@nodes.0/@connectors.0");

assert resource.getURIFragment(connector).equals("//@nodes.0/@connectors.0");
Mad Matts
  • 1,118
  • 10
  • 14
  • Thank you for your answer. What i want is to get the URIfragment. – user5772710 Dec 07 '16 at 15:44
  • Is ot possible to get this : //@nodes.0/@connectors.0 as a String? – user5772710 Dec 07 '16 at 15:50
  • 1
    `resource.getURIFragment(EObject eObject)` returns the URI fragment as String (The path in the containment tree to the eObject) – Mad Matts Dec 07 '16 at 16:24
  • Yes it works. But in my file xmi above, I use : final URI fileUri = URI.createFileURI(absolutePath), if I want to generate only the id of the node, here (0), how can i process ? Thanks – user5772710 Dec 09 '16 at 14:22
  • Do you want to parse the XMI file to EObjects? – Mad Matts Dec 09 '16 at 17:36
  • have a look at [how to read eobjects from a ecore file](http://stackoverflow.com/a/38249127/6242846) you can combine this, first load the EObject tree from the file and then you can use the resource to get the specific EObject from your uri fragment – Mad Matts Dec 11 '16 at 20:50