我刚开始喜欢UML2并陷入以下问题。
我定义了一个包含两个构造型的配置文件:ServiceRequest和TransitionEdge。 ServiceRequest使用两个附加属性(cpu,memory)扩展Action元类,而TransitionEdge使用名为“probability”的附加属性扩展ActivityEdge元类。所有这些属性都是float类型,定义为PrimitiveType。
我创建了一个应用了配置文件和构造型的活动图。在活动图中,每个边都是在配置文件中定义的TransitionEdge,并且已将值分配给probability属性。
使用配置文件和活动模型,我使用以下Java代码加载它们。
// Load the profile
URI profileUri = URI.createURI(PROFILE_NAME);
ResourceSet profileSet = new ResourceSetImpl();
profileSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
profileSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
profileSet.createResource(profileUri);
Resource profileResource = profileSet.getResource(profileUri, true);
Profile profile = (Profile)EcoreUtil.getObjectByType(profileResource.getContents(), UMLPackage.Literals.PROFILE);
Profile sopraProfile = (Profile)profile.getOwnedMember(PROFILE_NAME);
Stereotype serviceRequestStereotype = (Stereotype)sopraProfile.getOwnedMember(STEREOTYPE_SERVICE_REQUEST);
Stereotype transitionEdgeStereotype = (Stereotype)sopraProfile.getOwnedMember(STEREOTYPE_TRANSITION_EDGE);
// Load the model
URI modelUri = URI.createURI(MODEL_NAME);
ResourceSet modelSet = new ResourceSetImpl();
modelSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
modelSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
modelSet.createResource(modelUri);
Resource modelResource = modelSet.getResource(modelUri, true);
Model model = (Model)EcoreUtil.getObjectByType(modelResource.getContents(), UMLPackage.Literals.MODEL);
EList<Element> elements = model.getOwnedElements();
for(Element e : elements){
if(e instanceof Activity){
Activity activity = (Activity)e;
EList<ActivityEdge> edges = activity.getEdges();
for(ActivityEdge edge : edges){
System.out.println(edge.getValue(transitionEdgeStereotype, "probability"));
}
}
}
在元素上调用getValue方法时抛出异常。 以下是错误消息:
org.eclipse.uml2.uml.internal.impl.PropertyImpl@7a6d6a3f (name: base_ActivityEdge, visibility: <unset>) (isLeaf: false) (isStatic: false) (isOrdered: false, isUnique: true, isReadOnly: false) (aggregation: none, isDerived: false, isDerivedUnion: false, isID: false)
org.eclipse.uml2.uml.internal.impl.PropertyImpl@255a8ce4 (name: probability, visibility: public) (isLeaf: false) (isStatic: false) (isOrdered: false, isUnique: true, isReadOnly: false) (aggregation: none, isDerived: false, isDerivedUnion: false, isID: false)
Exception in thread "main" java.lang.IllegalArgumentException: org.eclipse.uml2.uml.internal.impl.StereotypeImpl@442f4161 (name: TransitionEdge, visibility: <unset>) (isLeaf: false, isAbstract: false, isFinalSpecialization: false) (isActive: false)
at org.eclipse.uml2.uml.internal.operations.ElementOperations.getValue(ElementOperations.java:527)
at org.eclipse.uml2.uml.internal.impl.ElementImpl.getValue(ElementImpl.java:296)
at test.Test.main(Test.java:68)
我无法弄清楚发生了什么。有什么建议?非常感谢!!
答案 0 :(得分:2)
Element.getValue(...)方法需要传递实际的Stereotype实例,该实例与模型一起使用。
在第一个块中,您将从单独的ResourceSet中检索您的Stereotype实例,而不是在第二个块中用于模型的ResourceSet。因此,在加载模型时会再次加载配置文件,并创建另一个Stereotype实例。你需要将那个传递给Element.getValue(...)。
这是最安全的方法:
Activity activity = ...
Stereotype transitionEdgeStereotype =
activity.getAppliedStereotype(STEREOTYPE_TRANSITION_EDGE);
if (transitionEdgeStereotype != null) {
System.out.println(edge.getValue(transitionEdgeStereotype, "probability"));
}
修改:在这种情况下,STEREOTYPE_TRANSITION_EDGE需要是构造型的完全限定名称,包括配置文件名称,例如: ServiceProfile :: TransitionEdge。