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

Hi,
If you are using SPARQL 1.1 you can use Property Paths
(http://www.w3.org/TR/sparql11-query/#propertypaths).

I this case the query should be:

PREFIX rev: <http://dbpedia.org/review/>
PREFIX pd: <http://dbpedia.org/product/>
SELECT DISTINCT ?person ?product
WHERE
{ pd:04 ^rev:5/(rev:5/^rev:5)* ?person .
  pd:04 (^rev:5/rev:5)* ?product .
}

Anyway, there could be performance issues, so you have to check if
this approach is actually feasible in your case.

Best,
Miguel

On Thu, Jul 10, 2014 at 5:33 AM, Lei Xu <lei.xu@marklogic.com> wrote:
> 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><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>
>
> 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 10:51:17 UTC