Please help me with the SPARQL query.
I have an ontology with the class 'Building' as a subclass of owl:Thing
. 'Building' has it's own sub classes like: Church, Medical, Shop.
Each of these sub classes has it's own labels ( seeAlso
). For instance: Shop has labels like supermarket, bakery, market etc. Church has labels like Chapel, Cathedral etc.
The Individuals look like this:
I need to do a SPARQL query, which will retrieve the individuals according to the labels.
So let's say I want to get all the individuals of that subclass which has the labels of type seeAlso
"bakery". In this case I expect to get bakery1 and supermarket1
I have tried this, but it seems like this query is wrong:
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#>
SELECT ?individual ?class ?label
WHERE {
?individual rdf:type owl:NamedIndividual .
?class rdf:type owl:Class .
FILTER(?label="bakery")
}
If I delete the line with FILTER
, I will get only the individuals and classes, but not related to each other as they should. Protege just returns me all the possible connections Class - Individual
Given comments to the answer, the intent is to build synonyms, not separate instances. There are a few ways to do this, but SKOS is specifically designed for such vocabulary relationships. The property skos:prefLabel
is used for the display label, and synonyms can be defined by skos:altLabel
. The hierarchy you have can be retained, just use skos:altLabel
instead of 'rdf:seeAlso' (which is normally used for reference links ourtide of the ontology).
So the data, in a Turtle text serialization would look like:
:Shop rdfs:subClassOf :Building .
:Shop skos:prefLabel "Shop"^^xsd:string .
:Shop skos:altLabel "supermarket"^^xsd:string .
:Shop skos:altLabel "bakery"^^xsd:string .
:Shop skos:altLabel "market"^^xsd:string .
For the query, the OP says to match by the string - the preflabel
in this case:
SELECT ?individual ?label
WHERE {
?individual skos:prefLabel "Shop"^^xsd:string .
?individual skos:altLabel ?label
}