Search code examples
javaontologyowl-api

How to retrieve the set of all classes that have necessary and sufficient restrictions


In OWL API we can retrieve:

  • the subclasses of a specified class
  • the superclasses of a specified class
  • the set of classes that are equivalent to a specified class

What i want is to retrieve from an ontology the set of all Defined classes (classes that has sufficient restriction, i.e., non primitive classes).

Kindly how can we do that?

for example:

OWLClass CheesyPizza = factory.getOWLClass(":CheesyPizza",pm); 
OWLClassExpression hasCheese_some_restriction = 
            factory.getOWLObjectSomeValuesFrom(hasTopping, CheeseTopping);
OWLClassExpression Pizza_hasCheeseTopping_intersection =
            factory.getOWLObjectIntersectionOf(Pizza, hasCheese_some_restriction);
OWLEquivalentClassesAxiom CheesyPizza_Definition = 
            factory.getOWLEquivalentClassesAxiom(CheesyPizza, Pizza_hasCheeseTopping_intersection);
tempChange.add(new AddAxiom(localOntology, CheesyPizza_Definition));
managerLocal.applyChanges(tempChange);

Cheesy Pizza is a Defined class, lets say we have in the Ontology many other Defined Classes (classes that has the necessary and sufficient). I want to get Those classes (In other words i want to get the classes that have three lines in the yellow icon in protege).

I understand that there is no short hand function to do that. Therefore i added this section 2 of code:

OWLOntologyWalker walker = new OWLOntologyWalker(Collections.singleton(localOntology));
OWLOntologyWalkerVisitorEx<Object> visitor = new OWLOntologyWalkerVisitorEx<Object>(walker)
{
 @Override
 public Object visit(OWLEquivalentClassesAxiom ce)
  {
  System.out.println(" " + getCurrentAxiom());
// this will print the relating axioms EquivalentClasses(<http://www.semanticweb.org/ontologies/myontology/CheesyPizza> ObjectIntersectionOf(<http://www.semanticweb.org/ontologies/myontology/Pizza> ObjectSomeValuesFrom(<http://www.semanticweb.org/ontologies/myontology/hasTopping> <http://www.semanticweb.org/ontologies/myontology/CheeseTopping>)) )
// this axioms is correct and it is the axiom we want
// now lets try to get our Class CheesyPizza!
  for (OWLClass clazz : getCurrentAxiom().getClassesInSignature())  
  {
  if(! clazz.isAnonymous())
  System.out.println(clazz);
// this is printing 
//<http://www.semanticweb.org/ontologies/myontology/CheesyPizza>
//<http://www.semanticweb.org/ontologies/myontology/CheeseTopping>
//<http://www.semanticweb.org/ontologies/myontology/Pizza>
  }
}

I just want to get CheesyPizza, Cheesy Pizza is defined as shown in the 1st section of code, as an equivalent to an anonymous class of the intersection between the class pizza and hasCheese_some_restriction.

how to get the CheesyPizza class (its the only class that is not anonymous and has OWLEquivalentClassesAxiom attached to it. Why is this so complicated? :(


Solution

  • The OWLOntology::getAxioms(OWLClass c) - or OWLOntology::axioms(OWLClass c) on version 5 - should do what you're looking for.