ha93
ha93

Reputation: 15

SPARQL - Query based on some property

I want to get all the pizza names which has cheese toppings but the result shows (_:b0) which is kind of an owl restriction following is my query

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT ?X WHERE {
    ?X rdfs:subClassOf* [
        owl:onProperty pizza:hasTopping ;
        owl:someValuesFrom pizza:CheeseTopping
    ]
}

using Pizza ontology from stanford

Upvotes: 1

Views: 568

Answers (2)

Jang-Vijay Singh
Jang-Vijay Singh

Reputation: 732

This works (Without reasoning enabled)

 PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
 SELECT ?X ?topping WHERE {
     ?X rdfs:subClassOf ?Y .  
     ?Y owl:someValuesFrom ?topping .
     ?topping rdfs:subClassOf* pizza:CheeseTopping
 } 
 ORDER BY ?X

Some are listed more than once as they could contain more than one CheeseTopping. To remove duplicates:

 PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
 SELECT DISTINCT ?X WHERE {
        ?X rdfs:subClassOf ?Y .  
        ?Y owl:someValuesFrom ?topping .
        ?topping rdfs:subClassOf* pizza:CheeseTopping
 }
 ORDER BY ?X

This works if you enable a reasoner:

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?X WHERE {
    ?X rdfs:subClassOf pizza:CheeseyPizza
}

Ref: Used the pizza ontology from here: http://protege.stanford.edu/ontologies/pizza/pizza.owl

Upvotes: 2

UninformedUser
UninformedUser

Reputation: 8465

That query works but is really complex and might be incomplete because some pizzas use complex OWL constructs:

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?pizza WHERE {

  {
    ?pizza rdfs:subClassOf* pizza:Pizza .
    ?pizza owl:equivalentClass|rdfs:subClassOf [
      rdf:type owl:Restriction ;
      owl:onProperty pizza:hasTopping ;
               owl:someValuesFrom/rdfs:subClassOf* pizza:CheeseTopping 
      ]

   } UNION {
    ?pizza  owl:equivalentClass  _:b0 .
    _:b0    rdf:type             owl:Class ;
            owl:intersectionOf   _:b1 .
    _:b1 (rdf:rest)*/rdf:first ?otherClass.
     ?otherClass rdf:type owl:Restriction ;
               owl:onProperty pizza:hasTopping ;
               owl:someValuesFrom/rdfs:subClassOf* pizza:CheeseTopping 


   }
}

Upvotes: 1

Related Questions