Using the "GRAPH" Keyword in SPARQL to Retrieve Remote Charts

I want to use SPARQL for a relatively simple task: given the FOAF graph, I would like to analyze the elements that I find there, get their tags (if they exist), and then use them as new graphs from which you can find information about these people.

So, for example, you could provide a simple use case when I want to run a SPARQL query to list all my favorite products (according to my FOAF file), as well as favorite products of all my friends.

Here's what it looks like at the moment. Please note that for testing at the moment, all I am trying to do with the query below is to get the name of a friend through a variable? Name3. Running this query returns no results for? Graph and? Name3, although I know that rdfs: seeAlso is a link to some valid RDF files, of which at least two must have a name attribute. Thanks for any input you have.

PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?name1 ?name2 ?graph ?name3 FROM <my-rdf-file> WHERE { ?person1 foaf:knows ?person2 . ?person1 foaf:name ?name1 . ?person2 foaf:name ?name2 . OPTIONAL { ?person2 rdfs:seeAlso ?graph . GRAPH ?graph { ?person3 foaf:name ?name3 . } } } 
+7
source share
2 answers

GRAPH does not imply retrieving deleted data in storage, which would be too dangerous for security. There may be some systems where you can enable this, but this is non-standard.

However, in the SPARQL 1.1 Update, there is a LOAD keyword that does this, although you can write:

 LOAD <uri> 

Which will take the graph to the repository so you can write:

 PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?graph FROM <my-rdf-file> WHERE { ?person1 foaf:knows ?person2 . ?person1 foaf:name ?name1 . ?person2 foaf:name ?name2 . OPTIONAL { ?person2 rdfs:seeAlso ?graph . } } 

Provide bindings for? graph into the LOAD statement set, and then run the original query.

NB in ​​some systems, for example. 4store you need to enable LOAD, this is not allowed by default, so check the documentation in the store you are using.

+4
source

Instead of using LOAD as soon as you find that the graphs you need to include, according to Steve's example above, it would be wise to alternatively use FROM NAMED, specifying all the relevant graphs in the resulting query, i.e.

 PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?name1 ?name2 ?name3 FROM <my-rdf-file> FROM NAMED <discovered-graph-URI-1> FROM NAMED <discovered-graph-URI-n> WHERE { GRAPH <my-rdf-file> { ?person1 foaf:knows ?person2 . ?person1 foaf:name ?name1 . ?person2 foaf:name ?name2 . } OPTIONAL { ?person2 rdfs:seeAlso <discovered-graph-URI-1> . GRAPH <discovered-graph-URI-1> { ?person3 foaf:name ?name3 . } ?person2 rdfs:seeAlso <discovered-graph-URI-n> . GRAPH <discovered-graph-URI-n> { ?person3 foaf:name ?name3 . } } } 

This way you get around security issues and you don’t need to maintain data in your own three-dimensional space.

+5
source

All Articles