fertapric/RDF.py — Issue #4: Issue with lang = NULL or datatype= NULL for Literal
Author: cardinal27513
Created: 2011-11-13 04:11:09+00:00
State: open

When a Literal is retrieved from SQLite, there is an issue like if ‘lang’ is set to ‘NULL’ while the datatype is to a valid datatype.
RDFlib Literal will raise exception and saying that
("A Literal can only have one of lang or datatype, "
TypeError: A Literal can only have one of lang or datatype, per http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal

I’m not an expert on this area, I assume the ‘NULL’ is coming from SQLite query. So the only solution that I’ve is to map ‘NULL’ to Python None… Below is my solution, but if you get a better one please let me know.

def extractTriple(tupleRt,store,hardCodedContext=None):
subject,predicate,obj,rtContext,termComb,objLanguage,objDatatype = tupleRt
if objLanguage=='NULL': objLanguage = None
if objDatatype=='NULL': objDatatype = None

==================

DEBUG:ReaderPlugin:SELECT DISTINCT ?p ?v ?c WHERE { ?p ?v . OPTIONAL { ?v ?c } }

extractTriple:: objLanguage=None,objDatatype=None
extractTriple:: objLanguage=NULL,objDatatype=http://www.w3.org/2001/XMLSchema#anyURI
lang=NULL, datatype=http://www.w3.org/2001/XMLSchema#anyURI

Traceback (most recent call last):
File "C:\testclient\RDF\surf_rdflib_sqlite.py", line 88, in
e.load()
File "c:\python27\lib\site-packages\surf-1.1.4_r371-py2.7.egg\surf\resource\__init__.py", line 526, in load
results_d = self.session[self.store_key].load(self, True)
File "c:\python27\lib\site-packages\surf-1.1.4_r371-py2.7.egg\surf\store.py", line 161, in load
return self.reader.load(resource, direct)
File "c:\python27\lib\site-packages\surf-1.1.4_r371-py2.7.egg\surf\plugin\reader.py", line 116, in load
return self._load(subj, direct, resource.context)
File "c:\python27\lib\site-packages\surf-1.1.4_r371-py2.7.egg\surf\plugin\query_reader.py", line 120, in _load
result = self._execute(query)
File "c:\python27\lib\site-packages\surf.rdflib-1.0.0_r362-py2.7.egg\surf_rdflib\reader.py", line 81, in _execute
return self.__graph.query(q_string)
File "C:\Python27\Lib\site-packages\rdflib-3.1.0-py2.7.egg\rdflib\graph.py", line 800, in query
return result(processor.query(query_object, initBindings, initNs, **kwargs))
File "C:\Python27\lib\site-packages\rdfextras-0.1-py2.7.egg\rdfextras\sparql\processor.py", line 43, in query
extensionFunctions=extensionFunctions)
File "C:\Python27\lib\site-packages\rdfextras-0.1-py2.7.egg\rdfextras\sparql\algebra.py", line 360, in TopEvaluate
result = expr.evaluate(tripleStore,passedBindings,query.prolog)
File "C:\Python27\lib\site-packages\rdfextras-0.1-py2.7.egg\rdfextras\sparql\algebra.py", line 946, in evaluate
top.topLevelExpand(left.constraints, prolog)
File "C:\Python27\lib\site-packages\rdfextras-0.1-py2.7.egg\rdfextras\sparql\query.py", line 449, in topLevelExpand
self.expand(constraints)
File "C:\Python27\lib\site-packages\rdfextras-0.1-py2.7.egg\rdfextras\sparql\query.py", line 544, in expand
self.expandAtClient(constraints)
File "C:\Python27\lib\site-packages\rdfextras-0.1-py2.7.egg\rdfextras\sparql\query.py", line 629, in expandAtClient
for tripleOrQuad in searchRT:
File "C:\Python27\Lib\site-packages\rdflib-3.1.0-py2.7.egg\rdflib\graph.py", line 916, in triples
for (s, p, o), cg in self.store.triples((s, p, o), context=context):
File "C:\Python27\lib\site-packages\rdf.py-1.0-py2.7.egg\rdfstorage\SQLite.py", line 379, in triples
s,p,o,(graphKlass,idKlass,graphId) = extractTriple(rt,self,context)
File "C:\Python27\lib\site-packages\rdf.py-1.0-py2.7.egg\rdfstorage\AbstractSQLStore.py", line 103, in extractTriple
o=createTerm(obj,objTerm,store,objLanguage,objDatatype)
File "C:\Python27\lib\site-packages\rdf.py-1.0-py2.7.egg\rdfstorage\AbstractSQLStore.py", line 118, in createTerm
rt = Literal(termString,objLanguage,objDatatype)
File "C:\Python27\Lib\site-packages\rdflib-3.1.0-py2.7.egg\rdflib\term.py", line 307, in __new__
raise TypeError("A Literal can only have one of lang or datatype, "
TypeError: A Literal can only have one of lang or datatype, per http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal