Welcome to openkapow Sign in | Join
in Search
.

Grouping only specific elements in the RESTOutput object

Last post 05-13-2008, 2:25 PM by Simon. 7 replies.
Sort Posts: Previous Next
  •  05-05-2008, 8:17 AM 19145

    Grouping only specific elements in the RESTOutput object

    Hello, 

    I am wondering if it is possible to group some of the output objects in a sub-element of the RESTOutput object? More specifically, I am trying to create a robot that returns detailed information about a vehicle along with a set of thumbnail URLs.

    Ideally, I would like the result to be something similar to the XML snippet below: 

    <result>
        <vehicle>
            <color>black</color>
            <milage>156616</milage>
            <thumbnails>
                <thumbnail>http://w...image1.jpg</thumbnail>
                <thumbnail>http://w...image2.jpg</thumbnail>
                <thumbnail>http://w...image3.jpg</thumbnail>
            </thumbnails>
        </vehicle>
    </result>
     

    I use extract actions to extract attributes like color, milage etc and then a For Each action to extract each thumbnail URL. My problem is that when I try my robot, the result contains several vehicle elements, each with a single thumbnail element. It looks similar to this:

     <result>
        <vehicle>
            <color>black</color>
            <milage>156616</milage>
            <thumbnail>http://w...image1.jpg</thumbnail>
        </vehicle>
        <vehicle>
            <color>black</color>
            <milage>156616</milage>
            <thumbnail>http://w...image2.jpg</thumbnail>
        </vehicle>
        <vehicle>
            <color>black</color>
            <milage>156616</milage>
            <thumbnail>http://w...image3.jpg</thumbnail>
        </vehicle>
    </result>

    Is it possible to make the robot add all the output added after the for each action to a sub-element (such as <thumbnails>)? Or, is there any other way I can have the set of thumbnails grouped together?

    Cheers,
    Emil

    Filed under: , ,
  •  05-05-2008, 2:49 PM 19155 in reply to 19145

    Re: Grouping only specific elements in the RESTOutput object

    You are looking to provide multiple levels of hierarchy is you REST results.  This isn't possible today.  We are aware of this issue.

    Your best approach is to iterate over the images and use a converter in the step to take the image URL and append them to one field separated by columns called thumbnails.  You will then have to parse that field to get the comma separated values.

  •  05-06-2008, 11:55 PM 19198 in reply to 19155

    Re: Grouping only specific elements in the RESTOutput object

    Thanks for your reply Chris. Too bad this feature is not available yet ... But, I'll stay tuned, hoping that it will be added in the future :-)

    However, I like your idea of having a comma separated list of thumbnail URLs, it's sounds like a much cleaner solution than what I have at the moment. Only problem now is that I don't know how to achieve this kind of behaviour using RoboMaker.

    > take the image URL and append them to one field separated by columns called thumbnails

    How exactly do I do this? I tried to create a global variable before entering the For Each loop, but I couldn't figure out how to append the extracted URL to this variable. Also, what action would I use to assign the value of a global variable to an output attribute?

  •  05-07-2008, 9:31 AM 19227 in reply to 19198

    Re: Grouping only specific elements in the RESTOutput object

    Hi Emil

    Use converters with "extract" to add multiple items separated by commas (or whatever you want) to a single attribute.

    here's an example: http://openkapow.com/files/folders/tutorials_and_examples/entry19226.aspx

    notice the steps "append to title" and how they are configured.

    -Simon 

  •  05-08-2008, 7:11 AM 19251 in reply to 19227

    Re: Grouping only specific elements in the RESTOutput object

    Hi Simon,

    thanks for your reply and for the sample code.

    However, in my case the number of thumbnails is unknown, why I need to iterate over them somehow. When I use any of the For Each loops, a new element is added for each iteration, meaning that I'm still stuck at square one ...

    Is there any way I can create a loop that doesn't create new sub-elements for each iteration?

  •  05-08-2008, 8:42 AM 19256 in reply to 19251

    Re: Grouping only specific elements in the RESTOutput object

    What if you remove "return results" from the end of your for each loop? and perhaps add it as a branch right before the beginning of your loop. Then your robot would loop loop loop and then go back an return results.
     

  •  05-13-2008, 5:28 AM 19328 in reply to 19256

    Re: Grouping only specific elements in the RESTOutput object

    Simon, reading your reply makes my question sound pretty stupid. In hindsight it is quite obvious that returning the result object in each iteration will not work. However, simply removing the return action was not enough. The changes I did to the result object in each loop iteration were not reflected in the final result object I returned after the loop had finished. It took me a while to figure out why, until I read this paragraph in the documentation:

    "A global variable is a variable that keeps its value during the entire execution of the robot. This is different from object attributes, whose values are not kept across loop iterations and branches."

    The solution was to create a global variable before entering the loop and then append each thumbnail URL to it. I thought I would document my solution here, in case anyone else runs into the same kind of problem.

    To append the URL, I used three steps (performed each iteration):

    1. Extract the URL to a temporary output attribute
    2. Use "Get Global Variable" to store the variable in an attribute
    3. Use "Assign Global Variable" and write an expression which concatenates these two attributes. Assign the resulting value to the global variable.

    (It is possible that the second step is unnecessary, but I could not figure out how to use the global variable in the assignment expression)

    After the loop has finished executing (and in a different branch), store the global variable in the proper attribute before finally returning the result object. Done.

    Thanks for all the help, the robot is working as expected now.

  •  05-13-2008, 2:25 PM 19336 in reply to 19328

    Re: Grouping only specific elements in the RESTOutput object

    Hi

    Thanks for the reply and your solution to the problem.
     

View as RSS news feed in XML
.
Copyright 2006, 2007 KapowTech.com All Rights Reserved Company | Contact | Terms | Privacy