1

我是 eclipse UML2 的新手,遇到了以下问题。

我定义了一个具有两个原型的配置文件:ServiceRequest 和 TransitionEdge。ServiceRequest 使用两个附加属性(cpu、内存)扩展了 Action 元类,TransitionEdge 使用称为“概率”的附加属性扩展了 ActivityEdge 元类。所有这些属性都是定义为 PrimitiveType 的 float 类型。

我创建了一个应用了配置文件和原型的活动图。在活动图中,每条边都是配置文件中定义的一个TransitionEdge,并且已经为概率属性分配了一个值。

有了配置文件和活动模型,我使用以下 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)

我无法弄清楚发生了什么事。有什么建议么?非常感谢!!

4

1 回答 1

2

Element.getValue(...) 方法期望传递实际的 Stereotype 实例,该实例与模型一起使用。

在第一个块中,您从一个单独的 ResourceSet 中检索您的 Stereotype 实例,而不是用于第二个块中的模型的资源集。因此,在加载模型时会再次加载配置文件,并创建另一个 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。

于 2013-03-13T10:06:05.890 回答