to navigate the whole network to the end of all branches and nodes by SPARQL

Hello all,

Trying to create something similar with Amazon’s recommendation list by RDF and SPARQL.

I have n-quad rdf like below.
subject:person predicate:review_score object:product

<http://dbpedia.org/person/A><http://dbpedia.org/review/5><http://dbpedia.org/product/04><http://source-description/review> .
<http://dbpedia.org/person/A><http://dbpedia.org/review/5><http://dbpedia.org/product/16><http://source-description/review> .
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/16><http://source-description/review> .
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/04><http://source-description/review> .
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/07><http://source-description/review> .
<http://dbpedia.org/person/C><http://dbpedia.org/review/5><http://dbpedia.org/product/07><http://source-description/review> .
<http://dbpedia.org/person/C><http://dbpedia.org/review/5><http://dbpedia.org/product/10><http://source-description/review> .
<http://dbpedia.org/person/D><http://dbpedia.org/review/5><http://dbpedia.org/product/10><http://source-description/review> .
<http://dbpedia.org/person/D><http://dbpedia.org/review/5><http://dbpedia.org/product/11><http://source-description/review> .
<http://dbpedia.org/person/E><http://dbpedia.org/review/4><http://dbpedia.org/product/12><http://source-description/review> .
<http://dbpedia.org/person/E><http://dbpedia.org/review/5><http://dbpedia.org/product/14><http://source-description/review> .

The query starts with a specified product – for example product 04.
Step 1. Find all the people who have reviewed this specified product as score = 5.
Step 2. Find all the reviewed products by the people from Step 1 as score = 5.
Step 3. Find all the people who have reviewed the products from Step 2 as score = 5.
...
...
Step n. Until no more descendants nodes.
Eventually I want to have a list with 2 arrays:

1. All the matching products.
2. All the related people.

Because E’s review on product 12 only scores 4 then the branch from E will be excluded. So the expected result should look like this.

<http://dbpedia.org/person/A><http://dbpedia.org/review/5><http://dbpedia.org/product/04>
<http://dbpedia.org/person/A><http://dbpedia.org/review/5><http://dbpedia.org/product/16>
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/16>
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/04>
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/07>
<http://dbpedia.org/person/C><http://dbpedia.org/review/5><http://dbpedia.org/product/07>
<http://dbpedia.org/person/C><http://dbpedia.org/review/5><http://dbpedia.org/product/10>
<http://dbpedia.org/person/D><http://dbpedia.org/review/5><http://dbpedia.org/product/10>
<http://dbpedia.org/person/D><http://dbpedia.org/review/5><http://dbpedia.org/product/11>

My SPARQL looks like below:

PREFIX rev: <http://dbpedia.org/review/>
PREFIX pd: <http://dbpedia.org/product/>
SELECT distinct ?s2 ?o2  WHERE
{ ?s rev:5 pd:04.
  ?s rev:5 ?o2 .
  ?s2 rev:5 ?o2 .
}

And the result is like below:

<http://dbpedia.org/person/A><http://dbpedia.org/review/5><http://dbpedia.org/product/04<http://dbpedia.org/person/A%3E%3Chttp://dbpedia.org/review/5%3E%3Chttp://dbpedia.org/product/04>>
<http://dbpedia.org/person/A><http://dbpedia.org/review/5><http://dbpedia.org/product/16<http://dbpedia.org/person/A%3E%3Chttp://dbpedia.org/review/5%3E%3Chttp://dbpedia.org/product/16>>
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/16<http://dbpedia.org/person/B%3E%3Chttp://dbpedia.org/review/5%3E%3Chttp://dbpedia.org/product/16>>
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/04<http://dbpedia.org/person/B%3E%3Chttp://dbpedia.org/review/5%3E%3Chttp://dbpedia.org/product/04>>
<http://dbpedia.org/person/B><http://dbpedia.org/review/5><http://dbpedia.org/product/07<http://dbpedia.org/person/B%3E%3Chttp://dbpedia.org/review/5%3E%3Chttp://dbpedia.org/product/07>>
<http://dbpedia.org/person/C><http://dbpedia.org/review/5><http://dbpedia.org/product/07<http://dbpedia.org/person/C%3E%3Chttp://dbpedia.org/review/5%3E%3Chttp://dbpedia.org/product/07>>

It is different from expected. There must be something wrong with the query pattern. I would like to navigate all the way to the end of the network with certain constraints.
What I am looking for is a feature similar with Oracle SQL’s SYS_CONNECT_BY_PATH function. But I do not know how to realize it with SPARQL.

Any ideas and hints will be appreciated.

Thanks,
Lei Xu

Received on Thursday, 10 July 2014 08:34:11 UTC