Skip to content

Commit

Permalink
Merge pull request #1041 from dwinston/add-service-clause-to-docs
Browse files Browse the repository at this point in the history
Add SERVICE clause to documentation
  • Loading branch information
nicholascar committed May 11, 2020
2 parents a3245fb + e2d6dae commit 6197531
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
29 changes: 29 additions & 0 deletions docs/intro_to_sparql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ Variables can also be pre-bound, using ``initBindings`` kwarg can be
used to pass in a ``dict`` of initial bindings, this is particularly
useful for prepared queries, as described below.

Query a Remote Service
^^^^^^^^^^^^^^^^^^^^^^

The SERVICE keyword of SPARQL 1.1 can send a query to a remote SPARQL endpoint.

.. code-block:: python
import rdflib
g = rdflib.Graph()
qres = g.query('''
SELECT ?s
WHERE {
SERVICE <http://dbpedia.org/sparql> {
?s <http://purl.org/linguistics/gold/hypernym> <http://dbpedia.org/resource/Leveller> .
}
} LIMIT 3''')
for row in qres:
print(row.s)
This example sends a query to `DBPedia
<https://dbpedia.org/>`_'s SPARQL endpoint service so that it can run the query and then send back the result:

.. code-block:: text
http://dbpedia.org/resource/Elizabeth_Lilburne
http://dbpedia.org/resource/Thomas_Prince_(Leveller)
http://dbpedia.org/resource/John_Lilburne
Prepared Queries
^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def evalPart(ctx, part):
def evalServiceQuery(ctx, part):
res = {}
match = re.match('^service <(.*)>[ \n]*{(.*)}[ \n]*$',
part.get('service_string', ''), re.DOTALL)
part.get('service_string', ''), re.DOTALL | re.I)

if match:
service_url = match.group(1)
Expand Down
15 changes: 15 additions & 0 deletions test/test_sparql_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def test_service_with_implicit_select_and_prefix():
for r in results:
assert len(r) == 3


def test_service_with_implicit_select_and_base():
g = Graph()
q = '''base <http://example.org/>
Expand All @@ -116,6 +117,20 @@ def test_service_with_implicit_select_and_base():
assert len(r) == 3


def test_service_with_implicit_select_and_allcaps():
g = Graph()
q = '''SELECT ?s
WHERE
{
SERVICE <http://dbpedia.org/sparql>
{
?s <http://purl.org/linguistics/gold/hypernym> <http://dbpedia.org/resource/Leveller> .
}
} LIMIT 3'''
results = g.query(q)
assert len(results) == 3


#def test_with_fixture(httpserver):
# httpserver.expect_request("/sparql/?query=SELECT * WHERE ?s ?p ?o").respond_with_json({"vars": ["s","p","o"], "bindings":[]})
# test_server = httpserver.url_for('/sparql')
Expand Down

0 comments on commit 6197531

Please sign in to comment.