Jena Ontology API如何检索将注释附加到类属性关系的公理

时间:2013-06-25 11:35:06

标签: jena ontology owl

我注释了下面描述的关系

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

TV类具有名为hasFeature的Object属性,其值为PowerConsumption类。注释适用于此属性关系。使用以下公理添加OWL文件以表示添加的注释。如何使用Jena检索此公理并获取isNegative的注释值?

<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>

1 个答案:

答案 0 :(得分:1)

Jena是一个以RDF为中心的API,尽管它以OntModel的形式提供了一些抽象。即便如此,OntModels也没有提供一种方便的方法来访问公理和注释它们。使用更加以OWL为中心的API可能会更好,例如恰当命名的OWL API

尽管如此,OWL可以被序列化为RDF,虽然可能存在陷阱(因为OWL本体可以序列化为RDF的方式可能存在差异),但您可能会获得所需的结果。这是加载本体的一小部分的Java代码,在里面找到owl:Axiom,并确定它们的哪个属性是注释属性。

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 );
                }
            }
        }
    }
}

输出显示您感兴趣的声明。

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

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

<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>