我一直在尝试使用 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。我做错了什么?有人可以帮助我吗?