repairframe.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,767 行 · 第 1/5 页
JAVA
1,767 行
OWLClass cla = (OWLClass) claList.get(i);
Set mups = new HashSet((Set) claMUPSMap.get(cla));
StringWriter st = new StringWriter();
PrintWriter buffer = new PrintWriter(st);
cfRend.setWriter(buffer);
cfRend.printRepairSOS(mups, cla, false, this);
String sosStr = st.toString();
sosStr = sosStr.replaceAll("problem:", "problem: "+swoopModel.shortForm(cla.getURI()));
html += "<font face=\"Verdana\" size=2>"+ sosStr + "<br>";
}
html += "</body></html>";
// System.out.println(html);
indentPane.setText(html);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/*
* write logFile for user study
*/
private void writeLogFile() {
try {
String fname = "REPAIR-LOG_"+String.valueOf(logCtr)+".txt";
FileWriter fw = new FileWriter(new File(fname));
fw.write(logFile);
fw.close();
System.out.println("Wrote Repair Log File: "+fname);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void genPlan() {
List plans = this.generatePlans();
if (plans.size()>0) {
Set mainSolnSet = new HashSet();
mainSolnSet.addAll((List) plans.get(plans.size()-1)); // *** last plan is most optimum ***
planSolnAxioms[0] = mainSolnSet;
}
this.refreshPlan(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == generateBtn) {
genPlan();
}
else if (e.getSource() == rankBtn) {
if (DEBUG) logFile += NEWLINE + "Recompute RANKS (WA: "+this.w_arityFld.getText()+" WI: "+this.w_impactFld.getText()+" WU: "+this.w_usageFld.getText()+" "+swoopModel.getTimeStamp();
this.refreshRanks();
}
else if (e.getSource() == execBtn) {
if (DEBUG) logFile += NEWLINE + "EXECUTE Plan "+swoopModel.getTimeStamp();
executePlan();
// ****** write log file here *******
if (DEBUG) this.writeLogFile();
}
else if (e.getSource() == clearBtn) {
if (DEBUG) logFile += NEWLINE + "CLEAR Plan "+swoopModel.getTimeStamp();
planSolnAxioms[0] = new HashSet();
this.refreshPlan();
}
else if (e.getSource() == saveBtn) {
// save plan in a new tab
if (DEBUG) logFile += NEWLINE + "SAVE Plan "+swoopModel.getTimeStamp();
JEditorPane copyPane = new JEditorPane();
copyPane.setEditable(false);
copyPane.setContentType("text/html");
copyPane.addHyperlinkListener(this);
planCtr = planTabs.getTabCount()+1;
Set copySoln = new HashSet(planSolnAxioms[0]);
planSolnAxioms[planCtr-1] = copySoln;
this.setCopyPaneText(copyPane, copySoln);
planTabs.add("Saved Plan "+String.valueOf(planCtr), copyPane);
}
else if (e.getSource() == previewBtn) {
if (DEBUG) logFile += NEWLINE + "PREVIEW Plan "+swoopModel.getTimeStamp();
this.previewEffect(false);
}
else if (e.getSource() == rewriteChk) {
this.rewriteEnabled = rewriteChk.isSelected();
if (planSolnAxioms[0].size()>0 && this.updatePlanChk.isSelected()) {
this.genPlan();
}
}
else if (e.getSource() == viewGlobalChk) {
this.viewGlobal = viewGlobalChk.isSelected();
// indentPane.setVisible(!viewGlobal);
if (viewGlobal) axiomTablePane.setDividerLocation(1000);
else {
axiomTablePane.setDividerLocation(0);
this.refreshIndentPane();
indentPane.setCaretPosition(0);
}
}
else if (e.getSource() == keptBtn) {
keptFrame.setVisible(true);
}
else if (e.getSource() == removedBtn) {
removedFrame.setVisible(true);
}
else if (e.getSource() == impactUnsatChk) {
this.enableImpactUnsat = impactUnsatChk.isSelected();
this.axiomSOSMap.clear();
this.rankBtn.doClick();
}
}
/*
* preview effect of executing current repair plan
*/
private String previewEffect(boolean concise) {
String html = "";
int num = 0;
String title = planTabs.getTitleAt(planTabs.getSelectedIndex());
if (title.indexOf("Main")==-1) num = Integer.parseInt(title.substring(title.lastIndexOf(" ")+1), title.length())-1;
Set soln = planSolnAxioms[num];
OWLOntology copyOnt = swoopModel.cloneOntology(ontology);
try {
OWLOntBuilder ob = new OWLOntBuilder(copyOnt);
ob.addAxiom = false;
for (Iterator iter = soln.iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
axiom.accept(ob);
}
// also consider rewrites!
ob.addAxiom = true;
for (Iterator iter = rewriteAxiomSet.iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
axiom.accept(ob);
}
copyOnt = ob.currentOnt;
PelletReasoner newPellet = new PelletReasoner();
newPellet.setOntology(copyOnt);
Set copyUnsat = new HashSet(currUnsat);
Set newUnsat = newPellet.equivalentClassesOf(copyOnt.getOWLDataFactory().getOWLNothing());
copyUnsat.removeAll(newUnsat);
if (!concise)
html = "<head><body>"+insFont();
else
html += "PREVIEW:<br>";
html += "<b>Unsatisfiable</b> ";
if (!concise) html += "<br>";
html += "Fixed:"+String.valueOf(copyUnsat.size())+" ";
if (!concise) {
for (Iterator iter = copyUnsat.iterator(); iter.hasNext();) {
OWLClass cla = (OWLClass) iter.next();
html += " <a href=\""+cla.getURI()+"\">"+swoopModel.shortForm(cla.getURI())+"</a> ";
}
html += "<br><br>";
}
html += "Remaining:" + String.valueOf(newUnsat.size())+" ";
if (!concise) {
for (Iterator iter = newUnsat.iterator(); iter.hasNext();) {
OWLClass cla = (OWLClass) iter.next();
html += " <a href=\""+cla.getURI()+"\">"+swoopModel.shortForm(cla.getURI())+"</a>";
}
}
// JOptionPane.showMessageDialog(this, "Unsatisfiable Concepts Fixed: "+new JScrollPane(list1)+" Remaining: "+newUnsat.size(), "Preview Solution", JOptionPane.INFORMATION_MESSAGE);
//***************************************************
// also preview cumulative impact
Set entail = new HashSet();
for (Iterator iter = planSolnAxioms[0].iterator(); iter.hasNext();) {
OWLObject ax = (OWLObject) iter.next();
if (axiomSOSMap.containsKey(ax)) {
Set sos = (HashSet) axiomSOSMap.get(ax);
entail.addAll(sos);
}
}
// now compute lost entailments
Set lost = new HashSet();
EntailmentChecker chk = new EntailmentChecker(newPellet, copyOnt);
for (Iterator iter = entail.iterator(); iter.hasNext();) {
OWLObject ent = (OWLObject) iter.next();
if (!chk.isEntailed(ent)) {
lost.add(ent);
}
}
entail.removeAll(lost);
// also compute special case entailments retained
// check equivalence between unsatisfiable concepts just turned satisfiable
OWLClass[] list = new OWLClass[copyUnsat.size()];
int ctr = -1;
for (Iterator iter = copyUnsat.iterator(); iter.hasNext();) {
list[++ctr] = (OWLClass) iter.next();
}
for (int i=0; i<ctr-1; i++) {
for (int j = i+1; j<ctr; j++) {
OWLClass cla1 = list[i];
OWLClass cla2 = list[j];
Set s = new HashSet();
s.add(cla1);
s.add(cla2);
OWLClassAxiom ax = ontology.getOWLDataFactory().getOWLEquivalentClassesAxiom(s);
if (chk.isEntailed(ax)) {
entail.add(ax);
}
// ax = ontology.getOWLDataFactory().getOWLSubClassAxiom(cla1, cla2);
// if (chk.isEntailed(ax)) {
// entail.add(ax);
// }
// ax = ontology.getOWLDataFactory().getOWLSubClassAxiom(cla2, cla1);
// if (chk.isEntailed(ax)) {
// entail.add(ax);
// }
}
}
// also check retaining of extended impact
for (Iterator iter = axiomSOSMap.keySet().iterator(); iter.hasNext();) {
OWLObject ax = (OWLObject) iter.next();
for (Iterator iter2= ((HashSet) axiomSOSMap.get(ax)).iterator(); iter2.hasNext();) {
OWLObject ent = (OWLObject) iter2.next();
if (chk.isEntailed(ent)) {
entail.add(ent);
}
}
}
// finally print
if (!concise) html += "<hr>"+insFont();
else html += "<br>";
html += "<b>Entailments</b>";
if (concise) {
html += " Lost: "+String.valueOf(lost.size())+" Retained: "+String.valueOf(entail.size());
}
else {
swoopModel.repairColor = true;
swoopModel.repairSet = new HashSet(currUnsat);
html += "<br>Lost:";
for (Iterator iter = lost.iterator(); iter.hasNext();) {
OWLObject ax = (OWLObject) iter.next();
html += "<br>"+getOWLObjectHTML(ax);
if (whyMap.containsKey(ax)) {
this.hcodeAxiomMap.put(String.valueOf(ax.hashCode()), ax);
html += " (<a href=\":WHY:"+ax.hashCode()+"\">Why?</a>)";
}
}
html += "<br><br>Retained:";
for (Iterator iter = entail.iterator(); iter.hasNext();) {
OWLObject ax = (OWLObject) iter.next();
html += "<br>"+getOWLObjectHTML(ax);
if (whyMap.containsKey(ax)) {
this.hcodeAxiomMap.put(String.valueOf(ax.hashCode()), ax);
html += " (<a href=\":WHY:"+ax.hashCode()+"\">Why?</a>)";
}
}
swoopModel.repairColor = false;
swoopModel.repairSet = new HashSet();
}
if (!concise) this.displayPopup(html, "Preview Effect of Repair Solution", 600, 400);
}
catch (OWLException ex) {
ex.printStackTrace();
}
return html;
}
/*
* execute current repair plan
*/
private void executePlan() {
try {
// execute / apply current solution
int num = 0;
String title = planTabs.getTitleAt(planTabs.getSelectedIndex());
if (title.indexOf("Main")==-1) num = Integer.parseInt(title.substring(title.lastIndexOf(" ")+1), title.length())-1;
Set soln = new HashSet(planSolnAxioms[num]);
OWLOntBuilder ob = new OWLOntBuilder(ontology);
ob.addAxiom = false;
for (Iterator iter = soln.iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
axiom.accept(ob);
// also remove axiom from forced axiom set or blocked axiom set
this.removedAxiomSet.remove(axiom);
this.keptAxiomSet.remove(axiom);
}
// add rewritten axioms
ob.addAxiom = true;
for (Iterator iter = rewriteAxiomSet.iterator(); iter.hasNext();) {
OWLObject axiom = (OWLObject) iter.next();
axiom.accept(ob);
}
List changes = new ArrayList(ob.changes);
swoopModel.addCommittedChanges(changes);
ontology = ob.currentOnt;
pellet.setOntology(ontology);
swoopModel.clearCaches(ontology); // *** KEY CHANGE ***
this.launch();
this.repaint();
if (this.updatePlanChk.isSelected()) {
this.genPlan();
this.refreshPlan();
}
}
catch (OWLException ex) {
ex.printStackTrace();
}
}
/*
* recompute ranks when user changes weights of parameters
*/
private void refreshRanks() {
double ari = Double.parseDouble(w_arityFld.getText());
double imp = Double.parseDouble(w_impactFld.getText());
double usa = Double.parseDouble(w_usageFld.getText());
this.setWeights(ari, imp, usa);
this.computeRanks(axiomUnsatClaMap.keySet());
this.sortAndDisplayAxioms(CURR_METRIC);
this.refreshIndentPane();
}
/*
* Load locally saved hashmap for SOS
*/
private void loadImpactSOSFile() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("axiomSOSMap"));
axiomSOSMap = (HashMap) in.readObject();
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/*
* Save axiomSOSMap to disk
*/
private void saveImpactSOSFile(HashMap axiomSOSMap) {
try {
System.out.println("Saving file axiomSOSMap");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("axiomSOSMap"));
out.writeObject(axiomSOSMap);
out.close();
System.out.println("SOS computed and stored");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?