当我在模型中创建资源并打印 URI 时,我获得了正确的命名空间。例如,URI 是
http://www.somesite1.com/rdfdump/resources/resourceid-1
但是,当我将资源导出到 RDF file (link to the file) 时然后导入它,我得到指向磁盘中文件物理位置的资源 URI,例如
file:///D:/somefolder/resources/resourceid-1
我将 com.hp.hpl.jena.rdf.model.Model
与 com.hp.hpl.jena.ontology.OntModel
结合使用。 RDF 模型存储资源,我使用 OntModel
定义本体。按以下方式初始化它们。
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());
这就是我创建资源的方式。
String uri = BASE_URL + "resources/resourceid-1";
com.hp.hpl.jena.rdf.model.Resource rdfResource = model.createResource(uri, ocResource );
您能指出我哪里出错了吗?为什么我在导入文件时没有获得正确的 URI?
我注意到的一件事是,如果 Jena RDF 模型和 OntModel 具有相同的基本 URL,即 http://www.somesite.com/...
,那么我无法使用
ocResource.listInstances()
尽管有资源,但它给了我 0 资源。
最佳答案
您的 RDF/XML 文档未指定 xml:base
,但使用相对 URI:
<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
参数的读取方法之一,例如 read(model,uri,base,hintLang) .
虽然你提到你这样做:
String uri = BASE_URL + "resources/resourceid-1";
Resource rdfResource = model.createResource(uri, ocResource );
我预计您在某处遗漏了BASE_URL
前缀(毕竟,您没有向我们展示您的所有代码)并执行了类似以下操作:
String uri = "resources/resourceid-1";
Resource rdfResource = model.createResource(uri, ocResource );
关于java - Jena 模型中 RDF 资源的 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657447/