Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL queries with relational operator

I want to use relational AND/OR operator onto SPARQL queries.

Here query :

SELECT DISTINCT ?dbpedia_link str(?name) as ?label str(?label1) as ?label1 ?freebase_link WHERE {
            ?dbpedia_link rdfs:label ?label1 . 
            ?dbpedia_link foaf:name ?name .
            {
                { ?dbpedia_link rdf:type dbpedia-owl:Film .}
                UNION
                { ?dbpedia_link rdf:type dbpedia-owl:Person .}
            }
            ?dbpedia_link owl:sameAs ?freebase_link .
            FILTER regex(?freebase_link, "^http://rdf.freebase.com") .
            FILTER (lang(?label1) = 'en'). 
            ?name bif:contains "Akshay_Kumar" . 
            ?dbpedia_link dcterms:subject ?sub 
        }

In this query, I have used Akshay_Kumar which is single name. Now I want that, how can I use place multiple names at once using relational AND/OR operator. In short, how can we use relational operators in sparql.

Executing sparql query URL : http://dbpedia.org/sparql

like image 730
iNikkz Avatar asked Aug 21 '14 11:08

iNikkz


1 Answers

Conjunction

AND is easy. If you want to say that X is related to both Y by property P and to Z by property Q, you just include both triples:

X P Y .
X Q Z .

That can be abbreviated:

X P Y ; Q Z .

Now, if the properties are the same, so that you're saying X is related to Y and Z by property P, it becomes:

X P Y ;
  P Z .

SPARQL provides a shorthand for that (and it's shared with the Turtle serialization of RDF):

X P Y, Z .

Disjunction

OR is a bit more complicated, but you've already shown one way of doing it. If you want to say that X is related to either Y or Z, you can do:

{ X P Y } UNION { X P Z }

SPARQL 1.1 includes VALUES which can make this a bit simpler:

VALUES ?xpValue { Y Z }
X P ?xpValue

Updating Your Query

Your query actually isn't legal SPARQL 1.1 (or SPARQL), even though Virtuoso (the endpoint that DBpedia runs) accepts it. You can test with sparql.org's query validator. Also note that you're using bif:contains, which provides a string containment function, but SPARQL 1.1 actually provides a contains function that you can use. It also provides strstarts which you can use to recognize freebase links a bit more efficiently. It also provides langMatches, which is the correct way to check the language tags of strings. Using those, I'd rewrite your query like the following, which finds resources whose names contain "John" or "Mary". (The limit is just to keep the results short for this example.)

select ?resource ?name ?label ?freebase where {
  values ?type { dbpedia-owl:Film dbpedia-owl:Person }
  values ?namePart { "John" "Mary" }

  ?resource rdfs:label ?label ;
            foaf:name ?name ;
            a ?type ;
            owl:sameAs ?freebase .

  filter ( langMatches( lang(?label), 'en' ) &&
           strstarts(str(?freebase), "http://rdf.freebase.com" ) &&
           contains( ?name, ?namePart ) )
}
limit 10

SPARQL results

like image 104
Joshua Taylor Avatar answered Sep 21 '22 23:09

Joshua Taylor