Using python's rdflib
, I am trying to insert new triples into a graph based on some basic manipulations with existing triples. For instance, in this basic example, I create a graph that I populate using INSERT DATA
with 4 triples. I then try to "symmetrize" the situation with INSERT
, but this fails. What am I doing wrong?
>>>
>>> import rdflib
>>> g = rdflib.Graph()
>>> g.update("INSERT DATA {<a> <b> <c>; <d> <e>.<f> <g> <h>; <i> <j>.}")
>>> print(list(g.triples((None, None, None))))
[(rdflib.term.URIRef('a'), rdflib.term.URIRef('d'), rdflib.term.URIRef('e')), (rdflib.term.URIRef('f'), rdflib.term.URIRef('g'), rdflib.term.URIRef('h')), (rdflib.term.URIRef('a'), rdflib.term.URIRef('b'), rdflib.term.URIRef('c')), (rdflib.term.URIRef('f'), rdflib.term.URIRef('i'), rdflib.term.URIRef('j'))]
>>> g.update("""INSERT {GRAPH {?c ?b ?a.}} WHERE {GRAPH {?a ?b ?c.}}""")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rdflib/graph.py", line 1161, in update
return processor.update(update_object, initBindings, initNs, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rdflib/plugins/sparql/processor.py", line 56, in update
strOrQuery = translateUpdate(parseUpdate(strOrQuery), initNs=initNs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/rdflib/plugins/sparql/parser.py", line 1071, in parseUpdate
return UpdateUnit.parseString(q, parseAll=True)[0]
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyparsing.py", line 1955, in parseString
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyparsing.py", line 3814, in parseImpl
raise ParseException(instring, loc, self.errmsg, self)
pyparsing.ParseException: Expected end of text, found 'I' (at char 0), (line:1, col:1)
>>>
INSERT {GRAPH {?c ?b ?a.}} WHERE {GRAPH {?a ?b ?c.}}
This is not legal SPARQL. The GRAPH
keyword in both the INSERT
and the WHERE
clauses needs to be followed by either a variable or an IRI indicating the source and target named graph.
Alternatively, if you are not interested in using named graphs and just want to modify the default graph, just remove the GRAPH
keywords (and the extra curly braces) completely.