Search code examples
serializationrdfjenaowlturtle-rdf

Apache Jena - Is it possible to write to output the BASE directive?


I just started using Jena Apache, on their introduction they explain how to write out the created model. As input I'm using a Turtle syntax file containing some data about some OWL ontologies, and I'm using the @base directive to use relative URI's on the syntax:

@base <https://valbuena.com/ontology-test/> .

And then writing my data as:

<sensor/AD590/1> a sosa:Sensor ;
    rdfs:label "AD590 #1 temperatue sensor"@en ;
    sosa:observes <room/1#Temperature> ;
    ssn:implements <MeasureRoomTempProcedure> . 

Apache Jena is able to read that @base directive and expands the relative URI to its full version, but when I write it out Jena doesn't write the @base directive and the relative URI's. The output is shown as:

<https://valbuena.com/ontology-test/sensor/AD590/1> a sosa:Sensor ;
    rdfs:label "AD590 #1 temperatue sensor"@en ;
    sosa:observes <https://valbuena.com/ontology-test/room/1#Temperature> ;
    ssn:implements <https://valbuena.com/ontology-test/MeasureRoomTempProcedure> .  

My code is the following:

Model m = ModelFactory.createOntologyModel();
String base = "https://valbuena.com/ontology-test/";

InputStream in = FileManager.get().open("src/main/files/example.ttl");
if (in == null) {
   System.out.println("file error");
   return;
} else {
   m.read(in, null, "TURTLE");
}

m.write(System.out, "TURTLE");

There are multiple read and write commands that take as parameter the base:

  • On the read() I've found that if on the data file the @base isn't declared it must be done on the read command, othwerwise it can be set to null.
  • On the write() the base parameter is optional, it doesn't matter if I specify the base (even like null or an URI) or not, the output is always the same, the @base doesn't appear and all realtive URI's are full URI's.

I'm not sure if this is a bug or it's just not possible.


Solution

  • It seems that the command used on the introduction tutorial of Jena RDF API is not updated and they show the reading method I showed before (FileManager) which now is replaced by RDFDataMgr. The FileManager way doesn't work with "base" directive well.

    After experimenting I've found that the base directive works well with:

    Model model = ModelFactory.createDefaultModel();
    RDFDataMgr.read(model,"src/main/files/example.ttl");
    model.write(System.out, "TURTLE", base);
    

    or

    Model model = ModelFactory.createDefaultModel();
    model.read("src/main/files/example.ttl");
    model.write(System.out, "TURTLE", base);
    

    Although the model.write() command is said to be legacy on RDF output documentation (whereas model.read() is considered common on RDF input documentation, don't understand why), it is the only one I have found that allows the "base" parameter (required to put the @base directive on the output again), RDFDataMgr write methods don't include it.

    Thanks to @AndyS for providing a simpler way to read the data, which led to fix the problem.