oclmodel-driven-development

OCL - calling rule


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))

enter image description here


Solution

  • Your OCL expression simply 'create' elements (regarding the name of the thismodule function) from the elements that are in s.source. The created elements are then returned as a Collection:

    How to change the expression if the relation elements is not 0..* anymore but 0..1 or 1..1?

    Indeed, collect(...) works with collections, but -> is also an implicit converter to a Set. The page 15 of the OCL specification states:

    The "->" navigation shorthand performs an implicit set conversion of an object.

    anObject->union(aSet) is a shorthand for anObject.oclAsSet()->union(aSet)

    It means that in the case element (I removed the final 's') is a "single" relationship and s.source.element returns a single element, the call s.source.element->... is equivalent to s.source.element.oclAsSet()->.... In your case, whether elements is many or not, the expression is still the same:

    s.source.elements -> collect (s|thisModule.CreateAnyMatchClass(s))
    

    This expression will work in both cases.

    If you really don't want the collect and if you have your elements relationship which is single, you can write this also:

    thisModule.createAnyMatchClass(s.source.elements)