我想要创建一个 SPARQL 查询,它返回与 Person
相关的所有本体类/属性的列表。 。例如,像 Person
的子类(派生自)
<rdfs:subClassOf rdf:resource="http://dbpedia.org/ontology/Person"/>
或者域/范围为 Person
<rdfs:domain rdf:resource="http://dbpedia.org/ontology/Person"/>
.
例如,类似 "http://dbpedia.org/ontology/OfficeHolder"
的结果& "http://dbpedia.org/ontology/Astronaut"
应该由查询返回,因为第一个查询有 rdfs:domain
人,而第二个是 rdfs:subClassOf
人。
这是我编写的查询:
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 dbo: <http://dbpedia.org/ontology/>
select distinct ?s
where {
{
?s rdfs:domain dbo:Person .
}
union
{
?s rdfs:range dbo:Person .
}
union
{
?s rdfs:subClassOf dbo:Person .
}
}
现在,此查询返回明确提及 Person
的所有类的列表在其属性中,但错过了像 Singer
这样的类,它是 MusicalArtist
的子类,属于 Person 的域。
我想要一个查询,列出所有此类/属性,这些类/属性与 Person 直接相关或通过“继承”相关。有什么建议么?
最佳答案
看来,您混淆了类和属性...仔细阅读 RDFS 1.1 ,很短。
如果您想检索与dbo:Person
“相关”的类和属性,请使用 property paths :
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 dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?p ?s WHERE
{
{
?s (rdfs:subPropertyOf|owl:equivalentProperty|^owl:equivalentProperty)*/
rdfs:domain/
(rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)*
dbo:Person .
BIND (rdfs:domain AS ?p)
}
UNION
{
?s (rdfs:subPropertyOf|owl:equivalentProperty|^owl:equivalentProperty)*/
rdfs:range/
(rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)*
dbo:Person .
BIND (rdfs:range AS ?p)
}
UNION
{
?s (rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)*
dbo:Person .
BIND (rdfs:subClassOf AS ?p)
}
# FILTER (STRSTARTS(STR(?s), "http://dbpedia.org/"))
}
关于SPARQL 查询与 Person 相关的所有类的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44623986/