Cannot get to work SPARQL SELECT FROM, FROM NAMED queries

176 views
Skip to first unread message

Jeroen537

unread,
Nov 27, 2015, 6:10:23 AM11/27/15
to rdflib-dev
I have been searching for a way to get SPARQL queries with FROM and/or FROM NAMED working. I tried various versions of rdflib, including 4.2.2-dev.

One version of the problem is included below. I thought that there might be an issue with how I have to name the graphs I want to select in the query, but actually it seems to me that things already go wrong with parsing the query. From the sources it looks to me that FROM, FROM NAMED and also FROM DEFAULT clauses should be recognized, if not implemented.

Can anyone point me to a way to get this working? In the docs or sources, nor on the Web I have found any working examples.

Thanks in advance!

Jeroen.

===== mytest.py =====

from rdflib import Graph, Dataset, URIRef
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID

ds = Dataset()
ng1 = Graph(ds.store, identifier=URIRef('http://www.example.org/ds-ng-1'))
ng2 = Graph(ds.store, identifier=URIRef('http://www.example.org/ds-ng-2'))
gdef = Graph(ds.store, identifier=DATASET_DEFAULT_GRAPH_ID)

gdef.parse(data='''
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://www.example.org/ds-ng-1> dc:date "2005-07-14T03:18:56+0100"^^xsd:dateTime .
<http://www.example.org/ds-ng-2> dc:date "2005-09-22T05:53:05+0100"^^xsd:dateTime .
''', format='turtle')

ng1.parse(data='''
@prefix dc: <http://purl.org/dc/elements/1.1/> .

[] dc:title "Harry Potter and the Philospher's Stone" .
[] dc:title "Harry Potter and the Chamber of Secrets" .
''', format='turtle')

ng2.parse(data='''
@prefix dc: <http://purl.org/dc/elements/1.1/> .

[] dc:title "Harry Potter and the Sorcerer's Stone" .
[] dc:title "Harry Potter and the Chamber of Secrets" .
''', format='turtle')

queryString = '''
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT *
FROM NAMED <http://www.example.org/ds-ng-1>
FROM NAMED <http://www.example.org/ds-ng-2>
}
{ ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }
}
'''

qres = ds.query(queryString)

for row in qres:
print(row)

===== result =====

Traceback (most recent call last):
File "/Users/(...)/Developer/Eclipse workspace/RDFTutorial/src/sparqltest/mytest.py", line 44, in <module>
qres = ds.query(queryString)
File "/Users/(...)/Developer/Eclipse workspace/RDFTutorial/rdflib_sources/rdflib_4.2.2_dev/rdflib/graph.py", line 1084, in query
query_object, initBindings, initNs, **kwargs))
File "/Users/(...)/Developer/Eclipse workspace/RDFTutorial/rdflib_sources/rdflib_4.2.2_dev/rdflib/plugins/sparql/processor.py", line 72, in query
parsetree = parseQuery(strOrQuery)
File "/Users/(...)/Developer/Eclipse workspace/RDFTutorial/rdflib_sources/rdflib_4.2.2_dev/rdflib/plugins/sparql/parser.py", line 1053, in parseQuery
return Query.parseString(q, parseAll=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pyparsing.py", line 1041, in parseString
raise exc
pyparsing.ParseException: Expected "{" (at char 194), (line:8, col:1)

===== end =====

gjh

unread,
Nov 27, 2015, 12:44:36 PM11/27/15
to rdfli...@googlegroups.com
On Fri, 2015-11-27 at 03:10 -0800, Jeroen537 wrote:
> Can anyone point me to a way to get this working?

There are unbalanced parentheses in queryString, as indicated by the
exception message:

> pyparsing.ParseException: Expected "{" (at char 194), (line:8, col:1)


> queryString = '''
> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>
> SELECT *
> FROM NAMED <http://www.example.org/ds-ng-1>
> FROM NAMED <http://www.example.org/ds-ng-2>
> }
> { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }
> }
> '''
>

The URIs for the named graphs would seem to be dereferenced by default,
leading to a 404 and ...

> Exception: Could not load http://www.example.org/ds-ng-1 as either
> RDF/XML, N3 or NTriples


Some illumination here:

http://answers.semanticweb.com/questions/17709/sparql-engines-that-supp
ort-dereferencing-from-named-uris


Cheers

Graham


gjh

unread,
Nov 27, 2015, 6:09:13 PM11/27/15
to rdfli...@googlegroups.com
===== mytest.py =====
>
> from rdflib import Graph, Dataset, URIRef
> from rdflib.graph import DATASET_DEFAULT_GRAPH_ID

import rdflib.plugins.sparql
rdflib.plugins.sparql.SPARQL_LOAD_GRAPHS = False

Will see you right.

Cheers,

Graham
Message has been deleted
Message has been deleted

Jeroen537

unread,
Nov 28, 2015, 9:25:38 AM11/28/15
to rdflib-dev, g...@bel-epa.com
Thanks Graham, for pointing out the silly mistake with the mismatched parentheses.

It did not solve the problem completely, however.

Thanks to your second answer I found that my second and real mistake was the way I made reference to the graphs in the FROM NAMED clause.

This is now cleared up and I can move on. Everything works, including a FROM clause. Thanks again!

Jeroen.

Op zaterdag 28 november 2015 00:09:13 UTC+1 schreef gjh:

Op zaterdag 28 november 2015 00:09:13 UTC+1 schreef gjh:

Op zaterdag 28 november 2015 00:09:13 UTC+1 schreef gjh:

Reply all
Reply to author
Forward
0 new messages