问题
A sample RDF dataset has entries owl#NamedIndividual
grouped by owl#Class
and a custom relation named IsMemberOf
. When I try to obtain a list of results segregated by their type, it works well but the moment I add a way to obtain their corresponding IsMemberOf
I don't get expected results.
Here is three SPARQL 1.1 queries I gave to Virtuoso (sample dataset below):
Query 1
sparql select * from <test>
where {
#If I uncomment the next line I don't get the proper results
# ?s <IsMemberOf> ?member_of.
?s rdfs:label ?name.
{
?s rdf:type/rdfs:subClassOf* <livingthings>.
} UNION {
?s a <rock>.
}
};
Query 2
sparql select * from <test>
where {
#If I uncomment the next line I get no results at all
# ?s <IsMemberOf> ?member_of.
?s rdfs:label ?name.
?s rdf:type/rdfs:subClassOf* <livingthings>.
};
Query 3
sparql select * from <test>
where {
?s <IsMemberOf> ?member_of.
?s rdfs:label ?name.
?s rdf:type <dog>.
#If I replace the previous line with the next line I get no results at all
# ?s rdf:type/rdfs:subClassOf* <livingthings>.
};
This is how the data is grouped:
livingthings
->mammal
--->dog
----->doggy_the_dog
----->fido_the_dog
--->cat
---->katy_the_cat
--->elephant
----->eli_the_elephant
->reptile
--->snake
----->snakey_the_snake
nonlivingthings
->rock
--->rocky_the_rock
--->ralf_the_rock
group_a
->rocky_the_rock
->ralf_the_rock
->snakey_the_snake
->doggy_the_dog
group_b
->fido_the_dog
->katy_the_cat
group_c
->eli_the_elephant
Finaly here is the data I used
sparql create silent graph <test>;
sparql insert into graph <test> {
<nonlivingthings> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<nonlivingthings> <http://www.w3.org/2000/01/rdf-schema#label> "Non-living things".
<rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<rock> <http://www.w3.org/2000/01/rdf-schema#label> "Rock".
<rock> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <nonlivingthings>.
<rocky_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<rocky_the_rock> <IsMemberOf> <group_a>.
<rocky_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <rock>.
<rocky_the_rock> <http://www.w3.org/2000/01/rdf-schema#label> "Rocky the rock".
<ralf_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<ralf_the_rock> <IsMemberOf> <group_a>.
<ralf_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <rock>.
<ralf_the_rock> <http://www.w3.org/2000/01/rdf-schema#label> "Ralf the rock".
<livingthings> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<livingthings> <http://www.w3.org/2000/01/rdf-schema#label> "Living things".
<mammal> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<mammal> <http://www.w3.org/2000/01/rdf-schema#label> "Mammal".
<mammal> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <livingthings>.
<reptile> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<reptile> <http://www.w3.org/2000/01/rdf-schema#label> "Reptile".
<reptile> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <livingthings>.
<dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<dog> <http://www.w3.org/2000/01/rdf-schema#label> "Dog".
<dog> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
<cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<cat> <http://www.w3.org/2000/01/rdf-schema#label> "Cat".
<cat> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
<elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<elephant> <http://www.w3.org/2000/01/rdf-schema#label> "Elephant".
<elephant> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
<snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
<snake> <http://www.w3.org/2000/01/rdf-schema#label> "Snake".
<snake> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <reptile>.
<snakey_the_snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<snakey_the_snake> <IsMemberOf> <group_a>.
<snakey_the_snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <snake>.
<snakey_the_snake> <http://www.w3.org/2000/01/rdf-schema#label> "Snakey the snake".
<doggy_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<doggy_the_dog> <IsMemberOf> <group_a>.
<doggy_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <dog>.
<doggy_the_dog> <http://www.w3.org/2000/01/rdf-schema#label> "Doggy the dog".
<fido_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<fido_the_dog> <IsMemberOf> <group_b>.
<fido_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <dog>.
<fido_the_dog> <http://www.w3.org/2000/01/rdf-schema#label> "Fido the dog".
<katy_the_cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<katy_the_cat> <IsMemberOf> <group_b>.
<katy_the_cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <cat>.
<katy_the_cat> <http://www.w3.org/2000/01/rdf-schema#label> "Katy the cat".
<eli_the_elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
<eli_the_elephant> <IsMemberOf> <group_c>.
<eli_the_elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <elephant>.
<eli_the_elephant> <http://www.w3.org/2000/01/rdf-schema#label> "Eli the elephant".
};
Why do I only get the following incomplet result when I uncomment ?s <IsMemberOf> ?member_of.
in query one?
s member_of name
rocky_the_rock group_a Rocky the rock
ralf_the_rock group_a Ralf the rock
All of the results under the <livingthings>
class are left out. Is it because of a bug in Virtuoso or the way my SPARQL query is formed?
回答1:
Virtuoso seems to handle user defined relation oddly in a query when mixed with a property path.
Here is the Virtuoso specific way to address this issue.
Query 1 answer
sparql select * from <test>
where {
?s rdfs:label ?name.
{
?s <IsMemberOf>{1} ?member_of.
?s rdf:type/rdfs:subClassOf* <livingthings>.
} UNION {
?s <IsMemberOf>{1} ?member_of.
?s a <rock>.
}
};
Query 2 answer
sparql select * from <test>
where {
?s <IsMemberOf>{1} ?member_of.
?s rdfs:label ?name.
?s rdf:type/rdfs:subClassOf* <livingthings>.
};
Applying an explicit property path for the user defined relation as such ?s <IsMemberOf>{1} ?member_of.
gives the expected results.
In query one the line ?s <IsMemberOf>{1} ?member_of.
is applied to each term inside each union element. If not then the results are not as desired. It does solve the issue but it is a bit nonsensical. That is why I'll leave the question open for a few days to see if anyone can provide logical explanation to why things are that way in Virtuoso. If it's just a bug I'll contact them via their mailing list.
回答2:
It's tough to say exactly what the problem here is. The queries you've shown aren't legal SPARQL by themselves (e.g., in SPARQL you do select ...
, not sparql select ...;
), but that might be the Virtuoso interface you're using. Another possible issue is that <IsMemberOf>
and similar are relative IRIs, and how they get resolved may be different between the data loading and the query processing, etc., so it's not necessarily the case that <IsMemberOf>
in one place actually refers to the same IRI that <IsMemberOf>
refers to in another. You really should use absolute IRIs. I don't know whether or not that's the problem here.
That said, I took your data and resolved the IRIs against http://example.org/ so that they're all absolute, and it looks like the queries work the way you'd want them to. E.g., running the following queries with Jena works just fine.
base <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select * where {
?s <IsMemberOf> ?member_of.
?s rdfs:label ?name.
?s a/rdfs:subClassOf* <livingthings>.
}
-------------------------------------------------------
| s | member_of | name |
=======================================================
| <doggy_the_dog> | <group_a> | "Doggy the dog" |
| <eli_the_elephant> | <group_c> | "Eli the elephant" |
| <fido_the_dog> | <group_b> | "Fido the dog" |
| <katy_the_cat> | <group_b> | "Katy the cat" |
| <snakey_the_snake> | <group_a> | "Snakey the snake" |
-------------------------------------------------------
Here's the updated data:
<http://example.org/elephant>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Elephant" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/mammal> .
<http://example.org/snakey_the_snake>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/snake> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Snakey the snake" ;
<http://example.org/IsMemberOf>
<http://example.org/group_a> .
<http://example.org/cat>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Cat" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/mammal> .
<http://example.org/mammal>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Mammal" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/livingthings> .
<http://example.org/katy_the_cat>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/cat> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Katy the cat" ;
<http://example.org/IsMemberOf>
<http://example.org/group_b> .
<http://example.org/fido_the_dog>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/dog> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Fido the dog" ;
<http://example.org/IsMemberOf>
<http://example.org/group_b> .
<http://example.org/dog>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Dog" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/mammal> .
<http://example.org/eli_the_elephant>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/elephant> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Eli the elephant" ;
<http://example.org/IsMemberOf>
<http://example.org/group_c> .
<http://example.org/reptile>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Reptile" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/livingthings> .
<http://example.org/nonlivingthings>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Non-living things" .
<http://example.org/livingthings>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Living things" .
<http://example.org/doggy_the_dog>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/dog> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Doggy the dog" ;
<http://example.org/IsMemberOf>
<http://example.org/group_a> .
<http://example.org/rocky_the_rock>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/rock> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Rocky the rock" ;
<http://example.org/IsMemberOf>
<http://example.org/group_a> .
<http://example.org/rock>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Rock" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/nonlivingthings> .
<http://example.org/ralf_the_rock>
a <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/rock> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Ralf the rock" ;
<http://example.org/IsMemberOf>
<http://example.org/group_a> .
<http://example.org/snake>
a <http://www.w3.org/2002/07/owl#Class> ;
<http://www.w3.org/2000/01/rdf-schema#label>
"Snake" ;
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://example.org/reptile> .
来源:https://stackoverflow.com/questions/31565565/multiple-statements-query-sparql-1-1-property-paths-virtuoso-7-2-x