Search code examples
sparqldbpedia

Ask if SPARQL resource exists


What is a good way to check if a SPARQL resource exists? I'm searching for the equivalent of firing a HTTP GET request to e.g. http://dbpedia.org/resource/Game_of_Thrones and check the HTTP status code but I'd like to do it with a SPARQL query.

I thought about something like this:

ASK {<http://dbpedia.org/resource/Game_of_Thrones> a <http://dbpedia.org/resource/>} 

I'm sure there is a good way to do this but I can't find it.

Note: I don't want to check for the existance of a specific triple. I just want to know if a resource exists.


Solution

  • SPARQL is an RDF query language. An RDF triple consists of subject, predicate and object.

    You just need to check if an URI is present in any of these positions, using UNION as disjunction.

    ASK {
        VALUES (?r) { (dbr:Game_of_Thrones) }
            { ?r ?p ?o }
            UNION
            { ?s ?r ?o }
            UNION
            { ?s ?p ?r }
        } 
    

    Although the SPARQL specification doesn't allow [] in predicate position, DBpedia (i.e., the Virtuoso server serving the DBpedia endpoint) understands this:

    ASK {
        VALUES (?r) { (dbr:Game_of_Thrones) }
            { ?r [] [] }
            UNION
            { [] ?r [] }
            UNION
            { [] [] ?r }
        }