1

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?

  • What do you mean by "is not created as desired"? – Stanislav Kralin Jul 22 '20 at 13:36
  • @StanislavKralin in that way a [ ] is created, instead I want to obtain _:refPeriodStep1. Do you have any idea? – iacopo pacifici Jul 22 '20 at 14:29
  • 2
    If preserving blank node labels matters, use URIs instead. – Stanislav Kralin Jul 22 '20 at 14:50
  • @StanislavKralin using URIs like this: g.add((URIRef('_', 'refPeriodStep1'), RDF.type, URIRef(qb4o + 'HierarchyStep'))), I get this: <_refPeriodStep1> a qb4o:HierarchyStep . – iacopo pacifici Jul 22 '20 at 15:06
  • Am I using URIRef incorrectly? – iacopo pacifici Jul 22 '20 at 15:06
  • yes, you are ... a URI is **not** a blank node. You have to use a proper URI in it's form an it defintely does never start with a `_` - that is a blank node. Anyways, what Stanislav was suggesting is that if you want to be able to identify the blank node later on by it's name, why not using a URI then? I mean, that's the whole purpose of a URI, Uniform Resource **Identifier**. – UninformedUser Jul 23 '20 at 05:47

1 Answers1

3

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> .
mgc
  • 5,223
  • 1
  • 24
  • 37