事实上,我使用带有 OWL API 的 java prgramation 在 OWL2 语言上定义了一个本体。我将所需的 jar 集成到我的项目中以使用推理引擎 Pellet。我的问题是我如何在我的本体中检测逻辑上等价的概念组?这里是我使用 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;
}
我需要 Pellet 的 Java 代码,它允许检测组逻辑上等效的概念。我会很感激你的帮助。提前致谢