Search code examples
semantic-webowl

Using OWL API, given an OWLClass, how can I get <rdfs:label> of it?


Using OWL API 3.4.9.

Given an OWLClass and on ontology, how can I get <rdfs:label> of that OWLClass in that ontology?

I hope to get the label in the type of String.


Solution

  • Inspired from the guide to the OWL-API, the following code should work (not tested):

    //Initialise
    OWLOntologyManager m = create();
    OWLOntology o = m.loadOntologyFromOntologyDocument(pizza_iri);
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    
    //Get your class of interest
    OWLClass cls = df.getOWLClass(IRI.create(pizza_iri + "#foo"));
    
    // Get the annotations on the class that use the label property (rdfs:label)
    for (OWLAnnotation annotation : cls.getAnnotations(o, df.getRDFSLabel())) {
      if (annotation.getValue() instanceof OWLLiteral) {
        OWLLiteral val = (OWLLiteral) annotation.getValue();
        // look for portuguese labels - can be skipped
          if (val.hasLang("pt")) {
            //Get your String here
            System.out.println(cls + " labelled " + val.getLiteral());
          }
       }
    }