0

Im have a problem trying to do a sparql query using subClasses.

I have the following ontology:

enter image description here

where Quesos is a SubClassOf Ingrediente as you can see in the next image and Quesos have some other members too.

enter image description here

I want to have some recipes back from some ingredients. For example:

I want to have all the recipes that contains tomato, salt and cheese (where cheese could be any cheese) and I want to have back all the recipes than contains those ingredients.

Here is the problem: If i put the ingredient (like salt or tomato) then the query works fine, but if I put "Quesos" then I've got no answer. I don´t know how to play with the subclasses in a sparql query.

So far I have the following query:

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 rec:<http://www.receta.org#>
SELECT ?r ?cal ?tiempo ?dif (COUNT (?i) as ?cantIng)(GROUP_CONCAT(DISTINCT ?listaIngredientes) as ?listaIng)(GROUP_CONCAT(DISTINCT ?modoPreparacion) as ?Preparacion) 
                WHERE { 
                ?x rdf:type rec:Receta .
                ?x rdfs:label ?r.
                ?x rec:Ingrediente rec:Sal.
                ?x rec:Ingrediente rec:Tomate.
                ?x rec:Calorias ?cal.
                ?x rec:tiempoPreparacion ?tiempo.
                ?x rec:dificultad ?dif.
                ?x rec:listaIngredientes ?listaIngredientes.
                ?x rec:modoPreparacion ?modoPreparacion.
                } 
                 GROUP BY ?r ?cal ?tiempo ?dif
                ORDER BY ?cantIng 

And I need to add the "subclassOf" line, but I cant find the way. Anyone can help? thank you!

SomeAnonymousPerson
  • 3,173
  • 1
  • 21
  • 22
  • Your ontology is a bit messy, and this leads to confusion. For one thing, judging from the example query and the screenshots, you are using `rec:Ingrediente` both as a class name and a property name. In the screenshot, it's a class, but in the query, you are using it as if it were a property. – Jeen Broekstra Jul 07 '15 at 04:38

1 Answers1

2

This actually has nothing to do with subclasses, but just with the difference between an instance and a class. Since rec:Quesos is the class of all cheeses, and each specific type of cheese is modeled as an instance of rec:Quesos, you can query this by adding a graph pattern to your query that, instead of matching a specific ingredient (such as rec:Sal or rec:Tomate), matches any ingredient of the type rec:Quesos:

?x rec:Ingrediente ?i . 
?i a rec:Quesos.

Or more shortly (since you don't actually need the value of ?i for anything else):

?x rec:Ingrediente [ a rec:Quesos ].
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73