0

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

any
  • 325
  • 5
  • 17

1 Answers1

1

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:

  • s.source.elements return (supposedly) a Collection (could be a Set/Sequence...) by navigating from s
  • collect(...) gathers the results of its parameter expression

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)
Community
  • 1
  • 1
Vincent Aranega
  • 1,441
  • 10
  • 21
  • when I write "class <- s.source -> collect (sipe|thisModule.CreateAnyMatchClass(sipe))" it doesn't create model (I have an error). Do you know what can I do? I attached my metamodel. "s" is refered to "TransformationRule" in metamodel. (when I replace source with targets I have no error, when I change source reference to 0..* still it has an error) – any Feb 02 '17 at 11:46
  • At this point, your questions are not directly related to OCL but to ATL and the way it managed the transformation. I think you will have more chance to have a relevant answer in the Eclipse forum dedicated to ATL. – Vincent Aranega Feb 02 '17 at 14:02
  • Thank you. it works with the last line of code you write. I was surprised when I restart eclipse it works! – any Feb 02 '17 at 14:19