繁体   English   中英

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

[英]Using UML2 to parse a XMI file, how to handle stereotypes

我使用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>

我的.uml文件.uml文件夹中有default.profile.umlLocalProfil.profile.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());

我想也许我忘了走一步,但我看不到。

我希望有一个人可以帮助我 :)

我找到了解决方案:

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"));

确实,这是一条错误的道路。 现在,一切正常,希望这篇文章对其他人有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM