ontologywithclasshierarchygraph.java

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

JAVA
1,285
字号
/*
 * Created on Jul 19, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.mindswap.swoop.utils.graph.hierarchy;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.io.StringWriter;
import java.net.URI;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import org.mindswap.swoop.ModelChangeEvent;
import org.mindswap.swoop.SwoopModel;
import org.mindswap.swoop.SwoopModelListener;
import org.mindswap.swoop.reasoner.SwoopReasoner;
import org.mindswap.swoop.renderer.SwoopEntityRenderer;
import org.mindswap.swoop.renderer.entity.ConciseFormatEntityRenderer;
import org.mindswap.swoop.utils.EConnectionOntologyHelper;
import org.mindswap.swoop.utils.graph.hierarchy.classlist.ClassList;
import org.mindswap.swoop.utils.graph.hierarchy.classlist.ClassListHistoryItem;
import org.mindswap.swoop.utils.graph.hierarchy.classlist.ClassListHistoryManager;
import org.mindswap.swoop.utils.graph.hierarchy.classlist.HighlightClassListHistoryItem;
import org.mindswap.swoop.utils.graph.hierarchy.classlist.SelectionClassListHistoryItem;
import org.mindswap.swoop.utils.graph.hierarchy.colors.DefaultColorScheme;
import org.mindswap.swoop.utils.graph.hierarchy.colors.GraphColorScheme;
import org.mindswap.swoop.utils.graph.hierarchy.colors.OverlayGraphScheme;
import org.mindswap.swoop.utils.graph.hierarchy.colors.PartitionFocusColorScheme;
import org.mindswap.swoop.utils.graph.hierarchy.layout.SizeAwareCircleLayout;
import org.mindswap.swoop.utils.graph.hierarchy.popup.ListSelectionPopup;
import org.mindswap.swoop.utils.graph.hierarchy.popup.OntologyGraphAxiomWalker;
import org.mindswap.swoop.utils.graph.hierarchy.ui.CCGraphPanel;
import org.mindswap.swoop.utils.ui.EntityComparator;
import org.mindswap.swoop.utils.ui.SwoopIcons;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLEntity;
import org.semanticweb.owl.model.OWLException;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.helper.OntologyHelper;

import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.ConstantVertexAspectRatioFunction;
import edu.uci.ics.jung.graph.decorators.ConstantVertexStringer;
import edu.uci.ics.jung.graph.decorators.EdgeShape;
import edu.uci.ics.jung.graph.decorators.EdgeShapeFunction;
import edu.uci.ics.jung.graph.decorators.EllipseVertexShapeFunction;
import edu.uci.ics.jung.graph.decorators.VertexAspectRatioFunction;
import edu.uci.ics.jung.graph.decorators.VertexFontFunction;
import edu.uci.ics.jung.graph.decorators.VertexPaintFunction;
import edu.uci.ics.jung.graph.decorators.VertexShapeFunction;
import edu.uci.ics.jung.graph.decorators.VertexSizeFunction;
import edu.uci.ics.jung.graph.decorators.VertexStringer;
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.DirectedSparseGraph;
import edu.uci.ics.jung.utils.Pair;
import edu.uci.ics.jung.utils.UserDataContainer;
import edu.uci.ics.jung.visualization.FRLayout;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.ISOMLayout;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PickSupport;
import edu.uci.ics.jung.visualization.PickedInfo;
import edu.uci.ics.jung.visualization.SpringLayout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.ZoomPanGraphMouse;
import edu.uci.ics.jung.visualization.contrib.DAGLayout;
import edu.uci.ics.jung.visualization.contrib.KKLayout;

/**
 * @author Dave Wang
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class OntologyWithClassHierarchyGraph extends JPanel 
	implements ActionListener, SwoopModelListener, KeyListener 
{

	class SwoopOntologyVertexComparator implements Comparator {

		public int compare(Object o1, Object o2) {
			try {
				SwoopOntologyVertex ontv1 = (SwoopOntologyVertex) o1;
				SwoopOntologyVertex ontv2 = (SwoopOntologyVertex) o2;
				String name1 = ((OntologyGraphNode) ontv1.getUserDatum(OntologyWithClassHierarchyGraph.DATA)).getURI().toString();
				String name2 = ((OntologyGraphNode) ontv2.getUserDatum(OntologyWithClassHierarchyGraph.DATA)).getURI().toString();
				return name1.compareTo(name2);
			} catch (Exception e) {
				System.out.println(o1.getClass().getName());
				System.out.println(o2.getClass().getName());
				e.printStackTrace();
			}
			return -1;
		}

	}

	class OntologyPanel extends JPanel implements ActionListener {
		private VisualizationViewer myVV = null;

		private SwoopOntologyVertex myVertex = null;

		private JCheckBox myCheckBox = null;

		public OntologyPanel(SwoopOntologyVertex vertex, VisualizationViewer vv) {
			super();
			myVertex = vertex;
			myCheckBox = new JCheckBox(vertex.toString());
			myCheckBox.addActionListener(this);
			myCheckBox.getModel().setSelected(true);
			myCheckBox.setBackground(Color.WHITE);
			myVV = vv;

			setLayout(new BorderLayout());
			add(myCheckBox, BorderLayout.WEST);

			setMaximumSize(new Dimension(150, 26));
			setPreferredSize(new Dimension(150, 26));
			setSize(new Dimension(150, 26));
			setVisible(true);
			this.setBackground(Color.WHITE);
		}

		public void actionPerformed(ActionEvent e) {
			myVertex.setVisible(myCheckBox.getModel().isSelected());
			myVV.repaint();
		}
	}
	
	// just an index variable used a key for OntologyGraphNode for a Vertex
	public static final String DATA = "DATA";

	// just a name for the popup meny for Vetices
	public static final String VERTEX_POPUP_NAME = "Vertex Popup Menu";

	public static final String CLASS_POPUP_NAME = "Class Popup Menu";

	// some string constants for menu items
	public static final String SHOW_SUBCLASS_AXIOMS = "Show Subclass Axioms";

	public static final String SHOW_DOMAIN_AXIOMS = "Show Domain Axioms";

	public static final String SHOW_RANGE_AXIOMS = "Show Range Axioms";

	public static final String SHOW_INDIVIDUAL_AXIOMS = "Show Individual Axioms";

	public static final String SUBCLASS_AXIOM_LIST = "Subclass Axiom List";

	public static final String DOMAIN_AXIOM_LIST = "Domain Axiom List";

	public static final String RANGE_AXIOM_LIST = "Range Axiom List";

	public static final String INDIVIDUAL_LIST = "Individual List";

	public static final String SHOW_RELATED_CLASSES = "Show Related Classes";

	public static final int BASIC_COLOR = 0;

	public static final int PARTITION_FOCUS_COLOR = 1;

	public static final int OVERLAY_GRAPH_COLOR = 2;

	public static final int NORMAL_VIEW = 0;

	public static final int RELATION_VIEW = 1;
	
	public static final int CONCISEVIEW = 1;

	private static int MIN = 10;

	private static int MAX = 100;

	private static final EdgeShapeFunction LINE = new EdgeShape.Line();

	private static final EdgeShapeFunction CURVE = new EdgeShape.QuadCurve();

	private final EdgeShapeFunction CURVED_LINE = new EdgeShapeFn();

	private final UserDataContainer.CopyAction SHARE = new UserDataContainer.CopyAction.Shared();

	private final VertexStringer SHORT_LABEL = new VertexLabel(true);

	private final VertexStringer LONG_LABEL = new VertexLabel(false);

	private final VertexStringer NO_LABEL = new ConstantVertexStringer("");

	protected DirectedSparseGraph graph;

	protected OntologyVisualizationViewer vv;

	private OntologyWithClassHierarchyRenderer pr;

	private VertexSize vSize = new VertexSize();

	private VertexFont vFont = new VertexFont();

	private JCheckBox v_content = null;

	private JSplitPane mySplitPane = null; // separates selection panes on left
										   // and visualization graph on right

	private JEditorPane myEntityRendererPane = null;
	
	private JScrollPane myListScrollPane = null;

	private JList myClassList = null;

	private JLabel myCurrentClassLabel = null;

	private JButton myForwardListButton = null;

	private JButton myBackListButton = null;

	private JButton myFocusButton = null;
	private JButton myViewButton  = null;
	
	private int currentView = NORMAL_VIEW;

	private JTabbedPane myTabs = null;
	
	private JScrollPane myOntologyScrollPane = null;

	private JSplitPane myLeftSplitPane = null;

	private JButton mySearchButton = null;

	private JTextField mySearchField = null;

	private JPopupMenu myVertexPopupMenu = null;

	private JMenuItem myShowSubclassAxioms = null;

	private JMenuItem myShowDomainAxioms = null;

	private JMenuItem myShowRangeAxioms = null;

	private JMenuItem myShowIndividualAxioms = null;

	private JPopupMenu myClassPopupMenu = null;

	private JMenuItem myShowRef = null;

	private ClassListHistoryManager myClassListHistoryManager = null;

	private VertexAspectRatioFunction vAspect = new ConstantVertexAspectRatioFunction(
			1.0f);

	private VertexShapeFunction vShape = new EllipseVertexShapeFunction(vSize,
			vAspect);

	private EdgeShapeFunction eShape = CURVED_LINE;

	private OntologyWithClassHierarchyGraphProperties myProps = null;

	private SwoopModel myModel = null;

	private OWLClass owlThing = null;

	private OWLClass owlNothing = null;

	// vertex color objects
	private BasicVertexColor myBasicColor = null;

	// color scheme that oversees the colors in the graph
	private GraphColorScheme myColorScheme = null;

	// OverlayGraph object
	private OverlayGraph myOverlayGraph = null;

	private Vector partitions = null;

	private HashMap myReasonerMap = null;

	// partitions is a vector of OWLOntologies
	public OntologyWithClassHierarchyGraph(SwoopModel model, Vector partitions) {
		this.myModel = model;
		this.myModel.addListener(this);
		this.myProps = new OntologyWithClassHierarchyGraphProperties(myModel,
				partitions);
		this.graph = new DirectedSparseGraph();
		this.myColorScheme = new DefaultColorScheme();
		//this.myOverlayGraph = new OverlayGraph(this.graph, this.myModel, this);
		this.myReasonerMap = new HashMap();
		this.partitions = partitions;

		Set allOnts = new HashSet();
		for (Iterator i = partitions.iterator(); i.hasNext();) {
			OWLOntology ont = (OWLOntology) i.next();
			allOnts.add(ont);
			boolean isNotEconn = ont.getForeignOntologies().isEmpty();
			if (isNotEconn) // get import closure
			{
				try {
					allOnts.addAll(OntologyHelper.importClosure(ont));
				} catch (OWLException e) {
					e.printStackTrace();
				}
			} else // get econnected closure
			{
				allOnts.addAll(EConnectionOntologyHelper.getEConnectedClosure( new HashSet(), myModel, ont) );
			}
		}
		this.partitions.clear();
		for (Iterator iter = allOnts.iterator(); iter.hasNext();)
			this.partitions.add(iter.next());

		// now call swoopModel to create reasoner instance for each ontology,
		// process ontology and put it back in the vector: reasoners
		// finally, swoopModel directly notifies this class instance
		// see modelChanged(..) in this class
		myModel.callFromMotherShip(allOnts, myReasonerMap);
	}

	/*
	 * display mothership UI after swoopModel has processed ontologies that are
	 * going to be displayed and updates the reasoners vector accordingly
	 */
	private void displayMotherShip() {
		// preserve the currently selected ontology
		for (Iterator i = partitions.iterator(); i.hasNext();) {
			OWLOntology ont = (OWLOntology) i.next();
			// if a node containing this ontology already exists, we skip it
			if (graph.getUserDatum(ont) != null)
				continue;
			OntologyGraphNode node = buildOntologyNode(ont);
			addToGraph(graph, node);
		}

		this.setupUI();
		this.getParent().validate();
		vv.autoPanZoom();
		
		//myOverlayGraph.precomputeAllRelations(); // precompute for the relations
												 // in the class trees
	}

	/*
	 * Reads in an OWLOntology and build a ontology node with a class tree
	 * inside, where the tree represents the subclass tree structure :) The
	 * class tree is a tree by current SWOOP reasoner (so told with no reasoner,
	 * but inferred if with Pellet).
	 *  
	 */
	private OntologyGraphNode buildOntologyNode(OWLOntology ontology) {
		try {
			owlThing = ontology.getOWLDataFactory().getOWLThing();
			owlNothing = ontology.getOWLDataFactory().getOWLNothing();
			ClassTreeNode root = buildRootTreeNode(ontology);
			return new OntologyGraphNode(ontology, root, this);
		} catch (OWLException e) {
			e.printStackTrace();
		}

		return null;
	}

	public ClassTreeNode buildRootTreeNode(OWLOntology ontology)
			throws OWLException {
		SwoopReasoner reasoner = (SwoopReasoner) myReasonerMap.get(ontology
				.getURI());
		Set set = reasoner.equivalentClassesOf(owlThing);
		set.add(owlThing);
		ClassTreeNode root = buildTreeNode( ontology, set, 0);
		return root;
	}

	public ClassTreeNode buildTreeNode(OWLOntology ontology, Set concepts, int depth )
			throws OWLException {

		if (concepts.contains(owlNothing))
			return null;

		// would the set ever be empty?
		if (concepts.isEmpty())
			return null;

		OWLClass clss = (OWLClass) concepts.iterator().next();

		// hacky hacky hack!!!!! (BUGBUGBUG) ~ Bernardo?
		if (((ontology.isForeign(clss)))
				&& (!clss.getURI().toString().equals(
						SwoopEntityRenderer.OWL_THING))) {
			return null;
		}

⌨️ 快捷键说明

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