How can I tell Stardog to use inference when query

2019-07-25 09:39发布

问题:

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()