You can use product in an OCL expression when you need every pairing between the objects in one collection and the objects in another collection.
Syntax
product ( c2 : Collection(T2) ) : Set(Tuple(first : T, second : T2))
What product returns
product returns a Set of Tuple values representing the Cartesian product of the collection before product and c2.
A Cartesian product contains one tuple for every possible pair:
firstis an element from the collection beforeproduct.secondis an element fromc2.
The result is a Set, so the result contains tuple values rather than an ordered sequence.
Example
If the first collection contains 1 and 2, and the second collection contains 'A' and 'B', the expression below creates all four possible pairs.
Set{1, 2}.product(Set{'A', 'B'})
The result is a set equivalent to:
Set{
Tuple{first = 1, second = 'A'},
Tuple{first = 1, second = 'B'},
Tuple{first = 2, second = 'A'},
Tuple{first = 2, second = 'B'}
}
Use product when the rule or query needs to consider every combination of two collections. For example, a model can use the returned tuples as the input to a further OCL expression that evaluates each pair.
These expressions demonstrate product with different collection types:
| Expression | Result |
|---|---|
| Sequence{3, 4}->product(Bag{3.0, 4.0}) | Set{Tuple{3, 3.0}, Tuple{3, 4.0}, Tuple{4, 3.0}, Tuple{4, 4.0}} |
| Set{3, 4}->product(OrderedSet{3.0, 4.0}) | Set{Tuple{3, 3.0}, Tuple{3, 4.0}, Tuple{4, 3.0}, Tuple{4, 4.0}} |
Empty collections
If either collection has no elements, there are no pairs to create. The Cartesian product is therefore an empty set.
Related OCL operators
product is a collection operation. For the wider operator set and guidance on finding available operators in the OCL editor, see Documentation:OCLOperators and Documentation:OCL General Operators. OCL expressions are declarative and must not have side effects; see Documentation:OCL Expressions.
