ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Refactor code to move tree items...

Post new topic  Reply to topic
 Refactor code to move tree items... « View previous topic :: View next topic » 
Author Message
klabran
PostPosted: Thu Dec 08, 2005 12:39 pm    Post subject: Refactor code to move tree items... Reply with quote

Master

Joined: 19 Feb 2004
Posts: 259
Location: Flagstaff AZ

I am reviewing some code that moves tree items under another tree. I know I could probably write it more efficiently but I am not sure how. I have been reading on for loops and while loops with references but all of my attempts have not panned out.

I have an incoming message like
<xmlroot>
<physicalfeatures>
<feature></feature>
</physicalfeatures>
<otherphysicalfeatures>
<feature></feature>
</otherphysicalfeatures>
</xmlroot>

I need it to look like

<xmlroot>
<physicalfeatures>
<feature></feature>
<feature></feature>
</physicalfeatures>
</xmlroot>

This is my existing code that works.

Code:

DECLARE intPF INTEGER 1;
DECLARE intEF INTEGER CARDINALITY(refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[]);


-- Combine any Other Features
WHILE intPF <= CARDINALITY(refArrestIn.OtherPersonPhysicalCharacter.OtherPersonPhysicalCharacter_ROW[]) DO
SET refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[intEF + intPF]=(SELECT R.PhysicalFeatureCategoryText,R.PhysicalFeatureLocationText,R.PhysicalFeatureDescriptionText FROM refArrestIn.OtherPersonPhysicalCharacter.OtherPersonPhysicalCharacter_ROW[intPF] AS R);
SET intPF = intPF + 1;
END WHILE


I believe that perhaps a for or while loop with a reference would be better but at this point I can't seem to get anything to work.

How would you refactor this code?

Thanks,
Kevin
Back to top
View user's profile Send private message Visit poster's website
klabran
PostPosted: Thu Dec 08, 2005 1:27 pm    Post subject: Reply with quote

Master

Joined: 19 Feb 2004
Posts: 259
Location: Flagstaff AZ

So far I have managed to refactor it with a for loop but to be honest I actually think this is worst on clarity and I don't know if it's faster.... ?

Code:

   declare i integer 1;
   declare y integer cardinality(refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[]);
   for ref as refArrestIn.OtherPersonPhysicalCharacter.OtherPersonPhysicalCharacter_ROW[] do
      SET refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[y + i]=(SELECT R.PhysicalFeatureCategoryText,R.PhysicalFeatureLocationText,R.PhysicalFeatureDescriptionText FROM ref AS R);
      set i=i+1;
      move ref nextsibling;
   end for;
Back to top
View user's profile Send private message Visit poster's website
wschutz
PostPosted: Thu Dec 08, 2005 6:18 pm    Post subject: Reply with quote

Jedi Knight

Joined: 02 Jun 2005
Posts: 3316
Location: IBM (retired)

Is this Broker v6? I think you can do it easily with xpath if its v6.
_________________
-wayne
Back to top
View user's profile Send private message Send e-mail AIM Address
elvis_gn
PostPosted: Thu Dec 08, 2005 8:40 pm    Post subject: Reply with quote

Padawan

Joined: 08 Oct 2004
Posts: 1905
Location: Dubai

Hi klabran,

Do u have the tag feature occuring in many places in the tree.... or do u need to pick it up from only physicalfeatures and otherphysicalfeatures ...?

Are there multiple instances of physicalfeatures ...?

Also in the code u user the word physical, but I did not notice it in the XML ...

I am thinking of some logic here but I need to know the above.....Maybe your entire sample input would help.

Regards.
Back to top
View user's profile Send private message Send e-mail
mgk
PostPosted: Fri Dec 09, 2005 1:47 am    Post subject: Reply with quote

Padawan

Joined: 31 Jul 2003
Posts: 1642

Hi
I guess the answer depends on why you are refactoring the code. If it is for readability then you can choose anything you prefer that works . However, if it is for speed / performance then in V5 and especially in V6 SELECT is almost always the fastest way to transform a message. So in this case I think using SELECT is fine I doubt if using references or xpath will improve its performance. This is because you will always end up executing more statements that if you used SELECT. The one thing I did notice is that your original code your cardinality function was in the condition of the while loop. Move this out to a local variable if you can, as calling cardinality each time on a tree that is not changing in the loop is a waste of time.

You could save a little time if you had a reference to PersonPhysicalDetails and used this in the SET as this could save you two navigation steps each time around your WHILE loop.

Regards,
_________________
MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.


Last edited by mgk on Fri Dec 09, 2005 7:24 am; edited 1 time in total
Back to top
View user's profile Send private message
klabran
PostPosted: Fri Dec 09, 2005 7:08 am    Post subject: Reply with quote

Master

Joined: 19 Feb 2004
Posts: 259
Location: Flagstaff AZ

Quote:

Is this Broker v6? I think you can do it easily with xpath if its v6.


This is v5 broker.

Quote:

Do u have the tag feature occuring in many places in the tree.... or do u need to pick it up from only physicalfeatures and otherphysicalfeatures ...?

Are there multiple instances of physicalfeatures ...?

Also in the code u user the word physical, but I did not notice it in the XML ...


I have the tag physicalfeatures and otherphysicalfeatures in two places in the tree and both can be 0 to many. I need to move other underneath the physical for one big tree. My sample XML was to illustrate the idea only (it does not match exactly my actual xml structure).

Quote:

I guess the answer depends on why you are refactoring the code.


I am doing it mainly for readability & a far second, speed. Also, I am doing it for learning purposes.

Quote:

The one thing I did notice is that your original code your cardinality function was in the condition of the while loop. Move this out to a local variable if you can, as calling cardinality each time on a tree that is not changing in the loop is a waste of time.


Already did it.
Back to top
View user's profile Send private message Visit poster's website
klabran
PostPosted: Fri Dec 09, 2005 8:25 am    Post subject: Reply with quote

Master

Joined: 19 Feb 2004
Posts: 259
Location: Flagstaff AZ

So then it seems that my original code even in my haste and lack of in depth ESQL knowledge was likely the best for readability and speed.... Wow. Go Figure! That would be a first.

Code:

-- Physical Features
   SET refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[]=(SELECT R.PhysicalFeatureCategoryText,R.PhysicalFeatureLocationText,R.PhysicalFeatureDescriptionText FROM refArrestIn.PersonPhysicalFeature.PersonPhysicalFeature_ROW[] AS R);   
   
   -- Existing and Other Feature counts
   DECLARE intEF INTEGER CARDINALITY(refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[]);
   DECLARE intOF INTEGER CARDINALITY(refArrestIn.OtherPersonPhysicalCharacter.OtherPersonPhysicalCharacter_ROW[]);

-- Combine Other Features with Existing Features
   SET intCntr = 1;
   WHILE intCntr <= intOF DO
        SET refArrestOut.ActivitySuspect.PersonPhysicalDetails.PersonPhysicalFeature[intEF + intCntr] = (SELECT R.PhysicalFeatureCategoryText,R.PhysicalFeatureLocationText,R.PhysicalFeatureDescriptionText FROM refArrestIn.OtherPersonPhysicalCharacter.OtherPersonPhysicalCharacter_ROW[intCntr] AS R);
      SET intCntr = intCntr + 1;
   END WHILE;


Except for the additional need for a refence on ActivitySuspect.PersonPhysicalDetails.

On a side note... I tried to move the tree using [< + 1] and [< + intCntr] but I was suprised in both instances that this doesn't seem to do what I thought it would. AKA - [< + 1] I thought would put me at the last record index + 1 (AKA a new last row). Instead it just replaced the last row of features over and over.

[< + intCntr] basically overwrote any elements in the original tree if the "other" tree had more elements.... Hmmmm.... [< +intCntr] I thought would put me at last record index + intCntr

I thought I could do this and eliminate the need to call cardinality on the first physical feature tree (intEF). Again, I imagine the Cardinality call is better for efficiency but I am experimenting with other ways to solve this....


Thanks,

Kevin
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Refactor code to move tree items...
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.