如何在 UML 图中描述有序集合的元素索引?

How to depict index of elements of ordered collection in UML diagrams?

我想在 UML 对象图中描述有序集合的元素索引。 我能够在 UML Spec 2.5.1 中找到的唯一信息是关于关联语义的部分 11.5.3.1.

When one or more ends of the Association are ordered, links carry ordering information in addition to their end values.

但是要么没有关于此类订购信息的符号的指导,要么我只是没有找到它。我想我在一些工具中看到了一个冒号,后面跟着索引。我想知道是否有关于如何在链接上描述索引的共识或参考?

编辑:
尽管现有答案已经是整体的,但让我添加一些说明和背景。如第一句话所述,我想在对象图中使用此显式信息(可能括号令人困惑,我将其删除)。对象图用作测试用例规范的一部分,以传达输入的对象结构、预期结果和实际结果。在这方面,集合中对象的顺序可能会发挥作用,例如,想象一个用于正确实现排序算法规范的测试用例规范。

我没有特意指定集合的​​种类,因为我看不出只要集合是有序的,这会对答案产生什么影响。通常,我会想到 sequence/list。

在这种情况下我不需要 OCL,但我很欣赏考虑到这一点的答案,因为对集合元素顺序的约束是密切相关的。

UML

没有预见到在 UML 中表示有序集合的索引。在第 7.5.3.2 节中定义了排序对于具有多重性的元素是有意义的:

If the MultiplicityElement is specified as ordered (i.e., isOrdered is true), then the collection of values in an instantiation of this Element is ordered. This ordering implies that there is a mapping from positive integers to the elements of the collection of values. If a MultiplicityElement is not multivalued, then the value for isOrdered has no semantic effect.

映射的正整数对应于您所说的索引。但是 UML 规范中没有任何定义:即使索引应该从 0、1 或任何任意值开始。更没有说索引必须是连续的。

UML 规范在同一节中解释说,有序集合的语义还取决于它们元素的唯一性:

isOrdered    isUnique    Collection Type
false        true        Set
true         true        OrderedSet
false        false       Bag
true         false       Sequence

遗憾的是,OrderedSetSequence 未在 UML 规范中定义。

更精确地定义排序的唯一情况是定义为派生联合的属性(第 9.5.3 节):

then the ordering of the union is defined by evaluating the subsetting properties in the order in which they appear in the result of allAttributes() and concatenating the results.

结论: ¨没有办法定义顺序是什么(例如link某些属性的顺序),并且没有预见到引用排序中的索引。

OCL

OCL 语言是 UML 的伙伴。它用于编写更正式和精确的约束。它为集合定义了更多语义:

The OrderedSet is a Set, the elements of which are ordered. It contains no duplicates. OrderedSet is itself an instance of the metatype OrderedSetType. An OrderedSet is not a subtype of Set, neither a subtype of Sequence. The common supertype of Sets and OrderedSets is Collection.

A sequence is a collection where the elements are ordered. An element may be part of a sequence more than once. Sequence is itself an instance of the metatype SequenceType. Sequence is not a subtype of Bag. The common supertype of Sequence and Bag is Collection.

OCL 在可用于 SequenceOrderedSet 的几个操作中使用索引的概念:

  • 表达式at(i)标识第i个元素
  • 表达式indexOf(v)returns元素v的索引
  • 表达式first()returns第一个元素,被理解为它的索引为1
  • 表达式 last() 返回最后一个元素,据了解其索引对应于集合的大小。

这些表达式与有序集合有关,并没有为集合和包等无序集合定义。

结论您可以通过使用 OCL 在 UML 约束中使用索引,甚至可以在约束的帮助下将它们关联到属性的顺序

编辑:关于对象图的更多信息

对象图表示对象的实例。因此,这些对象之间的关联线表示您引用中提到的“links”。

虽然存在用于指定对象属性值的符号,但没有为 link 定义任何内容:

  • 实际上,您可以只在 link 的末尾编号(不应与多重性混淆,因为它是 link 而不是关联)。如果您担心有些混淆,您可以在数字前加上非正式的 #Nr. .
  • 或者,如果您必须保持 100% 合规,您可以将订单信息放在备注符号中。
  • 阅读第 126 页第 9.8.4 节,并考虑使用 = 在实例中指定值,我认为可以说 order=1 是有效的,因为唯一的值未定义为link通过两端的实例是订购信息。