Python 三元组组合的递归SPARQL查询

Python 三元组组合的递归SPARQL查询,python,sparql,owl,rdflib,Python,Sparql,Owl,Rdflib,我使用ontospy在Python中递归运行以下查询: SELECT ?c WHERE { ?c rdfs:subClassOf ?restriction . ?restriction owl:onProperty :has_part ; owl:someValuesFrom ?p . VALUES ?p { <some_uri> } } 这可能吗?我不能给出正式的证明,但这看起来不可能。这不是设计属性路径的目的,也不是存在某些扩展(,)的原因 在某些承诺下

我使用ontospy在Python中递归运行以下查询:

SELECT ?c WHERE {
    ?c rdfs:subClassOf ?restriction .
    ?restriction owl:onProperty :has_part ; owl:someValuesFrom ?p .
    VALUES ?p { <some_uri> }
}

这可能吗?

我不能给出正式的证明,但这看起来不可能。这不是设计属性路径的目的,也不是存在某些扩展(,)的原因

在某些承诺下(例如,使用树状结构),使用
过滤器不存在
可以找出一些问题,但是,这不是一个通用的解决方案

我们的想法是在两个查询中实现这一点。本质上,这是
SELECT
over
CONSTRUCT
。顺便说一句,这样的SPARQL扩展已经实现了


让我们使用Ontospy的基础,因为

Ontospy不提供任何本体编辑功能,也不能用于查询triplestore

输入(
ontology.ttl


如果不想修改初始RDFLib图,只需创建另一个:

导入rdflib
g1=rdflib.Graph()
g1.parse(“ontology.ttl”,format=“n3”)
qres=g1.query(
“”“前缀:
构造{c:hasSome?p}其中{
?c rdfs:子类[owl:onProperty:hasPart;
owl:someValuesFrom?p]}“”)
g2=rdflib.Graph();
对于QRE中的三倍:#相当多的三倍
g2.添加(三重)
qres=g2.query(
“”“前缀:
选择?a?b,其中{?a:hasSome+?b}“”)
对于QRE中的行:
打印(“%s:hasSome+%s”%行)

在这两种情况下,您可能都可以使用或代替第二个查询。

显然,可以编写类似于
(rdfs:subassof/owl:someValuesFrom)*
的内容,但是,一般来说,不可能以这种方式指定对
:has\u part
属性的限制。另请参阅和链接的,还有。@StanislavKralin我明白了,谢谢你提供的信息。引用这家伙的话,“这太糟糕了,伙计!”如果相关的话,请添加“python”标记。是的,我也这么想。谢谢你的回答,我今天再看一遍。非常感谢你的帮助!谢谢Stanislav,如果没有其他人发布更好的答案,我将在答案到期前奖励该答案。@5弗,如你所愿!也许@AndyS[eaborn]可以给出一个正式的证明,或者克劳迪奥·古铁雷斯的文章中也有类似的证明。顺便说一句,SPARQL形式语义中似乎存在一些问题(参见中的第2节)。也许你也可以在
?c rdfs:subClassOf ?restriction .
?restriction owl:onProperty :has_part ; owl:someValuesFrom ?p .
@prefix : <http://www.example.org/ontology#> .
@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#> .
@base <http://www.example.org/ontology> .

<http://www.example.org/ontology> rdf:type owl:Ontology .

:hasPart rdf:type owl:ObjectProperty .

:Country rdf:type owl:Class ;
         rdfs:subClassOf [ rdf:type owl:Restriction ;
                           owl:onProperty :hasPart ;
                           owl:someValuesFrom :State
                         ] .

:State rdf:type owl:Class ;
       rdfs:subClassOf [ rdf:type owl:Restriction ;
                         owl:onProperty :hasPart ;
                         owl:someValuesFrom :City
                       ] .

:City rdf:type owl:Class .
:Country :hasSome+ :State
:State   :hasSome+ :City
:Country :hasSome+ :City