mupsfinder.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,003 行 · 第 1/3 页
JAVA
1,003 行
// System.out.println("calling RACER");
// sos = this.getBlackBoxSOS(ont, cla);
// System.out.println("sos size: "+sos.size());
// MUPS.add(sos);
// if (allMUPS) HSTMUPS(sos, ont, cla, MUPS, new ArrayList(), new HashSet(), new HashSet());
// System.out.println("time: "+(testTimer.getTotal()+racerTime)+" mups size: "+MUPS);
// logFile += " "+String.valueOf(testTimer.getTotal()+racerTime) + " ("+MUPS.size()+")";
// racerTime = 0; //***** key
// ** KAON2
// useKAON = true;
// useRACER = false;
// System.out.println("calling KAON2");
// sos = this.getBlackBoxSOS(ont, cla);
// System.out.println("sos size: "+sos.size());
// MUPS = new ArrayList();
// MUPS.add(sos);
// if (allMUPS) HSTMUPS(sos, ont, cla, MUPS, new ArrayList(), new HashSet(), new HashSet());
// System.out.println("time: "+testTimer.getTotal()+" mups size: "+MUPS);
// logFile += " "+testTimer.getTotal() + " ("+MUPS.size()+")";
// ** Pellet
useRACER = false;
useKAON = false;
System.out.println("calling Pellet");
sos = this.getBlackBoxSOS(ont, cla);
System.out.println("sos size: "+sos.size());
MUPS = new ArrayList();
MUPS.add(sos);
if (allMUPS) HSTMUPS(sos, ont, cla, MUPS, new ArrayList(), new HashSet(), new HashSet());
System.out.println("time: "+testTimer.getTotal()+" mups size: "+MUPS);
logFile += " "+testTimer.getTotal() + " ("+MUPS.size()+")";
}
public Set getBlackBoxSOS(OWLOntology ont, OWLClass cla) throws Exception {
Set sos = new HashSet();
// reset all maps and set limit
axiomLimit = 40;
usageMap.clear();
axiomMap.clear();
signatureMap.clear();
testTimer = new Timer("total");
testTimer.start();
// add axioms related to class
Set axioms = swoopModel.getAxioms(cla, ont);
axiomMap.put(cla.getURI(), axioms);
OWLOntBuilder ob = new OWLOntBuilder();
ob.addAxiom = true; // add axiom mode set to true
// add all base axioms to testOnt via ob
for (Iterator iter = axioms.iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
axiom.accept(ob);
}
// toggle a variable that considers entity usage while expanding axiom set
boolean expandMore = false;
testTimer.stop();
// add linked references iteratively
while (checkSatisfiability(ob, cla)) {
testTimer.start();
Set newAxioms = this.expandAxiomSet(axioms, ont, expandMore);
// System.out.println("Size of axioms: "+axioms.size());
// add axioms from latest to testOnt
for (Iterator it = newAxioms.iterator(); it.hasNext();) {
OWLObject axiom = (OWLObject) it.next();
axiom.accept(ob);
}
if (newAxioms.isEmpty() && expandMore) {
System.out.println("ERROR: Could not find axioms responsible for error!");
testTimer.stop();
return sos;
}
else if (newAxioms.isEmpty()) {
expandMore = true;
}
// System.out.println(axioms);
testTimer.stop();
}
// now axioms contains cla unsatisfiable
// remove one axiom at a time and if it turns satisfiable, add it to sos
// System.out.println("Found concept unsatisfiable: #axioms = "+axioms.size());
testTimer.start();
// fast pruning
List axiomList = new ArrayList(axioms);
int pruneWindow = 10;
if (axiomList.size()>pruneWindow) {
axioms.clear();
int parts = axiomList.size() / pruneWindow;
for (int part=0; part<parts; part++) {
for (int i=part*pruneWindow; i<part*pruneWindow+pruneWindow; i++) {
ob.addAxiom = false;
((OWLObject)axiomList.get(i)).accept(ob);
}
testTimer.stop();
if (checkSatisfiability(ob, cla)) {
testTimer.start();
for (int i=part*pruneWindow; i<part*pruneWindow+pruneWindow; i++) {
axioms.add(axiomList.get(i));
ob.addAxiom = true;
((OWLObject)axiomList.get(i)).accept(ob);
}
testTimer.stop();
}
testTimer.start();
}
if (axiomList.size()>parts*pruneWindow) {
// add remaining from list to axioms
for (int i=parts*pruneWindow; i<axiomList.size(); i++) {
axioms.add(axiomList.get(i));
}
}
}
// slow pruning
for (Iterator iter = new HashSet(axioms).iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
axioms.remove(axiom);
ob.addAxiom = false;
axiom.accept(ob);
testTimer.stop();
if (checkSatisfiability(ob, cla)) {
testTimer.start();
sos.add(axiom);
axioms.add(axiom);
ob.addAxiom = true;
axiom.accept(ob);
testTimer.stop();
}
testTimer.start();
}
testTimer.stop();
return sos;
}
private boolean checkSatisfiability(OWLOntBuilder ob, OWLDescription clazz) throws Exception {
// check satisfiability of clazz in ont in ob
OWLOntology newOnt = ob.currentOnt;
boolean sat = false;
boolean checkConsistency = false;
if (clazz == null) {
checkConsistency = true;
}
else {
// check if clazz is not present at all
if (clazz instanceof OWLClass && newOnt.getClass(((OWLClass) clazz).getURI())==null) return true;
clazz = ob.visitDescription(clazz);
}
// use reasoner to check class consistency
String fname = "file:///C:/Docume~1/UMD/MyDocu~1/Semant~1/SWOOP/test.owl";
// if (useRACER) {
// // communicate with Racer using JRacer API
// try {
// // write ontology locally!
// this.writeOntology(newOnt);
//
// // communicate with RACER
// RacerServer racer = new RacerServer("localhost",8088);
// racer.openConnection();
//// racer.send("(logging-on)");
// racer.send("(owl-read-document \""+fname+"\")");
// String response = "";
// PrintStream temp = System.out;
// System.setOut(new PrintStream(new FileOutputStream(new File("time.txt"))));
// if (checkConsistency) {
// response = racer.send("(time (abox-consistent-p))");
// }
// else {
// response = racer.send("(time (concept-satisfiable? "+"|"+((OWLClass) clazz).getURI().toString()+"|))");
// }
// BufferedReader in = new BufferedReader(new FileReader(new File("time.txt")));
// String line = null, hack = "";
// while (( line = in.readLine()) != null) {
// hack += line;
// }
// System.setOut(temp);
// int spos = hack.lastIndexOf("took");
// int epos = hack.lastIndexOf("seconds");
// if (spos!=-1 && epos!=-1) {
// String time = hack.substring(spos+4, epos);
// racerTime += (long) Float.parseFloat(time)*1000;
// }
// sat = (response.equalsIgnoreCase("nil")) ? false : true;
// }
// catch (Exception ex) {System.out.println("No RACER: "+ex.getMessage());}
// }
if (useKAON) {
// write ontology!
this.writeOntology(newOnt);
// communicate with KAON2 using API
KAON2Connection connection = KAON2Manager.newConnection();
DefaultOntologyResolver resolver = new DefaultOntologyResolver();
resolver.registerReplacement(newOnt.getLogicalURI().toString(), fname); //"http://kaon2.semanticweb.org/example1","file:src/ex1/example1.xml");
connection.setOntologyResolver(resolver);
// read in ontology
try {
Ontology ontology = connection.openOntology(newOnt.getLogicalURI().toString(), new HashMap());
org.semanticweb.kaon2.api.owl.elements.OWLClass cla = KAON2Manager.factory().owlClass(((OWLClass) clazz).getURI().toString());
org.semanticweb.kaon2.api.reasoner.Reasoner reasoner = ontology.createReasoner();
testTimer.start();
if (checkConsistency) sat = reasoner.isSatisfiable();
else sat = reasoner.isSatisfiable(cla);
testTimer.stop();
}
catch (Exception ex) {System.out.println("No KAON: "+ex.getMessage());}
}
else {
// create new instance of pellet and check sat. of clazz
PelletReasoner newPellet = new PelletReasoner();
newPellet.setOntology(newOnt, false);
testTimer.start();
if (checkConsistency) sat = newPellet.isConsistent();
else sat = newPellet.isConsistent(clazz);
testTimer.stop();
}
return sat;
}
public Set expandAxiomSet(Set axioms, OWLOntology ont, boolean expandMore) {
try {
Set newEntities = new HashSet();
for (Iterator iter = new HashSet(axioms).iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
if (signatureMap.containsKey(axiom)) {
newEntities.addAll((HashSet) signatureMap.get(axiom));
}
else {
Set ents = swoopModel.getAxiomSignature(axiom, ont);
signatureMap.put(axiom, ents);
newEntities.addAll(ents);
}
if (expandMore) {
// expand entity set to include usage entities
for (Iterator iter2 = new HashSet(newEntities).iterator(); iter2.hasNext();) {
Set usage = new HashSet();
OWLEntity ent = (OWLEntity) iter2.next();
if (ent==null || ent.getURI()==null) {
continue;
}
// check in local cache first before using OntologyHelper
if (usageMap.containsKey(ent.getURI())) {
usage = (HashSet) usageMap.get(ent.getURI());
}
else {
usage = OntologyHelper.entityUsage(ont, ent);
usageMap.put(ent.getURI(), usage);
}
// only add entities because axioms are returned
for (Iterator it = usage.iterator(); it.hasNext();) {
Object e = it.next();
if (e instanceof OWLEntity) newEntities.add(e);
else if (e instanceof OWLObject) {
if (signatureMap.containsKey(e)) {
newEntities.addAll((HashSet) signatureMap.get(e));
}
else {
Set ents = swoopModel.getAxiomSignature((OWLObject) e, ont);
signatureMap.put(e, ents);
newEntities.addAll(ents);
}
}
}
}
}
}
// get axioms for all newEntities either from local cache or from swoopModel
Set newAxioms = new HashSet();
for (Iterator iter2 = newEntities.iterator(); iter2.hasNext();) {
OWLEntity ent = (OWLEntity) iter2.next();
if (ent==null || ent.getURI()==null) {
continue;
}
if (axiomMap.containsKey(ent.getURI())) {
newAxioms.addAll((HashSet) axiomMap.get(ent.getURI()));
}
else {
Set ax = swoopModel.getAxioms(ent, ont);
axiomMap.put(ent.getURI(), ax);
newAxioms.addAll(ax);
}
}
if (axioms.containsAll(newAxioms)) {
return new HashSet();
}
else {
// determine latest axioms
Set before = new HashSet(axioms);
Set latest = new HashSet(axioms);
latest.addAll(newAxioms);
latest.removeAll(before);
// // set a limit on axioms to be added
if (latest.size()>axiomLimit) {
// only let limited entities remain in latest
Set copyLatest = new HashSet(latest);
latest.clear();
for (int ctr = 0; ctr < axiomLimit; ctr++) {
Object ax = copyLatest.iterator().next();
latest.add(ax);
copyLatest.remove(ax);
}
axiomLimit *= 1.25; // slowly increase axiom limit
}
newAxioms = latest;
axioms.addAll(newAxioms);
return newAxioms;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?