Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use SPARQL OPTIONAL to retrieve attributes for a resource that may exist?

Tags:

sparql

dbpedia

I'm trying to use a SPARQL query to retrieve information about a DBpedia resource (a Person). I'd like to use the same query to retrieve data about any Person by parameterizing the resource URI. Since some attributes may not exist for a particular resource, I'm making use of the OPTIONAL statement. Here is my query:

    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbpprop: <http://dbpedia.org/property/>
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    SELECT DISTINCT ?label ?abstract ?placeOfBirth 
        ?birthPlace ?birthDate ?deathDate ?page ?thumbnail 
    WHERE { 
        <http://dbpedia.org/resource/Neil_Simon> rdfs:label ?label ;
            dbo:abstract ?abstract ;
            foaf:page ?page .
        OPTIONAL {
            <http://dbpedia.org/resource/Neil_Simon> dbpprop:placeOfBirth ?placeOfBirth ;
                dbpprop:birthPlace ?birthPlace ;
                dbo:birthDate ?birthDate ;
                dbo:deathdate ?deathDate ;
                dbo:thumbnail ?thumbnail .
        }
        FILTER (LANG(?label) = 'en')    
        FILTER (LANG(?abstract) = 'en')
    }
    LIMIT 1

I've left everything except label, abstract and page in OPTIONAL, since if I use the same query for another person, they may not have those properties. The problem is, none of those optional attributes are showing up in the results. In Neil Simon's case, you can see that there are values for birthDate, birthPlace and thumbnail: http://dbpedia.org/resource/Neil_Simon. However, those values don't show up when I run the query: DBpedia SPARQL query. What am I doing wrong, and how can I optionally retrieve those properties?

like image 207
Ganesh Kumaraswamy Avatar asked Mar 01 '11 23:03

Ganesh Kumaraswamy


People also ask

What is optional SPARQL?

OPTIONAL is a binary operator that combines two graph patterns. The optional pattern is any group pattern and may involve any SPARQL pattern types. If the group matches, the solution is extended, if not, the original solution is given (q-opt3. rq).

Where is SPARQL used?

SPARQL can be used to express queries across diverse data sources, whether the data is stored natively as RDF or viewed as RDF via middleware. SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions.

What is construct query in SPARQL?

The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.


1 Answers

Although you have used an OPTIONAL construct the map pattern itself needs all the attributes within to match. So only if you have birthPlace, birthDate, deathDate and thumbnail the inner optional construct is satisfied

I would suggest breaking the OPTIONAL construct up into multiple OPTIONAL constructs.

like image 152
uncaught_exceptions Avatar answered Oct 06 '22 00:10

uncaught_exceptions