Sorting a collection of numbers

391 views Asked by At

Link to OCL documentation

If I have a collection of Cars, I can sort it by engine power like this:

collectionOfCars -> sortedBy(car|car.power)

or simply

 collectionOfCars -> sortedBy(power)

How to sort a collection of numbers? Are the following expressions correct?

collectionOfNumbers -> sortedBy(a|a)

collectionOfNumbers -> sortedBy()
1

There are 1 answers

0
Vincent Aranega On

You can totally have collection of integers in OCL (as well as collection of String and all OCL "primitive types"), they are called collections of literals (p.25 on OCL specification). You can build them from scratch using {}, e.g. Sequence{3,2,4,1}.

Also, consider this c.ownedAttribute.name (where c is a class), you will build a collection of String (this expression use the implicit iterator .)

If you want to sort your collection of literal, you can do collection->sortedBy(a|a) as proposed.

Sequence{3,2,4,1}->sortedBy(a|a) result Sequence{1,2,3,4} : Seqence(Integer)

Bag{8,7,8,9}->sortedBy(i|i) result Sequence{7,8,8,9} : Sequence(Integer)

You can find more examples in this paper "Object Constraint Language (OCL): A Definitive Guide" by Jordi Cabot and Martin Gogolla, you can find the paper here: http://www.db.informatik.uni-bremen.de/publications/Cabot_2012_SFM.pdf