turtleentityrenderer.java

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

JAVA
1,609
字号
			buffer2.println("<FONT FACE=\""+swoopModel.getFontFace()+"\" SIZE="+fontSize+">" );				        super.render(entity, swoopModel, buffer);	        			// Actually used Namespace declarations			for (Iterator it = myQNameProvider.touchedPrefixes.keySet().iterator(); it.hasNext();){				String prefix = (String) it.next();				String ns = (String) myQNameProvider.touchedPrefixes.get(prefix);				if (!editorEnabled) buffer2.println("@prefix " + escape(prefix) + ": " + LT + "<a href=" + escape(ns) + ">"+escape(ns)+"</a>"+GT + PERIOD + BREAK);				else buffer2.println("@prefix " + escape(prefix) + ": " + LT + escape(ns)+GT + PERIOD + BREAK);			}			// if showing fulll name, add prefix			if (!myModel.getShowQNames())				if (!editorEnabled) buffer2.println("@prefix : " + LT + "<a href=" + escape(defaultNS) + ">" + escape(defaultNS) + "</a>"+GT + PERIOD + BREAK );				else buffer2.println("@prefix : " + LT + escape(defaultNS) +GT + PERIOD + BREAK );				        String turtleText = repairForEndOfStatement(sw.getBuffer().toString()) + BREAK +								getOtherStatements(entity) + "</FONT>";	        			buffer2.println(BREAK);			String headerText = sw2.getBuffer().toString();	        writer.write(headerText + turtleText);	    }	    catch (OWLException ex){	    	ex.printStackTrace();	    }	    catch (IOException ex){	    	ex.printStackTrace();	    }	  }				public SwoopRenderingVisitor createVisitor() {		if (myModel.getShowQNames()) return new TurtleVisitor(this.myQNameProvider);		return  new TurtleVisitor( new TurtleShortFormProvider() );	}		protected void renderAnnotationProperty(OWLAnnotationProperty prop) throws OWLException {		// TODO Auto-generated method stub	}	/* Well dodgy coding */	protected void renderAnnotationContent(Object o) throws OWLException {		if (o instanceof URI) {			print( encodeHLink((URI)o, myQNameProvider.shortForm((URI)o)) );		} else if (o instanceof OWLIndividual)		{			TurtleVisitor TV = (TurtleVisitor)visitor;			OWLIndividual ind = (OWLIndividual)o;			ind.accept(TV);			print( TV.result() );		}		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("^^" + escape(dvdt));				// 		if (!dv.getURI().toString().equals(				// XMLSchemaSimpleDatatypeVocabulary.INSTANCE.getString())) {				// 		    print( "^^" + dv.getURI() );				// 		}			} else {				if (dvlang != null) {					print("@" + escape(dvlang));				}			}		} else {			print("\"" + escape(o) + "\"");		}	}			/** Render the annotations for an object */	protected void renderAnnotations(OWLNamedObject object ) throws OWLException {		/* Bit nasty this -- annotations result in a new axiom */		TurtleVisitor TV = (TurtleVisitor)visitor;				String indentation = TV.getIndentString();				if (!object.getAnnotations(reasoner.getOntology()).isEmpty()) {			for (Iterator it = object.getAnnotations(reasoner.getOntology()).iterator(); it.hasNext();) {				OWLAnnotationInstance oai = (OWLAnnotationInstance) it.next();				print(indentation + encodeHLink(oai.getProperty().getURI(), myQNameProvider.shortForm(oai.getProperty().getURI())) + TAB);				/* Just whack out the content. This isn't quite right... */				renderAnnotationContent(oai.getContent());				if (it.hasNext())					print(";"+BREAK);				/* Do we need to do this??? */				visitor.reset();				oai.accept(visitor);			}		}			}			protected void renderClass(OWLClass clazz) throws OWLException {		try{			OWLClass owlThing = reasoner.getOntology().getOWLDataFactory().getOWLThing();						TurtleVisitor TV =  (TurtleVisitor)visitor;									// rendering type			ShortFormProvider shortForms = null;			if (myModel.getShowQNames()) shortForms = this.myQNameProvider;			else shortForms = new TurtleShortFormProvider();			String classQName = shortForms.shortForm(clazz.getURI());			URI classURI = new URI(OWL_CLASS);						// print the clacss and its type			println(encodeHLink(clazz.getURI(), classQName) + TAB + 					encodeHLink(new URI(RDF_TYPE_URI), "a") + SPACE +					encodeHLink(classURI, myQNameProvider.shortForm(classURI)) + ";");							TV.indentLevel++;							// print ANNOTATIONS			if(!clazz.getAnnotations(reasoner.getOntology()).isEmpty()) {				renderAnnotations(clazz);				println(";");			}						// print INTERSECTIONs equivalent to this class			Iterator it = OWLDescriptionFinder.getIntersections(clazz, reasoner.getOntologies()).iterator();					while(it.hasNext()) {				OWLAnd intersection = (OWLAnd) it.next();				intersection.accept(TV);				print(TV.getIndentString() + TV.result() +";" + BREAK);			}									// print UNIONs equivalent to this class			it = OWLDescriptionFinder.getUnions(clazz, reasoner.getOntologies()).iterator();			while(it.hasNext()) {				OWLOr union = (OWLOr) it.next();				union.accept(TV);				print(TV.getIndentString() + TV.result() + ";" + BREAK);			}			// print ENUMERATIONs of classes that are equivalent to this class					it = OWLDescriptionFinder.getEnumerations(clazz, reasoner.getOntologies()).iterator();			while(it.hasNext()) {				OWLEnumeration oneOf = (OWLEnumeration) it.next();				oneOf.accept(TV);				print(TV.getIndentString() + TV.result() + ";" + BREAK);					}			// print EQUIVALENT classes			Set eqs = OWLDescriptionFinder.getEquivalentClasses(clazz, reasoner.getOntologies());			it = eqs.iterator();						while(it.hasNext()) {				OWLDescription desc = (OWLDescription) it.next();				if(!(desc instanceof OWLRestriction || desc instanceof OWLClass))					it.remove();			}			if(reasoner.isConsistent(clazz))				eqs.addAll(reasoner.equivalentClassesOf(clazz));  // add all named equivalent classes			it = eqs.iterator();			URI uri = new URI(OWL_EQUIVALENTCLASS);			while (it.hasNext())			{				OWLDescription eq = (OWLDescription)it.next();				print( TV.getIndentString() + encodeHLink(uri, myQNameProvider.shortForm(uri)) + TAB);				TV.reset();				if (!(eq instanceof OWLClass))  // only indent if eq is an anon class				{					TV.indentLevel ++;					print(BREAK + TV.getIndentString());				}				eq.accept(TV);						if (!(eq instanceof OWLClass)) // un-indent					TV.indentLevel --;				print(TV.result() + ";" + BREAK);				}						// DISJOINT CLASSES			Set disjoints = OWLDescriptionFinder.getDisjoints(clazz, reasoner.getOntologies());			uri = new URI(OWL_DISJOINTWITH);			for (it = disjoints.iterator(); it.hasNext(); )			{				OWLDescription desc = (OWLDescription)it.next();				TV.reset();				desc.accept(TV);				print(TV.getIndentString() + encodeHLink(uri, myQNameProvider.shortForm(uri)) + TAB + TV.result() + ";" + BREAK);			}						// SUBCLASS OF						// acquiring all non-inferred super classes (named or anon)			Set supers = OWLDescriptionFinder.getSuperClasses(clazz, reasoner.getOntologies());			if(reasoner.isConsistent(clazz)) {				// remove all the named classes because reasoner will eventually add them				it = supers.iterator();				while(it.hasNext())					if(it.next() instanceof OWLClass)						it.remove();						// add all the named superclasses (including inferred)				supers.addAll(reasoner.superClassesOf(clazz));				// remove owl:Thing from the superclass set				it = supers.iterator();				while(it.hasNext()) {					Object o = it.next();					if(o instanceof Set && ((Set)o).contains(owlThing))						it.remove();				}			}			uri = new URI(RDFS_SUBCLASSOF);			for (it = supers.iterator(); it.hasNext(); )			{				Object obj = (Object)it.next();								// if there is more than one element for this line				// we only print the first one. rest are either				// equivalent classes (or properties) 				if(obj instanceof Collection)					obj = ((Collection)obj).iterator().next();								TV.reset();				OWLDescription desc = (OWLDescription)obj;				print(TV.getIndentString() + encodeHLink(uri, myQNameProvider.shortForm(uri)) + TAB);				if (desc instanceof OWLRestriction)				{					TV.indentLevel ++;					print(BREAK);				}				else if (desc instanceof OWLBooleanDescription )				{					TV.indentLevel ++;					print(BREAK);									}				desc.accept(TV);				if (desc instanceof OWLRestriction)				{					print( TV.getIndentString() + TV.result());					TV.indentLevel --;				}				else if (desc instanceof OWLBooleanDescription )				{					print( TV.getIndentString() + "[ " + TV.result()  + " ]");					TV.indentLevel --;				}				else					print(TV.result());								pw.print(";" + BREAK);							}									TV.indentLevel --;		}		catch (Exception ex)		{			ex.printStackTrace();		}	}	protected void renderDataProperty(OWLDataProperty prop) throws OWLException {		try{			TurtleVisitor TV =  (TurtleVisitor)visitor;						// rendering type			ShortFormProvider shortForms = null;			if (myModel.getShowQNames()) shortForms = this.myQNameProvider;			else shortForms = new TurtleShortFormProvider();			String classQName = shortForms.shortForm(prop.getURI());			URI typeURI = new URI(OWL_DATAPROPERTY);						// print the clacss and its type			println(encodeHLink(prop.getURI(), classQName) + TAB + 					encodeHLink(new URI(RDF_TYPE_URI), "a") + SPACE +					encodeHLink(typeURI, myQNameProvider.shortForm(typeURI)) + ";");			TV.indentLevel ++;			// annotations			if(!prop.getAnnotations(reasoner.getOntology()).isEmpty()) {				renderAnnotations(prop);				println(";");			}						// is functional			if (prop.isFunctional(reasoner.getOntologies())) {				typeURI = new URI(OWL_FUNCTIONALPROP);				println(TV.getIndentString() + encodeHLink(new URI(RDF_TYPE_URI), "a") + TAB +						encodeHLink(typeURI, myQNameProvider.shortForm(typeURI)) + ";");			}									// domain			for (Iterator it = reasoner.domainsOf(prop).iterator(); it.hasNext();) {				URI uri = new URI(RDFS_DOMAIN);				print(TV.getIndentString() + encodeHLink(uri, myQNameProvider.shortForm(uri)) + TAB);				OWLDescription dom = (OWLDescription) it.next();				TV.reset();				dom.accept(TV);				println( TV.result() + ";");			}						// range			for (Iterator it = reasoner.rangesOf(prop).iterator(); it.hasNext();) {				URI uri = new URI(RDFS_RANGE);				print(TV.getIndentString() + encodeHLink(uri, myQNameProvider.shortForm(uri)) + TAB);				OWLDataRange ran = (OWLDataRange) it.next();				TV.reset();				ran.accept(TV);				println( TV.result() + ";");			}						// print out its superclasses			for (Iterator it = reasoner.superPropertiesOf(prop).iterator(); it.hasNext();) 			{				URI uri = new URI(RDFS_SUBPROPERTYOF);				print(TV.getIndentString() + encodeHLink(uri, myQNameProvider.shortForm(uri)) + TAB);				Object obj = it.next();								if (obj instanceof OWLDescription)				{					OWLDescription dom = (OWLDescription) it.next();					TV.reset();					dom.accept(TV);					println( TV.result() + ";");				}				else // could be a set of OWLDataproperty				{					Set set = (Set)obj;					for (Iterator i = set.iterator(); i.hasNext();)					{						OWLDataProperty dom = (OWLDataProperty)i.next();						TV.reset();						dom.accept(TV);						println( TV.result() + ";");					}				}			}		}		catch (Exception ex)		{ ex.printStackTrace(); }	}	protected void renderDataType(OWLDataType datatype) throws OWLException {		// TODO Auto-generated method stub	}	protected void renderIndividual(OWLIndividual ind) throws OWLException {		try{						ShortFormProvider shortForms = null;			if (myModel.getShowQNames()) shortForms = this.myQNameProvider;			else shortForms = new TurtleShortFormProvider();						OWLClass owlThing = reasoner.getOntology().getOWLDataFactory().getOWLThing();						if ( ind.isAnonymous() ) 				print("[ ]"); // rendering anon individuals (not sure if this is correct). 				else 				println(encodeHLink(ind.getURI(), shortForms.shortForm(ind.getURI())) );						TurtleVisitor TV = (TurtleVisitor)visitor;			TV.indentLevel++;						// defined types						Set types = OWLDescriptionFinder.getTypes(ind, reasoner.getOntologies());/*			if(reasoner.isConsistent()) {				// remove all the named classes because reasoner will eventually add them				Iterator i = types.iterator();				while(i.hasNext())					if(i.next() instanceof OWLClass)						i.remove();						// add all the named superclasses				types.addAll(reasoner.typesOf(ind));			}			// remove owl:Thing from the superclass set			Iterator i = types.iterator();			while(i.hasNext()) {				Object o = i.next();				if(o instanceof Set && ((Set)o).contains(owlThing))					i.remove();			}*/			//System.out.println(types.size());			for (Iterator it = types.iterator(); it.hasNext();) {				Object obj = (Object)it.next();								// if there is more than one element for this line				// we only print the first one. rest are either				// equivalent classes (or properties) 				if(obj instanceof Collection)					obj = ((Collection)obj).iterator().next();								OWLDescription desc = (OWLDescription)obj;				TV.reset();				desc.accept(TV);				print(TV.getIndentString() + TAB + encodeHLink(new URI(RDF_TYPE_URI), "a") + TAB + TV.result() + ";");				if (it.hasNext())					println();			}						if (types.size() != 0)				println();						// annotations							if(!ind.getAnnotations(reasoner.getOntology()).isEmpty()) {				renderAnnotations(ind);				println(";");			}	

⌨️ 快捷键说明

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