Search code examples
jenaontologyprotege

Can I add annotations to object property assertion from jena?


I want to add an annotation (comment) to an object property assertion like the one below.

Mobile → hasCamera → 8MP

I am able to get this specific assertion as a Statement object. Now I want to add annotations to this statement object, but there aren't any direct methods to do this with Jena. On the other hand I am able to easily achieve this in Protege tool. So is this just a feature of Protégé, or is there any possible way to do this with Jena?

Basically, I'm interested in this, to add a weight score to the property (link) between the two resources viz. Mobile and 8MP.


Solution

  • Jena, though it has some support for OWL via OntModels, is really an RDF-based API. Statements in Jena are just a class representing the triple, and don't themselves represent a resource. If you want to create annotated object property assertions, you'll need to have a look at Section 2.3 Translation of Axioms with Annotations from the OWL 2 Web Ontology Language Mapping to RDF Graphs. Specifically, it looks like 2.3.1 Axioms that Generate a Main Triple is what you'll want:

    If the row of Table 1 corresponding to the type of ax' contains a single main triple s p xlt ., then the axiom ax is translated into the following triples:

    s p xlt .
    _:x rdf:type owl:Axiom .
    _:x owl:annotatedSource s .
    _:x owl:annotatedProperty p .
    _:x owl:annotatedTarget xlt .
    TANN(annotation1, _:x)
    ...
    TANN(annotationm, _:x)
    

    This is the case if ax' is of type … ObjectPropertyAssertion ….

    The referenced Table 1 appears earlier in section 2.1 Translation of Axioms without Annotations.

    So, to add the triple

    Mobile hasCamera 8MP
    

    with the annotation

    hasWeightScore 6.7
    

    you can use the following code:

    import com.hp.hpl.jena.ontology.AnnotationProperty;
    import com.hp.hpl.jena.ontology.Individual;
    import com.hp.hpl.jena.ontology.ObjectProperty;
    import com.hp.hpl.jena.ontology.OntModel;
    import com.hp.hpl.jena.ontology.OntModelSpec;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.Resource;
    import com.hp.hpl.jena.vocabulary.OWL;
    import com.hp.hpl.jena.vocabulary.OWL2;
    
    public class AnnotatedAxioms {
        public static void main(String[] args) {
            final String ns = "http://example.org/";
            final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
            model.setNsPrefix( "ex", ns );
    
            final Individual mobile = model.createIndividual( ns+"Mobile", OWL.Thing );
            final ObjectProperty hasCamera  = model.createObjectProperty( ns+"hasCamera" );
            final Individual eightMP = model.createIndividual( ns+"8MP", OWL.Thing );
    
            final AnnotationProperty hasWeightScore = model.createAnnotationProperty( ns+"hasWeightScore" );
    
            final Resource axiom = model.createResource( OWL2.Axiom );
            axiom.addProperty( OWL2.annotatedSource, mobile );
            axiom.addProperty( OWL2.annotatedProperty, hasCamera );
            axiom.addProperty( OWL2.annotatedTarget, eightMP );
            axiom.addLiteral( hasWeightScore, 6.7 );
    
            model.write( System.out, "RDF/XML-ABBREV" );
        }
    }
    

    which produces the following ontology:

    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:ex="http://example.org/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
      <owl:ObjectProperty rdf:about="http://example.org/hasCamera"/>
      <owl:AnnotationProperty rdf:about="http://example.org/hasWeightScore"/>
      <owl:Axiom>
        <ex:hasWeightScore rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >6.7</ex:hasWeightScore>
        <owl:annotatedTarget>
          <owl:Thing rdf:about="http://example.org/8MP"/>
        </owl:annotatedTarget>
        <owl:annotatedProperty rdf:resource="http://example.org/hasCamera"/>
        <owl:annotatedSource>
          <owl:Thing rdf:about="http://example.org/Mobile"/>
        </owl:annotatedSource>
      </owl:Axiom>
    </rdf:RDF>