Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Collection union with distinct types
Collection union with distinct types [message #66215] Thu, 29 January 2009 14:04 Go to next message
Alexandre Torres is currently offline Alexandre TorresFriend
Messages: 139
Registered: July 2009
Senior Member
Hi, I'm getting an unexpected hard time to perform a union between 2
collections with different types.
I want to create a Set of uml::Element that contains all elements of a set
of uml::Class and a set of uml::Property, but I really don't know how to
write this as an expression - I mean, the best way.

I've tried something like:
setA = Classes, setB = Properties

setA.oclAsType(Set(uml::Element))->union(setB.oclAsType(Set(uml::Element)))

but oclAsType doesn't seem to work for collections.
I ended up with the following working solution, but somehow I feel it can
be further improved:

let sel:Set(uml::Element)=Set{} in
sel->union(setA)->union(setB)

Is this the correct solution?

Thanks
Re: Collection union with distinct types [message #66236 is a reply to message #66215] Thu, 29 January 2009 16:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.zeligsoft.com

Hi, Alexandre,

See some comments in-line, below.

HTH,

Christian

Alexandre Torres wrote:
> Hi, I'm getting an unexpected hard time to perform a union between 2
> collections with different types.
> I want to create a Set of uml::Element that contains all elements of a
> set of uml::Class and a set of uml::Property, but I really don't know
> how to write this as an expression - I mean, the best way.
>
> I've tried something like:
> setA = Classes, setB = Properties
>
> setA.oclAsType(Set(uml::Element))->union(setB.oclAsType(Set(uml::Element)))
>
> but oclAsType doesn't seem to work for collections.

Right. The oclAsType operation is defined for the special OclAny type,
and in the OCL 2.0 spec collection types specifically are the only types
in the OCL universe that do not conform to OclAny. So, they simply
don't have this operation.

The OCL 2.1 spec will fix that.


> I ended up with the following working solution, but somehow I feel it
> can be further improved:
>
> let sel:Set(uml::Element)=Set{} in sel->union(setA)->union(setB)
>
> Is this the correct solution?

It's the only solution, I'm afraid.

You ought to be able to do simply:

setA->union(setB)

because setA is of type Set(Class) which inherits union(Set(Element))
from Set(Element). Actually, the least common type in this case is
probably Set(NamedElement), but that's not important. However, this
currently doesn't work because the semantics of type parameters in OCL
is undefined, so MDT OCL has pretty strict type matching rules in
resolution of the generic operation signature.

I think the OCL 2.1 spec will fix this problem, too.

>
> Thanks
>
>
Re: Collection union with distinct types [message #66256 is a reply to message #66236] Thu, 29 January 2009 16:23 Go to previous messageGo to next message
Alexandre Torres is currently offline Alexandre TorresFriend
Messages: 139
Registered: July 2009
Senior Member
Hi Chris, thanks for the answer.
I hope this example can help somebody else too... I needed some time to
figure out the use of let to solve this :)
Re: Collection union with distinct types [message #66329 is a reply to message #66256] Fri, 30 January 2009 13:47 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.zeligsoft.com

Hi, Alex,

Certainly, I think it can. Especially since I have added it as an FAQ
to our wiki :-)

http://wiki.eclipse.org/MDT/OCL/FAQ#How_do_I_combine_collect ions_of_different_types.3F

How about you do the next one? :-P

cW

Alexandre Torres wrote:
> Hi Chris, thanks for the answer. I hope this example can help somebody
> else too... I needed some time to figure out the use of let to solve
> this :)
>
Previous Topic:linebreak between Strings?
Next Topic:Ocl Completion and stereotypes
Goto Forum:
  


Current Time: Sat Jul 27 16:04:03 GMT 2024

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

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

Back to the top