【发布时间】:2025-12-10 05:55:01
【问题描述】:
我正在使用 Jena 来查询我的本体,并且我正在遵循this tutorial 的第 8 步:查询模型。这里查询的RDF文件vc-db-1.rdf是由Step 3: Writing RDF生成的,如下图:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" >
<rdf:Description rdf:nodeID="A0">
<vcard:Family>Smith</vcard:Family>
<vcard:Given>John</vcard:Given>
</rdf:Description>
<rdf:Description rdf:about="http://somewhere/JohnSmith">
<vcard:N rdf:nodeID="A0"/>
<vcard:FN>John Smith</vcard:FN>
</rdf:Description>
</rdf:RDF>
示例代码为tutorial 7,可here下载。
我注意到那一行
ResIterator iter = model.listResourcesWithProperty(VCARD.FN);
VCARD.FN 只是 RDF 中的一个属性名称,而不是我的代码中定义的变量。不过这里可以成功识别,代码运行没有任何问题。
但我自己的 RDF 文件并非如此。我用 Protege 创建了一个本体pottery.owl,并将其保存为 RDF/XML 语言。文件内容如下:
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#"
xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:swrl="http://www.w3.org/2003/11/swrl#"
xmlns="http://www.owl-ontologies.com/Ontology1369190090.owl#"
xmlns:swrlb="http://www.w3.org/2003/11/swrlb#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.owl-ontologies.com/Ontology1369190090.owl" >
<rdf:Description rdf:about="">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
</rdf:Description>
<rdf:Description rdf:about="#pottery">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="#pottery_instance_1">
<rdf:type rdf:resource="#pottery"/>
<pottery.colors rdf:datatype="http://www.w3.org/2001/XMLSchema#string">blue</pottery.colors>
</rdf:Description>
<rdf:Description rdf:about="#pottery.colors">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="#pottery"/>
</rdf:Description>
</rdf:RDF>
<!-- Created with Protege (with OWL Plugin 3.4.8, Build 629) http://protege.stanford.edu -->
本体包含一个类pottery、一个实例pottery_instance_1和一个数据类型属性pottery.colors。
我在原始代码中修改了这些行:
static final String inputFileName = "pottery.owl";
// ...
ResIterator iter = model.listResourcesWithProperty(pottery.colors);
// ...
System.out.println(" " + iter.nextResource()
.getProperty(pottery.colors)
.getString());
这次我得到了错误“pottery cannot be resolved to a variable.”
这里有什么诀窍?这和两个RDF的格式不同有什么关系吗?或者是其他东西?谢谢。
【问题讨论】: