Search code examples
c#dotnetrdf

DotNetRDF: how to force CompressingTurtleWriter to use QNames instead of full IRI


I'm using DotNetRDF Framework (v2.6) to export database content into turtle files. I add all the QNames with the following command to the graph:

exportGraph.NamespaceMap.AddNamespace("medi", new Uri("http://sphn.ch/rdf/ontology/Swissmedic/"));
exportGraph.NamespaceMap.AddNamespace("sphn_gtin", new Uri("http://sphn.ch/rdf/ontology/GTIN/"));
exportGraph.NamespaceMap.AddNamespace("sphn_atc", new Uri("http://sphn.ch/rdf/ontology/ATC/")); 

and I write the exportGraph to the turtle file using:

CompressingTurtleWriter turtlewriter = new CompressingTurtleWriter();
turtlewriter.Save(exportGraph, CreateFileName());

I would like all output to be formatted like this:

medi:11275028 medi:ATC sphn_atc:A01AD11;
              medi:DateIntroduced "1982-03-15T00:00:00"^^xsd:dateTime;
              medi:GTIN sphn_gtin:7680112750289;
              medi:Label "Malveol, émuls, 100 ml"@fr,
                         "Malveol, Emuls, 100 ml"@de;
              medi:PrescriptionCategory "D"^^xsd:string;
              medi:Producer "Laboratoires Magistra SA"^^xsd:string;
              medi:SwissmedicNumber "11275028"^^xsd:string;
              medi:TherapyGroup "12.03.20."^^xsd:string;
              a medi:Swissmedic.

But it comes out like this:

<http://sphn.ch/rdf/ontology/Swissmedic/11275028> medi:ATC sphn_atc:A01AD11; 
                                                  medi:DateIntroduced "1982-03-15T00:00:00"^^xsd:dateTime;
                                                  medi:GTIN <http://sphn.ch/rdf/ontology/GTIN/7680112750289>;
                                                  medi:Label "Malveol, émuls, 100 ml"@fr,
                                                             "Malveol, Emuls, 100 ml"@de;
                                                  medi:PrescriptionCategory "D"^^xsd:string;
                                                  medi:Producer "Laboratoires Magistra SA"^^xsd:string;
                                                  medi:SwissmedicNumber "11275028"^^xsd:string;
                                                  medi:TherapyGroup "12.03.20."^^xsd:string;
                                                  a medi:Swissmedic.

I would like the subject be written with the QName as well as the GTIN object. In other exports I find it even more random when the prefix is used or the full IRI.

So far I was investigating what options I can set when creating the CompressingTurtleWriter() but there is only CompressenLevel and TurtleSyntax that is either original or W3C both don't change anything in the output. Also if I set turtlewriter.PrettyMode = true it doesn't change anything in the output. Finally there is a TurtleFormatter but I can't find a way how to use it in combination with the CompressingTurtleWriter().

Did anybody have a similar issue and how can I change this? Thanks Katie


Solution

  • The original Turtle spec didn't allow a digit as the first character of the local part of the compact IRI (the part after the :), but the W3C version of the spec does allow that. So setting the syntax mode to TurtleSyntax.W3C should produce the output you expect.

    There is one exception however. If your graph has a high ratio of unique subject nodes to triples (if (# unique subjects)/(# triples) > 0.75), then the writer goes into "high speed mode" which will disable all compression. This behaviour can be disabled by setting the HighSpeedModePermitted property on the writer to false.