1

I have a UML model (which is actually irrelevant as it could be any MOF model) with an ordered set I need to access from the last to the first element. I thus need to reverse it through OCL. I have seen there is the sortedBy method that needs an OCLExpression to use as sorting criterion but I can't get how to use it.

Any idea?

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84

1 Answers1

2

If you are using Acceleo (your comment is suggesting that you are), you have two options:

  • use "myOrderedSet->reverse()" provided by the Acceleo library (that's not 100% pure OCL)
  • use something like "myOrderedSet->iterate(elem: MyType| myOrderedSet.insertAt(0, elem))"

Edit: It's been a long time since I used iterate since I use reverse all the time, the correct for of the iterate is this: link. And in case anyone had a doubt, here is the answer with reverse: link

Regards,

Stephane Begaudeau

sbegaudeau
  • 1,504
  • 13
  • 18
  • iterate does not seem to be an OCL valid method for an OrderedSet. – Andrea Sindico Feb 23 '12 at 14:08
  • Well since it seems that you are really using Acceleo so "->reverse()" is the easiest way to go and as my link to the Acceleo documentation shows it, it is implemented on OrderedSet and Sequence: [link](https://plus.google.com/photos/105625286543794908607/albums/5552047126204483729/5712578132905610898). Just be sure that you have an OrderedSet and not a Set that does not have the reverse operation. – sbegaudeau Feb 24 '12 at 06:04
  • I have edited my answer with a correction on the declaration of iterate and a screenshot of both solutions tested in Acceleo. – sbegaudeau Feb 24 '12 at 06:18
  • thank you very much Stephane. I am actually trying to get the fragments contained in an Interaction in the exact order they are contained in the model but reversed. To this end I am doing interaction.fragment->asOrderedSet() and it actually returns the ordered set of fragments. However it marks as error both interaction.fragment->asOrderedSet()->reverse() and iterate but as I get to my Office I will check the installed ACCELEO version. Thank you again for your kind help – Andrea Sindico Feb 24 '12 at 06:29