3

How does one go about getting the number of elements in a class's attribute that has 0..* multiplicity?

I can only think of either using an << iterate>> construct to do so but that seems silly or a counter whenever something is added. This seems inelegant if not inefficient.

Jordan McBain
  • 275
  • 2
  • 11

2 Answers2

3

If you want to refer to the cardinality of an attribute in an activity diagram, you can use the size() function. Example:

diamond

If your activity diagram is meant to be read by humans, not by machines, you can also just simply write "number of elements in object.attr".

If you want to access the cardinality in order to create a loop, you might prefer the expansion region. An iterate construct in activity diagrams can be achieved by using an expansion region with mode = iterative. Suppose class Order has attribute orderline of type OrderLine[1..*]. The following diagram shows how to iterate over all orderlines.

activity-diagram

See section 16.12 of the UML 2.5.1 specification for more information.

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • Thanks. For Cameo users: https://docs.nomagic.com/display/MD190/Built-in+operations – Jordan McBain Oct 19 '22 at 12:43
  • there is one small mistake in this answer: the arrow `->` notation is only for collections. `object` is not a collection, therefore it should be `object.attr->size()`. – Axel Scheithauer Oct 24 '22 at 08:10
  • Another problem is the expansion region. It needs to have an object flow with a collection arriving at the expansion node. Therefore, the order input parameter should be connected to it directly and the transformation `order.orderline` should be used on the object flow. The text `for each order.orderline` has no meaning here. The expansion region already means, that it will be executed for each of the elements of the incoming collection. – Axel Scheithauer Oct 24 '22 at 08:31
1

A multiplicity of 0..* means that for a given instance a of A there is a collection of associated instances of B that has has at minimum 0 and at maximum * (i.e. no upper limit) elements:

enter image description here

The same is true for an attribute b:B [0..*] that a class A could have.

The number of elements in the collection is called cardinality. In a constraint, you can refer to the cardinality with

self.b->size() 

There is also a convenient way to check if the collection is empty or not:

self.b->isEmpty()
self.b->notEmpty()
Christophe
  • 68,716
  • 7
  • 72
  • 138