Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Collecting objects from a hierarchy
Collecting objects from a hierarchy [message #68308] Mon, 02 March 2009 14:13 Go to next message
Eclipse UserFriend
Originally posted by: jose.hernandez.smartstream-stp.com

Hi,



I would like to build a collection by traversing a tree upwards from a given
node so that I can then determine the uniqueness of a given property across
the collection of items in a branch. E.g. I need to collect all the
ancestors of a starting node into a collection to make sure all nodes in a
given branch have a unique name. All nodes have a 'parent()' method and a
'name' attribute.



Could someone please tell me how all the ancestors of a given item can be
inserted into a collection in OCL?



Thanks

Jos
Re: Collecting objects from a hierarchy [message #68321 is a reply to message #68308] Mon, 02 March 2009 20:35 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.zeligsoft.com

Hi, Jose,

The MDT OCL implementation provides a non-standard "closure" iterator
that is convenient for situations like this. So, if you want to check
that some object and all of its ancestors have unique names, you could do:

context SomeClass
inv unique_names:
self->closure(parent())->including(self)->isUnique(name)

Note that the closure iterator computes a set. If ordering is
important, or standard-compliance, or both, then another approach would
work as long as the property or operation being followed doesn't result
in cycles:

context SomeClass
def ancestorChain : Sequence(SomeClass) =
if parent().oclIsUndefined() then
Sequence{}
else
Sequence{parent()}->union(parent().ancestorChain)
endif
inv unique_names:
Sequence{self}->union(ancestorChain)->isUnique(name)

HTH,

Christian

Jose Hernandez wrote:
> Hi,
>
>
>
> I would like to build a collection by traversing a tree upwards from a given
> node so that I can then determine the uniqueness of a given property across
> the collection of items in a branch. E.g. I need to collect all the
> ancestors of a starting node into a collection to make sure all nodes in a
> given branch have a unique name. All nodes have a 'parent()' method and a
> 'name' attribute.
>
>
>
> Could someone please tell me how all the ancestors of a given item can be
> inserted into a collection in OCL?
>
>
>
> Thanks
>
> José
>
>
Re: Collecting objects from a hierarchy [message #68341 is a reply to message #68321] Tue, 03 March 2009 12:59 Go to previous message
Eclipse UserFriend
Originally posted by: jose.hernandez.smartstream-stp.com

Hi Christian,

Order is not important in this case so the first solution works well.
Thanks for your help.

Regards
Jose

"Christian W. Damus" <cdamus@zeligsoft.com> wrote in message
news:gohfv1$q4m$1@build.eclipse.org...
> Hi, Jose,
>
> The MDT OCL implementation provides a non-standard "closure" iterator that
> is convenient for situations like this. So, if you want to check that
> some object and all of its ancestors have unique names, you could do:
>
> context SomeClass
> inv unique_names:
> self->closure(parent())->including(self)->isUnique(name)
>
> Note that the closure iterator computes a set. If ordering is important,
> or standard-compliance, or both, then another approach would work as long
> as the property or operation being followed doesn't result in cycles:
>
> context SomeClass
> def ancestorChain : Sequence(SomeClass) =
> if parent().oclIsUndefined() then
> Sequence{}
> else
> Sequence{parent()}->union(parent().ancestorChain)
> endif
> inv unique_names:
> Sequence{self}->union(ancestorChain)->isUnique(name)
>
> HTH,
>
> Christian
>
> Jose Hernandez wrote:
>> Hi,
>>
>>
>>
>> I would like to build a collection by traversing a tree upwards from a
>> given node so that I can then determine the uniqueness of a given
>> property across the collection of items in a branch. E.g. I need to
>> collect all the ancestors of a starting node into a collection to make
>> sure all nodes in a given branch have a unique name. All nodes have a
>> 'parent()' method and a 'name' attribute.
>>
>>
>>
>> Could someone please tell me how all the ancestors of a given item can be
>> inserted into a collection in OCL?
>>
>>
>>
>> Thanks
>>
>> Jos
Previous Topic:Re: EClass defined in ecore model not (always) loaded correctly
Next Topic:[Announce] MDT OCL 1.3.0 I200903050949 is available
Goto Forum:
  


Current Time: Fri Apr 19 20:40:25 GMT 2024

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

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

Back to the top