How to return all S->P->O triples from a starting resource to a specified path depth?

假装没事ソ 提交于 2019-12-04 16:28:28

Here is a SPARQL query that works in Virtuoso. Note the SPARQL W3C standard does not support this syntax and it will fail in other triplestores.

PREFIX p: <http://www.example.org/person/> 
PREFIX x: <example.org/foo/>
# CONSTRUCT {?s ?p ?o}  # If you wish to return the graph
SELECT ?s ?p ?o   # To return the triples
FROM <http://localhost:8890/MYGRAPH>
where { p:Person_1 (x:foo|!x:bar){1,3} ?s  .
  ?s ?p ?o .
}LIMIT 100

See also K. Idehen's wiki entry here: http://linkedwiki.com/exampleView.php?ex_id=141

And thanks to @Joshua Taylor for advice in the same area.

Kingsley Uyi Idehen

Working Drafts of SPARQL 1.1 Property Paths included the {n,m} operator for handling this issue, which was implemented (and will remain supported) in Virtuoso. Here's a tweak to @tim's response.

  • Live SPARQL Query Results Page using the DBpedia endpoint (which is a Virtuoso instance).

  • Live SPARQL Query Definition Page that opens up query source code in the default DBpedia query editor.

  • Actual Query Example:

    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
    SELECT DISTINCT ?s AS ?Entity 
                    ?o AS ?Category 
    WHERE { 
            ?s  rdf:type       <http://dbpedia.org/ontology/AcademicJournal> ; 
                rdf:type{1,3}  ?o 
          } 
    LIMIT 100
    
Kingsley Uyi Idehen

Should you be looking for LinkedIn-like presentation of Contact Networks and Degrees of Separation between individuals, here is an example using Virtuoso-specific SPARQL Extensions that solve this particular issue:

SELECT ?o                                              AS ?WebID
       ((SELECT COUNT (*) WHERE {?o foaf:knows ?xx}))  AS ?contact_network_size
       ?dist                                           AS ?DegreeOfSeparation 
       <http://www.w3.org/People/Berners-Lee/card#i>   AS ?knowee
  WHERE
    {
      {
        SELECT ?s ?o
        WHERE
          {
            ?s foaf:knows ?o
          }
      } OPTION (TRANSITIVE, t_distinct, t_in(?s), t_out(?o), t_min (1), t_max (4), t_step ('step_no') AS ?dist) .
      FILTER (?s= <http://www.w3.org/People/Berners-Lee/card#i>)
      FILTER (isIRI(?s) and isIRI(?o))
    }
  ORDER BY ?dist DESC (?contact_network_size)
  LIMIT 500

Note: this approach is the only way (at the current time) to expose actual relational hops between entities in an Entity Relationship Graph that includes Transitive relations.

Kingsley Uyi Idehen

Bearing in mind that the r{n,m} operator was deprecated in the final SPARQL 1.1 (but will remain supported in Virtuoso), you can use r/r?/r? instead of r{1,3}, if you want to work strictly off the current spec:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
SELECT DISTINCT ?s AS ?Entity 
                ?o AS ?Category 
WHERE { 
        ?s  rdf:type                          <http://dbpedia.org/ontology/AcademicJournal> ; 
            rdf:type / rdf:type? / rdf:type?  ?o 
       }
LIMIT 100

Here's a live example, against the DBpedia instance hosted in Virtuoso.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!