我正在尝试对 OWL 文件进行 SPARQL 查询(请参阅pastebin),但这是我第一次处理 OWL 文件,因此我在 Python 中使用 RDFLIB 进行 SPARQL 查询。当我运行代码时,我得到一个我不理解的错误(如下所示的代码)。这可能是什么原因造成的?
代码
import rdflib
graph = rdflib.Graph()
prefix="""PREFIX dc:<http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX olia-ar: <http://purl.org/olia/arabic_khoja.owl#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lexvo: <http://lexvo.org/id/iso639-3/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p1: <http://protege.stanford.edu/plugins/owl/protege#>
PREFIX base: <http://www.owl-ontologies.com/unnamed.owl#>
PREFIX xmlns:<http://www.owl-ontologies.com/unnamed.owl#>
"""
queryString = prefix+"\n"+"""SELECT ?animalClass
WHERE {
?animal :Has_Usage "Ride";
rdf:type ?animalClass.
}"""
graph.parse('animal.owl')
result = graph.query(queryString)
for row in result:
print row
错误
Traceback (most recent call last):
File "D:\Python27\animal.py", line 22, in <module>
result = graph.query(queryString)
File "build\bdist.win32\egg\rdflib\graph.py", line 1007, in query
query_object, initBindings, initNs, **kwargs))
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\processor.py", line 49, in query
loadContexts=loadContexts)
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\algebra.py", line 358, in TopEvaluate
None)
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\algebra.py", line 179, in ReduceToAlgebra
right = ReduceGraphPattern(right,prolog)
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\algebra.py", line 63, in ReduceGraphPattern
bgp=BasicGraphPattern(list(unRollTripleItems(triple,prolog)),prolog)
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\evaluate.py", line 301, in unRollTripleItems
for item in unRollRDFTerm(items, queryProlog):
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\evaluate.py", line 281, in unRollRDFTerm
convertTerm(propVal.property, queryProlog),
File "D:\Python27\lib\site-packages\rdfextras-0.4-py2.7.egg\rdfextras\sparql\evaluate.py", line 151, in convertTerm
base = queryProlog.prefixBindings[u'']
KeyError: u''