conciseplainvisitor.java

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

JAVA
543
字号
/*
GNU Lesser General Public License

ConciseFormatVisitor.java
Copyright (C) 2005 MINDSWAP Research Group, University of Maryland College Park

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package org.mindswap.swoop.utils.graph.hierarchy.popup;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.mindswap.swoop.SwoopModel;
import org.mindswap.swoop.renderer.BaseEntityRenderer;
import org.mindswap.swoop.renderer.SwoopRenderingVisitor;
import org.semanticweb.owl.impl.model.OWLInversePropertyAxiomImpl;
import org.semanticweb.owl.io.ShortFormProvider;
import org.semanticweb.owl.model.OWLAnd;
import org.semanticweb.owl.model.OWLAnnotationProperty;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLDataAllRestriction;
import org.semanticweb.owl.model.OWLDataCardinalityRestriction;
import org.semanticweb.owl.model.OWLDataEnumeration;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDataPropertyInstance;
import org.semanticweb.owl.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLDataSomeRestriction;
import org.semanticweb.owl.model.OWLDataType;
import org.semanticweb.owl.model.OWLDataValue;
import org.semanticweb.owl.model.OWLDataValueRestriction;
import org.semanticweb.owl.model.OWLDescription;
import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owl.model.OWLDisjointClassesAxiom;
import org.semanticweb.owl.model.OWLEnumeration;
import org.semanticweb.owl.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owl.model.OWLEquivalentPropertiesAxiom;
import org.semanticweb.owl.model.OWLException;
import org.semanticweb.owl.model.OWLFunctionalPropertyAxiom;
import org.semanticweb.owl.model.OWLIndividual;
import org.semanticweb.owl.model.OWLIndividualTypeAssertion;
import org.semanticweb.owl.model.OWLInverseFunctionalPropertyAxiom;
import org.semanticweb.owl.model.OWLInversePropertyAxiom;
import org.semanticweb.owl.model.OWLNot;
import org.semanticweb.owl.model.OWLObjectAllRestriction;
import org.semanticweb.owl.model.OWLObjectCardinalityRestriction;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLObjectPropertyInstance;
import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owl.model.OWLObjectSomeRestriction;
import org.semanticweb.owl.model.OWLObjectValueRestriction;
import org.semanticweb.owl.model.OWLOr;
import org.semanticweb.owl.model.OWLProperty;
import org.semanticweb.owl.model.OWLPropertyDomainAxiom;
import org.semanticweb.owl.model.OWLSameIndividualsAxiom;
import org.semanticweb.owl.model.OWLSubClassAxiom;
import org.semanticweb.owl.model.OWLSubPropertyAxiom;
import org.semanticweb.owl.model.OWLSymmetricPropertyAxiom;
import org.semanticweb.owl.model.OWLTransitivePropertyAxiom;
import org.semanticweb.owl.model.helper.OWLObjectVisitorAdapter;


public class ConcisePlainVisitor extends OWLObjectVisitorAdapter implements SwoopRenderingVisitor
//	Uncomment for explanation
// ,OWLExtendedObjectVisitor
{
    	
	public static final String FORALL     = "\u2200";  // all restriction
	public static final String EXISTS     = "\u2203";  // some restriction
	public static final String MEMBEROF   = ".";
	public static final String EQU        = "=";
	public static final String GREATEQU   = "\u2265";
	public static final String LESSEQU    = "\u2264";
	public static final String SUBCLASSOF   = "\u2286";   // subset
	public static final String DISJOINT     = "\u2260";
	public static final String EQUIVALENTTO = "\u2261";  // identical
	
	public static final String INTERSECTION = "\u2293";  // AND
	public static final String UNION        = "\u2294";  // OR
	public static final String NOT          = "\u00ac";  // NOT
	
	public static final String ISA          = "a"; 
		
	ShortFormProvider shortForms; 
	StringWriter sw;
	PrintWriter pw;
	String imageURI = "";
	SwoopModel swoopModel;
	
	public ConcisePlainVisitor( ShortFormProvider shortForms, SwoopModel swoopModel )
	{
		this.shortForms = shortForms;
		this.swoopModel = swoopModel;
		reset();
	}
	
	public String result() {
		return sw.toString();
	}
	
	/* Replace " with \" and \ with \\ */
	private static String escape(Object o) {
		/* Should probably use regular expressions */
		StringBuffer sw = new StringBuffer();
		String str = o.toString();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c != '"' && c != '\\') {
				sw.append(c);
			} else {
				sw.append('\\');
				sw.append(c);
			}
		}
		return sw.toString();
	}



	public void reset() {
		sw = new StringWriter();
		pw = new PrintWriter( sw );
	}
		
	public void visit( OWLClass clazz ) throws OWLException {
		pw.print( shortForms.shortForm( clazz.getURI() ) );
	}
	
	public void visit( OWLIndividual ind ) throws OWLException 
	{
		if ( ind.isAnonymous() ) 
			pw.print(   ind.getAnonId().getFragment()  );
		else 
			pw.print(  shortForms.shortForm( ind.getURI() )  );
	}
	
	
	public void visit( OWLObjectProperty prop ) throws OWLException 
	{
		pw.print( shortForms.shortForm( prop.getURI() )  );
	}
	
	public void visit( OWLAnnotationProperty prop ) throws OWLException 
	{
		pw.print( shortForms.shortForm( prop.getURI() ) );
	}
	
	public void visit( OWLDataProperty prop ) throws OWLException 
	{
		pw.print(  shortForms.shortForm( prop.getURI() )  );
	}
	
	public void visit( OWLDataValue cd ) throws OWLException 
	{	
		pw.print( "\"" + escape( cd.getValue() ) + "\"");
		/* Only show it if it's not string */
		URI dvdt = cd.getURI();
		String dvlang = cd.getLang();
		if ( dvdt!=null) {
			pw.print( "^^" + "<" + shortForms.shortForm(dvdt) + ">");
		} 
		else 
		{
			if (dvlang!=null) {
				pw.print( "@" + dvlang );
			}
		}
	}

	public void visit( OWLAnd and ) throws OWLException {
		pw.print("(");
		for ( Iterator it = and.getOperands().iterator(); it.hasNext(); ) 
		{
			OWLDescription desc = (OWLDescription) it.next();
			desc.accept( this );
			if (it.hasNext()) {
				pw.print(" " + ConcisePlainVisitor.INTERSECTION + " ");
			}
		}
		pw.print(")");
	}

	public void visit( OWLOr or ) throws OWLException {
		pw.print("(");
		for ( Iterator it = or.getOperands().iterator(); it.hasNext(); ) 
		{
			OWLDescription desc = (OWLDescription) it.next();
			desc.accept( this );
			if (it.hasNext()) 
			{
				pw.print(" " + ConcisePlainVisitor.UNION + " ");
			}
		}
		pw.print(")");
	}

	public void visit( OWLNot not ) throws OWLException {
		pw.print("(" + ConcisePlainVisitor.NOT);
		OWLDescription desc = not.getOperand();
		desc.accept( this );
		pw.print(")");
	}

	public void visit( OWLEnumeration enumeration ) throws OWLException {
		pw.print("{");
		for ( Iterator it = enumeration.getIndividuals().iterator(); it.hasNext(); ) 
		{
			OWLIndividual desc = (OWLIndividual) it.next();
			desc.accept( this );
			if (it.hasNext()) 
				pw.print(", ");
		}
		pw.print("}");
	}

	public void visit( OWLObjectSomeRestriction restriction ) throws OWLException {
		pw.print("(" + ConcisePlainVisitor.EXISTS);
		restriction.getObjectProperty().accept( this );
		pw.print(" "+ ConcisePlainVisitor.MEMBEROF + " ");
		restriction.getDescription().accept( this );
		pw.print(")");
	}

	public void visit( OWLObjectAllRestriction restriction ) throws OWLException {
		pw.print("(" + ConcisePlainVisitor.FORALL);
		restriction.getObjectProperty().accept( this );
		pw.print(" "+ ConcisePlainVisitor.MEMBEROF + " ");
		restriction.getDescription().accept( this );
		pw.print(")");
	}

	public void visit( OWLObjectValueRestriction restriction ) throws OWLException {
		pw.print("(" + ConcisePlainVisitor.EXISTS);
		restriction.getObjectProperty().accept( this );
		/* Changed from hasValue */
		pw.print(" "+ ConcisePlainVisitor.MEMBEROF + " {");
		restriction.getIndividual().accept( this );
		pw.print("})");
	}

	public void visit( OWLDataSomeRestriction restriction ) throws OWLException {
		pw.print("(" + ConcisePlainVisitor.EXISTS);
		restriction.getDataProperty().accept( this );
		pw.print(" "+ ConcisePlainVisitor.MEMBEROF + " ");
		restriction.getDataType().accept( this );
		pw.print(")");
	}

	public void visit( OWLDataAllRestriction restriction ) throws OWLException {
		pw.print("(" + ConcisePlainVisitor.FORALL);
		restriction.getDataProperty().accept( this );
		pw.print(" "+ ConcisePlainVisitor.MEMBEROF + " ");
		restriction.getDataType().accept( this );
		pw.print(")");
	}

⌨️ 快捷键说明

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