owlcs / owlapi

OWL API main repository
822 stars 315 forks source link

How to get a class within a SubClass #1036

Closed rwynne closed 2 years ago

rwynne commented 2 years ago

Below is an inferred view of subclasses for SNOMED class Atorvastatin calcium (substance)

Class Pyrrole (substance) is displayed twice, however would like to get the class Atorvastatin (substance) for predicate Is modification of (attribute) from the "first" Pyrrole class.

How would I do this with the API? Should it be handled like an OWLNaryBooleanClassExpression as if it were an EquivalentClass? My dependencies are currently the ELK reasoner 0.5.0-SNAPSHOT and owlapi-distribution 5.1.10.

image

syntax: SubClassOf(:108601004 ObjectIntersectionOf(:115667008 :373267003 ObjectSomeValuesFrom(:726542003 :734592007) ObjectSomeValuesFrom(:738774007 :373444002)))

Wishing you all a Happy New Year.

ykazakov commented 2 years ago

Something like this:

OWLOntologyManager man = OWLManager.createOWLOntologyManager();
OWLDataFactory f = man.getOWLDataFactory();
OWLClass cls = f.getOWLClass(IRI.create(":108601004"));
OWLObjectProperty p = f.getOWLObjectProperty(IRI.create(":738774007"));
List<OWLClassExpression> result = new ArrayList<>(); // the list of all fillers for the property
for (OWLSubClassOfAxiom ax : ont.getSubClassAxiomsForSubClass(cls)) {
    OWLClassExpression sup = ax.getSuperClass();
    if (sup instanceof OWLObjectIntersectionOf) {
        OWLObjectIntersectionOf inters = (OWLObjectIntersectionOf) sup; 
        for (OWLClassExpression conj : inters.getOperands()) {
            if (conj instanceof OWLObjectSomeValuesFrom) {
                OWLObjectSomeValuesFrom exist = (OWLObjectSomeValuesFrom) conj;
                if (exist.getProperty().equals(p)) {
                    result.add(exist.getFiller());
                }
            }
        }
    }
}

Note: the inferred information is only shown with the yellow background. Since you do not need to consider this part, you do not need to use the reasoner.

rwynne commented 2 years ago

Exactly what I needed. Thanks @ykazakov !