mupsfinder.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 943 行 · 第 1/3 页
JAVA
943 行
OWLDescription desc = (OWLDescription) iter2.next();
if (desc instanceof OWLClass)
newDisSet.add(copyOnt.getClass(((OWLClass) desc).getURI()));
else
newDisSet.add(desc);
}
OWLDisjointClassesAxiom newDis = copyOnt.getOWLDataFactory().getOWLDisjointClassesAxiom(newDisSet);
RemoveClassAxiom r = new RemoveClassAxiom(copyOnt, (OWLClassAxiom) newDis, null);
r.accept((ChangeVisitor) copyOnt);
}
else if (axiom instanceof OWLEquivalentClassesAxiom) {
OWLEquivalentClassesAxiom equ = (OWLEquivalentClassesAxiom) axiom;
Set equSet = equ.getEquivalentClasses();
Set newEquSet = new HashSet();
List equList = new ArrayList();
for (Iterator iter2 = equSet.iterator(); iter2.hasNext();) {
OWLDescription desc = (OWLDescription) iter2.next();
if (desc instanceof OWLClass) {
newEquSet.add(copyOnt.getClass(((OWLClass) desc).getURI()));
equList.add(copyOnt.getClass(((OWLClass) desc).getURI()));
}
else {
newEquSet.add(desc);
equList.add(desc);
}
}
OWLEquivalentClassesAxiom newEqu = copyOnt.getOWLDataFactory().getOWLEquivalentClassesAxiom(newEquSet);
RemoveClassAxiom r = new RemoveClassAxiom(copyOnt, (OWLClassAxiom) newEqu, null);
r.accept((ChangeVisitor) copyOnt);
if (equList.size()==2) {
OWLDescription desc1 = (OWLDescription) equList.get(0);
OWLDescription desc2 = (OWLDescription) equList.get(0);
if (desc1 instanceof OWLClass) {
RemoveEquivalentClass re = new RemoveEquivalentClass(copyOnt, (OWLClass) desc1, desc2, null);
re.accept((ChangeVisitor) copyOnt);
}
if (desc2 instanceof OWLClass) {
RemoveEquivalentClass re = new RemoveEquivalentClass(copyOnt, (OWLClass) desc2, desc1, null);
re.accept((ChangeVisitor) copyOnt);
}
}
}
else if (axiom instanceof OWLSubClassAxiom) {
OWLSubClassAxiom subA = (OWLSubClassAxiom) axiom;
OWLDescription sub = subA.getSubClass();
OWLDescription sup = subA.getSuperClass();
OWLDescription newSub = sub;
if (sub instanceof OWLClass) newSub = copyOnt.getClass(((OWLClass) sub).getURI());
OWLDescription newSup = sup;
if (sup instanceof OWLClass) newSup = copyOnt.getClass(((OWLClass) sup).getURI());
OWLSubClassAxiom newSubA = copyOnt.getOWLDataFactory().getOWLSubClassAxiom(newSub, newSup);
OntologyChange r = new RemoveClassAxiom(copyOnt, (OWLClassAxiom) newSubA, null);
r.accept((ChangeVisitor) copyOnt);
if (newSub instanceof OWLClass) {
r = new RemoveSuperClass(copyOnt, (OWLClass) newSub, newSup, null);
r.accept((ChangeVisitor) copyOnt);
}
}
else if (axiom instanceof OWLPropertyDomainAxiom) {
OWLPropertyDomainAxiom opd = (OWLPropertyDomainAxiom) axiom;
OWLProperty prop = opd.getProperty();
OWLProperty newProp = null;
if (prop instanceof OWLDataProperty) newProp = copyOnt.getDataProperty(prop.getURI());
else newProp = copyOnt.getObjectProperty(prop.getURI());
OWLDescription desc = opd.getDomain();
OWLDescription newDesc = desc;
if (desc instanceof OWLClass) newDesc = copyOnt.getClass(((OWLClass) desc).getURI());
RemoveDomain rd = new RemoveDomain(copyOnt, newProp, newDesc, null);
rd.accept((ChangeVisitor) copyOnt);
}
else if (axiom instanceof OWLObjectPropertyRangeAxiom) {
OWLObjectPropertyRangeAxiom opd = (OWLObjectPropertyRangeAxiom) axiom;
OWLObjectProperty prop = opd.getProperty();
OWLObjectProperty newProp = copyOnt.getObjectProperty(prop.getURI());
OWLDescription desc = opd.getRange();
OWLDescription newDesc = desc;
if (desc instanceof OWLClass) newDesc = copyOnt.getClass(((OWLClass) desc).getURI());
RemoveObjectPropertyRange ropr = new RemoveObjectPropertyRange(copyOnt, newProp, newDesc, null);
ropr.accept((ChangeVisitor) copyOnt);
}
else if (axiom instanceof OWLObjectPropertyInstance) {
OWLObjectPropertyInstance oop = (OWLObjectPropertyInstance) axiom;
OWLIndividual sub = oop.getSubject();
if (sub.isAnonymous()) {
sub = copyOnt.getOWLDataFactory().getAnonOWLIndividual(sub.getAnonId());
}
else {
sub = copyOnt.getIndividual(sub.getURI());
}
OWLObjectProperty prop = copyOnt.getObjectProperty(oop.getProperty().getURI());
OWLIndividual obj = copyOnt.getIndividual(oop.getObject().getURI());
RemoveObjectPropertyInstance ropi = new RemoveObjectPropertyInstance(copyOnt, sub, prop, obj, null);
ropi.accept((ChangeVisitor) copyOnt);
}
else if (axiom instanceof OWLSameIndividualsAxiom) {
OWLSameIndividualsAxiom osi = (OWLSameIndividualsAxiom) axiom;
Set newInd = new HashSet();
for (Iterator it = osi.getIndividuals().iterator(); it.hasNext();) {
newInd.add(copyOnt.getIndividual(((OWLIndividual) it.next()).getURI()));
}
OWLSameIndividualsAxiom copyInd = copyOnt.getOWLDataFactory().getOWLSameIndividualsAxiom(newInd);
RemoveIndividualAxiom ria = new RemoveIndividualAxiom(copyOnt, copyInd, null);
ria.accept((ChangeVisitor) copyOnt);
}
else if (axiom instanceof OWLDifferentIndividualsAxiom) {
OWLDifferentIndividualsAxiom osi = (OWLDifferentIndividualsAxiom) axiom;
Set newInd = new HashSet();
for (Iterator it = osi.getIndividuals().iterator(); it.hasNext();) {
newInd.add(copyOnt.getIndividual(((OWLIndividual) it.next()).getURI()));
}
OWLDifferentIndividualsAxiom copyInd = copyOnt.getOWLDataFactory().getOWLDifferentIndividualsAxiom(newInd);
RemoveIndividualAxiom ria = new RemoveIndividualAxiom(copyOnt, copyInd, null);
ria.accept((ChangeVisitor) copyOnt);
}
//TODO: more removal!
// test if copyOnt has changed
//FIXME: not working when individual obj prop assertions are actually removed
// if (copyOnt.equals(onto)) {
// System.out.println("Ontology hasn't changed after removing axiom "+axiom);
// continue;
// }
// get class in copyOnt
cla = copyOnt.getClass(cla.getURI());
// early path termination
boolean earlyTermination = false;
for (Iterator i=satPaths.iterator(); i.hasNext();) {
Set satPath = (HashSet) i.next();
if (satPath.containsAll(currPath)) {
System.out.println("EARLY PATH TERMINATION!");
earlyTermination = true;
break;
}
}
if (!earlyTermination) {
// check if there is a new mups of class
Set newMUPS = new HashSet();
String expl = "";
if (testTimer.isStarted()) testTimer.stop();
if (useTableau) {
// use tableau tracing
newMUPS = this.getTableauSOS(copyOnt, cla);
}
else {
// use black box
newMUPS = this.getBlackBoxSOS(copyOnt, cla);
}
testTimer.start();
if (!newMUPS.isEmpty()) {
if (!MUPS.contains(newMUPS)) {
// print explanation for new MUPS
MUPS.add(newMUPS);
explStr.add(expl);
System.out.println("FOUND NEW MUPS - MUPS COUNT: "+MUPS.size());
// recurse!
HSTMUPS(newMUPS, copyOnt, cla, MUPS, explStr, satPaths, currPath);
}
}
else {
satPaths.add(new HashSet(currPath));
}
}
currPath.remove(axiom);
if (testTimer.isStarted()) testTimer.stop();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
if (testTimer.isStarted()) testTimer.stop();
}
}
public void cleanLog() throws Exception {
BufferedReader in = new BufferedReader(new FileReader(new File("debugEvalLog.txt")));
String line = null;
String newLog = "";
swoopModel.setShowQNames(false);
while (( line = in.readLine()) != null) {
if (line.indexOf("(")!=-1) {
int token1Start = line.indexOf("(");
int token1End = line.indexOf(",");
int token2End = line.indexOf(")");
String token1 = line.substring(token1Start+1, token1End);
String token2 = line.substring(token1End+1, token2End);
line = (line.substring(0, token1Start) +
swoopModel.shortForm(new URI(token1)) + "," +
swoopModel.shortForm(new URI(token1)) +
line.substring(token2End));
}
newLog += line + NEWLINE;
}
logFile = newLog;
System.out.print("Cleaned file..");
this.writeLogFile();
}
public Set getTableauSOS(OWLOntology ont, OWLDescription clazz) {
try {
Reasoner pelletDebug = new Reasoner();
pelletDebug.setOntology(ont);
pelletDebug.getKB().setDoExplanation(true);
pelletDebug.getKB().doDependencyTracking = true;
boolean consistent = true;
Timer timers = new Timer("Pellet Debugging Check");
timers.start();
if (clazz!=null) consistent = pelletDebug.isConsistent(clazz);
else consistent = pelletDebug.isConsistent();
timers.stop();
System.out.println(timers);
if (consistent) {
// no SOS cos ABox is consistent!
System.out.println("No SOS since ABox is consistent");
return new HashSet();
}
Set explanationSet = pelletDebug.getKB().getExplanationSet();
// prune the axioms in case there are additional axioms
Set prunedSet = new HashSet(explanationSet);
for (Iterator iter = explanationSet.iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
prunedSet.remove(axiom);
boolean sat = false;
sat = this.checkSatisfiability(prunedSet, clazz);
if (sat) prunedSet.add(axiom);
}
explanationSet = prunedSet;
// end of pruning
return explanationSet;
}
catch (OWLException ex) {
ex.printStackTrace();
}
return new HashSet();
}
public boolean checkSatisfiability(Set axioms, OWLDescription clazz) {
// create a new ontology with axioms
// and check satisfiability of clazz
boolean sat = false;
try {
OWLOntBuilder ontBuilder = new OWLOntBuilder();
// create new ontology using axioms
// use OWLOntBuilder to build a new ontology given axioms
for (Iterator iter = axioms.iterator(); iter.hasNext();) {
OWLObject obj = (OWLObject) iter.next();
obj.accept(ontBuilder);
}
// if clazz is not in ontology, return true
OWLOntology newOnt = ontBuilder.currentOnt;
if (clazz!=null && clazz instanceof OWLClass && newOnt.getClass(((OWLClass) clazz).getURI())==null) return true;
else if (clazz!=null) {
// get clazz in newOnt
clazz = ontBuilder.visitDescription(clazz);
}
// create new instance of pellet and check sat. of clazz
PelletReasoner newPellet = new PelletReasoner();
newPellet.setOntology(newOnt, false);
if (clazz!=null) sat = newPellet.isConsistent(clazz);
else sat = newPellet.isConsistent();
}
catch (Exception ex) {
System.out.println(ex.getMessage()); // clazz (description) may not be in ontology!
// ex.printStackTrace();
return true;
}
return sat;
}
public void removeOnt() throws Exception {
if (entTest.isEmpty()) this.loadMap();
entTest.remove(new URI("http://sweet.jpl.nasa.gov/ontology/earthrealm.owl"));
this.writeTestEntsMap();
}
public static void main(String[] args) {
try {
MUPSFinder t = new MUPSFinder();
//*** part 1: run through test ontologies, collect interesting entailments,
// i.e., unsat classes and inferred subsumption/instantiation
// and store them in a map - entTest..write to file "debugTestEnts.map"
// t.init();
// t.removeOnt();
// t.viewEntailmentMap();
//*** part 2: read debugTestEnts map, for each ontology, select 20 entailments randomly
// and run through various reasoners, collect timing information and store in logFile
// write to "debugEvalLog.txt"
t.runTests();
t.cleanLog();
// System.out.println(t.logFile);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?