我想从Java程序中获取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 source = de.tesis.dynaware.grapheditor.model.impl.GConnectorImpl@21dd34be(id:null,type:tree-output,x:0.0,y:0.0,connectionDetachedOnDrag:false)
答案 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");