package it.unibz.inf.ontop.owlapi.example;

/*
 * #%L
 * ontop-quest-owlapi
 * %%
 * Copyright (C) 2009 - 2014 Free University of Bozen-Bolzano
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.*;

import it.unibz.inf.ontop.injection.OntopSQLOWLAPIConfiguration;
import it.unibz.inf.ontop.owlapi.resultset.MaterializedGraphOWLResultSet;
import it.unibz.inf.ontop.owlapi.OntopOWLAPIMaterializer;
import org.semanticweb.owlapi.model.OWLAxiom;

/**
 * A very simple example that shows how to generate triples in an N-Triple file,
 * using the data of a database and a set of mappings from the OBDA file.
 * 
 * NOTE: This process does not involve reasoning since no ontology is given. The
 * result are the DIRECT triples generated by the mappings.
 */
public class ABoxMaterializerExample {

	/*
	 * Use the sample database using H2 from
	 * https://babbage.inf.unibz.it/trac/obdapublic/wiki/InstallingTutorialDatabases
	 */
	private static final String OBDA_FILE = "src/test/resources/example/exampleBooks.obda";
	private static final String PROPERTY_FILE = "src/test/resources/example/exampleBooks.properties";
	private static final String OUTPUT_FILE = "src/test/resources/example/exampleBooks.txt";

	
	public void generateTriples() throws Exception {

		OntopSQLOWLAPIConfiguration configuration = OntopSQLOWLAPIConfiguration.defaultBuilder()
				.nativeOntopMappingFile(OBDA_FILE)
				.propertyFile(PROPERTY_FILE)
				.enableTestMode()
				.build();

		/*
		 * Start materializing data from database to triples.
		 */
		OntopOWLAPIMaterializer materializer = OntopOWLAPIMaterializer.defaultMaterializer(configuration);

		try (MaterializedGraphOWLResultSet graphResultSet = materializer.materialize()) {

			/*
			 * Print the triples into an external file.
			 */
			File fout = new File(OUTPUT_FILE);
			if (fout.exists()) {
				fout.delete(); // clean any existing output file.
			}

			try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fout, true)))) {
				while (graphResultSet.hasNext()) {
					OWLAxiom individual = graphResultSet.next();
					out.println(individual.toString());
				}
				out.flush();
			}

			long numberOfTriples = graphResultSet.getTripleCountSoFar();
			System.out.println("Generated triples: " + numberOfTriples);
		}
	}

	public static void main(String[] args) {
		try {
			ABoxMaterializerExample example = new ABoxMaterializerExample();
			example.generateTriples();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
