I'm using the RDFlib to automate the process of creating the DSD. I want to get a format as follows:
_:refPeriodStep1 a qb4o:HierarchyStep;
etc...
but using this:
graph.add((BNode('refPeriodStep1'), RDF.type, URIRef(qb4o + 'HierarchyStep')))
the file is not created as desired.
I tried to use the function n3():
graph.add((BNode('refPeriodStep1').n3(), RDF.type, URIRef(qb4o + 'HierarchyStep')))
but I get the following error:
AssertionError: Subject _:refPeriodStep1 must be an rdflib term
Is there any way to get it as wanted?
As suggested in comment, if the name of the blank node matter, you should probably use an URI instead.
Your last comment suggest you are not using URIRef correctly when stating URIRef('_', 'refPeriodStep1')
(according to my understanding).
If you want your refPeriodStep1
to not be a blank node while keeping it (somehow) unprefixed, you can define an empty prefix using the namespace_manager
of your graph.
For example the following code is creating a refPeriodStep
of type qb4o:HierarchyStep
using Namespace
fonctionnalities instead of URIRef directly (this is probably the easiest way to do if you reuse a lot your namespaces as stated here in the documentation) :
from rdflib.namespace import NamespaceManager
from rdflib import BNode, Namespace, Graph
QB4O = Namespace('http://example.com/qb4o#')
n = Namespace('http://example.com/base-ns#')
g = Graph()
g.namespace_manager = NamespaceManager(Graph())
g.namespace_manager.bind('qb4o', QB4O)
g.namespace_manager.bind('', n)
g.add((n['refPeriodStep1'], RDF.type, QB4O['HierarchyStep']))
g.serialize('test.ttl', format='turtle')
This will output the following (in turtle) which is close to what you expect :
@prefix : <http://example.com/base-ns#> .
@prefix qb4o: <http://example.com/qb4o#> .
@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#> .
:refPeriodStep1 a qb4o:HierarchyStep .
Note that if you try to serialize a blank node in n-triples
format, RDFLib will keep the name used when creating that blank node, such as :
_:refPeriodStep1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/qb4o#HierarchyStep> .