14

Seems I don't grok SPARQL DESCRIBE queries. I need to retrieve the full graphs of resources matching a condition. On one SPARQL endpoint I have tried (Norwegian Rådata Nå, http://data.bibsys.no/data/query_authority.html) this works just fine:

PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

DESCRIBE ?person WHERE {
  ?person rdf:type foaf:Person .
  FILTER EXISTS { ?person owl:sameAs ?sameAs }
}
LIMIT 100

I get a result with Person resources that have an owl:sameAs triple.

On other SPARQL endpoints, Onki Light (http://sparql.onki.fi/sparql.tpl) and DBPedia (http://dbpedia.org/sparql) a similar query

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

DESCRIBE ?x WHERE {
  ?x rdf:type skos:Concept .
  FILTER EXISTS { ?x skos:prefLabel ?prefLabel }
}
LIMIT 100

returns lots of resources which have neither rdf:Type = skos:Concept nor a skos:prefLabel, so I am obviously not doing the query correctly. How should this kind of query be stated to produce the result I need?

Yuri
  • 3,283
  • 1
  • 19
  • 37
Nils Weinander
  • 2,019
  • 1
  • 14
  • 19

3 Answers3

11

The "received resources" without skos:prefLabel or skos:Concept are probably related to a resource that meets your requirements.

The SPARQL DESCRIBE query does not actually return resources matched by the graph pattern of the query, but an RDF graph that "describes" those resources. It is up to the sparql service to choose what triples are included to describe a resource. (see the standard below)

The W3C Proposed Recommendation on SPARQL 1.1 says:

The DESCRIBE form returns a single result RDF graph containing RDF data about resources. [...] The description is determined by the query service.

So, the resources you unexpectedly receive maybe describing the resources you actually want. To investigate your issue: Check the triples you should actually receive for a relation to your wanted resource. A good way is to start with LIMIT 1 to see the effect of DESCRIBE queries.

Maybe a SELECT query is what you need? It returns only the resources matched by the graph pattern.

Community
  • 1
  • 1
Thomas
  • 1,460
  • 12
  • 20
  • Thanks! I tried DESCRIBE and got the expected resource and a bunch of related resources, just as you write. SELECT or CONSTRUCT are not viable options in the case at hand, as I don't know the actual structure of the data. But, knowing what the "extra" resources are, I can just ignore them when I process the result. – Nils Weinander Mar 04 '13 at 10:09
2

As stated by @Thomas, DESCRIBE is underspecified a bit by the standard, hence you will get inconsistent results. However, using CONSTRUCT, you can return what many engines will return for DESCRIBE, i.e. the SPO plus the OPS, and do so consistently across services. Here a query that does this:

CONSTRUCT {
   ?person ?p ?o .
   ?s ?p1 ?person .
}
WHERE {
   ?person rdf:type foaf:Person .
   FILTER EXISTS { ?person owl:sameAs ?sameAs } .
   ?person ?p ?o .
   ?s ?p1 ?person .
}

This gets you an RDF graph that "describes" the resources bound to ?person, namely all properties of ?person and all properties whose value (object) is ?person.

scotthenninger
  • 3,613
  • 1
  • 12
  • 23
  • Interesting! I tried this on dbpedia (the Rådata Nå SPARQL endpoint is currently not working): PREFIX rdf: PREFIX skos: CONSTRUCT { ?concept ?p ?o . ?s ?p1 ?concept . } WHERE { ?concept rdf:type skos:Concept . FILTER EXISTS { ?concept skos:prefLabel ?prefLabel } . ?concept ?p ?o . ?s ?p1 ?concept . } LIMIT 100 With mixed results. Some data are just what I want, others do not fulfil the conditions. – Nils Weinander May 02 '16 at 09:02
  • Try increasing the LIMIT. SPARQL will not get data in any specific order, so you could also try LIMIT/OFFSET combinations with ORDER BY. – scotthenninger May 02 '16 at 13:07
1

SELECT or CONSTRUCT are not viable options in the case at hand, as I don't know the actual structure of the data

Why do you say that? I think that you either want to get only the resources:

select ?x WHERE {
  ?x rdf:type skos:Concept .
  FILTER EXISTS { ?x skos:prefLabel ?prefLabel }
}
LIMIT 100

or all their outgoing triples:

select ?x ?p ?y WHERE {
  ?x rdf:type skos:Concept.
  FILTER EXISTS { ?x skos:prefLabel ?prefLabel }
  ?x ?p ?y
}
LIMIT 100
Vladimir Alexiev
  • 2,176
  • 1
  • 16
  • 28
  • Thanks! I tried something like that but bungled the SPARQL syntax. Next issue is the best way to build an actual graph from the SPARQL XML result. – Nils Weinander Jan 08 '15 at 20:37