【问题标题】:jena api get range of ObjectPropertyjena api 获取 ObjectProperty 的范围
【发布时间】:2014-06-16 18:48:15
【问题描述】:

我有 OWL 文件,我可以浏览它并浏览类和属性,但我无法检索正确的 ObjectProperty 范围。 这是我的 OWL 文件的一部分:

<owl:ObjectProperty rdf:about="&aat;aat2209_located_in">
        <rdfs:label xml:lang="en">located in</rdfs:label>
        <rdfs:label xml:lang="it">si trova in</rdfs:label>
        <rdfs:comment xml:lang="en">The property defines a relationship between places or places and things</rdfs:comment>
        <rdfs:comment xml:lang="it">La proprietà definisce la relazione tra luoghi o tra luoghi e cose</rdfs:comment>
        <rdfs:domain>
            <owl:Class>
                <owl:unionOf rdf:parseType="Collection">
                    <rdf:Description rdf:about="&dbpedia-owl;Artwork"/>
                    <rdf:Description rdf:about="&dbpedia-owl;Cave"/>
                </owl:unionOf>
            </owl:Class>
        </rdfs:domain>
        <rdfs:range>
            <owl:Class>
                <owl:unionOf rdf:parseType="Collection">
                    <rdf:Description rdf:about="&lodmt;ArchaeologicalSite"/>
                    <rdf:Description rdf:about="&dbpedia-owl;Building"/>
                </owl:unionOf>
            </owl:Class>
        </rdfs:range>
    </owl:ObjectProperty>

这是我探索 OWL 文件的代码的一部分

...    
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
            InputStream in =getClass().getResourceAsStream("/"+DATA_IRI);
            inf.read(in, "");
            OntClass obj = inf.getOntClass(uri);
            ExtendedIterator<OntProperty> propIter = obj.listDeclaredProperties(false);
            if(propIter.hasNext()){
                while (propIter.hasNext()) {
                    Set<PropertyModel> properties = new HashSet<PropertyModel>();
                    final OntProperty ontProperty = (OntProperty) propIter.next();
                    ExtendedIterator<? extends OntProperty> eqProp = ontProperty.listEquivalentProperties();
                    if(eqProp.hasNext()){
                        while (eqProp.hasNext()) {
                            OntProperty property = (OntProperty) eqProp.next();
                            PropertyModel propModel = new PropertyModel();
                            propModel.setLabel(property.getLocalName());
                            propModel.setUri(property.getURI());
                            propModel.setRange(property.getRange().getLocalName());
                            properties.add(propModel);
                        }
                    }
    ...

每次我打电话给property.getRange() 我都会得到这个结果:http://www.w3.org/2002/07/owl#Thing

有人帮帮我吗?

【问题讨论】:

  • 属性可以有多个范围,owl:Thing 肯定会成为每个对象属性的其中之一。那么,这不是一个错误的答案,只是没有你想要的那么具体。不过,您可能会得到更具体的答案..
  • 您的 OWL 文件不完整。您不需要向我们提供所有数据,但您需要向我们提供足够完整的信息,以便我们 可以在 Jena 中加载它。您没有封闭的 rdf:RDF 元素、命名空间声明、实体声明。它也不是最小的;我们不需要 rdfs:label 等,只需要 rdfs:range。
  • 这是我的 OWL 文件的一部分!
  • 我没有说它不是您的 OWL 文件的一部分。我说它不是完整us加载到耶拿是不够的),它不是minimal(它有更多的信息,什么我们需要解决这个问题)。见How to create a Minimal, Complete, and Verifiable example

标签: rdf jena owl rdfs


【解决方案1】:

如果您提供完整但最少的数据来处理,那就容易多了。在这种情况下,我将您的数据修改为以下内容,它是完整的,因为它是一个可加载的 RDF 文档,并且最小的是它只包含对象属性声明和范围公理。

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:ObjectProperty rdf:about="http://stackoverflow.com/q/24250198/1281433/aat2209_located_in">
        <rdfs:range>
            <owl:Class>
                <owl:unionOf rdf:parseType="Collection">
                    <rdf:Description rdf:about="http://stackoverflow.com/q/24250198/1281433/ArchaeologicalSite"/>
                    <rdf:Description rdf:about="http://stackoverflow.com/q/24250198/1281433/Building"/>
                </owl:unionOf>
            </owl:Class>
        </rdfs:range>
    </owl:ObjectProperty>
</rdf:RDF>

以下代码加载它并显示属性的范围:

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class RangeExample {
    public static void main(String[] args) {
        OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_RULE_MEM_INF );
        model.read( "..../data.owl" );
        OntProperty locatedIn = model.getOntProperty( "http://stackoverflow.com/q/24250198/1281433/aat2209_located_in" );
        ExtendedIterator<? extends OntResource> ranges = locatedIn.listRange();
        while ( ranges.hasNext() ) { 
            System.out.println( ranges.next() );
        }
    }
}

我的输出是

http://www.w3.org/2002/07/owl#Thing
-3e020093:146a7247e66:-7ffb
http://www.w3.org/2000/01/rdf-schema#Resource

owl:Thing 和 rdfs:Resource 存在,因为任何时候你有x located_in y,你可以确定y 是一个owl:Thing 和一个rdfs:Resource,因为located_in 是一个对象属性。另一个-3e020093:146a7247e66:-7ffb是RDF空白节点的标识符,即OWL联合类表达式。

为什么没有返回其他类?

根据以下评论,听起来需要更多讨论。

我得到相同的结果,但我会得到 ArchaeologicalSite 和 Building 作为结果。

您要求提供您声明的特定对象属性的范围。在 OWL 中,属性 p 的范围是任何 D 类,例如“if p(x,y), then D(y)”。也就是说,在 RDF 术语中,如果您的属性 p 具有 D 作为范围,那么只要存在三元组x p y,那么您也可以推断出三元组y rdf:type D。这是规则:

x p y     p rdfs:range D
-----------------------
   y rdf:type D

您要询问的范围是联合类。您班级的规则实例如下:

x located_in y     located_in rdfs:range (ArchaeologicalSite OR Building)
-------------------------------------------------------------------------
     y rdf:type (ArchaeologicalSite OR Building)

推断这个是有意义的

y rdf:type ArchaeologicalSite

或者那个

y rdf:type Building

因为这比您在x located_in y 中获得的信息更多。为了类比,考虑这个例子:

  1. 您的开胃菜可以是汤或沙拉。 (hasAppetizer 的范围是“汤或沙拉”。)
  2. 您的开胃菜有一些未知的 x。

据此,我可以推断出 x 是汤还是沙拉,但我无法推断出是哪一种。因此,“汤或沙拉”是 hasAppetizer 的一个范围,但汤和沙拉都不是。

【讨论】:

  • 我得到相同的结果,但我会得到 ArchaeologicalSite 和 Building 作为结果。
  • 您询问了属性的范围,但这些都不是属性的范围。我已经用更多信息更新了我的答案。
  • 如果我想探索“RDF 空白节点,即 OWL 联合类表达式”?
  • 用OntResource的asClass方法是怎么回事,再用OntClass的各种isXXX方法看看是什么东西?在这种情况下,isUnionClass 应该返回 true,因此您可以 asUnionClass 然后使用 UnionClass#getOperands 或 UnionClass#listOperands。我刚刚通过浏览示例中使用的类的 Javadoc 找到了所有这些。这是一项重要的发展技能;文档可以帮助您并使您的生活更轻松,因此请务必使用它。 :)
猜你喜欢
  • 2014-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多