为何UML中CollaborationUse的classifier关联端 multiplicity设为[0..1]而非1?
UML规范中CollaborationUse相关问题解析
一、CollaborationUse的classifier关联端为何是[0..1]而非1?
根据UML规范的明确要求:
第265页11.8.5节:CollaborationUse作为NamedElement的特化,拥有
type : Collaboration [1..1]属性,指向其基础协作。
第7.2.3.1节:模型中除顶级包外的每个Element必须被恰好一个其他Element拥有。
但在“Collaborations”抽象语法图中,classifier关联端的multiplicity被标注为[0..1],核心原因是工具解析XMI时,LiteralInteger默认值与MultiplicityElement的解析规则产生了冲突:
1. XMI定义的差异对比
- CollaborationUse的classifier关联端(UML.xmi第18501行):
<packagedElement xmi:type="uml:Association" xmi:id="A_collaborationUse_classifier" name="A_collaborationUse_classifier"> <memberEnd xmi:idref="Classifier-collaborationUse"/> <memberEnd xmi:idref="A_collaborationUse_classifier-classifier"/> <ownedEnd xmi:type="uml:Property" xmi:id="A_collaborationUse_classifier-classifier" name="classifier" type="Classifier" association="A_collaborationUse_classifier"> <subsettedProperty xmi:idref="Element-owner"/> <!-- ??? --> <lowerValue xmi:type="uml:LiteralInteger" xmi:id="A_collaborationUse_classifier-classifier-_lowerValue"/> <!-- xxx --> </ownedEnd> </packagedElement>
该定义包含空的<lowerValue>元素,工具会生成一个LiteralInteger实例。
- Slot的owningInstance关联端(UML.xmi第17967行):
<ownedAttribute xmi:type="uml:Property" xmi:id="Slot-owningInstance" name="owningInstance" type="InstanceSpecification" association="A_slot_owningInstance"> <subsettedProperty xmi:idref="Element-owner"/> <ownedComment xmi:type="uml:Comment" xmi:id="Slot-owningInstance-_ownedComment.0" body="The InstanceSpecification that owns this Slot."> <annotatedElement xmi:idref="Slot-owningInstance"/> </ownedComment> </ownedAttribute>
此处未指定<lowerValue>元素,multiplicity被正确解析为1。
2. 规范规则的冲突点
根据UML官方定义:
第88页7.8.8.7节MultiplicityElement:
lowerBound():若lowerValue为null或其integerValue()为null,则返回1;否则返回lowerValue.integerValue()。
第125页LiteralInteger:- 属性
value : Integer [1..1] = 0,默认值为0;integerValue()直接返回该value。
当XMI中存在空的<lowerValue>元素时,生成的LiteralInteger实例value默认是0,且integerValue()永远不为null,导致lowerBound()返回0,最终multiplicity被解析为[0..1],偏离了预期的1。
二、Classifier的representation与collaborationUse的区别
- collaborationUse:是Classifier与CollaborationUse之间的通用关联,CollaborationUse作为Element,其owner子集指向这个关联端,完全遵循Element的所有权规则(除顶级包外必须有一个owner)。
- representation:是collaborationUse的子集,它特指被Classifier用来表示自身结构或行为的CollaborationUse。也就是说,并非所有的collaborationUse都属于representation,只有那些承担Classifier表现功能的CollaborationUse才会被纳入这个关联端。
内容的提问来源于stack exchange,提问作者Robert Hairgrove
相关产品推荐
相关产品推荐

