All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clarkparsia.pellet.owlapiv3.OWLClassTreePrinter Maven / Gradle / Ivy

There is a newer version: 2.3.6-ansell
Show newest version
package com.clarkparsia.pellet.owlapiv3;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

import org.mindswap.pellet.taxonomy.Taxonomy;
import org.mindswap.pellet.taxonomy.printer.TaxonomyPrinter;
import org.mindswap.pellet.taxonomy.printer.TreeTaxonomyPrinter;
import org.mindswap.pellet.utils.QNameProvider;
import org.mindswap.pellet.utils.TaxonomyUtils;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLNamedIndividual;

/**
 * TaxonomyPrinter for Taxonomies of OWLClasses (Taxonomy)
 * 
 * @author Blazej Bulka 
 */
public class OWLClassTreePrinter extends TreeTaxonomyPrinter implements TaxonomyPrinter {
	private QNameProvider qnames = new QNameProvider();

	public OWLClassTreePrinter() {
	}

	@Override
	protected void printNode( Set set ) {
		super.printNode(set);

		Set instances = getDirectInstances( taxonomy, set.iterator().next() );
		if( instances.size() > 0 ) {
			out.print( " - (" );
			boolean printed = false;
			Iterator ins = instances.iterator();
			for( int k = 0; ins.hasNext(); k++ ) {
				OWLNamedIndividual x = ins.next();

				if( printed )
					out.print( ", " );
				else
					printed = true;
				printURI( out, x );
			}
			out.print( ")" );
		}
	}

	@Override
	protected void printURI( PrintWriter out, OWLClass c ) {
		printIRI( out, c.getIRI() );
	}

	private void printURI( PrintWriter out, OWLNamedIndividual i ) {
		printIRI( out, i.getIRI() );
	}

	private void printIRI( PrintWriter out, IRI iri ) {
		out.print( qnames.shortForm( iri.toString() ) );
	}

	/**
	 * Retrieves direct instances of a class from Taxonomy
	 * 
	 * @param t
	 *            the taxonomy
	 * @param c
	 *            the class
	 * @return a set of direct instances
	 */
	@SuppressWarnings("unchecked")
	public static Set getDirectInstances( Taxonomy t, OWLClass c ) {

		Set instances = ( Set ) t.getDatum( c, TaxonomyUtils.INSTANCES_KEY );
		if( instances == null ) {
			if( t.contains(c) )
				return Collections.emptySet();

			throw new RuntimeException( c + " is an unknown class!" );
		}

		return Collections.unmodifiableSet( instances );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy