nlentityrenderer.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
//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.renderer.entity;import java.awt.Component;import java.io.PrintWriter;import java.io.StringWriter;import java.io.Writer;import java.net.URI;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import org.mindswap.swoop.SwoopDisplayPanel;import org.mindswap.swoop.SwoopModel;import org.mindswap.swoop.TermsDisplay;import org.mindswap.swoop.renderer.BaseEntityRenderer;import org.mindswap.swoop.renderer.SwoopEditableRenderer;import org.mindswap.swoop.renderer.SwoopEntityRenderer;import org.mindswap.swoop.renderer.SwoopRenderingVisitor;import org.mindswap.swoop.utils.HyperDAML;import org.semanticweb.owl.impl.model.OWLDataFactoryImpl;import org.semanticweb.owl.impl.model.OWLIndividualImpl;import org.semanticweb.owl.impl.model.OWLObjectPropertyInstanceImpl;import org.semanticweb.owl.impl.model.OWLObjectSomeRestrictionImpl;import org.semanticweb.owl.impl.model.OWLOntologyImpl;import org.semanticweb.owl.io.RendererException;import org.semanticweb.owl.io.vocabulary.OWLVocabularyAdapter;import org.semanticweb.owl.io.vocabulary.RDFSVocabularyAdapter;import org.semanticweb.owl.io.vocabulary.RDFVocabularyAdapter;import org.semanticweb.owl.model.OWLAnnotationInstance;import org.semanticweb.owl.model.OWLAnnotationProperty;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLDataFactory;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDataRange;import org.semanticweb.owl.model.OWLDataType;import org.semanticweb.owl.model.OWLDataValue;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;import org.semanticweb.owl.model.OWLEntity;import org.semanticweb.owl.model.OWLEnumeration;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLIndividualAxiom;import org.semanticweb.owl.model.OWLNamedObject;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLObjectPropertyInstance;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLProperty;import org.semanticweb.owl.model.OWLPropertyAxiom;public class NLEntityRenderer extends BaseEntityRenderer implements SwoopEntityRenderer {	public static final String INDENT = "  "; 		protected Set allURIs;	private List shortNames;	private Map known;	private int reservedNames;	private String contentType = "text/html";		public String getContentType() {		return contentType;	}		public String getName() {		return "Natural Language";	}		public SwoopRenderingVisitor createVisitor() {		return new NLVisitor(this, swoopModel);	}		public void render(OWLEntity entity, SwoopModel swoopModel, Writer writer) throws RendererException {			super.render(entity, swoopModel, writer);				}			/*	 *  SwoopRenderer method	 * 	 */	public Component getDisplayComponent( SwoopDisplayPanel panel )	{				if (!(panel instanceof TermsDisplay ))			throw new IllegalArgumentException();				return super.getEditorPane( this.getContentType(), (TermsDisplay)panel );	}				protected void renderAnnotationContent(Object o) throws OWLException {		if (o instanceof URI) {			print(o.toString());		} else if (o instanceof OWLIndividual) {			print(((OWLIndividual) o).getURI().toString());		} else if (o instanceof OWLDataValue) {			OWLDataValue dv = (OWLDataValue) o;			print("\"" + escape(dv.getValue()) + "\"");			/* Only show it if it's not string */			URI dvdt = dv.getURI();			String dvlang = dv.getLang();			if (dvdt != null) {				print("^^" + dvdt);				// 		if (!dv.getURI().toString().equals(				// XMLSchemaSimpleDatatypeVocabulary.INSTANCE.getString())) {				// 		    print( "^^" + dv.getURI() );				// 		}			} else {				if (dvlang != null) {					print("@" + dvlang);				}			}		} else {			print("\""+o.toString()+"\"");		}	}	/** Render the annotations for an object */	protected void renderAnnotations(OWLNamedObject object ) throws OWLException {		/* Bit nasty this -- annotations result in a new axiom */		if (!object.getAnnotations(reasoner.getOntology()).isEmpty()) {			for (Iterator it = object.getAnnotations(reasoner.getOntology()).iterator(); it.hasNext();) {				OWLAnnotationInstance oai = (OWLAnnotationInstance) it.next();				//print("(" + getShortForm(oai.getProperty().getURI()) + " ");				/* Just whack out the content. This isn't quite right... */				renderAnnotationContent(oai.getContent());				//		print( "\"" + oai.getContent() + "\"" );				print(" ");				/* Do we need to do this??? */				visitor.reset();				oai.accept(visitor);				// 		if (it.hasNext()) {				// 		    println();				// 		}			}		}			}		protected void renderClass(OWLClass clazz) throws OWLException {				boolean done = false;		String className = this.getShortForm(clazz.getURI());				// ** global reset tree **		((NLVisitor) visitor).resetNLTree(className, NLVisitor.LINK_EQUIVALENT, 0);				if(!clazz.getAnnotations(reasoner.getOntology()).isEmpty()) {			println("Annotations:");			renderAnnotations(clazz);			println("\n");			done = true;		}				if (clazz.getEquivalentClasses(reasoner.getOntologies()).size() > 0) {			println("Definition: (Necessary and Sufficient Conditions)");						for (Iterator it = clazz.getEquivalentClasses(reasoner.getOntologies()).iterator(); it.hasNext();) {								OWLDescription eq = (OWLDescription) it.next();								eq.accept(visitor);								// ** local reset tree **				((NLVisitor) visitor).setLinkContext(NLVisitor.LINK_EQUIVALENT);				((NLVisitor) visitor).resetParent();			}			visitor.reset();			((NLVisitor) visitor).printTree();						// do post processing here, before printing out the final string						print(postProcess(visitor.result()));			done = true;			println();			println();		}				if (!clazz.getSuperClasses(reasoner.getOntologies()).isEmpty()) {						// ** global reset tree **			((NLVisitor) visitor).resetNLTree(className, NLVisitor.LINK_SUBCLASS, 0);						println("Details: (Necessary Conditions)");						for (Iterator it = clazz.getSuperClasses(reasoner.getOntologies()).iterator(); it.hasNext();) {								OWLDescription eq = (OWLDescription) it.next();				eq.accept(visitor);											// ** local reset tree **				((NLVisitor) visitor).setLinkContext(NLVisitor.LINK_SUBCLASS);				((NLVisitor) visitor).resetParent();			}						visitor.reset();			((NLVisitor) visitor).printTree();						print(postProcess(visitor.result()));			done = true;		}		/*		 * This has changed -- used to be simply a oneof in the class definition. We now get a		 * special keyword in the vocabulary		 */		for (Iterator it = clazz.getEnumerations(reasoner.getOntologies()).iterator(); it.hasNext();) {			OWLDescription eq = (OWLDescription) it.next();			println(					"EnumeratedClass"					+ className					);					/* We know that the description has to be a oneof */			try {				OWLEnumeration enumeration = (OWLEnumeration) eq;				for (Iterator iit = enumeration.getIndividuals().iterator(); iit.hasNext();) {					OWLIndividual desc = (OWLIndividual) iit.next();					visitor.reset();					desc.accept(visitor);					print(" " + postProcess(visitor.result()));					// 		    if (iit.hasNext()) {					// 			print(" ");					// 		    }				}				println("");				done = true;			} catch (ClassCastException ex) {				throw new RendererException(ex.getMessage());			}		}

⌨️ 快捷键说明

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