问题
I have the following data graph:
@prefix hr: <http://learningsparql.com/ns/humanResources#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
hr:Another a rdfs:Class .
hr:Employee a rdfs:Class ;
rdfs:label "model" ;
rdfs:comment "a good employee" .
hr:Longer a hr:Employee ;
rdfs:label "model" ;
rdfs:comment "a good employee" .
hr:freestanding a rdf:Property ;
sch:rangeIncludes sch:Text .
hr:missing rdfs:comment "some comment about missing" .
hr:name a rdf:Property ;
sch:domainIncludes hr:Employee .
hr:nosuper a rdf:Property ;
sch:domainIncludes hr:Uncreated ;
sch:rangeIncludes sch:Text .
hr:randomtype a hr:invalidtype ;
rdfs:label "some label about randomtype" ;
rdfs:comment "some comment about randomtype" .
hr:typo a rdfs:Classs ;
rdfs:label "some label about typo" ;
rdfs:comment "some comment about typo" .
The only subjects defined in the triples are hr:Another, hr:Employee, hr:Longer, hr:freestanding, hr:missing, hr:name, hr:nosuper, hr:randomtype, and hr:typo. As proof, when I run the query:
SELECT DISTINCT ?s
WHERE {
?s ?p ?o .
}
I get the result:
--------------------------------------------------------------
| s |
==============================================================
| <http://learningsparql.com/ns/humanResources#freestanding> |
| <http://learningsparql.com/ns/humanResources#Another> |
| <http://learningsparql.com/ns/humanResources#typo> |
| <http://learningsparql.com/ns/humanResources#nosuper> |
| <http://learningsparql.com/ns/humanResources#Employee> |
| <http://learningsparql.com/ns/humanResources#randomtype> |
| <http://learningsparql.com/ns/humanResources#Longer> |
| <http://learningsparql.com/ns/humanResources#missing> |
| <http://learningsparql.com/ns/humanResources#name> |
--------------------------------------------------------------
However, if I execute this SPARQL query:
SELECT DISTINCT ?s
WHERE {
{
?s rdf:type* ?o .
}
}
I get the following results:
--------------------------------------------------------------
| s |
==============================================================
| <http://learningsparql.com/ns/humanResources#Longer> |
| rdfs:Class |
| sch:Text |
| "some comment about typo" |
| "some label about typo" |
| <http://learningsparql.com/ns/humanResources#nosuper> |
| "a good employee" |
| <http://learningsparql.com/ns/humanResources#Uncreated> |
| <http://learningsparql.com/ns/humanResources#missing> |
| "some comment about randomtype" |
| "model" |
| <http://learningsparql.com/ns/humanResources#freestanding> |
| "some label about randomtype" |
| "some comment about missing" |
| <http://learningsparql.com/ns/humanResources#Another> |
| <http://learningsparql.com/ns/humanResources#invalidtype> |
| <http://learningsparql.com/ns/humanResources#typo> |
| <http://learningsparql.com/ns/humanResources#randomtype> |
| <http://learningsparql.com/ns/humanResources#Employee> |
| rdfs:Classs |
| rdf:Property |
| <http://learningsparql.com/ns/humanResources#name> |
--------------------------------------------------------------
I am not sure why items like rdf:Property, "a good employee", sch:Text, etc. are being returned as subjects when they are not subjects in the data graph.
Why are they?
Clearly, this has something to do with how SPARQL handles property paths which I do not yet understand.
回答1:
You are using a property path with a zero-or-more wildcard ('*'). Effectively, the pattern ?s rdf:type* ?o
means "every value ?s
connected through zero or more type relations to any value ?o
".
Every value that appears in your graph (in fact every value in any graph) matches that, because every value has at least zero type relations. Therefore, a value like "some comment about typo"
get returned, because although it never occurs as a subject in your data, it is true that it is a value that has zero type relations to something else.
来源:https://stackoverflow.com/questions/61352612/why-does-this-sparql-query-return-certain-items-as-subjects