Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » "Select DISTINCT" with OCL
"Select DISTINCT" with OCL [message #1796894] Sun, 21 October 2018 18:06 Go to next message
Rémi Bernard is currently offline Rémi BernardFriend
Messages: 2
Registered: June 2018
Junior Member
Hi,

I have an OrderedSet of Stuff objects having a name attribute, and I want to filter this sequence by objects names so that I get exactly one Stuff of each given name in the sequence (i.e. remove name duplicates/triplicates/etc/).

I looked at the OCL reference but did not find anything that seemed to do the job. Then I opted for creating and invoking a Java Service but could not find the Java equivalent type for OCL OrderedSet, so I failed at writing the Java function for the service, and got stuck there...

What would be the right way to go here?

[Updated on: Sun, 21 October 2018 18:07]

Report message to a moderator

Re: "Select DISTINCT" with OCL [message #1796902 is a reply to message #1796894] Mon, 22 October 2018 05:51 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Selecting the unique names is easy - use a Set: elements->collect(name)->asSet()

Discarding the duplicates is then a matter of choosing one element for each unique name; you may have a smart algorithm for choosing which.

let uniqueNames = elements.name->asSet()
in uniqueNames->collect(n | elements->select(name = n)->any(true))

[In Eclipse OCL you could create a Map from name to element with the result that the values are a unique value, but unfortunately

https://bugs.eclipse.org/bugs/show_bug.cgi?id=540353 raised

there is no bulk constructor so you would need to iterate with a Map accumulator to including(K,V) one entry per iteration.]

elements->iterate(e; acc : Map(String, ElementType) = Map{} | acc->including(e.name, e))->values()

-- once collectMap added: elements->collectMap(e | e.name, e)->values()

(Above examples have not been tested. Most of the above incur quadratic cost. collectMap could be inherently linear. The looped select could be optimised to be linear. )

Regards

Ed Willink

Previous Topic:Announce OCL 6.5.0
Next Topic:"Create Dynamic Instance" in OCLinEcore tutorial
Goto Forum:
  


Current Time: Thu Apr 25 15:27:37 GMT 2024

Powered by FUDForum. Page generated in 0.02838 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top