playMaker

Author Topic: Datamaker random query  (Read 2454 times)

kretin

  • Playmaker Newbie
  • *
  • Posts: 7
Datamaker random query
« on: May 12, 2014, 10:03:06 PM »
I'm trying to use Datamaker to get a random question from the following xml:

Code: [Select]
<Level Name="01" Max="12"]
      <card>
        <answer>4</answer>
        <question>3+1</question>
        <question>1+3</question>
        <question>2+2</question>
        <question>4+0</question>
        <question>0+4</question>
      </card>
      <card>
        <answer>5</answer>
        <question>3+2</question>
        <question>2+3</question>
        <question>1+5</question>
        <question>5+1</question>
        <question>0+5</question>
      </card>
</Level>

Using Xml Select Single Node I can get a random question, but it still has the tags eg. <question>4+1</question>. If I try to get the "question" property of that result it comes up blank.
Using Xml Get Node Properties I can get the first question without tags, but can't seem choose a question.

I can figure out a few complicated ways to achieve my result, but I feel like I'm missing something simple.

Is there a way to specify which property to get as there is with selecting nodes?
Or is there a better format for my xml to work within Datamaker?

Thanks

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Datamaker random query
« Reply #1 on: May 13, 2014, 01:12:48 AM »
Hi,

 It's all good, It's just xpath the usual xpath headaches :)

I have put your xml inside a root like this

Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<root>
<Level Name="01" Max="12">
      <card>
        <answer>4</answer>
        <question>3+1</question>
        <question>1+3</question>
        <question>2+2</question>
        <question>4+0</question>
        <question>0+4</question>
      </card>
      <card>
        <answer>5</answer>
        <question>3+2</question>
        <question>2+3</question>
        <question>1+5</question>
        <question>5+1</question>
        <question>0+5</question>
      </card>
</Level>
</root>

Which I always encourage you to do, I would even create a "Levels" node to list of the levels.


Code: [Select]
/root/Level[@Name="01"]/card[1]/question[3]
will return the node "<question>2+2</question>"

so, use the action "Xml Select Single Node" and use this xpath above

In that action, click on "Get a property", the property will be just a dot "." ( which means the inner content of the selected node), and save it in a string variables, its value will be "2+2".

 If you have trouble, I'll do a micro sample.

tip: To work your way into all this, use an online xpath tester like http://www.xpathtester.com/xpath

also, within PlayMaker, and these actions, always ( during debugging at least), save the result in "store Reference", here I used "Question", then the result will persists in memory and you can see what was the exacty result returned by your query, use the action "Xml Get Node Properties" and in the source selection, select "In Memory" and as "memory reference" put "Question", and you'll see the xml result from your xpath query.

Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Datamaker random query
« Reply #2 on: May 13, 2014, 07:27:30 AM »
Hi,

 realized I haven't covered the randomess selection of your question. Easy :)

use random int ( you'll need to know the counts for the nodes prior doing this xpath). then in the xpath query, use "_x_" as a placeholder to insert variables:

_0_ will be replaced by the content of the first variable you created for this xpath.

Code: [Select]
/root/Level[@Name="_0_"]/card[_1_]/question[_2_]
so the first variable would be "01", the second would be 1 and the third one would be 3, and you would get question 3 of card 1 of level "01"

Bye,

 Jean


kretin

  • Playmaker Newbie
  • *
  • Posts: 7
Re: Datamaker random query
« Reply #3 on: May 13, 2014, 05:31:15 PM »
Thank you so much Jean,

It was the dot that I missed. I saw that in the descriptions, but didn't understand it.

Thank you for also explaining "Store Reference". I wondered what that was, and it will definitely save me some variables.

In the real XML I do actually have a root level and another group before <Level>.


Can you let me know if I'm on the right track in my next steps?

I've stored the results in a hashtable (Max= number of hashtable entries).

I plan to copy the keys and values to their own arrays and shuffle them.

Then display the contents of the arrays. The user chooses matches, and I check the selection against the hashtable to see if it's correct.

Thanks again.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Datamaker random query
« Reply #4 on: May 14, 2014, 12:32:15 AM »
Hi,

 xpath is very powerful, so I would not hesitate relying on more complex queries instead of trying to cache things here and there, which quickly become messy...

 typically, I cache within xmlMaker itself using memory reference, that's the most productive approach I found so far. and so you would only maintain fsm variables like your level id, card id and question id.

Bye,

 Jean