Closed daniel-dona closed 4 years ago
This is a real edge case. To the best I can tell the only time this will appear is when rdf:type
is the only RDF namespace reference. If, for example, you add:
:pm_0 rr:constant rdf:nil.
to the above then everything works as advertised.
There are two possible fixes: 1) In the serializer line line referenced above, add a test that says "and node is not rdf:type or node is in the predicate position" 2) Fix the turtle serializer to emit the following:
:pm_0 a rr:PredicateMap ;
rr:constant a .
I weakly favor the latter. Others?
As @hsolbrig points out, this isn't unexpected behavior because there isn't any use of the rdf:
namespace in the file since a
does not count, but what you are seeing with that trailing rdf:type is definitely a bug. A temporary workaround to force inclusion of the rdf:
namespace to be included anyway we have a workaround which is to add rdf
to roundtrip_prefixes
https://github.com/RDFLib/rdflib/blob/6197531775801c61a620c8094327d145516a5ed7/rdflib/plugins/serializers/turtle.py#L46 Here is an example of how I use roundtrip_prefixes
to make sure that the empty namespace persists.
Out of curiosity, is :pm_0 a a .
valid Turtle? I guess the ultimate case would be a a a .
;-)
Apparently not, the parser complains about bad syntax (in both cases). Looks like a
is only allowed in the predicate position.
Just checked with Eric P (Mr. Turtle). He confirmed that it is only allowed in the predicate position: https://www.w3.org/TR/turtle/#grammar-production-verb
FWIW -- if you need the rdflib 4.x behavior (I did in one situation) something like:
https://gist.github.com/hsolbrig/b9412e9557b47189bfcfba40089faf2b#file-gistfile1-txt
would work. You could also replace the TurtleSerializer
with the TortoiseSerializer
if you wanted this behavior globally - instead of:
tortoise.register()
you could
import rdflib.plugins.serializers.turtle
rdflib.plugins.serializers.turtle.TurtleSerializer = TurtleWithPrefixes
@hsolbrig the way I implemented roundtrip_prefixes
has an undocumented feature, which is that if you set it to True
it will roundtrip all the prefixes, so you don't need to do the Cornucopia
dance, you could just do the following.
class TurtleWithPrefixes(TurtleSerializer):
""" A turtle serializer that always emits prefixes """
roundtrip_prefixes = True
Ha! Missed that nuance. Of course -- that was what the second half of the test was. Thanks! I'll update my code accordingly.
So although there's another issue identified here (object use of rdf:type
) I'm going to close this issue as this issue isn't really an issue.
Please just yell and scream if this needs to be re-opened.
When using ttl/turtle serialization, the
rdf
prefix is not in the prefixes/namespaces of the output, even if it's in the input data.Also, adding
graph.bind("rdf", Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
doesn't make any difference.Testing code: https://gist.github.com/daniel-dona/d2fda3669aecdf859454e072f4a36e43
Result: