Jena模型中RDF资源的URI [英] URI of an RDF resource in Jena model
问题描述
当我在模型中创建资源并打印URI时,我得到了正确的名称空间.例如,URI是
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
但是,当我将资源导出到 RDF文件(链接到文件)并导入时,我得到了指向磁盘中文件物理位置的资源URI,例如
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
我正在将com.hp.hpl.jena.rdf.model.Model
与com.hp.hpl.jena.ontology.OntModel
结合使用. RDF模型存储资源,我使用OntModel
定义了一个本体.用以下方式初始化它们.
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 );
您能否指出我错在哪里?为什么导入文件时我没有得到正确的URI?
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?
我注意到的一件事是,如果我的Jena RDF模型和OntModel具有相同的基本URL,即说http://www.somesite.com/...
,那么我将无法使用
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()
尽管有资源,但它仍为我提供了0个资源.
It gives me 0 resources despite there being resources.
推荐答案
您的RDF/XML文档未指定xml:base
,但使用了相对URI:
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>
文档中没有xml:base
,因此显然可以解决相对URI
从中读取模型的文件的路径. (i)在文档中仅使用绝对URI,或者(ii)在文档中明确指定基数,或者(iii)使用带有base
参数的读取方法之一,例如
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 );
我希望您在某处省略了BASE_URL
前缀(毕竟,您并未向我们展示所有代码),并且做了类似的事情:
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 );
这篇关于Jena模型中RDF资源的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!