1. 在ecore模型中默认的引用是根据相对位置来索引的。
Example:<faults mappingAlarm="//@alarmRoot/@alarms.0"> 0为对应的相对位置
默认状态下的引用在实际运用中存在很多问题(特别在不同Ecore之间的关系引用),因为相对位置是会变化的。
2.根据某个属性值去引用:
在属性的容器的Impl类中重载eURIFragmentSegment()和eObjectForURIFragmentSegment()方法 如下(用%分割,后面跟属性值):
@Override
public String eURIFragmentSegment(EStructuralFeature eStructuralFeature,
EObject eObject) {
if (eObject instanceof ObjectEntity) {
StringBuilder result = new StringBuilder();
result.append('@');
if (eStructuralFeature != null) {
result.append(eStructuralFeature.getName());
result.append("%");
}
ObjectEntity entity = (ObjectEntity) eObject;
result.append(entity.getName());
return result.toString();
}
return super.eURIFragmentSegment(eStructuralFeature, eObject);
}
@Override
public EObject eObjectForURIFragmentSegment(String uriFragmentSegment) {
if (uriFragmentSegment.indexOf("%") > 0) {
int lastIndex = uriFragmentSegment.length() - 1;
int dotIndex = -1;
dotIndex = uriFragmentSegment.lastIndexOf('%', lastIndex - 1);
if (dotIndex >= 0) {
EStructuralFeature feature = eStructuralFeature(uriFragmentSegment
.substring(1, dotIndex));
EList<?> eList = (EList<?>) eGet(feature, false);
String objInfo = uriFragmentSegment.substring(dotIndex + 1);
for (Object obj : eList) {
if (feature == ModelPackage.eINSTANCE.getRoot_Mos()||feature == ModelPackage.eINSTANCE.getRoot_Ros()) {
if (obj instanceof ObjectEntity) {
ObjectEntity entity = (ObjectEntity) obj;
if (String.valueOf(entity.getName()).equals(objInfo)) {
return entity;
}
}
}
}
}
if (dotIndex < 0) {
return (EObject) eGet(
eStructuralFeature(uriFragmentSegment.substring(1)),
false);
}
}
return super.eObjectForURIFragmentSegment(uriFragmentSegment);
}
private EStructuralFeature eStructuralFeature(String name)
throws IllegalArgumentException {
EStructuralFeature eStructuralFeature = eClass().getEStructuralFeature(
name);
if (eStructuralFeature == null) {
throw new IllegalArgumentException("The feature '" + name
+ "' is not a valid feature");
}
return eStructuralFeature;
}
效果:
<elements name="test" commandCode="321434" chnDesc="" engDesc="" isLocalCommand="2"
moRef="../moro/Test1.model#//@ros%one">