2016-12-06 127 views
0

我想从一个Java程序获得的EReference的价值是否有可能通过名称获得EReference的价值?

我使用这个库:https://github.com/tesis-dynaware/graph-editor

这里是生成一个XMI文件的例子:

<?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> 

什么,我要的是获取EReference源的值,我必须得到的例子:/@nodes.0/@connectors.0

我试了下面的代码

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

,但我得到下面的结果:

valeur [email protected]1dd34be(ID:空,类型:树输出中,x:0.0,Y :0.0,connectionDetachedOnDrag:false)

+0

我不完全理解你想要完成的事情。 '/ @ nodes.0/@ connectors.0'是对树中第一个节点Object中第一个连接器Object的交叉引用,因此'source'引用应该正确地检索它....或者你想实际上检索字符串值'/ @ nodes.0/@ connectors.0'?你能更具体一点吗? –

回答

1

我再次阅读后,我想我明白你的问题。 您正试图在特定位置获得实际的EObject,例如://@nodes.0/@connectors.0? 这条路径被称为URIfragment。使用URIfragment,您可以从资源树的根目录获取存储在此位置的EObject。 你需要把你的GModel在EMF org.eclipse.emf.ecore.resource.Resource,如果没有这方面的资料(您可以检查gModel.eResource()

假设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"); 
+0

谢谢你的回答。我想要的是获取URIfragment。 – user5772710

+0

是否有可能得到这个://@nodes.0/@connectors.0作为一个字符串? – user5772710

+1

'resource.getURIFragment(EObject eObject)'将URI片段返回为String(包含树中eObject的路径) –

相关问题