RDFLIB中的OWL文件查询执行错误

时间:2013-06-22 13:23:40

标签: python-2.7 sparql owl rdflib

我试图在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''

1 个答案:

答案 0 :(得分:0)

我可以看到的一个明显的错误是您使用带前缀的名称:Has_Usage,但您没有定义一个空的前缀,即

PREFIX : <http://example.org/ns#>

我想这可能是你试图用PREFIX xmlns:声明做的事情?