different output from rdflib SPARQL query when using initBindings

余生颓废 提交于 2019-12-11 11:07:09

问题


I've been trying to run a query on rdflib graph (loaded with schema.rdfa from schema.org ) to find the ancestors.

E.g. As per Schema for Corporation the ancestors are
Thing > Organization > Corporation
This code works perfectly, giving me the output I desire, except that it does not use initBindings and I have to format the query string.

import rdflib
subject = rdflib.term.URIRef('http://schema.org/Corporation')
inplace_query = '''
SELECT ?class where {
    <%(subject_uri)s> rdfs:subClassOf* ?mid .
    ?mid rdfs:subClassOf* ?class .
}
group by ?class
order by count(?mid)
'''
iq = inplace_query % dict(subject_uri=subject.toPython())
print iq
# This prints out the following
'''
SELECT ?class where {
    <http://schema.org/Corporation> rdfs:subClassOf* ?mid .
    ?mid rdfs:subClassOf* ?class .
}
group by ?class
order by count(?mid)
'''

graph = rdflib.Graph()
graph.load("schema.rdfa", format="rdfa")
print list(graph.query(iq, initNs={"rdfs": "http://www.w3.org/2000/01/rdf-schema#"}))
# This prints out the following which is correct
'''
[
    (rdflib.term.URIRef(u'http://schema.org/Corporation'),),
    (rdflib.term.URIRef(u'http://schema.org/Organization'),),
    (rdflib.term.URIRef(u'http://schema.org/Thing'),)
]
'''

The following code uses initBindings, but gives me an extra term http://schema.org/Place.

import rdflib
from rdflib.plugins.sparql import prepareQuery
subject = rdflib.term.URIRef('http://schema.org/Corporation')
prepared_query_str = '''
SELECT ?class where {
    ?subject rdfs:subClassOf* ?mid .
    ?mid rdfs:subClassOf* ?class .
}
group by ?class
order by count(?mid)
'''
print prepared_query_str
# This prints the following output
'''
SELECT ?class where {
        ?subject rdfs:subClassOf* ?mid .
            ?mid rdfs:subClassOf* ?class .
}
group by ?class
order by count(?mid)
'''

graph = rdflib.Graph()
graph.load("schema.rdfa", format="rdfa")
print list(
    graph.query(
        prepared_query_str,
        initNs={"rdfs": "http://www.w3.org/2000/01/rdf-schema#"},
        initBindings={"subject": subject},
    )
)
# This prints out the following which is not correct ! Place does not belong here
'''
[
    (rdflib.term.URIRef(u'http://schema.org/Corporation'),),
    (rdflib.term.URIRef(u'http://schema.org/Place'),),
    (rdflib.term.URIRef(u'http://schema.org/Organization'),),
    (rdflib.term.URIRef(u'http://schema.org/Thing'),)
]
'''

I originally noticed the problem when I was converting the query into a prepared query, and had to use initBindings to substitute the "subject". I've narrowed it down to being an initBindings problem and not a preparedQuery problem, hence removed the prepared query compilation.

Shouldn't the two queries yield the exact same result ?

I've looked at this s/o question as a possible duplicate, but I am using initBindings and still getting this inconsitency.

来源:https://stackoverflow.com/questions/36642698/different-output-from-rdflib-sparql-query-when-using-initbindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!