使用耶拿解析schema.org ttl/owl文件 [英] Parsing schema.org ttl/owl file using Jena

查看:77
本文介绍了使用耶拿解析schema.org ttl/owl文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码生成器,该代码生成器根据此处定义的架构生成实体(Java语言中的POJO). http ://schema.rdfs.org/all.ttl .我正在使用耶拿(Jena)解析ttl文件并检索生成它们所需的元数据.

I'm writing a code generator that generate entities (POJO's in Java language) from the schema defined here http://schema.rdfs.org/all.ttl. I'm using Jena to parse the ttl file and retrieve the meta data that I need to generate them.

Jena成功解析了文件,但是由于某种原因,它没有列出给定实体(例如Person)的所有属性.我不确定自己是否做错了什么,使用了错误的API等.这是重新创建场景的代码示例:

Jena parses the file successfully, however, for some reason it does not list all the attributes of a given entity, e.g., Person. I'm not sure whether I'm doing something wrong, using the wrong API, etc. Here's the code sample that recreates the scenario:

    public class PersonParser {

    public static void main(String[] args) {
        OntModel model = ModelFactory.createOntologyModel();
        URL url = Thread.currentThread().getContextClassLoader().getResource("schema_org.ttl");
        model.read(url.toString(), "TURTLE");
        OntClass ontclass = model.getOntClass("http://schema.org/Person");
        Iterator<OntProperty> props = ontclass.listDeclaredProperties();
        while (props.hasNext()) {
            OntProperty p = props.next();
            System.out.println("p:" + p.getLocalName());
        }
    }
}

基本上,我只是在寻找一个名为Person的类,并试图列出其所有属性,而我得到的是:

Basically, I'm looking for only one class called Person and trying to list all its properties and what I get is:

p:alternateName
p:deathDate
p:alumniOf
p:sameAs
p:url
p:additionalName
p:homeLocation
p:description
p:nationality
p:sibling
p:follows
p:siblings
p:colleagues
p:memberOf
p:knows
p:name
p:gender
p:birthDate
p:children
p:familyName
p:jobTitle
p:workLocation
p:parents
p:affiliation
p:givenName
p:honorificPrefix
p:parent
p:colleague
p:additionalType
p:honorificSuffix
p:image
p:worksFor
p:relatedTo
p:spouse
p:performerIn

但是,如果您查看 http://schema.org/Person ,它会有很多特性它没有列出(例如address). http://schema.rdfs.org/all.ttl 中的schema:address声明为:

But if you look at http://schema.org/Person, it's got a bunch of properties that it did not list (for example address). The declaration of schema:address in http://schema.rdfs.org/all.ttl is:

schema:address a rdf:Property;
    rdfs:label "Address"@en;
    rdfs:comment "Physical address of the item."@en;
    rdfs:domain [ a owl:Class; owl:unionOf (schema:Person schema:Place schema:Organization) ];
    rdfs:range schema:PostalAddress;
    rdfs:isDefinedBy <http://schema.org/Person>;
    rdfs:isDefinedBy <http://schema.org/Place>;
    rdfs:isDefinedBy <http://schema.org/Organization>;
    .

有人遇到过这个吗?我应该使用其他的Jena接口来解析架构吗?

Has anyone come across this? Should I be using a different Jena interface to parse the schema?

推荐答案

请注意,listDeclaredProperties上的文档为(强调):

Note that the documentation on listDeclaredProperties is (emphasis added):

listDeclaredProperties

listDeclaredProperties

com.hp.hpl.jena.util.iterator.ExtendedIterator<OntProperty> listDeclaredProperties(boolean direct)

返回与类似框架的属性相关联的迭代器 此类的观点.这捕获了一个直观的概念 类的属性.这对于呈现本体很有用 用户界面中的类,例如通过自动构造一个 实例化该类的实例的窗体.中的属性 类的框架视图是通过比较以下域来确定的: 该类的OntModel中的属性以及该类本身.看: 将RDF呈现为框架以获取更多详细信息.

Return an iterator over the properties associated with a frame-like view of this class. This captures an intuitive notion of the properties of a class. This can be useful in presenting an ontology class in a user interface, for example by automatically constructing a form to instantiate instances of the class. The properties in the frame-like view of the class are determined by comparing the domain of properties in this class's OntModel with the class itself. See: Presenting RDF as frames for more details.

请注意,在许多情况下,确定属性是否已关联 使用类取决于RDFS或OWL推理.这种方法可能 因此,仅在具有附件的模型中返回完整结果 推理机.

Note that many cases of determining whether a property is associated with a class depends on RDFS or OWL reasoning. This method may therefore return complete results only in models that have an attached reasoner.

  • direct-如果为true,则将返回的属性限制为与此类直接相关的属性.如果为false,则为超类的属性 此类的不会在此的声明的属性中列出 课.
  • direct - If true, restrict the properties returned to those directly associated with this class. If false, the properties of super-classes of this class will not be listed among the declared properties of this class.

按其域与此类关联的属性的迭代.

An iteration of the properties that are associated with this class by their domain.

因此,即使在查看特定模式之前,也必须注意,除非使用推理机,否则可能无法获得预期的所有结果.然后,注意如何声明 address 属性:

So, even before looking at the particular schema, it's important to note that unless you're using a reasoner, you might not get all the results you expect. Then, notice how the address property is declared:

schema:address a rdf:Property;
    rdfs:label "Address"@en;
    rdfs:comment "Physical address of the item."@en;
    rdfs:domain [ a owl:Class; owl:unionOf (schema:Person schema:Place schema:Organization) ];
    rdfs:range schema:PostalAddress;
    rdfs:isDefinedBy <http://schema.org/Person>;
    rdfs:isDefinedBy <http://schema.org/Place>;
    rdfs:isDefinedBy <http://schema.org/Organization>;

地址 的域是联合类:人员放置组织.那是 Person 的超类,但它是一个复杂的类表达式,而不仅仅是一个简单的命名类,因此,如文档所述,您可能需要一个推理器,以使Jena认识到它是超类. 的人.

The domain of address is a union class: Person or Place or Organization. That's a superclass of Person, but it's a complex class expression, not just a simple named class, so you'll probably need a reasoner, as the documentation mentions, to get Jena to recognize that it's a superclass of Person.

我认为使用推理程序将使耶拿认识到 address 的域是 Person 的超类,因此将其包含在listDeclaredProperties的结果中.值得注意的是,这与OWL语义有何不同.

I think that using a reasoner will allow Jena to recognize that the domain of address is a superclass of Person, and thus include it in the result of listDeclaredProperties. It's worth noting how this differs from OWL semantics, though.

在OWL中,类D成为属性P的域的含义意味着,只要我们具有属性P的三元组,我们就可以推断出主题是D.这可以用规则表示

In OWL, what it means for a class D to be the domain of a property P means that whenever we have a triple with the property P, we can infer that the subject is a D. This can be expressed by the rule

P rdfs:domain D     X P Y
-------------------------
    X rdf:type D

因此,即使某人可能有一个地址,仅仅因为某物有一个地址也不足以告诉我们该某物 Person ;它仍然可以是 Place Organization .

So, even though a Person might have an address, just because something has an address isn't enough to tell us that that something is a Person; it could still be a Place or Organization.

这篇关于使用耶拿解析schema.org ttl/owl文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆