Author |
Message
|
altiva |
Posted: Mon Jun 30, 2014 1:03 pm Post subject: Merge two trees - ATTACH DETACH |
|
|
Newbie
Joined: 30 Jun 2014 Posts: 6
|
Hello,
I have a situation where I have to merge two trees. I found ATTACH DETACH would be ideal for this situation.
The problem is that I want to merge the children of two parent elements.
Code: |
School1
DifficultCourse
NiceSubject
EasyCourse
BoringSubject
School2
AverageCourse
AverageSubject |
should result in:
Code: |
School1
DifficultCourse
NiceSubject
EasyCourse
BoringSubject
AverageCourse
AverageSubject |
I tried to detach reference to School2 and attach it to School1 but it results in:
Code: |
School1
DifficultCourse
NiceSubject
EasyCourse
BoringSubject
School2
AverageCourse
AverageSubject |
Do I have to make it manually with REFERENCES or can it be achieved with ATTACH/DETACH?
Thank you |
|
Back to top |
|
 |
fjb_saper |
Posted: Mon Jun 30, 2014 4:22 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
You'll notice that for what you're trying to do, you're detaching at the wrong level in the tree.  _________________ MQ & Broker admin |
|
Back to top |
|
 |
altiva |
Posted: Mon Jun 30, 2014 11:05 pm Post subject: |
|
|
Newbie
Joined: 30 Jun 2014 Posts: 6
|
Of course I noticed that, the problem is that ATTACH works with references and I can't create a single reference that points to multiple elements.
That is the reason why I posted it. |
|
Back to top |
|
 |
fjb_saper |
Posted: Mon Jun 30, 2014 11:32 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
But you can detach all the children, one by one and attach them somewhere else, can't you?  _________________ MQ & Broker admin |
|
Back to top |
|
 |
altiva |
Posted: Mon Jun 30, 2014 11:49 pm Post subject: |
|
|
Newbie
Joined: 30 Jun 2014 Posts: 6
|
Maybe I was not clear in my original post.
I want to find a way to copy all the children of an element to another tree, without having to make a simple iteration-copy structure. |
|
Back to top |
|
 |
Esa |
Posted: Tue Jul 01, 2014 12:29 am Post subject: |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
altiva wrote: |
Maybe I was not clear in my original post.
I want to find a way to copy all the children of an element to another tree, without having to make a simple iteration-copy structure. |
I would have understanding if you wanted to copy all child elements of a reference without having to make a complex iteration structure...
In general ATTACH/DETACH is not the right way to merge two trees. But I guess you are in fact not merging trees, but reorganizing one single tree? |
|
Back to top |
|
 |
altiva |
Posted: Tue Jul 01, 2014 12:36 am Post subject: |
|
|
Newbie
Joined: 30 Jun 2014 Posts: 6
|
Yes, I'm reorganizing the first tree. I want to add the children of the second tree.
Is an iteration over the second tree using a CREATE statement over the first tree, the best solution to achieve this? |
|
Back to top |
|
 |
Esa |
Posted: Tue Jul 01, 2014 1:01 am Post subject: |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
Quote: |
But I guess you are in fact not merging trees, but reorganizing one single tree? |
altiva wrote: |
Yes, I'm reorganizing the first tree. I want to add the children of the second tree. |
altiva wrote: |
Is an iteration over the second tree using a CREATE statement over the first tree, the best solution to achieve this? |
It depends. |
|
Back to top |
|
 |
altiva |
Posted: Tue Jul 01, 2014 1:05 am Post subject: |
|
|
Newbie
Joined: 30 Jun 2014 Posts: 6
|
Esa wrote: |
Quote: |
But I guess you are in fact not merging trees, but reorganizing one single tree? |
altiva wrote: |
Yes, I'm reorganizing the first tree. I want to add the children of the second tree. |
altiva wrote: |
Is an iteration over the second tree using a CREATE statement over the first tree, the best solution to achieve this? |
It depends. |
I apologize if my capacity to express myself in english is not perfect. It is not my first language.
Considering the "it depends"... what are those dependencies?
I'm just looking for an efficient way to make this kind of operation. It seems that making a loop to iterate each children in tree 2, and creating a child on tree 1 is not the best solution. Is there a better approach? |
|
Back to top |
|
 |
Esa |
Posted: Tue Jul 01, 2014 1:29 am Post subject: |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
If both of your trees reside in the same message, ATTACH/DETACH is probably ok. That is what I was trying to ask.
If I understand correctly, your original problem may have been that when you detach and attach a reference that you are moving within a loop you end up continuing the loop in a wrong place after the ATTACH?
Then you need to loop like this:
Code: |
CREATE parentRef REFERENCE TO ...Shool2;
CREATE targetRef REFERENCE TO parentRef;
MOVE targetRef FIRSTCHILD;
WHILE LASTMOVE(targetRef) DO
DETACH targetRef;
ATTACH targetREF TO theOtherRef AS LASTCHILD;
MOVE targetRef TO parentRef;
MOVE targetRef FIRSTCHILD;
END WHILE; |
|
|
Back to top |
|
 |
altiva |
Posted: Tue Jul 01, 2014 1:33 am Post subject: |
|
|
Newbie
Joined: 30 Jun 2014 Posts: 6
|
Hello Esa,
That is not my problem at the moment. My current implementation is something like this:
Code: |
DECLARE refTreeTwo REFERENCE TO Environment.Variables.TreeTwo.*;
REFS: WHILE LASTMOVE(refTreeTwo) DO
CREATE LASTCHILD OF Environment.Variables.TreeOne DOMAIN 'XMLNSC' FROM refTreeTwo;
MOVE refTreeTwo NEXTSIBLING;
END WHILE REFS; |
What I was asking originally is if it is possible to copy TreeTwo, without having to use a while statement that iterates each child of TreeTwo. I thought it could be possible with ATTACH/DETACH, but apparently it is not possible. |
|
Back to top |
|
 |
mqjeff |
Posted: Tue Jul 01, 2014 5:32 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
altiva wrote: |
Hello Esa,
That is not my problem at the moment. My current implementation is something like this:
Code: |
DECLARE refTreeTwo REFERENCE TO Environment.Variables.TreeTwo.*;
REFS: WHILE LASTMOVE(refTreeTwo) DO
CREATE LASTCHILD OF Environment.Variables.TreeOne DOMAIN 'XMLNSC' FROM refTreeTwo;
MOVE refTreeTwo NEXTSIBLING;
END WHILE REFS; |
What I was asking originally is if it is possible to copy TreeTwo, without having to use a while statement that iterates each child of TreeTwo. I thought it could be possible with ATTACH/DETACH, but apparently it is not possible. |
Attach/Detach is better than CREATE LASTCHILD. It moves memory, rather than creating new memory.
Neither of these affect the structure and navigation of the logical message tree. It doesn't matter what method you use to manipulate the logical message tree, it still looks like it looks. So, no, there's no "easy" way to say "move every child of this element", you always have to iterate over each child.
Again, that's because of the nature of the logical message tree, not because of coding choices you make. |
|
Back to top |
|
 |
|