ontologywithclasshierarchygraph.java

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

JAVA
1,285
字号

		ClassTreeNode root = createNode( clss, depth );

		SwoopReasoner reasoner = (SwoopReasoner) this.myReasonerMap.get(ontology
				.getURI());
		Object obj = concepts.iterator().next();

		if (obj instanceof OWLClass) 
		{
			OWLClass c = (OWLClass) obj;
			Set subs = reasoner.subClassesOf(c);
			Iterator i = subs.iterator();
			
			int maxSubtreeDepth = -2;
			while (i.hasNext()) 
			{
				Set set = (Set) i.next();
				if (set.contains(c))
					continue;

				//SortedSet sortedSet = orderedEntities(set);
				ClassTreeNode node = buildTreeNode(ontology, set, depth + 1);
				
				// do not add owl:Nothing to the tree
				if (node != null) 
				{
					root.addChild(node);
					if ( node.getSubtreeDepth() > maxSubtreeDepth )
						maxSubtreeDepth = node.getSubtreeDepth();
				}
			}
			root.setSubtreeDepth( maxSubtreeDepth + 1 );
		}
		return root;
	}

	private ClassTreeNode createNode(OWLEntity entity, int depth ) {
		try {
			return new ClassTreeNode(myModel, entity.getURI(), depth);
		} catch (OWLException e) {
			e.printStackTrace();
		}
		return null;
	}

	private SortedSet orderedEntities(Set entities) 
	{
		SortedSet ss = new TreeSet(EntityComparator.INSTANCE);
		ss.addAll(entities);
		return ss;
	}

	public void showOntologyPopupMenu(MouseEvent e, Vertex pickedVertex) {
		myVertexPopupMenu.show(mySplitPane.getRightComponent(), e.getX(), e.getY());
	}

	public void showClassPopupMenu(MouseEvent e, ClassTreeNode node) {
		myClassPopupMenu.show(mySplitPane.getRightComponent(), e.getX(), e.getY());
	}

	public void populateClassList(ClassTreeNode node) {

		myClassList.setListData(node.getChildren());
		String label = null;
		// If selected node is top node (owl:Thing), use the ontology name as
		// label
		if (node.getURI().toString().equals("http://www.w3.org/2002/07/owl#Thing"))
			label = node.getOntologyNode().toString();
		else
			label = node.toString();

		// if current selection = the new selection, do nothing
		ClassListHistoryItem item = myClassListHistoryManager.getCurrent();
		if (item != null) {
			if (myClassListHistoryManager.getCurrent().getLabel().equals(label))
				return;
		}

		label = label + " (" + node.getChildren().size() + ")";
		myClassListHistoryManager.addItem(new SelectionClassListHistoryItem(
				this, myClassList.getModel(), node, label));
		myCurrentClassLabel.setText(label);
		this.myForwardListButton.setEnabled(false);
		this.myBackListButton.setEnabled(true);
		
		renderEntity();
	}

	public void populateClassListWithSearchTerm(Set nodes, String subText) 
	{
		myClassList.setListData(new Vector(nodes));
		String label = "search on: \"" + subText + "\"" + " (" + nodes.size() + ")";
		myClassListHistoryManager.addItem(new HighlightClassListHistoryItem(
				this, myClassList.getModel(), nodes, label));
		myCurrentClassLabel.setText(label);
		vv.repaint();
		this.myForwardListButton.setEnabled(false);
		this.myBackListButton.setEnabled(true);
		renderEntity();
	}

	public void loadClassListHistory(ClassListHistoryItem item) 
	{
		myCurrentClassLabel.setText(item.getLabel());
		myClassList.setModel(item.getModel());
		renderEntity();
	}

	private void forwardClassListHistory() 
	{
		ClassListHistoryItem item = myClassListHistoryManager.goForward();
		if (item != null) {
			item.executeLoadHistory();
			myBackListButton.setEnabled(!myClassListHistoryManager.isAtBeginning());
			myForwardListButton.setEnabled(!myClassListHistoryManager.isAtEnd());
			renderEntity();
		}
	}

	private void backClassListHistory() {
		ClassListHistoryItem item = myClassListHistoryManager.goBack();
		if (item != null) {
			item.executeLoadHistory();
			myBackListButton.setEnabled(!myClassListHistoryManager.isAtBeginning());
			myForwardListButton.setEnabled(!myClassListHistoryManager.isAtEnd());
			renderEntity();
		}
	}

	protected void setupUI() {

		myClassListHistoryManager = new ClassListHistoryManager();

		pr = new OntologyWithClassHierarchyRenderer();

		Layout layout = new SizeAwareCircleLayout(graph);
		vv = new OntologyVisualizationViewer(myModel, this, layout, pr);
		vv.setPickSupport(new SwoopPickSupport(vv));

		myBasicColor = new BasicVertexColor(vv);

		pr.setVertexPaintFunction(myBasicColor);
		pr.setVertexShapeFunction(vShape);
		pr.setVertexStringer(NO_LABEL);
		pr.setVertexLabelCentering(true);
		pr.setVertexFontFunction(vFont);
		pr.setEdgeShapeFunction(eShape);

		//vSize.setGraph( graph );

		CCGraphPanel gPanel = new CCGraphPanel(vv);
		
		ZoomPanGraphMouse gm = new ZoomPanGraphMouse(vv);
		vv.setGraphMouse(gm);
		vv.setToolTipListener(new VertexTips());
		vv.setBackground(Color.white);

		myOntologyScrollPane = getOntologyListPane();

		myClassList = new ClassList(this);

		myListScrollPane = new JScrollPane();
		myListScrollPane.getViewport().setView(myClassList);

		JPanel org1 = new JPanel();
		org1.setLayout(new BorderLayout());
		org1.add(getButtonPanel(), BorderLayout.NORTH);
		JPanel org2 = new JPanel();                    // contains label
		org2.setLayout(new BorderLayout());
		org2.add(getLabelPanel(), BorderLayout.NORTH);
		JPanel org3 = new JPanel();                    // contains classlist
		org3.setLayout(new BorderLayout());
		org3.add(myListScrollPane, BorderLayout.CENTER);
		org3.add(getSearchPanel(), BorderLayout.SOUTH);

		org2.add(org3, BorderLayout.CENTER);

		myTabs = new JTabbedPane();
		myTabs.add("Class List ", org2 );
		myTabs.add("Concise Format", this.getRendererPane() );
		org1.add( myTabs, BorderLayout.CENTER );
		
		myLeftSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, myOntologyScrollPane, org1);
		myLeftSplitPane.setOneTouchExpandable(true);

		mySplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false,
				myLeftSplitPane, gPanel);
		mySplitPane.setOneTouchExpandable(true);
		setLayout(new BorderLayout());
		add(mySplitPane);
		add(getControlPanel(), BorderLayout.SOUTH);

		// set up popup menus
		myVertexPopupMenu = getVertexPopupMenu();
		myClassPopupMenu = getClassPopupMenu();
		
		this.addKeyListener( this );
	}

	protected JPanel getButtonPanel() {
		JPanel buttonPanel = new JPanel();

		// load history button images (code from TermsDisplay)
		SwoopIcons swoopIcons = new SwoopIcons();
		ImageIcon prevIcon = (ImageIcon) SwoopIcons.prevIcon;
		if (SwoopIcons.prevIcon != null)
			myBackListButton = new JButton(prevIcon);
		else
			myBackListButton = new JButton("<");
		ImageIcon nextIcon = (ImageIcon) SwoopIcons.nextIcon;
		if (SwoopIcons.nextIcon != null)
			myForwardListButton = new JButton(nextIcon);
		else
			myForwardListButton = new JButton(">");

		myFocusButton = new JButton("Focus");

		myForwardListButton.addActionListener(this);
		myBackListButton.addActionListener(this);
		myFocusButton.addActionListener(this);

		buttonPanel.add(myBackListButton);
		buttonPanel.add(myForwardListButton);
		buttonPanel.add(new JLabel("  "));
		buttonPanel.add(myFocusButton);

		JPanel buttonContainer = new JPanel();
		buttonContainer.setLayout(new BorderLayout());
		buttonContainer.add(buttonPanel, BorderLayout.WEST);

		return buttonContainer;
	}

	protected JPanel getLabelPanel() {
		myCurrentClassLabel = new JLabel(" ");
		JPanel labelPanel = new JPanel();
		myCurrentClassLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		myCurrentClassLabel.setOpaque(true);
		myCurrentClassLabel.setBackground(new Color(10, 25, 84));
		myCurrentClassLabel.setForeground(Color.WHITE);

		//labelPanel.setBackground( Color.WHITE );
		labelPanel.setLayout(new BorderLayout());
		labelPanel.add(myCurrentClassLabel, BorderLayout.CENTER);

		return labelPanel;
	}

	protected JScrollPane getOntologyListPane() {
		JPanel hostPanel = new JPanel();
		hostPanel.setBackground(Color.WHITE);
		hostPanel.setLayout(new BoxLayout(hostPanel, BoxLayout.Y_AXIS));

		Set ontologySet = this.graph.getVertices();
		TreeSet orderedOntologySet = new TreeSet(
				new SwoopOntologyVertexComparator());
		orderedOntologySet.addAll(ontologySet);
		for (Iterator it = orderedOntologySet.iterator(); it.hasNext();) {
			SwoopOntologyVertex v = (SwoopOntologyVertex) it.next();
			OntologyPanel aPanel = new OntologyPanel(v, vv);
			hostPanel.add(aPanel);
		}
		return new JScrollPane(hostPanel);
	}

	protected JPanel getSearchPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		mySearchButton = new JButton("Search");
		mySearchField = new JTextField();
		panel.add(mySearchField, BorderLayout.CENTER);
		panel.add(mySearchButton, BorderLayout.EAST);
		mySearchButton.addActionListener(this);

		return panel;
	}

	protected JPanel getControlPanel() {
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));

		String[] layouts = { "KK Layout", "FR Layout", "Circle Layout",
				"Spring Layout", "ISOM Layout", "DAG Layout" };
		JComboBox jcb = new JComboBox(layouts);
		jcb.setActionCommand("layout");
		jcb.addActionListener(this);
		jcb.setSelectedItem(myProps.getPreferredLayout());
		Box layoutPanel = Box.createHorizontalBox();
		layoutPanel.add(new JLabel("Graph Layout"));
		layoutPanel.add(jcb);

		Box labelPanel = Box.createHorizontalBox();
		
		JCheckBox v_labels = new JCheckBox("Show partition labels");
		v_labels.setAlignmentX(Component.LEFT_ALIGNMENT);
		v_labels.setActionCommand("label");
		v_labels.addActionListener(this);
		v_labels.setSelected(false);

		JCheckBox v_font = new JCheckBox("Use bold font");
		v_font.setActionCommand("font");
		v_font.addActionListener(this);
		v_font.setSelected(false);
		labelPanel.add(v_labels);
		labelPanel.add(v_font);

		JCheckBox v_size = new JCheckBox(
				"Scale nodes with respect to number of entities in the partition");
		v_size.setAlignmentX(Component.LEFT_ALIGNMENT);
		v_size.addActionListener(this);
		v_size.setActionCommand("scale");
		v_size.setSelected(true);

		JCheckBox eShape = new JCheckBox("Do not overlap inverse edges");
		eShape.setAlignmentX(Component.LEFT_ALIGNMENT);
		eShape.addActionListener(this);
		eShape.setActionCommand("inverseEdge");
		eShape.setSelected(true);

		v_content = new JCheckBox("Draw content of the nodes");
		v_content.setAlignmentX(Component.LEFT_ALIGNMENT);
		v_content.addActionListener(this);
		v_content.setActionCommand("content");
		v_content.setSelected(true);

		JButton plus = new JButton("+");
		plus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				vv.scale(1.1, 1.1);
			}
		});
		JButton minus = new JButton("-");
		minus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				vv.scale(0.9, 0.9);
			}
		});
		JLabel zoomLabel = new JLabel("Zoom");
		zoomLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		Box zoomPanel = Box.createHorizontalBox();
		zoomPanel.add(zoomLabel);
		zoomPanel.add(plus);
		zoomPanel.add(minus);

		//controlPanel.add(labelPanel);
		//controlPanel.add(v_size);
		//controlPanel.add(v_content);
		//controlPanel.add(eShape);
		controlPanel.add(zoomPanel);
		//controlPanel.add(layoutPanel);

		return controlPanel;
	}
	
	protected JComponent getRendererPane()
	{
		myEntityRendererPane = new JEditorPane();
		myEntityRendererPane.setContentType("text/html");
		myEntityRendererPane.setText("No class selected.");
		myEntityRendererPane.setEditable( false );
		JScrollPane scrollpane = new JScrollPane();
		scrollpane.getViewport().setView( myEntityRendererPane );
		return scrollpane;
	}
	
	private void renderEntity()
	{
		// only render if concise format view is currently selected
		if ( myTabs.getSelectedIndex() != CONCISEVIEW)
			return; 
		ConciseFormatEntityRenderer rend = new ConciseFormatEntityRenderer();
		ClassTreeNode node = vv.getCurrentSelectedNode();
		URI uri = node.getURI();
		StringWriter writer = new StringWriter();
		try 
		{
			OWLOntology ont = myModel.getOntology(node.getOntologyNode().getURI());
			if ( myModel.getSelectedOntology() != ont)
				myModel.setSelectedOntology( ont );
			
			// if uri is owl:Thing, we create the entity.  Else, we get it from ontologies
			
			OWLEntity ent = ont.getOWLDataFactory().getOWLThing();			
			if ( !uri.toString().equals( ent.getURI().toString() ) )			
				ent = myModel.getEntity( ont ,uri, true);
				
			rend.render( ent, myModel, writer );
			myEntityRendererPane.setText( writer.toString() );
			myEntityRendererPane.setCaretPosition(0);
		} 
		catch (Exception e) 
		{ 
			e.printStackTrace();
			myEntityRendererPane.setText("I am sorry.  I don't understand " + uri);
		}
		
		//safsads
	}
	
	protected JPopupMenu getVertexPopupMenu() {
		JPopupMenu pop = new JPopupMenu(VERTEX_POPUP_NAME);
		myShowSubclassAxioms = new JMenuItem(SHOW_SUBCLASS_AXIOMS);
		myShowDomainAxioms = new JMenuItem(SHOW_DOMAIN_AXIOMS);
		myShowRangeAxioms = new JMenuItem(SHOW_RANGE_AXIOMS);
		myShowIndividualAxioms = new JMenuItem(SHOW_INDIVIDUAL_AXIOMS);

		myShowSubclassAxioms.addActionListener(this);
		myShowDomainAxioms.addActionListener(this);
		myShowRangeAxioms.addActionListener(this);
		myShowIndividualAxioms.addActionListener(this);

		pop.add(myShowSubclassAxioms);
		pop.add(myShowDomainAxioms);
		pop.add(myShowRangeAxioms);
		pop.add(myShowIndividualAxioms);

		return pop;
	}

	protected JPopupMenu getClassPopupMenu() {
		JPopupMenu pop = new JPopupMenu(VERTEX_POPUP_NAME);
		myShowRef = new JMenuItem(SHOW_RELATED_CLASSES);
		myShowRef.addActionListener(this);

		pop.add(myShowRef);

		return pop;
	}

⌨️ 快捷键说明

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