为什么我的查询无法使用RDFlib [英] Why my query doesn't work using RDFlib

查看:134
本文介绍了为什么我的查询无法使用RDFlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用RDFlib(SPARQL)查询OWL数据,但是我不明白为什么它不起作用.我在Protege中测试了相同的查询(SPARQL查询),并且效果很好! 这是我的代码:

I've been trying to query an OWL data using RDFlib (SPARQL), but I didn't get why it doesn't work. I tested the same query in Protege (SPARQL query) and it works perfectly! This is my code:

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. 我究竟做错了什么?有人可以帮助我吗?

My result is nothing. There's no error, but the length of the result file is 0. What am I doing wrong? Does anyone can help me?

推荐答案

当我在 sparql.org的查询处理器,我得到了一堆结果:

When I run this query (with prefixes defined) at sparql.org's query processor, I get a bunch of results:

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}.
  }
}

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

I'd note that you can significantly simplify this query with values since you're using union just to specify owl:ObjectProperty and owl: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定义任何前缀的任何原因,因为您没有在查询中使用任何此类名称空间.我假设这是您要查询的数据集,或者您可能希望结果以该前缀开头(因此,定义名称空间可使它们的打印输出更好).

I don't see any reason that you need to be defining any prefixes for http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf in your query, since you don't use any such namespace in your query. I'm assuming that that's the dataset that you're trying to query against, or perhaps that you expect the results to begin with that prefix (so perhaps defining the namespace makes their printed output nicer).

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

This strongly suggests that the problem is that the graph you're actually querying, from /localPath/a.owl isn't the same as that dataset, or that you're running some outdated versions of RDFlib, rdfextras, or both. I was able to run your Python code locally with RDFlib version 4.0.1 and rdfextras 0.4.

这篇关于为什么我的查询无法使用RDFlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆