简体   繁体   English

耶拿(Jena)和RDFS:从外部本体获取属性

[英]Jena & RDFS: Get property from external ontology

I have an RDFS ontology with a User class as such: 我有一个User类的RDFS本体,例如:

<rdfs:Class rdf:ID="User"> 
    <rdfs:subClassOf rdf:resource="http://xmlns.com/foaf/0.1/Agent" />
    <rdfs:comment> 
        The class of users, subclassing the foaf:Agent class.
    </rdfs:comment>  
</rdfs:Class>

The foaf:Agent class has a property called name , which I would like to load using Jena with something similar to: foaf:Agent类具有一个名为name的属性,我想使用Jena将该属性加载为类似以下内容:

Property name = model.getOntProperty(ns + "name");

The problem is that I get a NullPointerException when I try to use it like this: 问题是,当我尝试像这样使用它时,我得到了NullPointerException:

model.createStatement(resource, name, "Nicolas Cage");

I have tried with the foaf standard namespace as well ( ns = "http://xmlns.com/foaf/0.1/"; ), even though I am not sure how much sense that makes. 我也尝试过使用foaf标准命名空间( ns = "http://xmlns.com/foaf/0.1/"; ),即使我不确定这样做有多大意义。

In the end, I have no idea what the right approach for this is. 最后,我不知道什么是正确的方法。 Should I create it? 我应该创建它吗? Doesn't Jena somehow find it automatically in the external ontology? 耶拿(Jena)不会以某种方式在外部本体中自动找到它吗?

In RDF, resources, including properties, are identified by URIs. 在RDF中,资源(包括属性)由URI 标识 There's no sense in which you need to load those URIs. 无需加载这些URI。 Sometimes documents that are identified by URIs might contain statements that you want, in which case, you'd need to load a document containing those, but that's different than simply referencing the property. 有时,由URI标识的文档可能包含所需的语句,在这种情况下,您需要加载包含这些语句的文档,但这与仅引用该属性不同。 Here's an example: 这是一个例子:

import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;

public class UseFoafNameExample {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        Property name = model.createProperty( "http://xmlns.com/foaf/0.1/name" );
        Resource resource = model.createResource( "http://stackoverflow.com/q/23818390/1281433/NicholasCage" );
        Statement s = model.createStatement( resource, name, "Nicholas Cage" );
        model.add( s );
        RDFDataMgr.write( System.out, model, RDFFormat.RDFXML_ABBREV );
    }
}
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://xmlns.com/foaf/0.1/">
  <rdf:Description rdf:about="http://stackoverflow.com/q/23818390/1281433/NicholasCage">
    <j.0:name>Nicholas Cage</j.0:name>
  </rdf:Description>
</rdf:RDF>

Now, there's another problem in your question. 现在,您的问题中还有另一个问题。

The foaf:Agent class has a property called name, which I would like to load using Jena with something similar to: foaf:Agent类具有一个名为name的属性,我想使用Jena将该属性加载为类似以下内容:

the foaf:Agent class doesn't have a property; foaf:Agent类没有属性; that's not how RDFS (and OWL) classes work. RDFS(和OWL)类不是这样工作的。 There's a property name that has Agent as an rdfs:domain (I didn't check that this is actually the case, but it sounds reasonable, and I'm guessing that that's where the confusion arose from). 有一个将Agent作为rdfs:domain的属性name (我没有检查是否确实如此,但这听起来很合理,我猜这就是造成混淆的地方)。 That means that when you have a triple 这意味着当你有一个三重

x foaf:name "some name"

you can infer that 你可以推断

x rdf:type foaf:Agent

Of course, to do that sort of inference, you need to know about the triple 当然,要进行这种推断,您需要了解三元组

foaf:name rdfs:domain foaf:Agent

and that's what you may want to load an ontology from someplace else. 就是您可能希望从其他地方加载本体的原因。 Ie, you want to get the axioms that it contains, so that you can reason with them. 即,您想获取其中包含的公理 ,以便您可以对它们进行推理。

Jena will not automatically load something just because it is mentioned. 耶拿不会因为提及而自动加载某些东西。

rdf:resource="http://xmlns.com/foaf/0.1/Agent"

is just a mention of the name Agent and say nothing about fetching it. 只是提到了Agent这个名字,而对于获取它一事无成。 Its just any old URI to RDF - and it may not exist. 它只是RDF的所有旧URI-可能不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM