svalbuena
svalbuena

Reputation: 21

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:

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

Upvotes: 2

Views: 840

Answers (3)

Alwinius
Alwinius

Reputation: 1961

@AndyS's answer allowed me to write relative URIs to the file, but did not include the base in use for RDFXML variations. To get the xml base directive added correctly, I had to use the following

RDFDataMgr.read(graph, is, Lang.RDFXML);    
Map<String, Object> properties = new HashMap<>();
properties.put("xmlbase", "http://example#");
Context cxt = new Context();
cxt.set(SysRIOT.sysRdfWriterProperties, properties);
RDFWriter.create().source(graph).format(RDFFormat.RDFXML_PLAIN).base("http://example#").context(cxt).output(os);

Upvotes: 0

svalbuena
svalbuena

Reputation: 21

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.

Upvotes: 0

AndyS
AndyS

Reputation: 16700

First - consider using a prefix like ":" -- this is not the same as base but makes the output nice as well.

You can configure the base with (current version of Jena):

RDFWriter.create()
         .source(model)
         .lang(Lang.TTL)
         .base("http://base/")
         .output(System.out); 

Upvotes: 3

Related Questions