Wednesday, December 4, 2013

A hint how to use collect in OCL

Sometimes I want to merge several list into one big list. Consider this OCL expression

WorkPool.allinstances.buffInProcess->union(WorkPool.allinstances.buffopenplans)->union(Workpool.allinstances.buffforwarded)

So in this case Workpool contain some buffers or multilinks. I want to merge 3 multilinks into one list.
It can be done with collect.

WorkPool.allinstances->collect(buffInProcess->union(buffopenplans)->union(buffforwarded))

The result is exact the same but you do not need to use WorkPool.allinstances on many places.

Another case

Instead of

if myLooongexpression.condition then
  myLooongexpression.attributeone
else
  myLooongexpression.attributetwo
endif
 
do this

myLooongexpression->collect(if condition then attributeone else attributetwo endif)