How to request classes with an object property in Sparql

Does anyone know how to query classes with an object property in Sparql? Suppose we have an OWL file that contains the following

Human ----(hasPizza)---> Pizzas 

People and pizzas are classes (or concepts). In SPARQL, this query returns nothing:

 select ?x ?y where { ?x hasPizza ?y } 

But if I add two people (or entities) in accordance with concepts such as

 Human:Jim ----(hasPizza)---> Pizzas:cheesePizza 

this query will return ?x=Jim and ?y=cheesePizza How can I get ?x=Human and ?y=Pizza using SPARQL?

+8
rdf owl ontology sparql rdfs
source share
1 answer

Data like this (in RDF / XML):

 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:pizzas="http://example.org/pizzas#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"> <owl:Ontology rdf:about="http://example.org/pizzas"/> <owl:Class rdf:about="http://example.org/pizzas#Pizza"/> <owl:Class rdf:about="http://example.org/pizzas#Human"/> <owl:ObjectProperty rdf:about="http://example.org/pizzas#hasPizza"> <rdfs:domain rdf:resource="http://example.org/pizzas#Human"/> <rdfs:range rdf:resource="http://example.org/pizzas#Pizza"/> </owl:ObjectProperty> <owl:NamedIndividual rdf:about="http://example.org/pizzas#Jim"> <rdf:type rdf:resource="http://example.org/pizzas#Human"/> <pizzas:hasPizza> <owl:NamedIndividual rdf:about="http://example.org/pizzas#CheesePizza"> <rdf:type rdf:resource="http://example.org/pizzas#Pizza"/> </owl:NamedIndividual> </pizzas:hasPizza> </owl:NamedIndividual> </rdf:RDF> 

or the same, in a more readable Turtle:

 @prefix : <http://example.org/pizzas#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix pizzas: <http://example.org/pizzas#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . pizzas:Jim a pizzas:Human , owl:NamedIndividual ; pizzas:hasPizza pizzas:CheesePizza . pizzas:hasPizza a owl:ObjectProperty ; rdfs:domain pizzas:Human ; rdfs:range pizzas:Pizza . pizzas:Human a owl:Class . pizzas:Pizza a owl:Class . <http://example.org/pizzas> a owl:Ontology . pizzas:CheesePizza a pizzas:Pizza , owl:NamedIndividual . 

note that the statement Jim hasPizza CheesePizza is one triple in the graph. The axioms of the domain and range for the property of the hasPizza object are two triples: hasPizza rdfs:domain Human and hasPizza rdfs:range Pizza . SPARQL queries correspond to query patterns against triples on a chart. Thus, from a query like:

 prefix : <http://example.org/pizzas#> select ?x ?y where { ?x :hasPizza ?y } 

you will get results like

 $ arq --data pizzas.ttl --query query.sparql ----------------------- | x | y | ======================= | :Jim | :CheesePizza | ----------------------- 

because there is one triple in the graph whose predicate is :hasPizza , and that the triple has an object :Jim as an object and :CheesePizza as an object. It looks like you are really asking for the domain and property range :hasPizza , which are also easy to get. Use this query:

 prefix : <http://example.org/pizzas#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?domain ?range where { :hasPizza rdfs:domain ?domain ; rdfs:range ?range . } 

and you will get the following results:

 $ arq --data pizzas.ttl --query query.sparql ------------------- | domain | range | =================== | :Human | :Pizza | ------------------- 
+16
source share

All Articles