Skip to content

SPARQL Update insert into default graph does not work for Dataset #2019

@leng-lim

Description

@leng-lim

Using SPARQL Update INSERT or INSERT DATA statements with a rdflib.Dataset does not work as expected when inserting into the default graph. This works as expected when using a rdflib.Graph. The following example code shows the problem:

# The SPARQL Update INSERT statement.
sparql_update = """
    PREFIX book: <http://example.org/book/>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
    
    INSERT DATA {
           book:book1 dc:title "War of the Worlds" .
    }"""

# Initialise a Dataset and Graph.
ds: rdflib.Dataset = rdflib.Dataset()
g: rdflib.Graph = rdflib.Graph()

# Translate the update.
update = translateUpdate(parseUpdate(sparql_update))

# Apply the update.
ds.update(update)
g.update(update)

# Check inserted triples.
print(f"Dataset default graph triples - {len(list(ds.graph('urn:x-rdflib:default').triples((None, None, None))))}")
print(f"Graph triples - {len(list(g.triples((None, None, None))))}")

This gives me the following output:

Dataset default graph triples - 0
Graph triples - 1

I would expect that the default graph with the identifier urn:x-rdflib:default in the rdflib.Dataset would contain 1 triple. Furthermore, if I try and serialise the Dataset to trig using print(ds.serialize(format="trig")), I get the following error:

self = <rdflib.plugins.serializers.trig.TrigSerializer object at 0x7f8bdb920850>
spo = (rdflib.term.URIRef('http://example.org/book/book1'), rdflib.term.URIRef('http://purl.org/dc/elements/1.1/title'), rdflib.term.Literal('War of the Worlds'), rdflib.term.BNode('N60d1b8ccffe6435fbc96e39e79713cf7'))

    def preprocessTriple(self, spo):
>       s, p, o = spo
E       ValueError: too many values to unpack (expected 3)

../../textfsmgui/.venv/lib64/python3.7/site-packages/rdflib/plugins/serializers/turtle.py:105: ValueError

I don't have these problems if I use a SPARQL Update INSERT command with a named graph e.g:

# The SPARQL Update INSERT statement with a named graph.
sparql_update = """
    PREFIX book: <http://example.org/book/>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
    
    INSERT DATA {
          GRAPH <http://example.org/library> {
              book:book1 dc:title "War of the Worlds" .
          }
    }"""

This is happening with rdflib 6.1.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions