mardi 17 mars 2015

sparql: randomly select one connection for each node

Here is my problem that has been bothering me for many days: I have the following query results:



<node:1><urn:connectTo><node:2>
<node:1><urn:connectTo><node:3>
<node:1><urn:connectTo><node:4>
<node:2><urn:connectTo><node:10>
<node:2><urn:connectTo><node:11>
<node:2><urn:connectTo><node:12>
<node:3><urn:connectTo><node:21>
<node:3><urn:connectTo><node:13>
<node:3><urn:connectTo><node:41>
<node:3><urn:connectTo><node:100>
<node:4><urn:connectTo><node:119>
<node:4><urn:connectTo><node:120>


As you can see: every node has more than one connection. I want to select one connection for each node randomly.


I have failed by trying these solutions: (1)



select ?currentNode ?nextNode
where
{
?currentNode ?p ?nextNode
BIND(RAND() AS ?orderKey)
}
ORDER BY ?orderKey
LIMIT 1


(2)



select ?currentNode SAMPLE(?nextNode) as ?nextNode1
where
{
?currentNode ?p ?nextNode
}
GROUP BY ?currentNode


Note: the result gives the first connection of each node but not randomly


(3)



select ?currentNode ?nextNode (COUNT(?nextNode) AS ?noOfChoices)
where
{
?currentNode ?p ?nextNode
BIND(RAND() AS ?orderKey)
}
GROUP BY ?currentNode
ORDER BY ?orderKey
OFFSET (RAND()*?noOfChoices)
LIMIT 1


They all failed. Could anyone show a right solution to this problem? I really think this is a typical problem in many situations. Thank you!





Aucun commentaire:

Enregistrer un commentaire