为什么我的查询无法使用RDFlib

时间:2013-11-08 15:17:28

标签: python sparql rdflib

我一直在尝试使用RDFlib(SPARQL)查询OWL数据,但我不明白为什么它不起作用。我在Protege(SPARQL查询)中测试了相同的查询,它完美无缺! 这是我的代码:

import rdflib
from rdflib import plugin
from rdflib.graph import Graph

g = Graph()
g.parse("/localPath/a.owl")

from rdflib.namespace import Namespace
ns = Namespace("http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#")

plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')
#
qres = g.query(
                """
                    SELECT  DISTINCT ?varClass ?varSubClass ?varSubClassComment ?varProperty ?varPropComment
                        WHERE {
                                {
                                    ?varClass rdf:type owl:Class .
                                    ?varProperty rdf:type owl:ObjectProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment} .
                                    OPTIONAL{?varSubClass rdfs:subClassOf ?varClass ; rdfs:comment ?varSubClassComment} .
                        }
                    UNION
                        {
                                    ?varClass rdf:type owl:Class .
                                    ?varProperty rdf:type owl:DatatypeProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment}.
                        }
                             }
                """
                , initNs=dict(
                                ns=Namespace("http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#")
                              )
               )

for row in qres.result:
    #print ("%s %s %s %s %s" % row) # %s represent the fields selected in the query
    print row

print (len(qres.result))

我的结果一无所获。没有错误,但结果文件的长度为0。 我究竟做错了什么?有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

当我在sparql.org's query processor运行此查询(定义了前缀)时,我得到了一堆结果:

PREFIX ns: <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#>
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  DISTINCT ?varClass ?varSubClass ?varSubClassComment ?varProperty ?varPropComment
FROM <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf>
WHERE {
  {
    ?varClass rdf:type owl:Class .
    ?varProperty rdf:type owl:ObjectProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment} .
    OPTIONAL{?varSubClass rdfs:subClassOf ?varClass ; rdfs:comment ?varSubClassComment} .
  }
  UNION
  {
    ?varClass rdf:type owl:Class .
    ?varProperty rdf:type owl:DatatypeProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment}.
  }
}

我注意到您可以使用显着简化此查询,因为您只是使用union来指定owl:ObjectPropertyowl:DatatypeProperty

PREFIX ns: <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#>
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 DISTINCT ?varClass ?varSubClass ?varSubClassComment ?varProperty ?varPropComment
FROM <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf>
WHERE {
  VALUES ?propertyType { owl:ObjectProperty owl:DatatypeProperty }

  ?varClass rdf:type owl:Class .
  ?varProperty rdf:type ?propertyType ;
               rdfs:domain ?varClass .
  OPTIONAL{ ?varProperty rdfs:comment ?varPropComment }
  OPTIONAL{ ?varSubClass rdfs:subClassOf ?varClass ;
                         rdfs:comment ?varSubClassComment }
}

我认为您不需要在查询中为http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf定义任何前缀,因为您在查询中不使用任何此类命名空间。我假设那是你试图查询的数据集,或者你希望结果以该前缀开头(所以定义命名空间可能会使打印输出更好)。

这有力地表明问题在于您实际查询的图表,/localPath/a.owl与该数据集不同,或者您正在运行某些过时版本的RDFlib,rdfextras或两者兼而有之。我能够使用RDFlib版本4.0.1和rdfextras 0.4在本地运行Python代码。