SPARQL - Query based on some property - sparql

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

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

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
}
}

Related

How to return subgraph from rdf graph

I have an RDF graph G with several classes assuming for simplicity (Person and Parrot).
The class Person is connected to the class Parrot by the property hasAnimal, e.g.:
#PREFIX : <http://example.org/>
:Hugo rdf:type :Person .
:Hugo rdfs:label "Hugo" .
:Hugo :hasAnimal :Birdy.
:Birdy rdf:type :Parrot .
:Birdy rdfs:label :"Birdy" .
:LonleyBrido rdf:type :Parrot .
What is wanted is a subgraph of G that contains all the triples from Person and Parrot that are directly connected with each other, starting from Person. The initial Person does not matter to me, the important part is that only connected triples are extracted i.e. that only persons that do have a parrot or don't get outputted. What I have already tried is the following:
construct {
?person ?p ?o .
?parrot ?p2 ?o2 .
} where {
?person rdf:type :Person .
?person ?p ?o .
?person :hasAnimal ?parrot .
?parrot rdf:type :Parrot .
?parrot ?p2 ?o2 .
}
So the expected output would be:
:Hugo rdf:type :Person .
:Hugo rdfs:label "Hugo" .
:Hugo :hasAnimal :Birdy.
:Birdy rdf:type :Parrot .
:Birdy rdfs:label :"Birdy" .
I am executing this query on a rdflib graph.
Does anyone have a solution to this problem?
The solution is already described above:
import rdflib
from rdflib.namespace import RDF, RDFS
query = """
construct {
?person ?p ?o .
?parrot ?p2 ?o2 .
} where {
?person rdf:type :Person .
?person ?p ?o .
?person :hasAnimal ?parrot .
?parrot rdf:type :Parrot .
?parrot ?p2 ?o2 .
}
"""
g = rdflib.Graph()
g.parse("example.ttl", format="ttl")
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
EX= rdflib.Namespace("http://example.org/")
g.bind("example", EX)
result = g.query(query)

query multiple subclass property

I want to query a class "Soto Kebumen" based on the subclass with sparql
I have tried this query, but it cant query both "makanan_Dari some Jawa_Tengah" and
"memiliki_Bahan some Bunga_Brokoli"
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX kb: <http://www.qassobi.uns.ac.id/kebudayaan#>
PREFIX bm: <http://www.qassobi.uns.ac.id/bahan_makanan#>
PREFIX wl: <http://www.qassobi.uns.ac.id/wilayah#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?makanan
WHERE { ?makanan rdfs:subClassOf* kb:Makanan_Daerah .
?makanan rdfs:subClassOf [
owl:onProperty kb:makanan_Dari;
owl:someValuesFrom wl:Jawa_Tengah;
] .
?makanan rdfs:subClassOf [
owl:onProperty kb:memiliki_Bahan;
owl:someValuesFrom bm:Bunga_Kol;
]
}
But, it return a result if I only query one of each..
SELECT ?makanan
WHERE { ?makanan rdfs:subClassOf* kb:Makanan_Daerah .
?makanan rdfs:subClassOf [
owl:onProperty kb:makanan_Dari;
owl:someValuesFrom wl:Jawa_Tengah;
]
and
SELECT ?makanan
WHERE { ?makanan rdfs:subClassOf* kb:Makanan_Daerah .
?makanan rdfs:subClassOf [
owl:onProperty kb:memiliki_Bahan;
owl:someValuesFrom bm:Bunga_Kol;
] }
How to query multiple subclass property and get the above result?

SPARQL Query: How to get all individual and data property assertions?

Same as you know if we retrieve the object property or data property and subclass can do that by joining them in one variable :
Query 1
SELECT ?x ?y
WHERE { ?x rdfs:subClassOf ?y.
?x rdf:type owl:ObjectProperty.
}
the x var it's same object property and subclass of other class
im need to join (all individual "NamedIndividual")
with object property or subclass .
the problem that is ( ?x rdf:type owl:NamedIndividual . )
couldn't use "?x" in any other location same as :
?x rdfs:subClassOf ?y.
Query 2
SELECT ?x ?y
WHERE { ?x rdfs:subClassOf ?y.
?x rdf:type owl:NamedIndividual .
}
Query 3
SELECT ?x ?y
WHERE { ?x rdf:type owl:ObjectProperty.
?x rdf:type owl:NamedIndividual .
}
So: Query 2 and Query 3 cannot be implemented.
how I can solve this problem?

SPARQL: how to transfer owl:equivalentClass to rdfs:subClassOf (owl:Restriction) properties?

My question is about using SPARQL to query some owl ontology where owl:Restrictions are heavily used (in my case this is the "Cell Ontology").
Here is an example of some typical entry (in Turtle format, extracted from the above mentioned ontology):
### http://purl.obolibrary.org/obo/CL_0000792
obo:CL_0000792 rdf:type owl:Class ;
owl:equivalentClass [ owl:intersectionOf ( obo:CL_0000624
[ rdf:type owl:Restriction ;
owl:onProperty obo:RO_0002104 ;
owl:someValuesFrom obo:PR_000001380
]
[ rdf:type owl:Restriction ;
owl:onProperty obo:RO_0002215 ;
owl:someValuesFrom obo:GO_0050777
]
[ rdf:type owl:Restriction ;
owl:onProperty <http://purl.obolibrary.org/obo/cl#has_low_plasma_membrane_amount> ;
owl:someValuesFrom obo:PR_000001869
]
) ;
rdf:type owl:Class
] ;
rdfs:subClassOf obo:CL_0000624 ,
obo:CL_0000815 ,
[ rdf:type owl:Restriction ;
owl:onProperty obo:RO_0002104 ;
owl:someValuesFrom obo:PR_000001380
] ,
[ rdf:type owl:Restriction ;
owl:onProperty obo:RO_0002215 ;
owl:someValuesFrom obo:GO_0050777
] ,
[ rdf:type owl:Restriction ;
owl:onProperty <http://purl.obolibrary.org/obo/cl#has_low_plasma_membrane_amount> ;
owl:someValuesFrom obo:PR_000001869
] .
Here my ultimate goal is to transfer the owl equivalent properties to subClassOf properties:
CL_0000792 rdfs:subClassOf [
rdf:type owl:Restriction ;
owl:onProperty obo:RO_0002104 ;
owl:someValueFrom obo:PR_000001380
] ;
rdfs:subClassOf [
rdf:type owl:Restriction ;
owl:onProperty obo:cl#has_low_plasma_membrane_amount ;
owl:someValueFrom obo:PR_000001869
] .
What I do not achieve is to obtain all three properties from the rdfs:subclass part and then bind them properly to the subClassOf sorts of properties (then filtering out the obo:RO_0002215 would be easy).
EDIT: As I made some progress here a new SPARQL Query
EDIT2: following Damyan Ognyanov's answer updated the SPARQL query part which was ignoring the collection within the owl:intersectionOf part and which also more compact/elegant
Here my current SPARQL query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX tpo: <http://www.definiens.com/ontologies/TissuePhenomicsOntology>
PREFIX cl: <http://purl.obolibrary.org/obo/cl.owl>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX efo: <http://www.ebi.ac.uk/efo/efo.owl>
CONSTRUCT {
?cell rdfs:subClassOf [ rdf:type owl:Restriction ;
owl:onProperty ?cellProp ;
owl:someValuesFrom ?cellPropValue
] .
?cellProp ?cellPropProp ?cellPropObj .
?cellPropValue ?cellPropValueProp ?cellPropValuePropValue .
?cell ?cellProp2 ?cellProp2Obj .
}
FROM named cl:
FROM named tpo:
WHERE {
# query cl to get our information
graph cl:
{
?cell (rdfs:subClassOf|(owl:equivalentClass/owl:intersectionOf/rdf:rest*/rdf:first)) ?x .
?x owl:onProperty ?cellProp ;
owl:someValuesFrom ?cellPropValue .
?cellProp ?cellPropProp ?cellPropObj .
?cellPropValue ?cellPropValueProp ?cellPropValuePropValue .
?cell ?cellProp2 ?cellProp2Obj .
}
# limit ?cell to the entries already present in TPO
graph tpo:
{
?cell rdfs:subClassOf* obo:CL_0000000 .
}
}
If you replace the CONSTRUCT part with a SELECT * then it appears that all variables are correctly assigned, the information is there.
What I am still missing though is a proper CONSTRUCT part to reconstruct the "somewhat convoluted" owl:Property restriction. As such this query mostly returns a long list of blank nodes, which won't be parsed properly by Protege for instance.
#AKSW also rightly pointed out that SPARQL may not be the tool of choice to query and construct OWL graphs. It indeed appears clearly here that one needs to know the precise data structure in order to build a working query, in this manner at least.
?cell (rdfs:subClassOf|(owl:equivalentClass/owl:intersectionOf/rdf:rest*/rdf:first)) ?x .
?x owl:onProperty ?cellProp ;
owl:someValuesFrom ?cellPropValue .
?cellProp ?cellPropProp ?cellPropObj .
?cellPropValue ?cellPropValueProp ?cellPropValuePropValue .
?cell ?cellProp2 ?cellProp2Obj .
The value of owl:intersectionOf is an RDF list and the above Turtle snippet uses the RDF list syntax to enumerate the members of the owl:intersectionOf collection (e.g., entries are enclosed between ( and )).
So, you should also include rdf:rest*/rdf:first properties in your property paths, since these are used to construct the collection.
For the query, I'll introduce an additional variable where to bind the restriction of interest and use it to fetch the values of owl:onProperty and owl:someValuesFrom, e.g., your WHERE clause may look something like:
?cell (rdfs:subClassOf|(owl:equivalentClass/owl:intersectionOf/rdf:rest*/rdf:first)) ?x .
?x owl:onProperty ?cellProp ;
owl:someValuesFrom ?cellPropValue .
?cellProp ?cellPropProp ?cellPropObj .
?cellPropValue ?cellPropValueProp ?cellPropValuePropValue .
?cell ?cellProp2 ?cellProp2Obj .

Unsuccessful team-employs-player property chain

I am intrigued by Using Property Chains to get inferred Knowledge in an OWL Ontology(Protege)
The accepted answer has two solutions: two OWL expressions, or a SWRL rule. I get the sense that the OP found the all-OWL (property chain) solution confusing but was satisfied with the SWRL answer.
I am trying to implement the all-OWL solution. So far, I don't see an inference that Steven_Gerrard is employed by England when reasoning with Pellet.
I do see the inference
Steven_Gerrard R_NationalPlayer Steven_Gerrard
Is that an error?
Should I use a different reasoner? The OP got an error from FaCT++.
Is my substitution of some for values breaking the reasoning?
The answerer suggested a General Class Axiom of
hasNationalStatus value National_Player EquivalentTo R_NationalPlayer some Self
but Protege compalined about my use of vlaue. It does accept the following:
hasNationalStatus some ({National_Player}) EquivalentTo R_NationalPlayer some Self
Have I made some mistake in modelling Club, Country and Nationality?
My implementation:
#prefix : <http://example.com/> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
<http://example.com/playerEmployment.owl> rdf:type owl:Ontology .
<http://example.com/R_NationalPlayer> rdf:type owl:ObjectProperty .
<http://example.com/employs> rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf owl:topObjectProperty ;
owl:propertyChainAxiom ( [ owl:inverseOf <http://example.com/hasNationality>
]
<http://example.com/R_NationalPlayer>
) .
<http://example.com/hasNationalStatus> rdf:type owl:ObjectProperty .
<http://example.com/hasNationality> rdf:type owl:ObjectProperty .
<http://example.com/Club> rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty <http://example.com/employs> ;
owl:someValuesFrom <http://example.com/Player>
] .
<http://example.com/NationalStatus> rdf:type owl:Class .
<http://example.com/Nationality> rdf:type owl:Class .
<http://example.com/Player> rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty <http://example.com/hasNationalStatus> ;
owl:someValuesFrom <http://example.com/NationalStatus>
] ,
[ rdf:type owl:Restriction ;
owl:onProperty <http://example.com/hasNationality> ;
owl:someValuesFrom <http://example.com/Nationality>
] .
<http://example.com/England> rdf:type owl:NamedIndividual ,
<http://example.com/Club> ,
<http://example.com/Nationality> .
<http://example.com/National_Player> rdf:type owl:NamedIndividual ,
<http://example.com/NationalStatus> .
<http://example.com/Steven_Gerrard> rdf:type owl:NamedIndividual ,
<http://example.com/Player> ;
<http://example.com/hasNationalStatus> <http://example.com/National_Player> ;
<http://example.com/hasNationality> <http://example.com/England> .
[ rdf:type owl:Restriction ;
owl:onProperty <http://example.com/hasNationalStatus> ;
owl:someValuesFrom [ rdf:type owl:Class ;
owl:oneOf ( <http://example.com/National_Player>
)
] ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty <http://example.com/R_NationalPlayer> ;
owl:hasSelf "true"^^xsd:boolean
]
] .
Ugh. Pellet can make the desired inference from the ontology included in my question.
I was looking on Steven's Individual page. I should have been looking on England's individual page.