0
votes

I am looking at extracting classes and subclasses from an owl file. I am using the OwlApi with some guidance from dlquery tutorial example. It works well except for handling entities with reserved characters. I have been advised to use label annotation instead of extracting entities from IRIs, and specifically use the AnnotationValueShortFormProvider instead of SimpleShortFormProvider. Here is my piece of code to retrieve all subclasses. Let's use 'United States' for example as the entity.

private Set<OWLClass> getSubClasses(String cls, boolean direct) {
    if (cls.trim().length() == 0) {
        return Collections.emptySet();
    }
    OWLClassExpression classExpression = this.parser.parseClassExpression(cls);
    NodeSet<OWLClass> subClasses = this.reasoner.getSubClasses(classExpression, direct);
    return subClasses.getFlattened();
}

My parser is set as such:

this.parser = new DLQueryParser(rootOntology, shortFormProvider);

where shortFormProvider is an instance of AnnotationValueShortFormProvider

My question is, how do I instantiate classExpression without parsing the String 'United States', since parsing the string will extract the prefix/token ‘United’? Or is there another sample code block we could use to retrieve subclasses from using label annotations instead of an IRIs?

1

1 Answers

2
votes

If you have a label like 'United States', the Java string should look like "'United States'". Single quotes are used for multi word literal values.

If you have the label value you can also look up directly in the ontology without having to use a Manchester syntax parser. In the same documentation page where you can find the DL query example there are also examples on how to tho this.

for(OWLClass owlClass: o.getClassesInSignature()){
// Get the annotations on the class that use the label property
for (OWLAnnotation annotation : owlClass.getAnnotations(o, dataFactoryf.getRDFSLabel())) {
    if (annotation.getValue() instanceof OWLLiteral) {
        OWLLiteral val = (OWLLiteral) annotation.getValue();
        if (val.getLiteral().equals(inputLabel)) {
            // at this point, the owlClass variable is the OWLClass you were looking for
            NodeSet<OWLClass> subClasses = this.reasoner.getSubClasses(owlClass, direct);
            return subClasses.getFlattened();
        }
    }
}
}

https://github.com/owlcs/owlapi/wiki/Documentation