使用UML2解析XMI文件,如何处理构造型

时间:2013-08-05 09:10:44

标签: java parsing uml emf xmi

我使用modelio生成了一个UML文件。 我在课堂上应用了刻板印象。

我的.uml文件:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:LocalProfile="http:///schemas/LocalProfile/_TS6FwP2iEeKR1arwT6_zsA/0" xmlns:default="http:///schemas/default/_TTEd0P2iEeKR1arwT6_zsA/0" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" xsi:schemaLocation="http:///schemas/LocalProfile/_TS6FwP2iEeKR1arwT6_zsA/0 LocalProfile.profile.uml#_TUTM5v2iEeKR1arwT6_zsA http:///schemas/default/_TTEd0P2iEeKR1arwT6_zsA/0 default.profile.uml#_TUkSp_2iEeKR1arwT6_zsA">
  <uml:Model xmi:id="_TSufkP2iEeKR1arwT6_zsA" name="xmitojava">
    <eAnnotations xmi:id="_TSufkf2iEeKR1arwT6_zsA" source="Objing">
      <contents xmi:type="uml:Property" xmi:id="_TSufkv2iEeKR1arwT6_zsA" name="exporterVersion">
        <defaultValue xmi:type="uml:LiteralString" xmi:id="_TSufk_2iEeKR1arwT6_zsA" value="2.2"/>
      </contents>
    </eAnnotations>
    <ownedComment xmi:id="_TSuflP2iEeKR1arwT6_zsA">
      <body></body>
    </ownedComment>
    <packagedElement xmi:type="uml:Package" xmi:id="_TSuflf2iEeKR1arwT6_zsA" name="com.imcfr.pkg">
      <packagedElement xmi:type="uml:Class" xmi:id="_TSuflv2iEeKR1arwT6_zsA" name="Group">
        <ownedAttribute xmi:id="_TSufl_2iEeKR1arwT6_zsA" name="label" visibility="private" isUnique="false" isReadOnly="true">
          <type xmi:type="uml:PrimitiveType" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String"/>
        </ownedAttribute>
      </packagedElement>
      <packagedElement xmi:type="uml:Association" xmi:id="_TSufmP2iEeKR1arwT6_zsA" memberEnd="_TSufmf2iEeKR1arwT6_zsA _TSufn_2iEeKR1arwT6_zsA">
        <ownedEnd xmi:id="_TSufmf2iEeKR1arwT6_zsA" type="_TSufnP2iEeKR1arwT6_zsA" association="_TSufmP2iEeKR1arwT6_zsA">
          <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="_TSufmv2iEeKR1arwT6_zsA" value="*"/>
          <lowerValue xmi:type="uml:LiteralInteger" xmi:id="_TSufm_2iEeKR1arwT6_zsA"/>
        </ownedEnd>
      </packagedElement>
      <packagedElement xmi:type="uml:Class" xmi:id="_TSufnP2iEeKR1arwT6_zsA" name="User">
        <ownedAttribute xmi:id="_TSufnf2iEeKR1arwT6_zsA" name="login" visibility="protected" isUnique="false" isReadOnly="true">
          <type xmi:type="uml:PrimitiveType" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String"/>
        </ownedAttribute>
        <ownedAttribute xmi:id="_TSufnv2iEeKR1arwT6_zsA" name="password" visibility="protected" isUnique="false" isReadOnly="true">
          <type xmi:type="uml:PrimitiveType" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String"/>
        </ownedAttribute>
        <ownedAttribute xmi:id="_TSufn_2iEeKR1arwT6_zsA" name="group" type="_TSuflv2iEeKR1arwT6_zsA" isReadOnly="true" aggregation="shared" association="_TSufmP2iEeKR1arwT6_zsA">
          <lowerValue xmi:type="uml:LiteralInteger" xmi:id="_TSufoP2iEeKR1arwT6_zsA"/>
        </ownedAttribute>
      </packagedElement>
    </packagedElement>
    <profileApplication xmi:id="_TVRdQP2iEeKR1arwT6_zsA">
      <eAnnotations xmi:id="_TVT5gP2iEeKR1arwT6_zsA" source="http://www.eclipse.org/uml2/2.0.0/UML">
        <references xmi:type="ecore:EPackage" href="LocalProfile.profile.uml#_TUTM5v2iEeKR1arwT6_zsA"/>
      </eAnnotations>
      <appliedProfile href="LocalProfile.profile.uml#_TUSl0P2iEeKR1arwT6_zsA"/>
    </profileApplication>
    <profileApplication xmi:id="_TVUgkP2iEeKR1arwT6_zsA">
      <eAnnotations xmi:id="_TVUgkf2iEeKR1arwT6_zsA" source="http://www.eclipse.org/uml2/2.0.0/UML">
        <references xmi:type="ecore:EPackage" href="default.profile.uml#_TUkSp_2iEeKR1arwT6_zsA"/>
      </eAnnotations>
      <appliedProfile href="default.profile.uml#_TUkSoP2iEeKR1arwT6_zsA"/>
    </profileApplication>
  </uml:Model>
  <LocalProfile:Stereo xmi:id="_TVWVwP2iEeKR1arwT6_zsA" base_Class="_TSuflv2iEeKR1arwT6_zsA"/>
  <default:metaclass xmi:id="_TVW80P2iEeKR1arwT6_zsA" base_Classifier="_TSufnP2iEeKR1arwT6_zsA"/>
</xmi:XMI>

我的default.profile.umlLocalProfil.profile.uml位于与.uml文件相同的文件夹中。

我使用此代码获取我的UML2模型:

URI typesUri = URI.createFileURI(path);
ResourceSet set = new ResourceSetImpl();
set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
set.createResource(typesUri);
Map<URI, URI> uriMap = set.getURIConverter().getURIMap();
URI uri = URI.createURI("jar:file:/C:/Users/ffischer/Desktop/org.eclipse.uml2.uml.resources_3.1.1.v201008191505.jar!/");
uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));
Resource r = set.getResource(typesUri, true);

Model m = (Model) EcoreUtil.getObjectByType(r.getContents(), UMLPackage.Literals.MODEL);

一切都很好,除了我不能得到与我的班级相关的刻板印象。 (例如,此代码向我发送空数组:)

ClassImpl _c = (ClassImpl) el;
System.out.println(_c.getName() + " stereotypes :");
System.out.println(_c.getStereotypeApplications());
System.out.println(_c.getApplicableStereotypes());
System.out.println(_c.getAppliedStereotypes());

我想也许我忘了做一步,但我不知道哪一步。

我希望有人可以帮助我:)。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

uriMap.put(URI.createURI("LocalProfile.profile.uml"), URI.createURI("src/main/resources/LocalProfile.profile.uml"));
        uriMap.put(URI.createURI("default.profile.uml"), URI.createURI("src/main/resources/default.profile.uml"));

确实,错误的道路。现在,一切正常,希望这篇文章能帮助别人:)