12.3.5 示例

图12.19展示了一个Extension的简单示例,其中Stereotype Home扩展了元类Interface。

Example of Using an Extension

图12.19 Example of Using an Extension

stereotype Home的实例可以按序附加到或从一个Interface的实例移除,这提供了一种灵活的向包动态添加(或删除)Profile特定的信息的方法。

在图12.20中,应用了该Profile的模型中的每个元类Component的实例都必须应用一个stereotype Bean的实例,因为该Extension的isRequired=true。(因为该stereotype Bean是抽象的,这意味着元类的每个实例都必须被Bean的某个具体子类到的实例所stereotyped。)如果没有应用这样的一个Stereotype,则这个模型是非良构的。这为表示Extension需要依赖哪些Proflies来展示提供了一种方法。

Example of a Required Extension

图12.20 Example of a Required Extension

图12.21展示了一个EJB profile的简单示例。

Defining a Simple EJB Profile

图12.21 Defining a Simple EJB Profile

该Profile定义了抽象的stereotype Bean需要被应用到元类Component上,这意味着Bean的具体子类Entity或Session的一个实例要链接到每个Component实例上。作为Profile一部分的约束是在该Profile被应用到一个包时来检查的,为了使模型是良构的,这些约束必须要被满足。

Importing a Package from a Profile

图12.22 Importing a Package from a Profile

在图12.22中,包Types被Profile Manufacturer所引入。从而枚举Color和类JavaInteger如同标准的基本类型String那样在名为Device的Stereotype中用作属性的类型。

如果之后Profile Manufacturer被应用到一个包,那么来自Types的类型在该包中并不可用,除非该包显式的引用Types包。这意味着JavaInteger可以用作Stereotype属性的类型(例如在Device中)而不能作为一个普通属性的类型(作为类TV的一部分),除非包Factory也引入了包Types(它的确引入了)。

注意: 一旦Stereotype Device被应用到类TVvolume属性的值就被显式。

在给定Profiles Java和EJB的情况下,图12.23展示了它们如何被应用到包WebShopping

Profiles Applied to a Package

图12.23 Profiles Applied to a Package

在图12.24中,stereotype Clock被定义为可以按需应用到元类Class的实例上。

Defining a Stereotype

图12.24 Defining a Stereotype

Presentation Options for an Extended Class

图12.25 Presentation Options for an Extended Class

图12.26展示了在图12.24中示例的一个实例图。

注意:ExtensionEnd必须是组合的,此时衍生的isRequired属性是假。

图12.26展示了图12.24中定义的Stereotype Clock的实例。在这个定义中,被定义的元类(:Class; “name = Class”)是在UML2元模型(引用元模型)中定义的。在UML建模工具中,这种UML2标准元模型通常展现为“只读”形式,或者表示为被扩展元类的代理。

(因此它仍然与UML在相同的元层,并没有展示被stereotype扩展的模型的模型实例。这样的示例在图12.28和图12.29中。)Extension概念的语义解释了MOF等同的约束如何被附加到stereotypes上。

An Instance Diagram when Defining a Stereotype

图12.26 An Instance Diagram when Defining a Stereotype

图12.27展示了相同的名为Clock的Stereotype扩展了元类Component和Class(尽管Stereotype的每个实例只能扩展一个模型元素)。它还展示了不同的Stereotype如何扩展相同的元类。

Defining Multiple Stereotypes on Multiple Stereotypes

图12.27 Defining Multiple Stereotypes on Multiple Stereotypes

图12.28展示了图12.27中定义的Stereotype Clock如何应用到一个名为StopWatch的类。

Using a Stereotype

图12.28 Using a Stereotype

图12.29展示了当名为Clock的Stereotype应用到类StopWatch时的底层语义。右手边使用了实例图表示法来展示使用MOF等同的实例来理解对左边的UML图进行XMI序列化和行为理解。Stereotype和元类Class之间的Extension导致Stereotype Clock的实例和名为StopWatch的类之间的一个链接。

Showing Values of Stereotypes and a Simple Instance Specification

图12.29 Showing Values of Stereotypes and a Simple Instance Specification

接下来,如图12.30所示,Clock和Creator两个stereotypes应用到相同的模型元素上。

注意:每个应用的Stereotypes的属性值展示在注释符号内,附加到模型元素上。

Using Stereotypes and Showing Values

图12.30 Using Stereotypes and Showing Values

最后,另外两种表示法的图示在图12.31中展示。

Other Notational Forms for Depicting Stereotype Values

图12.31 Other Notational Forms for Depicting Stereotype Values

图12.32展示了一个具有profile定义的类、二元组合,以及非组合的关联的profile的示例。

Example of a Profile defining Classes and binary composite and non-composite Associations

图12.32 Example of a Profile defining Classes and binary composite and non-composite Associations

如下展示了图12.32中的profile的XMI序列化:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/YYYYMMnn"
      xmlns:uml="http://www.omg.org/spec/UML/YYYYMMnn" xmlns:mofext="http://www.omg.org/spec/MOF/YYYYMMnn">
  <uml:Profile xmi:type="uml:Profile" URI="http://www.example.org/IssuesProfile" xmi:id="id0"
        name="IssuesProfile" metamodelReference="id309">
    <packageImport xmi:id="id309">
      <importedPackage xmi:type="uml:Model" href="http://www.omg.org/spec/UML/YYYYMMnn/UML.xmi#_0"/>
    </packageImport>
    <packagedElement xmi:type="uml:Class" xmi:id="id312" name="IssueDetail">
      <ownedAttribute xmi:id="id315" name="number">
        <type xmi:type="uml:PrimitiveType"
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#Integer"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id318" name="url">
        <type xmi:type="uml:PrimitiveType"
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#String"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id321" name="text">
        <type xmi:type="uml:PrimitiveType"
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#String"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id324" name="issueComment" type="id343" aggregation="composite"
           association="id352">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id328" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id331"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id334" name="duplicatesIssue" type="id312" association="id364">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id337" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id340"/>
      </ownedAttribute>
    </packagedElement>
    <packagedElement xmi:type="uml:Class" xmi:id="id343" name="IssueComment">
      <ownedAttribute xmi:id="id346" name="number">
        <type xmi:type="uml:PrimitiveType"
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#Integer"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id349" name="text">
        <type xmi:type="uml:PrimitiveType"
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#String"/>
      </ownedAttribute>
    </packagedElement>
    <packagedElement xmi:type="uml:Association" xmi:id="id352" name="A_issueDetail_issueComment"
        memberEnd="id324 id355">
      <ownedEnd xmi:id="id355" name="issueDetail" type="id312" association="id352"/>
    </packagedElement>
    <packagedElement xmi:type="uml:Association" xmi:id="id364" name="A_issueDetail_duplicatesIssue"
        memberEnd="id334 id367">
      <ownedEnd xmi:id="id367" name="issueDetail" type="id312" association="id364">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id371" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id374"/>
      </ownedEnd>
    </packagedElement>
    <packagedElement xmi:type="uml:Stereotype" xmi:id="id377" name="IssueTag">
      <ownedAttribute xmi:id="id380" name="base_Element" association="id418">
        <type xmi:type="uml:Class" href="http://www.omg.org/spec/UML/YYYYMMnn/UML.xmi#Element"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id383" name="reviewed">
        <type xmi:type="uml:PrimitiveType" 
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#Boolean"/>
        <defaultValue xmi:type="uml:LiteralBoolean" xmi:id="id386"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id389" name="issueDetail" type="id312" aggregation="composite" association="id424">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id393" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id396"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id399" name="affectedBy" type="id312" association="id436">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id403" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id406"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="id409" name="relatedElement" association="id448">
        <type xmi:type="uml:Class" href="http://www.omg.org/spec/UML/YYYYMMnn/UML.xmi#Element"/>
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id412" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id415"/>
      </ownedAttribute>
    </packagedElement>
    <packagedElement xmi:type="uml:Extension" xmi:id="id418" name="Element_IssueTag" memberEnd="id421 id380">
      <ownedEnd xmi:type="uml:ExtensionEnd" xmi:id="id421" name="extension_IssueTag" type="id377" 
          aggregation="composite" association="id418"/>
    </packagedElement>
    <packagedElement xmi:type="uml:Association" xmi:id="id424" name="A_issueTag_issueDetail" 
        memberEnd="id389 id427">
      <ownedEnd xmi:id="id427" name="issueTag" type="id377" association="id424"/>
    </packagedElement>
    <packagedElement xmi:type="uml:Association" xmi:id="id436" name="A_issueTag_affectedBy"
        memberEnd="id399 id439">
      <ownedEnd xmi:id="id439" name="issueTag" type="id377" association="id436">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id442" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id445"/>
      </ownedEnd>
    </packagedElement>
    <packagedElement xmi:type="uml:Association" xmi:id="id448" name="A_issueTag_relatedElement"
        memberEnd="id409 id451">
      <ownedEnd xmi:id="id451" name="issueTag" type="id377" association="id448">
        <upperValue xmi:type="uml:LiteralUnlimitedNatural" xmi:id="id454" value="*"/>
        <lowerValue value="0" xmi:type="uml:LiteralInteger" xmi:id="id457"/>
      </ownedEnd>
    </packagedElement>
  </uml:Profile>
  <mofext:Tag xmi:type="mofext:Tag" org.omg.xmi.nsURI="http://www.example.org/IssuesProfile"/>
  <mofext:Tag xmi:type="mofext:Tag" org.omg.xmi.nsPrefix="IssuesProfile"/>
</xmi:XMI>

图12.33展示了应用图12.32中的profile的示例。

Diagram example of applying a profile defining Classes and Associations and of creating instances of such Classes. Tools can provide a notation similar to that of object diagrams for instances of Profile-defined Classes, DataTypes and Associations

图12.33 Diagram example of applying a profile defining Classes and Associations and of creating instances of such Classes. Tools can provide a notation similar to that of object diagrams for instances of Profile-defined Classes, DataTypes and Associations

如下展示了图12.33中示例的XMI序列化(不带profile定义的关联的链接实例):

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/YYYYMMnn"
    xmlns:uml="http://www.omg.org/spec/UML/YYYYMMnn" xmlns:IssuesProfile="http://www.example.org/IssuesProfile">
  <uml:Package xmi:id="ex0" name="IssueExample">
    <packagedElement xmi:type="uml:Class" xmi:id="ex57" name="B">
      <generalization xmi:id="ex60" general="ex65"/>
      <ownedAttribute xmi:id="ex63" name="x">
        <type xmi:type="uml:PrimitiveType" 
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#String"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="ex64" name="y"/>
    </packagedElement>
    <packagedElement xmi:type="uml:Class" xmi:id="ex65" name="A">
      <ownedAttribute xmi:id="ex69" name="width"/>
      <ownedAttribute xmi:id="ex70" name="length"/>
    </packagedElement>
    <profileApplication xmi:id="ex77">
      <appliedProfile href="http://www.example.org/IssuesProfile#id0"/>
    </profileApplication>
  </uml:Package>
  <IssuesProfile:IssueTag xmi:id="ex66" base_Element="ex65" relatedElement="ex69 ex70">
    <issueDetail xmi:id="ex67" number="1" url="http://www.example.org/issues/1" 
        text="Some attributes lack a type.">
      <issueComment xmi:id="ex68" number="3" text="Type should be string."/>
    </issueDetail>
  </IssuesProfile:IssueTag>
  <IssuesProfile:IssueTag xmi:id="ex58" base_Element="ex57" affectedBy="ex66 ex61" relatedElement="ex64">
    <issueDetail xmi:id="ex59" number="2" url="http://www.example.org/issues/2" 
        text="Some attributes lack a type." duplicatesIssue="ex67"/>
  </IssuesProfile:IssueTag>
  <IssuesProfile:IssueTag xmi:id="ex61" base_Element="ex60">
    <issueDetail xmi:id="ex62" number="4" url="http://www.example.org/issues/4"
        text="Why does B specialize A?"/>
  </IssuesProfile:IssueTag>
</xmi:XMI>

如下展示了图12.33中示例的XMI序列化(带profile定义的关联的链接实例):

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/YYYYMMnn"
    xmlns:uml="http://www.omg.org/spec/UML/YYYYMMnn" xmlns:IssuesProfile="http://www.example.org/IssuesProfile">
  <uml:Package xmi:id="ex0" name="IssueExample">
    <packagedElement xmi:type="uml:Class" xmi:id="ex73" name="B">
      <generalization xmi:id="ex76" general="ex81"/>
      <ownedAttribute xmi:id="ex79" name="x">
        <type xmi:type="uml:PrimitiveType" 
            href="http://www.omg.org/spec/UML/YYYYMMnn/PrimitiveTypes.xmi#String"/>
      </ownedAttribute>
      <ownedAttribute xmi:id="ex80" name="y"/>
    </packagedElement>
    <packagedElement xmi:type="uml:Class" xmi:id="ex81" name="A">
      <ownedAttribute xmi:id="ex85" name="width"/>
      <ownedAttribute xmi:id="ex86" name="length"/>
    </packagedElement>
    <packagedElement xmi:type="uml:Package" xmi:id="ex87" name="PrimitiveTypes"/>
    <profileApplication xmi:id="ex97">
      <appliedProfile href="http://www.example.org/IssuesProfile#id0"/>
    </profileApplication>
  </uml:Package>
  <IssuesProfile:IssueTag xmi:id="ex82" base_Element="ex81" relatedElement="ex85 ex86">
    <issueDetail xmi:id="ex83" number="1" url="http://www.example.org/issues/1"
        text="Some attributes lack a type.">
      <issueComment xmi:id="ex84" number="3" text="Type should be string."/>
    </issueDetail>
  </IssuesProfile:IssueTag>
  <IssuesProfile:A_issueTag_issueDetail xmi:id="d1e109" issueTag="ex82" issueDetail="ex83"/>
  <IssuesProfile:A_issueDetail_issueComment xmi:id="d1e110" issueDetail="ex83" issueComment="ex84"/>
  <IssuesProfile:A_issueTag_relatedElement xmi:id="ex82ex85" issueTag="ex82" relatedElement="ex85"/>
  <IssuesProfile:A_issueTag_relatedElement xmi:id="ex82ex86" issueTag="ex82" relatedElement="ex86"/>
  <IssuesProfile:IssueTag xmi:id="ex74" base_Element="ex73" affectedBy="ex82 ex77"
      relatedElement="ex80">
    <issueDetail xmi:id="ex75" number="2" url="http://www.example.org/issues/2"
        text="Some attributes lack a type." duplicatesIssue="ex83"/>
  </IssuesProfile:IssueTag>
  <IssuesProfile:A_issueTag_issueDetail xmi:id="d1e112" issueTag="ex74" issueDetail="ex75"/>
  <IssuesProfile:A_issueDetail_duplicatesIssue xmi:id="ex75ex83" issueDetail="ex75"
      duplicatesIssue="ex83"/>
  <IssuesProfile:A_issueTag_affectedBy xmi:id="ex74ex82" issueTag="ex74" affectedBy="ex82"/>
  <IssuesProfile:A_issueTag_affectedBy xmi:id="ex74ex77" issueTag="ex74" affectedBy="ex77"/>
  <IssuesProfile:A_issueTag_relatedElement xmi:id="ex74ex80" issueTag="ex74" relatedElement="ex80"/>
  <IssuesProfile:IssueTag xmi:id="ex77" base_Element="ex76">
    <issueDetail xmi:id="ex78" number="4" url="http://www.example.org/issues/4"
        text="Why does B specialize A?"/>
  </IssuesProfile:IssueTag>
  <IssuesProfile:A_issueTag_issueDetail xmi:id="d1e114" issueTag="ex77" issueDetail="ex78"/>
</xmi:XMI>

results matching ""

    No results matching ""