2

I'm trying to use SPARQL CONSTRUCT to strip a set of data from dbpedia - I'm only interested in a set of Artists, and I want Sesame as small as possible for speed.

What I thought I could do is use CONSTRUCT to get every predicate for a given artist. I can get the first CONSTRUCT clause working to make sure I get type "Person", but that only gives me triples satisfying that clause - I want their names, labels, birthPlaces etc to. My query below is trying to capture Monet's name in the second CONSTRUCT clause? If I have it right, this would give me a triple of

<http://dbpedia.org/resource/Claude_Monet>  
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   
<http://xmlns.com/foaf/0.1/Person>

and a triple like this

<http://dbpedia.org/resource/Claude_Monet>  
<http://xmlns.com/foaf/0.1/name>
"Claude Monet"@en

How do I get my query to use the object of Monet's name as a variable to use where I am inserting empty quotes please? Here's the query

PREFIX purl: <http://purl.org/dc/elements/1.1/>
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
CONSTRUCT {
  ?s a foaf:Person .
  ?s foaf:name ""
} WHERE { 
  ?s foaf:surname "Monet"@en . 
  ?s purl:description "Painter"@en
} LIMIT 100

Any help really appreciated

Mike

MikeB
  • 577
  • 5
  • 15
  • I'm glad to see that you answered your question! You may want to [accept it](http://meta.stackexchange.com/q/5234/225437), too. (It's quite alright to accept your own answer; after all, you're in the position to know what works best for you.) The code in your answer can be simplified a bit, though, and I did show how in [my answer](http://stackoverflow.com/a/19642051/1281433). – Joshua Taylor Oct 28 '13 at 18:13

3 Answers3

2

Here we are, this is it. Once I figured the variable bindings between the CONSTRUCT and WHERE it was actually straightforward.

Each WHERE statement selects those values from the repo, and requires a matching template statement in the CONSTRUCT referencing the same variable. The value is just replaced. Obvious I suppose.

Note to self - MUST stop thinking like RDBMS.

PREFIX purl: <http://purl.org/dc/elements/1.1/>
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
PREFIX dbont:  <http://dbpedia.org/ontology/>
PREFIX w3: <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT 
{
    ?s a foaf:Person .
    ?s foaf:name ?a .
    ?s foaf:surname ?b .
    ?s foaf:givenName ?c .
    ?s w3:label ?d .
    ?s purl:description ?e .
    ?s dbont:birthPlace ?f .
    ?s dbont:deathPlace ?g .
    ?s dbont:birthDate ?h .
    ?s dbont:deathDate ?i
}  
WHERE {
    ?s foaf:name ?a .
    ?s foaf:surname ?b .
    ?s foaf:givenName ?c .
    ?s w3:label ?d .
    ?s purl:description ?e .
    ?s dbont:birthPlace ?f .
    ?s dbont:deathPlace ?g .
    ?s dbont:birthDate ?h .
    ?s dbont:deathDate ?i .
    ?s purl:description "Painter"@en
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
MikeB
  • 577
  • 5
  • 15
  • I think that since your `CONSTRUCT` will include all the triples from the `WHERE`, you can just do `CONSTRUCT WHERE { ... }` (see [CONSTRUCT WHERE](http://www.w3.org/TR/sparql11-query/#constructWhere) in the SPARQL 1.1 recommendation). I realize that the `?s purl:description "Painter"@en`, isn't in the CONSTRUCT part, but that same triple will match `?s purl:description ?e`, so you'd get that triple in the construct too. – Joshua Taylor Oct 28 '13 at 17:10
  • This also means that you could use `construct where { ... ?s purl:description ?e, "Painter"@en }` which would be a little bit shorter. In fact, you could do `construct where { ?s foaf:name ?a ; foaf:surname ?b ; ... ; purl:description ?e, "Painter"@en }`. – Joshua Taylor Oct 28 '13 at 17:11
  • All that is to say that you can use [this simplified query to get these results (shown in the link)](http://pastebin.com/Au7jHfXb). – Joshua Taylor Oct 28 '13 at 17:59
1

As an alternative/shorthand, you can also use a DESCRIBE query, for example:

DESCRIBE <http://dbpedia.org/resource/Claude_Monet>

will give you a subgraph with all incoming and outgoing properties of <http://dbpedia.org/resource/Claude_Monet>, or:

DESCRIBE ?x WHERE { ?x purl:description "Painter"@en }

will give you a subgraph for all ?x that have a matching purl:description.

Of course, this give you less control over the exact triples being extracted, but on the upside it's shorter and will also not need to be adapted if certain triple patterns do not exist for a particular resource (e.g. your CONSTRUCT query will not match if for any reason a particular painter does not have a deathDate or foaf:surname or any of the other properties in your WHERE clause).

A small caveat: the precise contents of the result of a DESCRIBE query are implementation-dependent. Some triplestores may choose to only return outgoing properties, for example. But Sesame (and, I believe, the DBPedia endpoint as well) return what is known as a Symmetric Concise Bounded Description.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
1

I see that you've already answered you own question, but I do want to point out that when the triples to be returned by a construct query are exactly the same as those matched by the where part, and the where part doesn't contain anything too fancy, you can use the special shorthand construct where:

16.2.4 CONSTRUCT WHERE

A short form for the CONSTRUCT query form is provided for the case where the template and the pattern are the same and the pattern is just a basic graph pattern (no FILTERs and no complex graph patterns are allowed in the short form). The keyword WHERE is required in the short form.

Using a construct where, as well as using the same namespaces that the DBpedia SPARQL endpoint already defines (which are conventional, e.g., rdfs rather than w3, and dc rather than purl), your query becomes:

construct where {
    ?s foaf:name ?a ;
       foaf:surname ?b ;
       foaf:givenName ?c ;
       rdfs:label ?d ;
       dc:description ?e, "Painter"@en ;
       dbpedia-owl:birthPlace ?f ;
       dbpedia-owl:deathPlace ?g ;
       dbpedia-owl:birthDate ?h ;
       dbpedia-owl:deathDate ?i .
}

SPARQL results

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353