Jena Ontology API如何检索将注释附加到类属性关系的公理 [英] Jena Ontology API how to retrieve axiom that attach annotation to a class property relation

查看:83
本文介绍了Jena Ontology API如何检索将注释附加到类属性关系的公理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注释了下面描述的关系

I have annotated the relationship described below

TV subClassof : Restriction {hasFeature some PowerConsumption} ::: @isNegative=true.

TV类具有一个名为hasFeature的对象属性,其值在类PowerConsumption中.注释将应用于此属性关系.将使用以下公理添加OWL文件,以表示添加的注释.如何使用Jena检索此公理并获取isNegative的注释值?

The TV class has a Object property called hasFeature with values in class PowerConsumption. The annotation is applied to this property relation. The OWL file gets added with the following axiom to represent the added annotation. How can I retrieve this axiom and get the annotation value of isNegative using Jena?

<owl:Axiom>
  <isNegative>true</isNegative>
  <owl:annotatedSource rdf:resource="&product_ontolology;TV"/>
  <owl:annotatedProperty rdf:resource="&rdfs;subClassOf"/>
  <owl:annotatedTarget>
    <owl:Restriction>
      <owl:onProperty rdf:resource="&product_ontolology;hasFeature"/>
      <owl:someValuesFrom rdf:resource="&product_ontolology;PowerConsumption"/>
    </owl:Restriction>
  </owl:annotatedTarget>
</owl:Axiom>

推荐答案

Jena是以RDF为中心的API,尽管它以 OWL API ),可能会更好.

Jena is an RDF-centric API, although it provides some abstraction in the form of OntModel. Even so, OntModels don't provide a convenient way to access the axioms and to annotate them. You might have better luck using the a more OWL-centric API, such as the aptly named OWL API.

尽管如此,OWL可以序列化为RDF,并且可能会有陷阱(因为OWL本体可以序列化为RDF的方式可能有所不同),您可能会得到所需的结果.这是Java代码,它加载了本体的一小部分,在内部找到了owl:Axiom,并确定它们的哪些属性是注释属性.

Nonetheless, OWL can be serialized as RDF, and while there may be pitfalls (because there might be variation in the way that an OWL ontology can be serialized into RDF), you can probably get the sort of results you want. Here's Java code that loads a small portion of your ontology, finds the owl:Axioms inside, and determines which of their properties are annotation properties.

import java.util.HashSet;
import java.util.Set;

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.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL2;
import com.hp.hpl.jena.vocabulary.RDF;


public class AnnotationExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // create the model and load the data.
        Model model = ModelFactory.createDefaultModel().read( "products.owl" );

        // owlAnnotationProperties are the properties used to represent
        // annotated axioms in RDF/XML.
        Set<Property> owlAnnotationProperties = new HashSet<Property>() {{
            add( RDF.type );
            add( OWL2.annotatedProperty );
            add( OWL2.annotatedSource );
            add( OWL2.annotatedTarget );
        }};

        // Find the axioms in the model.  For each axiom, iterate through the 
        // its properties, looking for those that are *not* used for encoding the 
        // annotated axiom.  Those that are left are the annotations.
        ResIterator axioms = model.listSubjectsWithProperty( RDF.type, OWL2.Axiom );
        while ( axioms.hasNext() ) {
            Resource axiom = axioms.next();
            StmtIterator stmts = axiom.listProperties();
            while ( stmts.hasNext() ) {
                Statement stmt = stmts.next();
                if ( !owlAnnotationProperties.contains( stmt.getPredicate() )) {
                    System.out.println( stmt );
                }
            }
        }
    }
}

输出显示您感兴趣的语句.

The output shows the statement that you are interested in.

[630c9cd5:13f7b69db3c:-7ffe, http://www.example.com/products#isNegative, "true"^^http://www.w3.org/2001/XMLSchema#boolean]

这是我使用的OWL小型本体:

Here's the small OWL ontology I used:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:products="http://www.example.com/products#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Ontology rdf:about="http://www.example.com/products"/>
  <owl:Class rdf:about="http://www.example.com/products#TV">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:someValuesFrom>
          <owl:Class rdf:about="http://www.example.com/products#PowerConsumption"/>
        </owl:someValuesFrom>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="http://www.example.com/products#hasFeature"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:AnnotationProperty rdf:about="http://www.example.com/products#isNegative"/>
  <owl:Axiom>
    <owl:annotatedTarget>
      <owl:Restriction>
        <owl:someValuesFrom rdf:resource="http://www.example.com/products#PowerConsumption"/>
        <owl:onProperty rdf:resource="http://www.example.com/products#hasFeature"/>
      </owl:Restriction>
    </owl:annotatedTarget>
    <owl:annotatedProperty rdf:resource="http://www.w3.org/2000/01/rdf-schema#subClassOf"/>
    <owl:annotatedSource rdf:resource="http://www.example.com/products#TV"/>
    <products:isNegative rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean"
    >true</products:isNegative>
  </owl:Axiom>
</rdf:RDF>

这篇关于Jena Ontology API如何检索将注释附加到类属性关系的公理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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