repairframe.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,767 行 · 第 1/5 页

JAVA
1,767
字号
//The MIT License
//
// Copyright (c) 2004 Mindswap Research Group, University of Maryland, College Park
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

package org.mindswap.swoop.debugging;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
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.Set;
import java.util.TreeSet;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.mindswap.pellet.debug.owlapi.Reasoner;
import org.mindswap.swoop.SwoopFrame;
import org.mindswap.swoop.SwoopModel;
import org.mindswap.swoop.reasoner.PelletReasoner;
import org.mindswap.swoop.renderer.SwoopCellRenderer;
import org.mindswap.swoop.renderer.entity.ConciseFormatEntityRenderer;
import org.mindswap.swoop.renderer.entity.OWLObjectContainer;
import org.mindswap.swoop.utils.SetUtils;
import org.mindswap.swoop.utils.owlapi.EntailmentChecker;
import org.mindswap.swoop.utils.owlapi.OWLDescriptionFinder;
import org.mindswap.swoop.utils.owlapi.OWLOntBuilder;
import org.mindswap.swoop.utils.ui.EntityComparator;
import org.mindswap.swoop.utils.ui.JTabbedPaneWithCloseIcons;
import org.semanticweb.owl.impl.model.OWLDataFactoryImpl;
import org.semanticweb.owl.impl.model.OWLObjectPropertyRangeAxiomImpl;
import org.semanticweb.owl.impl.model.OWLPropertyDomainAxiomImpl;
import org.semanticweb.owl.model.OWLAnd;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLClassAxiom;
import org.semanticweb.owl.model.OWLDataFactory;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDataType;
import org.semanticweb.owl.model.OWLDescription;
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.OWLNot;
import org.semanticweb.owl.model.OWLObject;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOr;
import org.semanticweb.owl.model.OWLProperty;
import org.semanticweb.owl.model.OWLPropertyDomainAxiom;
import org.semanticweb.owl.model.OWLSubClassAxiom;
import org.semanticweb.owl.model.change.ChangeVisitor;
import org.semanticweb.owl.model.change.RemoveClassAxiom;
import org.semanticweb.owl.model.helper.OntologyHelper;

/**
 * @author Aditya
 *
 */
public class RepairFrame extends JFrame implements HyperlinkListener, ListSelectionListener, ActionListener, ItemListener {

	// Main Swoop stuff:
	SwoopFrame swoopHandler;
	SwoopModel swoopModel;
	PelletReasoner pellet;
	OWLOntology ontology;
	Reasoner pelletDebug;
	
	// UI stuff:
	JSplitPane axiomTablePane;
	JEditorPane repTable, planPane, indentPane, keptPane, removedPane;
	JFrame keptFrame, removedFrame;
	Font tahoma = new Font("Tahoma", Font.PLAIN, 11);
	JList classList;
	JButton rankBtn, generateBtn, settingsBtn, clearBtn, execBtn, saveBtn, previewBtn;
	JButton keptBtn, removedBtn;
	JComboBox granularityCombo;
	JCheckBox rewriteChk, viewGlobalChk, updatePlanChk, impactUnsatChk;
	JTextField w_arityFld, w_impactFld, w_usageFld;
	JTabbedPaneWithCloseIcons planTabs;
	boolean displayMUPSUnion = true, rewriteEnabled = false, viewGlobal = false;
	int planCtr = 0;
	public Set[] planSolnAxioms = new HashSet[100];
	final boolean DEBUG = true;
	String logFile = "";
	String NEWLINE = System.getProperty("line.separator");
	int logCtr = 0;
	
	// Core Repair Stuff:
	Set currUnsat, currRoots, currDerived;
	List currAxioms;
	HashMap claMUPSMap; // class->MUPS
	public HashMap axiomUnsatClaMap, axiomUsageMap, axiomSOSMap, axiomRanksMap; // ranking params
	public HashMap objectMap; // generically used to store contents of hyperlinked-popups
	public HashMap hcodeAxiomMap; 
	public HashMap whyMap;
	public Set keptAxiomSet, removedAxiomSet, rewriteAxiomSet; 
	private boolean enableImpactUnsat = true;
	private boolean turnOffUsage = false;
	private int baseRank = 10;
	
	// Repair Parameters:
	final int ARITY = 0;
	final int IMPACT = 1;
	final int USAGE = 2;
	final int RANK = 3;
	int CURR_METRIC = 3;
	double[] weights = new double[3];
	
	/*
	 * constructor: called from SwoopFrame
	 */
	public RepairFrame(SwoopFrame handler, SwoopModel model, PelletReasoner pelletReasoner) {
		
		// pass all required parameters
		this.swoopHandler = handler;
		this.swoopModel = model;
		this.pellet = pelletReasoner;
		
		// check if pellet has processed currently selected ontology
		if (pelletReasoner.getOntology()==null || !pellet.getOntology().equals(swoopModel.getSelectedOntology())) {
			try {
				pelletReasoner.setOntology(swoopModel.getSelectedOntology());
			} catch (OWLException e) {
				e.printStackTrace();
			}
		}
		this.ontology = swoopModel.getSelectedOntology(); // should be same as pellet.getOntology()!
		this.setWeights(0.9, 0.7, 0.1);
		this.resetSets();
		setupUI();
		if (DEBUG)
			try {
				logFile = "Launching Repair UI for Ontology "+swoopModel.shortForm(ontology.getURI())+" "+swoopModel.getTimeStamp();
			} catch (OWLException e) {
				e.printStackTrace();
			}
		// set log counter
		File file = null;
		do {
			logCtr++;
			file = new File("REPAIR-LOG_"+String.valueOf(logCtr)+".txt");			
		}
		while (file.exists());		
	}
	
	/* 
	 * set weights of parameters for computing axiom ranks
	 */
	private void setWeights(double arity, double impact, double usage) {
		weights[ARITY] = arity;
		weights[IMPACT] = impact;
		weights[USAGE] = usage;
	}
	
	private void resetSets() {
		this.keptAxiomSet = new HashSet();
		this.removedAxiomSet = new HashSet();
		this.rewriteAxiomSet = new HashSet();
	}
	
	/*
	 * reset (clear) all HashMaps
	 */
	private void resetMaps() {
		
		this.axiomUnsatClaMap = new HashMap();
		this.axiomSOSMap = new HashMap();
		this.axiomUsageMap = new HashMap();
		this.axiomRanksMap = new HashMap(); 
		
		this.claMUPSMap = new HashMap();
		
		this.objectMap = new HashMap();
		this.hcodeAxiomMap = new HashMap();
			
		this.whyMap = new HashMap();
	}
	
	/*
	 * compute new root, derived unsatisfiable classes
	 * also insert mups for roots into axiomClaMUPS map
	 */
	private void initRoots() {
		// initialization
		this.currUnsat = new HashSet();
		this.currAxioms = new ArrayList();
		this.currRoots = new HashSet();
		this.currDerived = new HashSet();
		
		// obtain root, derived and total unsatisfiable classes in ontology
		if (pellet.depFinder == null) pellet.autoRootDiscovery();
		if (pellet.depFinder == null) {
			// no root classes!
			JOptionPane.showMessageDialog(this, "No More Unsatisfiable Concepts in Ontology", "Ontology Repair", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		
		this.currRoots = new HashSet(pellet.depFinder.rootClasses);
		this.currDerived = new HashSet(pellet.depFinder.derivedClasses);
		this.currUnsat.addAll(currRoots);
		this.currUnsat.addAll(currDerived);
		
		// get all mups for root classes and insert them in axiomClaMUPS
		for (Iterator iter = currRoots.iterator(); iter.hasNext();) {
			OWLClass root = (OWLClass) iter.next();
			this.findMUPS(root);
		}
		// also dynamically set cell renderer based on currRoots
		classList.setCellRenderer(new SwoopCellRenderer(swoopModel, currRoots));
	}
	
	public void launch() {
//		this.modifyEquivalence();
		
		resetMaps();
		this.refreshPelletDebuggerOntology(ontology);
		
		initRoots();
		
		//*** Special Case: Compute impact and ranks for all root mups axioms ***
		if (!currRoots.isEmpty()) {
//			this.computeImpactSOS(); //EITHER USE THIS OR LINE BELOW
	//		this.loadImpactSOSFile();
			this.computeRanks(axiomUnsatClaMap.keySet());		
		}
		
		// UI changes:
		this.populateClassList();
		planTabs.removeAll();
		planSolnAxioms[0] = new HashSet();
		planCtr = 0;
		planTabs.addTab("Main Plan", new JScrollPane(planPane));
		this.refreshPlan();
	}
	
	private void computeRanks(Set axioms) {
		for (Iterator iter = axioms.iterator(); iter.hasNext();) {
			OWLObject axiom = (OWLObject) iter.next();
			// get arity
			int arity = ((HashSet) this.axiomUnsatClaMap.get(axiom)).size();
			// get impact
			int impact = 0;
			// *** special cases -- 2 types ***
			// Type 1: compute impact on entailments between unsatisfiable concepts
			if (this.enableImpactUnsat) this.computeImpactUnsat(); //***************
			// Type 2: normal impact between satisfiable concepts
			try {
				if (axiom instanceof OWLSubClassAxiom) {
					OWLSubClassAxiom subAx = (OWLSubClassAxiom) axiom;
					if (subAx.getSubClass() instanceof OWLClass && subAx.getSuperClass() instanceof OWLClass) {
						OWLClass sub = (OWLClass) subAx.getSubClass();
						OWLClass sup = (OWLClass) subAx.getSuperClass();
						for (Iterator it = SetUtils.union(pellet.descendantClassesOf(sub)).iterator(); it.hasNext();) {
							OWLDescription s1 = (OWLDescription) it.next();
							for (Iterator it2 = SetUtils.union(pellet.ancestorClassesOf(sup)).iterator(); it2.hasNext();) {	
								OWLDescription s2 = (OWLDescription) it2.next();
								if (!s2.equals(ontology.getOWLDataFactory().getOWLThing()) && !s1.equals(ontology.getOWLDataFactory().getOWLNothing())) {
									OWLSubClassAxiom ax = pellet.getOntology().getOWLDataFactory().getOWLSubClassAxiom(s1, s2);
									this.addAxiom(axiomSOSMap, axiom, ax);
								}
							}
						}
					}						
				}
				else if (axiom instanceof OWLDisjointClassesAxiom) {
					Set dis = ((OWLDisjointClassesAxiom) axiom).getDisjointClasses();
					boolean complex = false;
					for (Iterator d = dis.iterator(); d.hasNext();) {
						OWLDescription desc = (OWLDescription) d.next();
						if (!(desc instanceof OWLClass)) {
							complex = true;
							break;
						}
					}
					if (!complex) {
						List disjoints= new ArrayList(dis);
						for (Iterator d = new ArrayList(disjoints).iterator(); d.hasNext();) {
							Object obj = d.next();
							if (!obj.equals(ontology.getOWLDataFactory().getOWLNothing())) {
								disjoints.remove(obj);
								Set descendants = SetUtils.union(pellet.descendantClassesOf((OWLClass) obj));
								descendants.removeAll(pellet.equivalentClassesOf(ontology.getOWLDataFactory().getOWLNothing()));
								if (enableImpactUnsat) {
									descendants.addAll(((OWLClass) obj).getSubClasses(ontology));
								}
								for (Iterator it = descendants.iterator(); it.hasNext();) {
									OWLDescription desc1 = (OWLDescription) it.next();
									if (!desc1.equals(ontology.getOWLDataFactory().getOWLNothing())) {
										if (enableImpactUnsat || !pellet.equivalentClassesOf(desc1).contains(ontology.getOWLDataFactory().getOWLNothing())) {
											for (Iterator it2 = disjoints.iterator(); it2.hasNext();) {								
												OWLDescription desc2 = (OWLDescription) it2.next();
												if (!desc2.equals(desc1)) {
													Set newDis = new HashSet();
													newDis.add(desc1);
													newDis.add(desc2);
													OWLDisjointClassesAxiom ax = pellet.getOntology().getOWLDataFactory().getOWLDisjointClassesAxiom(newDis);
													this.addAxiom(axiomSOSMap, axiom, ax);
												}
											}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?