package de.tudresden.inf.lat.born.filter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.semanticweb.owlapi.functional.renderer.OWLFunctionalSyntaxRenderer;
import org.semanticweb.owlapi.io.OWLRenderer;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import de.tudresden.inf.lat.born.owlapi.processor.ProcessorConfigurationImpl;

/**
 * 
 * @author Julian Mendez
 *
 */
public class OntologyFilter {

	public static final String HELP = "Parameters: <input ontology> <output ontology>";

	public OWLOntology readOntology(String ontologyFileName) throws OWLOntologyCreationException, IOException {
		Objects.requireNonNull(ontologyFileName);
		return ProcessorConfigurationImpl.readOntology(new FileInputStream(ontologyFileName));
	}

	public OWLOntology filterOntology(OWLOntology owlOntology, OwlAxiomFilter filter)
			throws OWLOntologyCreationException {
		Objects.requireNonNull(owlOntology);
		Set<OWLAxiom> newAxioms = owlOntology.getAxioms().stream() //
				.filter(axiom -> filter.accept(axiom)) //
				.collect(Collectors.toSet());
		return owlOntology.getOWLOntologyManager().createOntology(newAxioms);
	}

	public void storeOntology(OWLOntology owlOntology, String fileName) throws IOException, OWLException {
		Objects.requireNonNull(owlOntology);
		Objects.requireNonNull(fileName);
		OWLRenderer renderer = new OWLFunctionalSyntaxRenderer();
		FileOutputStream output = new FileOutputStream(fileName);
		renderer.render(owlOntology, output);
		output.flush();
		output.close();
	}

	public void filter(String ontologyFileName, String filteredOntologyFileName, OwlAxiomFilter filter)
			throws IOException, OWLException {
		Objects.requireNonNull(ontologyFileName);
		Objects.requireNonNull(filteredOntologyFileName);
		storeOntology(filterOntology(readOntology(ontologyFileName), filter), filteredOntologyFileName);
	}

	public static void main(String[] args) throws IOException, OWLException {
		Objects.requireNonNull(args);
		if (args.length == 2) {
			String ontologyFileName = args[0];
			String outputFileName = args[1];
			OntologyFilter instance = new OntologyFilter();
			instance.filter(ontologyFileName, outputFileName, new ElAxiomFilter());

		} else {
			System.out.println(HELP);

		}
	}

}

@@ -248,47 +248,14 @@
 api.
-functional.renderer.OWLFunctionalSyntax
+io.OWL
 Rend
@@ -286,38 +286,33 @@
 cweb.owlapi.
-io.OWLRenderer
+model.IRI
 ;%0Aimport org
@@ -511,16 +511,78 @@
 ception;
+%0Aimport org.semanticweb.owlapi.owlxml.renderer.OWLXMLRenderer;
 %0A%0Aimport
@@ -809,16 +809,79 @@
 ology%3E%22;
+%0A%09public static final String NEW_ONTOLOGY_SUFFIX = %22-filtered%22;
 %0A%0A%09publi
@@ -1427,16 +1427,188 @@
 Set());%0A
+%09%09IRI oldOntologyIri = owlOntology.getOntologyID().getOntologyIRI().get();%0A%09%09IRI newOntologyIri = IRI.create(oldOntologyIri.toURI().toASCIIString() + NEW_ONTOLOGY_SUFFIX);%0A
 %09%09return
@@ -1668,16 +1668,32 @@
 ewAxioms
+, newOntologyIri
 );%0A%09%7D%0A%0A%09
@@ -1754,26 +1754,51 @@
 ing fileName
-) 
+, OWLRenderer renderer)%0A%09%09%09
 throws IOExc
@@ -1902,62 +1902,38 @@
 %0A%09%09O
-WLRenderer renderer = new OWLFunctionalSyntaxR
+bjects.requireNonNull(r
 enderer
-(
 );%0A%09
@@ -2166,29 +2166,51 @@
 ilter filter
-)
+,
 %0A%09%09%09
+OWLRenderer renderer) 
 throws IOExc
@@ -2328,16 +2328,52 @@
 eName);%0A
+%09%09Objects.requireNonNull(renderer);%0A
 %09%09storeO
@@ -2456,24 +2456,34 @@
 logyFileName
+, renderer
 );%0A%09%7D%0A%0A%09publ
@@ -2807,16 +2807,38 @@
 Filter()
+, new OWLXMLRenderer()
 );%0A%0A%09%09%7D 

Make filter create ontologies with new identifiers