use BNode with surf
zhiboz opened this issue · 5 comments
here is the code snippet from the federation.py
example
Movie = session.get_class(surf.ns.SURF['Movie'], store = 'local')
m1 = Movie('http://baseuri/m1')
m1.surf_title = "Movie 1"
trying to create a blank node with rdf.BNode but couldn't figure out how. any pointer would be appreciated!
the BNode in surf is the BNode from rdflib, here is a simple example of how to use it (from rdflib docs):
>>> from rdflib import BNode
>>> anode = BNode()
>>> anode
rdflib.term.BNode('AFwALAKU0')
>>> anode.n3()
u'_:AFwALAKU0'
thanks for your quick response!
i guess i knew it is also possible to do
from surf.rdf import BNode
what i couldn't figure out is how to use a bnode in surf
way, like
from surf.rdf import BNode
anode = BNode()
anode.foaf_name = 'Joe'
bnode.ex_employee = anode
you can only use surf resources that way, any rdflib terms function via their own apis, a surf resource can be created from the session like this:
Person = surf.ns.FOAF["Person"]
john_uri = "http://example/john"
instance = session.get_resource(john_uri, Person)
instead of giving your resource a URI you can try to pass a blank node:
Person = surf.ns.FOAF["Person"]
john_uri = BNode()
instance = session.get_resource(john_uri, Person)
I have not tested this though so not sure if this works, although it should. Let me know
well, made some progress with the above code suggested,
from surf import Store, Session, ns, query, rdf
store = Store(reader='rdflib', writer='rdflib')
session = Session(store)
Person = ns.FOAF["Person"]
john_uri = "http://example/john"
john = session.get_resource(john_uri, Person)
doe_uri = rdf.BNode()
doe = session.get_resource(doe_uri, Person)
doe.rdfs_label = 'hello'
john.rdfs_comment = doe
session.commit()
graph = store.reader.graph
print graph.serialize(format='n3')
when trying to serialize it, below is what it produces
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://example/john> a <http://xmlns.com/foaf/0.1/Person> ;
rdfs:comment <N5266ab8cbb6d4b24a8ff7c0ac431293a> .
<N5266ab8cbb6d4b24a8ff7c0ac431293a> a <http://xmlns.com/foaf/0.1/Person> ;
rdfs:label "hello" .
but we'd expect a blank node in [ ]
in serialization
<http://example/john> a <http://xmlns.com/foaf/0.1/Person> ;
rdfs:comment [ rdfs:label "hello"] .
surf delegates the serialization to the underlying rdflib graph, perhaps there is a bug in the rdflib serializer? It's been a while since I looked at RDF serialization formats. One way to debug this is to get the graph from the resource or the session and to try to serialize it yourself and see where the problem possibly occurs. Look for a .graph property. HTH, Cosmin