Search code examples
collectionsumldiagramnotation

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


I want to depict the index of elements of an ordered collection in an UML object diagram. The only information I was able to find in the UML Spec 2.5.1 was in the part about semantics of associations 11.5.3.1.

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

But either there is no guidance regarding the notation of such ordering information or I just didn't find it. I think I have seen a colon followed by the index in some tools. I wonder if there is a consensus or reference about how to depict indices on the links?

EDIT:
Although the existing answer is already holistic, let me add some clarification and context. As the first sentence already stated, I want to use this explicit information in an object diagram (maybe the parenthesis were confusing, I removed them). The object diagrams are used as part of test case sepcifications to communicate the object structure of the input, expected result and actual result. To that regard, the order of objects in a collection may play a role, e.g., imagine a test case specification for the correct implementation of the specification of a sorting algorithm.

I did not specify the kind of collection on purpose as I do not see how that would influence the answer as long as the collection is ordered. Typically, a sequence/list would come to my mind.

I do not need OCL in this case but I appreciate the answer taking that into consideration as formulating constraints on the order of collection elements is closely related.


Solution

  • UML

    There is nothing foreseen for representing the index of an ordered collection in UML. In section 7.5.3.2 it is defined that ordering makes sense in relation to elements with multiplicity:

    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.

    The positive integer that is mapped correspond to what you'd call an index. But nothing is defined in the UML specs: Not even if the index should start at 0, at 1 or at any arbitrary value. It's not even said that the indexes have to be consecutive.

    The UML specs explain in the same section, that the semantics of the ordered collections depend also on the unicity of their elements:

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

    Unfortunately, the OrderedSet and Sequence are not defined in the UML specifications.

    The only case where the ordering is defined more precisely is for properties defined as derived unions (section 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.

    Conclusion: ¨There is no way to define what the order is (e.g. link the order to some properties), and nothing is foreseen to refer to the indexes in the ordering.

    OCL

    The OCL language is a companion of UML. it is used to write more formal and precise constraints. It defines some more semantics for collections:

    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 uses the notion of index in several operations available for Sequence and OrderedSet:

    • The expression at(i) identifies the i-the element
    • The expression indexOf(v) returns the index of the element v
    • The expression first() returns the first element, being understood that its index is 1
    • The expression last() reutrns the last element, being udnerstood that its index correspond to the size of the collection.

    These expressions are related to ordered collections and are not defined for unordered collections such as sets and bags.

    Conclusion you can use indexes in UML constraints by using OCL and even relate them with the help of constraints an order to properties

    Edit: More about object diagrams

    Object diagram represents instances of objects. The association lines between these objects hence representthe “links” mentioned in your quote.

    While a notation exists to specify values of object properties, nothing is defined for the links:

    • Pragmatically, you could just number the end of the link (there should be no confusion with multiplicity since it’s a link and not an association). If you fear some confusion, you may prefix the number with an informal # or Nr. .
    • Alternatively, if you have to stay 100% compliant, you may put the order information in a note symbol.
    • Reading section 9.8.4 page 126, and considering the use of = to specifiy values within instances, I think that it could be argued that order=1 would be valid, since the only value that is not already defined for the link through the instances at both ends are the ordering information.