Serializing graph with named graphs

349 views
Skip to first unread message

raffaele...@gmail.com

unread,
Mar 26, 2015, 4:55:27 PM3/26/15
to rdfli...@googlegroups.com
Hello,

I'm trying to create a graph containing a number of named graphs and serialize it to trig (and possibly eventually to json-ld).

If I only add quads, they will not be included in the output:

G = ConjunctiveGraph()
g1 = Graph("default", "one")
G.add((URIRef("http://example.com"), RDFS.label, Literal("example 1"), g1))

Full working example is here: https://gist.github.com/raffazizzi/090e0fca05dec01ddeda

If I only create the assertion in the named graph, serializing the main graph yields nothing, understandably.

I only get the correct output when I add both a quad *and* add the same assertion to the named graph, thus duplicating the data (undesirable).

g1 = Graph("default", "one")
G.add((URIRef("http://example.com"), RDFS.label, Literal("example 1"), g1))
g1.add((URIRef("http://example.com"), RDFS.label, Literal("example 1")))

Full working example here: https://gist.github.com/raffazizzi/11f635eb9a6961658c2d

Any ideas on why this is happening? Is it an issue with the trig serializer, or am I misunderstanding how named graphs should work?

Thank you!
Raff

Drew Perttula

unread,
Mar 29, 2015, 3:06:25 AM3/29/15
to rdfli...@googlegroups.com
I'm not sure what you're trying to get with Graph("default", "one").
Graph identifiers are normally URIs, and this seems to work:

from rdflib import Graph, ConjunctiveGraph, Literal, URIRef
from rdflib.namespace import RDF, RDFS
G = ConjunctiveGraph()
G.add((URIRef("http://example.com"), RDFS.label, Literal("example 1"),
URIRef("http://example.com/graph1")))
print G.serialize(format='trig')

@prefix ns1: <http://example.com/> .
@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#> .

<ns1:graph1> {
<http://example.com> rdfs:label "example 1" .
}

Except for a weird bug where serialize omits the ns1 prefix line the
first time you run it but remembers the line the second time (!)


Message has been deleted

Raff

unread,
Mar 29, 2015, 9:09:00 PM3/29/15
to rdfli...@googlegroups.com, dr...@bigasterisk.com
I see. I was trying to instantiate and populate each graph separately instead of just creating quads. Thanks! Your suggestions brought me in the right direction.

R

Drew Perttula

unread,
Mar 30, 2015, 3:06:24 AM3/30/15
to rdfli...@googlegroups.com
On 03/29/2015 12:06 AM, Drew Perttula wrote:
>
> print G.serialize(format='trig')
>
> @prefix ns1: <http://example.com/> .
> @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#> .
>
> <ns1:graph1> {
> <http://example.com> rdfs:label "example 1" .
> }
>
> Except for a weird bug where serialize omits the ns1 prefix line the
> first time you run it but remembers the line the second time (!)
>
>

https://github.com/RDFLib/rdflib/pull/480 should fix the <ns1:graph1> to
have a prefix line and to not have the <>, which aren't correct for
abbreviated qnames.
Reply all
Reply to author
Forward
0 new messages