Question

In fact, I make an ontology defined on OWL2 language using java prgramation with OWL API. I integrate the required jar in my project to use the inference engine Pellet. My question is how I detect in my ontology groups of concepts that are logically equivalent? here the code which I use Pellet.

 import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StreamDocumentTarget;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyChange;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
import org.mindswap.pellet.KnowledgeBase;







/**
 *
 * @author hela
 */
public class Owl {

  public  void createNewOnto(List<String[][]> cps, LinkedList<Map<String, String>> rel, String uri ) throws OWLOntologyCreationException,
        OWLOntologyStorageException {
     OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLDataFactory factory = manager.getOWLDataFactory();
        IRI iri = IRI.create("http://www.co-ode.org/ontologies/Annot2Onto.owl");
        OWLOntology ontology = manager.createOntology(iri); 

OWLObjectProperty subTopicOf =factory.getOWLObjectProperty(IRI.create(iri+"/#sub-topicOf"));
OWLObjectProperty kindOf =factory.getOWLObjectProperty(IRI.create(iri+"/#kindOf"));
OWLClass thing = factory.getOWLClass(IRI.create(iri+"/#OWLThing"));
  manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(thing));
 Set<OWLAxiom> genders = new HashSet<OWLAxiom>();
 for(Map<String, String> rmp : rel){
     Set<OWLNamedIndividual> classes =ontology.getIndividualsInSignature();
  List< OWLNamedIndividual> listc = new ArrayList(classes);
   IRI ir = IRI.create(iri+"/#"+rmp.get("concept1"));
    OWLNamedIndividual c1=null;

 if(ontology.containsClassInSignature(ir)){
     int i=0;

     while(i<listc.size()&& c1==null){
         if(listc.get(i).toString().compareTo("<"+ir.toString()+">")==0){
             c1=listc.get(i);

              manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(c1));
             manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(thing, c1));
         }


         i++;
     }
 }

 else {
      c1 = factory.getOWLNamedIndividual(IRI.create(iri+"/#"+rmp.get("concept1")));

        //manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(c1));
        manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(thing, c1));
 }

 IRI ir2 = IRI.create(iri+"/#"+rmp.get("concept2"));
    OWLNamedIndividual c2=null;
 if(ontology.containsIndividualInSignature(ir2)){
     int i=0;

     while(i<listc.size()&& c2==null){

         if(listc.get(i).toString().compareTo("<"+ir2.toString()+">")==0){
             c2=listc.get(i);
              System.out.println("concept2 = "+c2.toString());
             manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(c2));
               manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(thing, c2));
         }
         i++;
     }
 }

 else{ 
      c2 = factory.getOWLNamedIndividual(IRI.create(iri+"/#"+rmp.get("concept2")));

       //manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(c2));
        manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(thing, c2));
 }

if(rmp.get("relation").compareTo("kind of")==0){
//domainAxiom = factory.getOWLObjectPropertyDomainAxiom(sorteDe,c1);
//rangeAxiom = factory.getOWLObjectPropertyRangeAxiom(sorteDe,c2);

genders.add(factory.getOWLObjectPropertyAssertionAxiom(kindOf, c1,
                c2));
}

else{

  genders.add(factory.getOWLObjectPropertyAssertionAxiom(subTopicOf, c1,c2));


}

    String[][] cp1 = this.getConcept(cps,rmp.get("concept1"));
    String[][] cp2 = this.getConcept(cps,rmp.get("concept2") );
    cps.remove(cp2);
    cps.remove(cp1);
    // Now we apply the change using the manager.
    //manager.applyChange(addAxiom1);
 }
    List<OWLOntologyChange> la=manager.addAxioms(ontology, genders);
    manager.applyChanges(la);

for(String[][] ct: cps){
    OWLNamedIndividual res=factory.getOWLNamedIndividual(IRI.create(iri+"/#"+ct[0][0]));
       manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(res));
      manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(thing, res));

}

File file = new File(uri+"/Annot2Onto.owl");
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createNonBufferingReasoner( ontology );
      manager.addOntologyChangeListener( reasoner );
reasoner.flush();
System.out.println(reasoner.isConsistent());
KnowledgeBase kb = reasoner.getKB();
kb.get
    manager.saveOntology(ontology, IRI.create(file.toURI()));
    manager.saveOntology(ontology, new StreamDocumentTarget(System.out));

}
  public String[][] getConcept(List<String[][]> cps, String s){
      String[][] cp =null;
      int i=0;
      while((i<cps.size()) && (cp==null) ){
          if(cps.get(i)[0][0].compareTo(s)==0)
              cp=cps.get(i);
      i++;
      }
      return cp;
  }

I need the java code for Pellet that allows to detect groups logically equivalent concepts. I will be grateful for your help. Thanks in advance

Was it helpful?

Solution

In OWLAPI, all results from OWLReasoner that are of type Node and NodeSet consist of sets of equivalent entities.

For OWLNamedIndividual, OWLReasoner::getSameIndividuals() returns a Node<OWLNamedIndividual> object, which contains all individuals inferred to be sameAs each other.

It is also possible to ask for the instances of a class: OWLReasoner::getInstances() will return a NodeSet<OWLIndividual>, which is a collection of Node objects, each corresponding to a set of OWLNamedIndividual objects that are also sameAs each other.

The same applies to sub/super/equivalent classes and properties.

Edit to include comments:

To get all named individuals in an ontology, grouped by sameAs in equivalence classes, an approach is to ask for all instances of owl:Thing

NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(dataFactory.getOWLThing(), false);

A NodeSet contains Nodes (in no particular order); each Node contains OWLNamedIndividual objects, in no particular order. All individuals in the same node are sameAs each other, i.e., each one can be considered a representative for its equivalence class. There is no notion of a canonical representative.

Similarly, to get all classes of equivalence for OWLClass

NodeSet<OWLClass> classes = reasoner.getSubClasses(dataFactory.getOWLThing(), false);

This is a collection of Node<OWLClass>, each of which contains equivalent classes. The order in which the Nodes are found in the NodeSet, and the order of the OWLClasses in a Node, play no role and might change from one call to the next.

From the OWLReasoner javadoc

Nodes The reasoner interface contains methods that return NodeSet objects. These are sets of Node objects. A Node contains entities. For a Node<OWLClass> of classes, each class in the node is equivalent to the other classes in the Node with respect to the imports closure of the root ontology. For a Node<OWLObjectProperty> of object properties, each object property in the Node is equivalent to the other object properties in the node with respect to the imports closure of the root ontology. For a Node of data properties, each data property in the Node is equivalent to the other data properties in the node with respect to the imports closure of the root ontology. For a Node of named individuals, each individual in the node is the same as the other individuals in the node with respect to the imports closure of the root ontology.

OTHER TIPS

Note: The approach in this answer is OK, but it's based on the mistaken assumption that the Jena API was being used (not much code was shown in the original version of the question).

This is really more of a question about how to use Jena's API for working with models, since Pellet can be used with other APIs (e.g, the OWLAPI) and how you'd retrieve this information will be different in those cases. The code you've shown so far, though, appears to be using Jena. The main approaches to consider here are:

  • listing all pairs of equivalent classes
  • for some particular class, list all the equivalent classes

The code below shows how to do both. Note that it's not strictly correct to say that you're listing all equivalent classes, because there are infinitely many of those. E.g.,

A ≡ A ⊓ ⊤ ≡ A ⊔ ⊥ ≡ A ⊓ A ⊓ ⊤

Strictly speaking, those are class expressions, and that's what there can be more than one of. Once you've determined, e.g., A ≡ B, there's really just one class, there just happen to be two class expressions that denote that class. It's sort of like asking, "what numbers are equal to 2?" The answer is "only 2", whereas to the question "what arithmetic expressions have the value 2?" the answer to that is {2, 1+1, 2 ×1, 4/2, …}. I only point this out because I'm not sure which results Pellet will give you, though I expect that it will just look for class expressions that are already present in your ontology.

import org.mindswap.pellet.jena.PelletReasonerFactory;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.OWL;

public class GetEquivalentClassesExample {
    public static void main(String[] args) {
        /*
         * Create an OntModel with an attached Pellet reasoner.
         */
        OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );

        /*
         * Load your data.
         */
        model.read( "..." );

        /*
         * To list all pairs of equivalent classes, you can just list
         * the statements with the property owl:equivalentClass
         */
        StmtIterator s = model.listStatements( null, OWL.equivalentClass, (RDFNode) null );
        while ( s.hasNext() ) { 
            System.out.println( s.next() );
        }

        /*
         * If you just want the classes that are equivalent to some particular
         * class, you can get a reference to that class, and then ask for its
         * equivalent classes.
         */
        OntClass klass = model.getOntClass( "..." );
        ExtendedIterator<OntClass> c = klass.listEquivalentClasses();
        while ( c.hasNext() ) {
            System.out.println( c.next() );
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top