Dealing with lists of entities (aka collections)

221 views
Skip to first unread message

Aurélien Mazurie

unread,
Jan 11, 2016, 7:22:33 PM1/11/16
to rdflib-dev
Dear all,
I am struggling with how RDFlib can be used not only to serialize but also to deserialize lists of literals. I thought the rdflib.collection module would help me, but somehow I haven't been able to make it work. I also read the excellent blog post at http://www.snee.com/bobdc.blog/2014/04/rdf-lists-and-sparql.html but again, without success.

Let us consider a triple (my_field, rdf:value my_values), with my_values being a list of literals (e.g., strings). My two questions are
(a) how do we store this information into, say, a N3 file using RDFlib?
(b) how to retrieve this information from the result of a SPARQL query performed on this same file?

I have a way to do (a) by manually creating a rdf:first entry then multiple chained rdf:rest and rdf:first entries. This is a big ugly, but at least my resulting RDF serialization appears to have all the elements of my list:

previous_node = None
for value in my_values:
    element_entity = rdflib.BNode()

    if (previous_node is None):
        g.add((dataset_entity, rdflib.RDF.value, element_entity))
        g.add((element_entity, rdflib.RDF.first, value))
    else:
        g.add((previous_node, rdflib.RDF.rest, element_entity))
        g.add((element_entity, rdflib.RDF.rest, value))

    previous_node = element_entity

What I can't figure out is how to retrieve this information from a SPARQL query. I may be doing things incorrectly at the SPARQL level or at the RDFlib level, hence my lack of success (so far) in solving my problem.

Is there a minimal example, using RDFlib, of serializing a RDF document with an entity being a list of literals, then another example of how to retrieve this list from a (RDFlib-mediated) SPARQL query?

Best,
Aurélien Mazurie

Gunnar Aastrand Grimnes

unread,
Jan 12, 2016, 5:12:51 AM1/12/16
to rdfli...@googlegroups.com
For the SPARQL part you have to look at Bob's post, but here is a
basic example how to parse, inspect and modify a collection:


from rdflib import Graph, Literal, URIRef
from rdflib.collection import Collection

g = Graph()

g.parse(data = '''<urn:thing> <urn:prop> ( 1 2 3 ) . ''', format="turtle")

collection_resource = g.value(URIRef('urn:thing'), URIRef('urn:prop'))

collection = Collection(g, collection_resource)

print list(collection)

collection.append(Literal(4))

print g.serialize(format='turtle')


(https://gist.github.com/gromgull/08bf58ceceef89d464ed)

This API could be better, it would be nice with a method on graph
objects to return a collection, like we have for Resource and Seq (but
not for Alt and Bag oddly enough)

If you just want to read the list, there is graph.items, which will
give you a generator.



- Gunnar
> --
> http://github.com/RDFLib
> ---
> You received this message because you are subscribed to the Google Groups
> "rdflib-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rdflib-dev+...@googlegroups.com.
> To post to this group, send email to rdfli...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rdflib-dev/9e6615fd-364c-4c2a-b406-3516aff6f8d2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://gromgull.net
Reply all
Reply to author
Forward
0 new messages