2013-03-13 74 views
1

我是eclipse UML2的新手,遇到以下問題。Eclipse UML2:獲取配置文件中定義的屬性的值

我定義了一個具有兩個原型的配置文件:ServiceRequest和TransitionEdge。 ServiceRequest使用兩個附加屬性(cpu,memory)擴展了Action元類,TransitionEdge使用名爲「probability」的附加屬性擴展了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方法時引發異常。 以下是錯誤消息:

[email protected] (name: base_ActivityEdge, visibility: <unset>) (isLeaf: false) (isStatic: false) (isOrdered: false, isUnique: true, isReadOnly: false) (aggregation: none, isDerived: false, isDerivedUnion: false, isID: false) 

[email protected] (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: [email protected] (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) 

我無法弄清楚發生了什麼事情。有什麼建議麼?非常感謝!!

回答

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。

+0

完美的作品。我從模型中獲取原型,然後將其傳遞給getValue方法。謝謝!! – 2013-03-13 21:12:44

相關問題