GithubHelp home page GithubHelp logo

Comments (4)

ashleysommer avatar ashleysommer commented on June 16, 2024 1

@mohmehmo
Another name for a RDF "list" is a Collection.
See the docs for RDF Collection here: https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.Graph.collection

See this example for how to use it:

g = rdflib.Graph()
my_list = g.collection(rdflib.URIRef("http://example.org/my_list"))
my_list.append(rdflib.URIRef("http://example.org/Huey"))
my_list.append(rdflib.URIRef("http://example.org/Dewey"))
my_list.append(rdflib.URIRef("http://example.org/Louie"))
g.add((URIRef("http://example.org/Donald"), URIRef("http://example.org/nephews"), my_list.uri))

n3_graph = g.serialize(format="n3")

from rdflib.

ashleysommer avatar ashleysommer commented on June 16, 2024 1

Hi @mohmehmo
I've tested your snippet above. In this case, the object in your triple should be myList or c.uri, both work.

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

myList = BNode()
g = Graph()
c = Collection(g, myList)
for item in [1, 2, 3, 4, 5]:
  c.append(Literal(item))

g.add((URIRef("http://example.org/"),URIRef("http://example.org/my_list"), myList))

# Test it:
print(g.serialize(format="turtle"))

or

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

myList = BNode()
g = Graph()
c = Collection(g, myList)
for item in [1, 2, 3, 4, 5]:
  c.append(Literal(item))

g.add((URIRef("http://example.org/"),URIRef("http://example.org/my_list"), c.uri))

# Test it:
print(g.serialize(format="turtle"))

from rdflib.

mohmehmo avatar mohmehmo commented on June 16, 2024

@ashleysommer
Thanks for your response. Your suggestion resolved a part of my problem. Consider I have the following script to create a list as a Collection.

myList= BNode()
g = Graph('Memory')
c = Collection(g, myList)
for item in [1, 2, 3, 4, 5]:
  c.append(Literal(item))

When I want to add c as an object of (S, P, O) to the main graph, I faced with an error saying "<object> must be an rdflib term". I have tried different approaches e.g. URIRef(c), c.uri, c.n3(), but none of them works without error.
Do you have any suggestions for resolving this issue?

from rdflib.

mohmehmo avatar mohmehmo commented on June 16, 2024

@ashleysommer
Thanks a lot for your support. it is fine now.

from rdflib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.