URI of an RDF resource in Jena model

2019-09-01 14:20发布

问题:

While I create resources in the model and print the URI, I get the correct namespace. For instance, the URI is

http://www.somesite1.com/rdfdump/resources/resourceid-1

However, when I export the resources to an RDF file (link to the file) and I import it, I get the resource URI pointing to the physical location of the file in the disk such as

file:///D:/somefolder/resources/resourceid-1

I am using com.hp.hpl.jena.rdf.model.Model in conjunction with com.hp.hpl.jena.ontology.OntModel. The RDF model stores resources and I define an ontology using OntModel. Initialize them in the following way.

com.hp.hpl.jena.rdf.model.Model model = ModelFactory.createDefaultModel();

OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, model);

NS = "http://www.somesite2.com/ontology"
BASE_URL = "http://www.somesite1.com/rdfdump/"
OntClass ocResource = ontModel.createClass(NS + "#RESOURCE");

//read an RDF file into the model
RDFDataMgr.read(model, file.toURI().toString());

This is the way I create a resource.

String uri = BASE_URL + "resources/resourceid-1";
com.hp.hpl.jena.rdf.model.Resource rdfResource = model.createResource(uri, ocResource );

Can you kindly point to me where is that I am going wrong and why is it that I do no get the correct URI when I import the file?

One thing I noted is, if I have the same base URL for both Jena RDF Model and OntModel, i.e., say http://www.somesite.com/..., then I cannot retrieve resources in the model using

ocResource.listInstances()

It gives me 0 resources despite there being resources.

回答1:

Your RDF/XML document doesn't specify an xml:base, but uses relative URIs:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://www.someaddress2.com/ontology#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
  <!-- … -->
  <rdf:Description rdf:nodeID="A1">
    <!-- "tag/tag/…" is relative -->
    <rdf:_1 rdf:resource="tag/tag/rs_tagapplication-guid-5CCC6F83F6DDEE9DC69390E3767DEA4BD8BD4DCD4E9F3570A67E8F7455785C51"/>
    <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag"/>
  </rdf:Description>
  <!-- … -->
</rdf:RDF>

There's no xml:base in the document, so the relative URIs are apparently being resolved against the path of the file from which the model is being read. Either (i) use only absolute URIs in your document or (ii) explicitly specify a base in the document or (iii) use one of the read methods that take a base argument, e.g., read(model,uri,base,hintLang).

Although you mention that you do:

String uri = BASE_URL + "resources/resourceid-1";
Resource rdfResource = model.createResource(uri, ocResource );

I expect that somewhere you left out the BASE_URL prefix (and after all, you haven't shown us all your code) and did something like:

String uri = "resources/resourceid-1";
Resource rdfResource = model.createResource(uri, ocResource );