Contexts in rdflib

A short example…

  1. from rdflib import Namespace, BNode, Literal, URIRef
  2. from rdflib.Graph import Graph, ConjunctiveGraph
  3. from rdflib.store.IOMemory import IOMemory
  4.  
  5. ns = Namespace("http://love.com#")
  6.  
  7. mary = URIRef("http://love.com/lovers/mary#")
  8. john = URIRef("http://love.com/lovers/john#")
  9.  
  10. cmary=URIRef("http://love.com/lovers/mary#")
  11. cjohn=URIRef("http://love.com/lovers/john#")
  12.  
  13. store = IOMemory()
  14.  
  15. g = ConjunctiveGraph(store=store)
  16. g.bind("love",ns)
  17.  
  18. gmary = Graph(store=store, identifier=cmary)
  19.  
  20. gmary.add((mary, ns[‘hasName’], Literal("Mary")))
  21. gmary.add((mary, ns[‘loves’], john))
  22.  
  23. gjohn = Graph(store=store, identifier=cjohn)
  24. gjohn.add((john, ns[‘hasName’], Literal("John")))
  25.  
  26. #enumerate contexts
  27. for c in g.contexts():
  28.     print "—- %s " % c
  29.  
  30. #separate graphs
  31. print gjohn.serialize(format=‘n3′)
  32. print gmary.serialize(format=‘n3′)
  33.  
  34. #full graph
  35. print g.serialize(format=‘n3′)

Tags: , ,

One Response to “Contexts in rdflib”

  1. Nodalities » Blog Archive » This Week’s Semantic Web Says:

    […] Contexts in rdflib […]

Leave a Reply

You must be logged in to post a comment.