How can I tell Stardog to use inference when querying it through SPARQLwrapper?

江枫思渺然 提交于 2019-12-11 04:31:35

问题


I have a SPARQL query that returns results in the Stardog query panel when inference is enabled, but not when it's disabled. When I try the query through python with SPARQLwrapper, I get no results. I tried with a different query, which doesn't rely on inference, and got the same results through the Stardog query panel without inference and through SPARQLwrapper. So I suspect reasoning is not being applied when I query through python, and that this is why there's no results. So my question is How can I tell Stardog to use inference when querying it through SPARQLwrapper?


回答1:


The documentation of Stardog is pretty good:

HTTP

For HTTP, the reasoning flag is specified with the other HTTP request parameters:

$ curl -u admin:admin -X GET "http://localhost:5822/myDB/query?reasoning=true&query=..."

which means simply add the param ?reasoning=true to the remote URL string.




回答2:


I had the exact same problem. The solution is to use addParameter when you build the query which adds the required reasoning=true to the URL.

A skeleton of a query could then look like this:

from SPARQLWrapper import SPARQLWrapper, JSON

endpoint = '<your endpoint>'

sparql = SPARQLWrapper(endpoint)

# add your username and password if required
sparql.setCredentials('<your username>', '<your password>')

rq = """

<your query string>

"""

sparql.setQuery(rq)
sparql.setReturnFormat(JSON)

# use reasoning
sparql.addParameter('reasoning', 'true')

data_json = sparql.query().convert()


来源:https://stackoverflow.com/questions/43695589/how-can-i-tell-stardog-to-use-inference-when-querying-it-through-sparqlwrapper

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