2

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.

svalbuena
  • 21
  • 1
  • 3
  • `RDFDataMgr.read(m, "src/main/files/example.ttl")` is simpler. – AndyS Mar 15 '18 at 13:41
  • @AndyS . Thanks! And.... that was the solution to the problem! It seems that the reading command I was using is legacy but the introduction tutorial still uses it. – svalbuena Mar 16 '18 at 00:46

3 Answers3

3

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); 
AndyS
  • 16,345
  • 17
  • 21
  • I wasn't able to fix the problem with this command, the source function doesn't allow Model class, with model.getGraph() it runs but the output doesn't show base directive. ":" can be a nice solution but I prefer to use base as Jena seems to be able to process it. – svalbuena Mar 16 '18 at 00:41
0

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.

svalbuena
  • 21
  • 1
  • 3
0

@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);
Alwinius
  • 1,941
  • 1
  • 9
  • 12