【问题标题】:Jena: property name cannot be resolved to a variable?Jena:属性名称无法解析为变量?
【发布时间】: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的格式不同有什么关系吗?或者是其他东西?谢谢。

【问题讨论】:

    标签: rdf jena ontology protege


    【解决方案1】:

    VCARD 是包com.hp.hpl.jena.vocabulary 中定义的java 类。它包含(现在,旧的)VCard 词汇表中所有项目的 Java 常量。

    如果您想从本体生成自己的类,请查看 Jena 附带的 schemagen 应用程序。

    【讨论】:

    • 非常感谢您的解决方案!无论如何,在尝试 schemagen 之前,我想知道是否存在任何其他方法可以直接使用我的本体中的属性而不生成类。在 schemagen 页面中,我看到我总是可以使用它的 URL 定义一个资源。还有更简单的方法吗?谢谢!