Question

I'm using the OWL API for OWL 2.0 and there is one thing I can't seem to figure out. I have an OWL/XML file and I would like to retrieve the annotations for my object property assertions. Here are snippets from my OWL/XML and Java code:

OWL:

<ObjectPropertyAssertion>
  <Annotation>
    <AnnotationProperty abbreviatedIRI="rdfs:comment"/>
    <Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#string">Bob likes sushi</Literal>
  </Annotation>
  <ObjectProperty IRI="#Likes"/>
  <NamedIndividual IRI="#UserBob"/>
  <NamedIndividual IRI="#FoodSushi"/>
</ObjectPropertyAssertion>

Java:

OWLIndividual bob = manager.getOWLDataFactory().getOWLNamedIndividual(IRI.create(base + "#UserBob"));
OWLObjectProperty likes = manager.getOWLDataFactory().getOWLObjectProperty(IRI.create(base + "#Likes"));
OWLIndividual sushi = factory.getOWLNamedIndividual(IRI.create(base + "#FoodSushi"));

OWLObjectPropertyAssertionAxiom ax =  factory.getOWLObjectPropertyAssertionAxiom(likes, bob, sushi);

  for(OWLAnnotation a: ax.getAnnotations()){
    System.out.println(a.getValue());
  }

Problem is, nothing gets returned even though the OWL states there is one rdfs:comment. It has been troublesome to find any documentations on how to retrieve this information. Adding axioms with comments or whatever is not an issue.

Was it helpful?

Solution

In order to retrieve the annotations you need to walk over the axioms of interest. Using the getSomething() adds things to the ontology, as noted in the comments, it is not possible to retrieve your axiom this way. Here is the code adapted from the OWL-API guide:

//Get rdfs:comment
final OWLAnnotationProperty comment = factory.getRDFSComment();

//Create a walker
OWLOntologyWalker walker = 
                        new OWLOntologyWalker(Collections.singleton(ontology));

//Define what's going to visited
OWLOntologyWalkerVisitor<Object> visitor = 
                                new OWLOntologyWalkerVisitor<Object>(walker) {

  //In your case you visit the annotations made with rdfs:comment
  //over the object properties assertions
  @Override
  public Object visit(OWLObjectPropertyAssertionAxiom axiom) {
    //Print them
    System.out.println(axiom.getAnnotations(comment));
    return null;
  }
};

//Walks over the structure - triggers the walk
walker.walkStructure(visitor);      
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top