test.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 940 行 · 第 1/3 页
JAVA
940 行
package org.mindswap.swoop.debugger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.mindswap.pellet.debug.owlapi.Reasoner;
import org.mindswap.pellet.debug.utils.Timer;
import org.mindswap.swoop.SwoopModel;
import org.mindswap.swoop.reasoner.PelletReasoner;
import org.mindswap.swoop.utils.SetUtils;
import org.mindswap.swoop.utils.owlapi.CorrectedRDFRenderer;
import org.mindswap.swoop.utils.owlapi.OWLOntBuilder;
import org.semanticweb.kaon2.api.DefaultOntologyResolver;
import org.semanticweb.kaon2.api.KAON2Connection;
import org.semanticweb.kaon2.api.KAON2Manager;
import org.semanticweb.kaon2.api.Ontology;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLClassAxiom;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDescription;
import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owl.model.OWLDisjointClassesAxiom;
import org.semanticweb.owl.model.OWLEntity;
import org.semanticweb.owl.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owl.model.OWLException;
import org.semanticweb.owl.model.OWLIndividual;
import org.semanticweb.owl.model.OWLObject;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLObjectPropertyInstance;
import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLObjectVisitor;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLProperty;
import org.semanticweb.owl.model.OWLPropertyDomainAxiom;
import org.semanticweb.owl.model.OWLSameIndividualsAxiom;
import org.semanticweb.owl.model.OWLSubClassAxiom;
import org.semanticweb.owl.model.change.AddEntity;
import org.semanticweb.owl.model.change.AddEquivalentClass;
import org.semanticweb.owl.model.change.AddIndividualClass;
import org.semanticweb.owl.model.change.ChangeVisitor;
import org.semanticweb.owl.model.change.OntologyChange;
import org.semanticweb.owl.model.change.RemoveClassAxiom;
import org.semanticweb.owl.model.change.RemoveDomain;
import org.semanticweb.owl.model.change.RemoveEntity;
import org.semanticweb.owl.model.change.RemoveEquivalentClass;
import org.semanticweb.owl.model.change.RemoveIndividualAxiom;
import org.semanticweb.owl.model.change.RemoveIndividualClass;
import org.semanticweb.owl.model.change.RemoveObjectPropertyInstance;
import org.semanticweb.owl.model.change.RemoveObjectPropertyRange;
import org.semanticweb.owl.model.change.RemoveSuperClass;
import org.semanticweb.owl.model.helper.OntologyHelper;
public class test {
SwoopModel swoopModel = new SwoopModel();
boolean DEBUG = true;
Map entTest = new HashMap();
String NEWLINE = System.getProperty("line.separator");
boolean useRACER, useKAON;
Map axiomMap = new HashMap();
Map signatureMap = new HashMap();
Map usageMap = new HashMap();
int axiomLimit = 40;
String logFile = "";
boolean allMUPS = true;
boolean useTableau = true;
long racerTime = 0;
Timer allMUPSTimer = new Timer("all MUPS");
public void init() throws Exception {
// check for existing entMap
loadMap();
// load ontologies
List testOnt = new ArrayList();
// read local file
// String fname = "testOntologies";
// BufferedReader in = new BufferedReader(new FileReader(new File(fname+".txt")));
// String line = null;
// while (( line = in.readLine()) != null) {
// URI ontURI = new URI(line);
// System.out.println("Loading ontology: "+ontURI);
// OWLOntology ont = swoopModel.loadOntology(ontURI);
// testOnt.add(ont);
// }
// read entire directory Swoop/test/ontologies
String loc = "C:/Documents and Settings/UMD/My Documents/Semantic Web/SWOOP/test/ontologies/onts/";
File dir = new File( loc );
File[] files = dir.listFiles();
for( int i = 0; i < files.length; i++ ) {
File file = files[i];
String fname = file.getAbsolutePath().replaceAll(" ", "%20");
while (fname.indexOf("\\")>=0) {
fname = fname.substring(0, fname.indexOf("\\")) + "/" + fname.substring(fname.indexOf("\\")+1, fname.length());
}
// fname = fname.replaceAll("\\", "/");
System.out.println(fname);
OWLOntology ont = swoopModel.loadOntology(new URI("file:///"+fname));
testOnt.add(ont);
}
System.out.println("DONE: Ontologies Loaded");
// select entailments in ontology
for (Iterator iter = testOnt.iterator(); iter.hasNext();) {
OWLOntology ont = (OWLOntology) iter.next();
this.selectEntailments(ont);
}
System.out.println("DONE: Entailments Selected");
}
public void viewEntailmentMap() throws Exception {
if (entTest.isEmpty()) this.loadMap();
System.out.println("Viewing entailment-test map");
for (Iterator iter = entTest.keySet().iterator(); iter.hasNext();) {
URI ontURI = (URI) iter.next();
Set ents = (HashSet) entTest.get(ontURI);
System.out.println(ontURI+" : "+ents.size());
}
}
public void loadMap() throws Exception {
File file = new File("debugTestEnts.map");
if (file.exists()) {
ObjectInputStream iis = new ObjectInputStream(new FileInputStream(file));
entTest = (HashMap) iis.readObject();
iis.close();
System.out.println("Loaded entailment test map");
}
}
public void writeTestEntsMap() throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("debugTestEnts.map")));
oos.writeObject(entTest);
oos.close();
System.out.println("Writing File: debugTestEnts.map");
}
public void writeOntology(OWLOntology ont) throws Exception {
CorrectedRDFRenderer rdfRend = new CorrectedRDFRenderer();
StringWriter st = new StringWriter();
rdfRend.renderOntology(ont, st);
FileWriter fw = new FileWriter(new File("test.owl"));
fw.write(st.toString());
fw.close();
}
public void writeLogFile() throws Exception {
FileWriter fw = new FileWriter(new File("debugEvalLog.txt"));
fw.write(logFile);
fw.close();
System.out.println("Written log: debugEvalLog.txt");
}
public void selectEntailments(OWLOntology ont) throws Exception {
// pass ont through pellet
PelletReasoner pellet = new PelletReasoner();
System.out.println("Processing ontology "+ont.getURI());
pellet.setOntology(ont);
Set entailments = new HashSet();
OWLClass thing = ont.getOWLDataFactory().getOWLThing();
System.out.println("Checking for unsatisfiable classes"+ont.getURI());
Set unsat = pellet.equivalentClassesOf(ont.getOWLDataFactory().getOWLNothing());
if (unsat.isEmpty()) {
// obtain inferred subsumption and instantiation entailments
System.out.println("No unsatisfiable classes found");
for (Iterator iter = ont.getClasses().iterator(); iter.hasNext();) {
OWLClass cla = (OWLClass) iter.next();
Set infSup = SetUtils.union(pellet.superClassesOf(cla));
Set assSup = cla.getSuperClasses(ont);
infSup.removeAll(assSup);
for (Iterator iter2 = infSup.iterator(); iter2.hasNext();) {
OWLClass sup = (OWLClass) iter2.next();
OWLSubClassAxiom axiom = ont.getOWLDataFactory().getOWLSubClassAxiom(cla, sup);
String sub = "sub("+cla.getURI()+","+((OWLClass) sup).getURI()+")";
if (!sup.equals(thing)) {
entailments.add(sub);
if (DEBUG) System.out.println("Inferred Subclass "+cla+" "+sup);
}
}
}
for (Iterator iter = ont.getIndividuals().iterator(); iter.hasNext();) {
OWLIndividual ind = (OWLIndividual) iter.next();
Set infInst = SetUtils.union(pellet.typesOf(ind));
Set assInst = ind.getTypes(ont);
infInst.removeAll(assInst);
for (Iterator iter2 = infInst.iterator(); iter2.hasNext();) {
OWLDescription desc = (OWLDescription) iter2.next();
// OWLIndividualTypeAssertion axiom = new OWLIndividualTypeAssertionImpl((OWLDataFactoryImpl) ont.getOWLDataFactory(), ind, desc);
String type = "type("+ind.getURI()+","+((OWLClass) desc).getURI()+")";
if (!desc.equals(thing) && !ind.isAnonymous()) {
entailments.add(type);
if (DEBUG) System.out.println("Inferred Instance "+ind+" "+desc);
}
}
}
}
else {
if (DEBUG) System.out.println("Found Unsatisfiable Classes: "+unsat.size());
OWLClass nothing = ont.getOWLDataFactory().getOWLNothing();
for (Iterator iter = unsat.iterator(); iter.hasNext();) {
OWLClass cla = (OWLClass) iter.next();
// OWLSubClassAxiom axiom = ont.getOWLDataFactory().getOWLSubClassAxiom(cla, nothing);
String sub = "sub("+cla.getURI()+","+nothing.getURI()+")";
entailments.add(sub);
}
}
entTest.put(ont.getPhysicalURI(), entailments);
this.writeTestEntsMap();
}
public void runTests() throws Exception {
this.loadMap();
for (Iterator iter = entTest.keySet().iterator(); iter.hasNext();) {
URI ontURI = (URI) iter.next();
Set ents = (HashSet) entTest.get(ontURI);
this.processOntEnts(ontURI, ents);
}
}
public void processOntEnts(URI ontURI, Set ents) throws Exception {
System.out.println("Processing Ontology: "+ontURI);
OWLOntology ont = swoopModel.loadOntology(ontURI);
// select atmost n entailments randomly from ents
Set selEnts = new HashSet(ents);
while (selEnts.size()>10) {
selEnts.remove(selEnts.iterator().next());
}
System.out.println("No. of entailments being tested: "+selEnts.size());
logFile += "Ontology: "+ontURI + NEWLINE;
for (Iterator iter = selEnts.iterator(); iter.hasNext();) {
String ent = (String) iter.next();
System.out.println("Testing entailment: "+ent);
logFile += ent;
OWLClass cla = null;
List remove = new ArrayList();
if (ent.startsWith("sub")) {
// subclass - sub(C1, C2)
String claURI1 = ent.substring(ent.indexOf("(")+1, ent.indexOf(","));
String claURI2 = ent.substring(ent.indexOf(",")+1, ent.indexOf(")"));
OWLClass cla1 = (OWLClass) swoopModel.getEntity(ont, new URI(claURI1), true); //ont.getClass(new URI(claURI1));
cla = cla1;
if (claURI2.indexOf("Nothing")==-1) {
OWLClass cla2 = (OWLClass) swoopModel.getEntity(ont, new URI(claURI2), true); // ont.getClass(new URI(claURI2));
OWLDescription notC2 = ont.getOWLDataFactory().getOWLNot(cla2);
Set and = new HashSet();
and.add(cla1);
and.add(notC2);
OWLDescription andDesc = ont.getOWLDataFactory().getOWLAnd(and);
// add new temp class to ontology equivalent to complex and description
cla = ont.getOWLDataFactory().getOWLClass(new URI(ont.getLogicalURI()+"#tempClass"));
AddEntity ae = new AddEntity(ont, cla, null);
ae.accept((ChangeVisitor) ont);
Set equs = new HashSet();
equs.add(cla);
equs.add(andDesc);
AddEquivalentClass aec = new AddEquivalentClass(ont, cla, andDesc, null);
aec.accept((ChangeVisitor) ont);
// this is another funky key owlapi bug!!! remove both equivalences
OWLEquivalentClassesAxiom equAx = ont.getOWLDataFactory().getOWLEquivalentClassesAxiom(equs);
remove.add(new RemoveClassAxiom(ont, equAx, null));
remove.add(new RemoveEquivalentClass(ont, cla, andDesc, null));
remove.add(new RemoveEntity(ont, cla, null));
}
}
else {
// type assertion - type(I1, C1)
String indURI = ent.substring(ent.indexOf("(")+1, ent.indexOf(","));
String claURI = ent.substring(ent.indexOf(",")+1, ent.indexOf(")"));
OWLIndividual ind = ont.getIndividual(new URI(indURI));
OWLClass type = ont.getClass(new URI(claURI));
OWLDescription notT = ont.getOWLDataFactory().getOWLNot(type);
AddIndividualClass aic = new AddIndividualClass(ont, ind, notT, null);
aic.accept((ChangeVisitor) ont);
remove.add(new RemoveIndividualClass(ont, ind, notT, null));
// add temp class
cla = ont.getOWLDataFactory().getOWLClass(new URI(ont.getLogicalURI()+"#tempClass"));
AddEntity ae = new AddEntity(ont, cla, null);
ae.accept((ChangeVisitor) ont);
AddEquivalentClass aec = new AddEquivalentClass(ont, cla, notT, null);
aec.accept((ChangeVisitor) ont);
Set equs = new HashSet();
equs.add(cla);
equs.add(notT);
OWLEquivalentClassesAxiom equAx = ont.getOWLDataFactory().getOWLEquivalentClassesAxiom(equs);
remove.add(new RemoveClassAxiom(ont, equAx, null));
remove.add(new RemoveEquivalentClass(ont, cla, notT, null));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?