RDFLib/rdflib — Issue #26: fix for GRAPH <some ctx> { ... }
Author: gjhiggins
Created: 2012-02-20 08:30:46+00:00
State: closed

drewpca, 2008-06-20T06:09:33.000Z

A query like
"SELECT ?s WHERE { GRAPH { ?s ?p ?o }}"
would fail in the svn version of rdflib. Below is a patch with a test case
that was failing, plus a simple fix to sparql/Algebra.py that seems to work.

Index: test/test_sparql_graph_graph_pattern.py
===================================================================
--- test/test_sparql_graph_graph_pattern.py	(revision 1460)
+++ test/test_sparql_graph_graph_pattern.py	(working copy)
@@ -1,5 +1,6 @@
-# -*- coding: UTF-8 -*-
+# -*- coding: utf-8 -*-
from rdflib import ConjunctiveGraph, URIRef, Literal, RDFS
+from rdflib.Graph import Graph
from StringIO import StringIO
import unittest

@@ -37,5 +38,20 @@
results == [doc1],
"expecting : %s . Got: %s"%([doc1],repr(results)))

+class TestSparqlGraph(unittest.TestCase):
+ """GRAPH <...> {...} blocks in a sparql query"""
+ def testGraph(self):
+ node = URIRef('http://example.com')
+ ctx = URIRef("http://example.com/other#context")
+ graph = ConjunctiveGraph()
+
+ subgraph = Graph(store=graph.store, identifier=ctx)
+ subgraph.add((node, node, node))
+
+ allQuads = list(graph.quads((None, None, None)))
+ self.assertEqual(allQuads, [(node, node, node, subgraph)])
+ q = "SELECT ?s WHERE { GRAPH {
?s ?p ?o }}"
+ self.assertEqual(len(list(graph.query(q))), 1)
+
Index: rdflib/sparql/Algebra.py
===================================================================
--- rdflib/sparql/Algebra.py	(revision 1460)
+++ rdflib/sparql/Algebra.py	(working copy)
@@ -1002,7 +1002,7 @@
#assert len(targetGraph) == 1
targetGraph = targetGraph[0]
else:
- targetGraph = Graph(tripleStore.store,graphName)
+ targetGraph = Graph(tripleStore.graph.store,graphName)
tripleStore = sparqlGraph.SPARQLGraph(targetGraph,
dSCompliance=\
DAWG_DATASET_COMPLIANCE)

Comment 1 by drewpca
AFAICT this is still an issue in svn r1460. When I ran the patched version just now,
the *other* test in that file was breaking, but I assume that's separate.

% nosetests test/test_sparql_graph_graph_pattern.py
testGraph (test.test_sparql_graph_graph_pattern.TestSparqlGraph) ... ok
test_OPT_FILTER (test.test_sparql_graph_graph_pattern.TestSparqlOPT_FILTER2) ... FAIL

======================================================================
FAIL: test_OPT_FILTER (test.test_sparql_graph_graph_pattern.TestSparqlOPT_FILTER2)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/my/dl/modified/rdflib/test/test_sparql_graph_graph_pattern.py", line 39, in
test_OPT_FILTER
"expecting : %s . Got: %s"%([doc1],repr(results)))
AssertionError: expecting : [rdflib.URIRef('http://eikeon.com/')] . Got: []

----------------------------------------------------------------------
Ran 2 tests in 0.164s

FAILED (failures=1)

Comment 2 by eik...@gmail.com

Comment 3 by eik...@gmail.com
This test case and patch did make it in to the 2.4.1 release and appears to be working. Drew, can you verify?

Comment 4 by drewpca
Still broken in r1759. I added my test case (and relocated a different one) and made
the Algebra.py fix again in http://code.google.com/p/rdflib/source/detail?r=1761

Please review that and I'll merge it in.

All the changes are in /branches/graphpat-27

Comment 5 by eik...@gmail.com

Comment 6 by eik...@gmail.com
These issues involve bits that have been moved out of rdflib proper for now. We will re-open them
or move them to rdfextas as appropriate.