UML 共享聚合是否意味着没有循环?

Does UML shared aggregation imply absence of cycles?

简而言之

共享聚合似乎表达的不过是一种简单的关联。它显然是一种“建模安慰剂”,没有 objective 使用它的理由(除非在建模者之间就特定附加语义达成一致的特定上下文中)。

然而,一些消息来源声称,共享聚合意味着实例的聚合图中没有循环。这将使聚合成为图形建模领域的强大工具。

但是我在规范中找不到这些声明的任何证据。我是否遗漏了一些间接证据(例如多个约束的相互作用)?

更多详情

引用

在他们的书 The Unified Modeling Language Reference Manual, 2nd Edition 中,Booch、Jacobson 和 Rumbaugh 声称(共享)聚合:

The distinction between aggregation and association is often a matter of taste rather than a difference in semantics. Keep in mind that aggregation is association. Aggregation conveys the thought that the aggregate is inherently the sum of its parts. In fact, the only real semantics that it adds to association is the constraint that chains of aggregate links may not form cycles, which is often important to know, however. (...) In spite of the few semantics attached to aggregation, everybody thinks it is necessary (for different reasons). Think of it as a modeling placebo.

其他相当消息灵通的来源(例如 uml-diagrams.org 和几篇学术论文)也认为有向无环图是理所当然的。

根据 UML 规范进行验证

书籍是2004年的,基于UML 2.0。但大多数这种说法仍在 UML 2.5.1 规范 中得到验证。共享聚合在其中以非常模糊的术语定义,与简单关联 (p.112) 相比没有任何 objective 优势:

Sometimes a Property is used to model circumstances in which one instance is used to group together a set of instances; this is called aggregation. To represent such circumstances, a Property has an aggregation property, of type AggregationKind; (...)
...
shared : Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.

关于聚合的另一个条款防止了一些特殊的循环情况(第 200 页):

An end Property of an Association may only be marked as a shared or composite aggregation if the Association is binary and the other end is not marked as a shared or composite aggregation.

然而,我没有发现任何证据表明在一般情况下“聚合链接链可能不会形成循环”。我也没有找到其他属性,例如传递性和反对称性,这些属性可以推断出不存在循环。

UML 发明者的声明现在过时了吗?还是我错过了当前规范中允许派生此声明的某些内容?

我认为我最喜欢的 UML 2.5 第 110 页对此非常清楚:

Sometimes a Property is used to model circumstances in which one instance is used to group together a set of instances; this is called aggregation. To represent such circumstances, a Property has an aggregation property, of type AggregationKind; the instance representing the whole group is classified by the owner of the Property, and the instances representing the grouped individuals are classified by the type of the Property. AggregationKind is an enumeration with the following literal values:

none Indicates that the Property has no aggregation semantics.
shared Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.
composite Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3).

Composite aggregation is a strong form of aggregation that requires a part object be included in at most one composite object at a time. If a composite object is deleted, all of its part instances that are objects are deleted with it.

我强调的。共享聚合在任何情况下都不在 运行 范围内,因为它的语义未定义。所以其含义仅针对复合聚合。


标准是什么?

UML 有相当长的历史。那里有很多引用。随着 UML 的发展,只有极少数的人会得到更新。尽管 Booch 等人。发明了 UML,他们不再定义标准。这是由 OMG 完成的,他们发布了 ISO 标准(如果你愿意,你可以为此额外付费)。相当多的术语具有古老的起源并且已经过时了。尽管如此,它们仍在被使用——而且是在现在错误的上下文中。

标准是否完善?

绝对不是。它在不断发展,但仍然存在一些缺陷或误解。在我看来,在 UML 2.0 中引入共享聚合并不是一个好主意。定义每个定义都没有定义的东西似乎很奇怪。看看这个概念的混乱证明我是对的。

您的分析是正确的。 UML 2 没有为共享聚合提供任何语义。

不过

UML 1 说:

Both kinds of aggregations define a transitive, antisymmetric relationship (i.e., the instances form a directed, non-cyclic graph).

我不知道,为什么这没有被移交给 UML 2。我只能推测这是一个把婴儿和洗澡水一起倒掉的案例。有些人想明确表示,规范将共享聚合的语义留给特定领域的解释。他们忘记了它实际上应该有一些语义,无论多么微弱。

我的看法

我们的情况是这样的:关于这个主题的大量书籍和网站、UML 1 规范和常识都同意,整体-部分关系,无论是共享的还是复合的,都是非循环的。 UML 2 对此主题保持沉默。

因此,我将把共享聚合解释为非循环的。当然,我会在建模指南中记录这种解释。但是,我不认为它会混淆我的图表的很多读者,即使他们没有阅读我的指南,因为它符合他们的预期。

最后一点:除非确实需要,否则不要使用聚合。根据我的经验,它会引起很多不必要的讨论。