What does the below code do? How can I write this OCL expression when there is one element instead of elements?
In other words, I don't understand which elements does the code collect? Since "collect" is used when we have more than one element, if I have one element (instead of elements), what changes occure to "-> collect (s|thisModule.CreateMatchClass(s))" part of that expression?
s.source.elements -> collect (s|thisModule.CreateAnyMatchClass(s))
Your OCL expression simply 'create' elements (regarding the name of the
thismodule
function) from the elements that are ins.source
. The created elements are then returned as aCollection
:s.source.elements
return (supposedly) aCollection
(could be aSet/Sequence...
) by navigating froms
collect(...)
gathers the results of its parameter expressionHow to change the expression if the relation
elements
is not0..*
anymore but0..1
or1..1
?Indeed,
collect(...)
works with collections, but->
is also an implicit converter to aSet
. The page 15 of the OCL specification states:It means that in the case
element
(I removed the final 's') is a "single" relationship ands.source.element
returns a single element, the calls.source.element->...
is equivalent tos.source.element.oclAsSet()->...
. In your case, whetherelements
is many or not, the expression is still the same:This expression will work in both cases.
If you really don't want the
collect
and if you have yourelements
relationship which is single, you can write this also: