Collection union with distinct types [message #66215] |
Thu, 29 January 2009 09:04  |
Eclipse User |
|
|
|
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 11:18   |
Eclipse User |
|
|
|
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
>
>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03031 seconds