Reputation: 21
I'm using Python with the help of the rdflib import to convert a knowledge graph that is in rdf format into a named graph format. I also wanted to add a triple where the named graph is claimed to be said by a source that is within the main graph, this is a node within the main graph. I want to serialize the main graph into a trigf file, the trig file should show the named graph and its triples, the triples of the main graph, and the triples where the named graphs are used to show that it's claimed by a source. The first problem is that the output does not show the named graph inside of the file and the second issue is that the square brackets appear around the URIRef of the graph. The latter is a problem, because GraphDB is not able to parse this trig file. Here's an example code that i have used to figure this problem out:
from rdflib import Graph, Literal, Namespace, RDF, URIRef
# Create an RDF graph and add some data to it
rdf_graph = Graph()
# Define a namespace for your data (optional but recommended)
my_ns = Namespace("http://example.com/")
# Add some triples to the graph
rdf_graph.add((my_ns.Alice, RDF.type, my_ns.Person))
rdf_graph.add((my_ns.Alice, my_ns.hasAge, Literal(30)))
rdf_graph.add((my_ns.Bob, RDF.type, my_ns.Person))
rdf_graph.add((my_ns.Bob, my_ns.hasAge, Literal(25)))
# Create another named graph
named_graph_uri = URIRef("http://example.com/graphs/graph1")
named_graph = Graph(identifier=named_graph_uri)
# Add triples to the named graph
named_graph.add((my_ns.Alice, my_ns.likes, my_ns.Bob))
named_graph.add((my_ns.Bob, my_ns.likes, my_ns.Alice))
# Add the knowledge and the source of the knowledge to main graph
rdf_graph.add((named_graph, my_ns.saidBy, my_ns.Alice))
# Serialize main graph with the named graph within the main graph
print(rdf_graph.serialize(destination='test_file.trig' format='trig'))
Here's the result that I get:
Default RDF graph:
@prefix ns1: <http://example.com/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ns1:Bob a ns1:Person ;
ns1:hasAge 25 .
[<http://example.com/graphs/graph1>] ns1:saidBy ns1:Alice .
ns1:Alice a ns1:Person ;
ns1:hasAge 30 .
Here's the result that I expect:
Default RDF graph:
@prefix ns1: <http://example.com/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ns1:Bob a ns1:Person ;
ns1:hasAge 25 .
ns1:graph1 ns1:saidBy ns1:Alice .
ns1:Alice a ns1:Person ;
ns1:hasAge 30 .
Upvotes: 2
Views: 134
Reputation: 3989
URI's to for most intents and purposes are opaque and precise. In your code you need to add another namespace to get the desired CURIE in your output, or change the graph URI to match the namespace you currently have. So either get rid of 'graphs' in the graph URI path:
named_graph_uri = URIRef("http://example.com/graph1")
OR add a new namespace for your graphs and change the code accordingly
from rdflib import Graph, Literal, Namespace, RDF, URIRef
# Create an RDF graph and add some data to it
rdf_graph = Graph()
# Define a namespace for your data (optional but recommended)
my_ns = Namespace("http://example.com/")
graphs_ns = Namespace("http://example.com/graphs/")
# Add some triples to the graph
rdf_graph.add((my_ns.Alice, RDF.type, my_ns.Person))
rdf_graph.add((my_ns.Alice, my_ns.hasAge, Literal(30)))
rdf_graph.add((my_ns.Bob, RDF.type, my_ns.Person))
rdf_graph.add((my_ns.Bob, my_ns.hasAge, Literal(25)))
# Create another named graph
named_graph_uri = graphs_ns.graph1
named_graph = Graph(identifier=named_graph_uri)
# Add triples to the named graph
named_graph.add((my_ns.Alice, my_ns.likes, my_ns.Bob))
named_graph.add((my_ns.Bob, my_ns.likes, my_ns.Alice))
# Add the knowledge and the source of the knowledge to main graph
rdf_graph.add((graphs_ns.graph1, my_ns.saidBy, my_ns.Alice))
# Serialize main graph with the named graph within the main graph
print(rdf_graph.serialize(destination='test_file.trig' format='trig'))
Upvotes: 1