I need to get All direct superclasses of one class from the ontology, and I need just named classes, not those gotten from equivalent expression with 'AllValuesFrom' restriction. I tried with the code below, it works for some ontologies that I create but others downloaded from the web (people ontology - pizza ontology) it doesn't work.
public void motherclasses_Of_One_class() {
for (OWLClass clss : ontology.getClassesInSignature())
{
if(reasoner.getSuperClasses(clss, true).getFlattened().size()>1) // if the class has more than one mother class
{// System.out.println(" \n ---------------- : \n");
System.out.println("\n class "+clss.getIRI().getFragment()+" has more than one mother classes : \n");
for(OWLClass parent: reasoner.getSuperClasses(clss, true).getFlattened())
System.out.println(parent.getIRI().getFragment());
}
}
}
I tried with this version of code too, the same result as the first version
NodeSet<OWLClass> superclasses = reasoner.getSuperClasses(clss, true);
for (org.semanticweb.owlapi.reasoner.Node<OWLClass> parentOWLNode: superclasses) {
OWLClassExpression parent = parentOWLNode.getRepresentativeElement();
System.out.println(parent.getClassesInSignature());
}
the problem with downloaded ontologies, that it returns wrong superclasses for a class. I check the .OWL file, then the ontology through protégé, I can't find from when the problem comes.
Please find below a wrong case to understand more what I mean.
In the example, 'cat_owner' class has just one mother 'person' class. As you can see, pet_owner and cat_liker classes are in the same hierarchical level as 'cat_owner' class, they can never be mothers for 'cat_owner' class, and more than that in the description of 'cat_owner' class there is just one superclass 'person' class... but in the program output I get them as superclasses of 'cat_owner' class when 'person' class is absent form the list. I can't understand why...
this is the output :
Please if you have any idea that may help, I would be grateful. Thank you
Your code is correct. As described in the comments (which could be answers, really), you're assuming that cat owner
cannot have cat liker
as superclass, but I don't believe the data you've shown proves that.
The hierarchical level for me is the basic graph order, parsing concepts from top to bottom without taking into account semantic relationships and inference.
If you want to navigate the classes without doing inference, you should not be using a reasoner for that purpose. You can navigate the asserted hierarchy by checking the subclass axioms in the ontology.