Jena Ontology API how to retrieve axiom that attac

2019-06-14 05:16发布

问题:

I have annotated the relationship described below

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

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>

回答1:

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.

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]

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>