How to correctly query the Pizza Ontology with SPARQL?

64 Views Asked by At

This request returns all pizzas with mushroom topping:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>

SELECT ?pizza 
WHERE {
    ?pizza rdfs:subClassOf+ pizza:Pizza .
    ?pizza rdfs:subClassOf [
                            a owl:Restriction ;
                            owl:onProperty pizza:hasTopping; 
                            owl:someValuesFrom pizza:MushroomTopping 
                ]
}

but I want to list all pizzas with VegetableTopping, which is a subclass of PizzaTopping. Additonally MushroomTopping is a subclassof VegetableTopping. The following query doesn't provided the intended results:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>

SELECT ?pizza 
WHERE {
    ?pizza rdfs:subClassOf+ pizza:Pizza .
    ?pizza rdfs:subClassOf [
                            a owl:Restriction ;
                            owl:onProperty pizza:hasTopping; 
                            owl:someValuesFrom pizza:VegetableTopping 
                ]
}
1

There are 1 best solutions below

0
Ortomala Lokni On

As said by UninformedUser in the comments, you have to make the topping a variable value, and then state that the variables binds to all subclasses of VegetableTopping using the rdfs:subClassOf* property path.

SELECT ?pizza WHERE {
    ?pizza rdfs:subClassOf+ pizza:Pizza .
    ?pizza rdfs:subClassOf [
                           a owl:Restriction ;
                           owl:onProperty pizza:hasTopping;
                           owl:someValuesFrom ?topping                  
                           ] .
    ?topping rdfs:subClassOf* pizza:VegetableTopping .
}