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
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.