<commit_before>package it.unibz.krdb.obda.owlrefplatform.core;

import it.unibz.krdb.obda.io.DataManager;
import it.unibz.krdb.obda.model.GraphResultSet;
import it.unibz.krdb.obda.model.OBDAConnection;
import it.unibz.krdb.obda.model.OBDAException;
import it.unibz.krdb.obda.model.OBDAModel;
import it.unibz.krdb.obda.model.OBDAResultSet;
import it.unibz.krdb.obda.model.OBDAStatement;
import it.unibz.krdb.obda.model.impl.OBDADataFactoryImpl;
import it.unibz.krdb.obda.ontology.Assertion;
import it.unibz.krdb.obda.owlapi3.OWLAPI3ABoxIterator;
import it.unibz.krdb.obda.owlrefplatform.core.abox.NTripleAssertionIterator;
import it.unibz.krdb.obda.owlrefplatform.core.abox.VirtualABoxMaterializer;
import it.unibz.krdb.obda.owlrefplatform.core.abox.VirtualABoxMaterializer.VirtualTriplePredicateIterator;
import it.unibz.krdb.obda.querymanager.QueryController;

import java.net.URI;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuestDBStatement implements OBDAStatement {

	final QuestStatement st;

	Logger log = LoggerFactory.getLogger(QuestDBStatement.class);

	protected transient OWLOntologyManager man = OWLManager
			.createOWLOntologyManager();

	protected QuestDBStatement(QuestStatement st) {
		this.st = st;
	}

	public int add(Assertion data) throws SQLException {
		return st.insertData(Collections.singleton(data).iterator(), false, -1,
				-1);
	}

	public int add(Iterator<Assertion> data) throws SQLException {
		return st.insertData(data, false, -1, -1);
	}

	/***
	 * As before, but using recreateIndexes = false.
	 * 
	 * @param data
	 * @throws SQLException
	 */
	public int add(Iterator<Assertion> data, int commit, int batch)
			throws SQLException {
		return st.insertData(data, false, commit, batch);
	}

	public int add(URI rdffile) throws OBDAException {
		return load(rdffile, false, -1, -1);
	}

	public int add(URI rdffile, int commit, int batch) throws OBDAException {
		return load(rdffile, false, commit, batch);
	}

	public int addWithTempFile(URI rdffile) throws OBDAException {
		return load(rdffile, true, -1, -1);
	}

	public int addFromOBDA(URI obdaFile) throws OBDAException {
		return loadOBDAModel(obdaFile, false, -1, -1);
	}

	public int addFromOBDA(URI obdaFile, int commitrate, int batchinserts)
			throws OBDAException {
		return loadOBDAModel(obdaFile, false, commitrate, batchinserts);
	}

	public int addFromOBDAWithTempFile(URI obdaFile) throws OBDAException {
		return loadOBDAModel(obdaFile, true, -1, -1);
	}

	/* Move to query time ? */
	private int load(URI rdffile, boolean useFile, int commit, int batch)
			throws OBDAException {
		// checkConnection();
		String pathstr = rdffile.toString();
		int dotidx = pathstr.lastIndexOf('.');
		String ext = pathstr.substring(dotidx);
		int result = -1;

		try {

			if (ext.toLowerCase().equals(".owl")) {

				OWLOntology owlontology = man
						.loadOntologyFromOntologyDocument(IRI.create(rdffile));
				Set<OWLOntology> ontos = man.getImportsClosure(owlontology);

				OWLAPI3ABoxIterator aBoxIter = new OWLAPI3ABoxIterator(ontos,
						st.questInstance.getEquivalenceMap());
				result = st.insertData(aBoxIter, useFile, commit, batch);
			} else if (ext.toLowerCase().equals(".nt")) {
				NTripleAssertionIterator it = new NTripleAssertionIterator(
						rdffile, st.questInstance.getEquivalenceMap());
				result = st.insertData(it, useFile, commit, batch);
			}
			return result;

		} catch (Exception e) {
			throw new OBDAException(e);
		} finally {
			st.close();
		}

	}

	/* Move to query time ? */
	private int loadOBDAModel(URI uri, boolean useFile, int commit, int batch)
			throws OBDAException {
		// checkConnection();
		VirtualTriplePredicateIterator assertionIter = null;
		try {
			OBDAModel obdaModel = OBDADataFactoryImpl.getInstance()
					.getOBDAModel();
			QueryController qcontroller = new QueryController();
			DataManager io = new DataManager(obdaModel, qcontroller);
			io.loadOBDADataFromURI(uri, URI.create(""),
					obdaModel.getPrefixManager());
			VirtualABoxMaterializer materializer = new VirtualABoxMaterializer(
					obdaModel);
			assertionIter = (VirtualTriplePredicateIterator) materializer
					.getAssertionIterator();
			int result = st.insertData(assertionIter, useFile, commit, batch);
			return result;

		} catch (Exception e) {
			throw new OBDAException(e);<commit_msg>change name of materializer<commit_after>package it.unibz.krdb.obda.owlrefplatform.core;

import it.unibz.krdb.obda.io.DataManager;
import it.unibz.krdb.obda.model.GraphResultSet;
import it.unibz.krdb.obda.model.OBDAConnection;
import it.unibz.krdb.obda.model.OBDAException;
import it.unibz.krdb.obda.model.OBDAModel;
import it.unibz.krdb.obda.model.OBDAResultSet;
import it.unibz.krdb.obda.model.OBDAStatement;
import it.unibz.krdb.obda.model.impl.OBDADataFactoryImpl;
import it.unibz.krdb.obda.ontology.Assertion;
import it.unibz.krdb.obda.owlapi3.OWLAPI3ABoxIterator;
import it.unibz.krdb.obda.owlrefplatform.core.abox.NTripleAssertionIterator;
import it.unibz.krdb.obda.owlrefplatform.core.abox.QuestMaterializer;
import it.unibz.krdb.obda.owlrefplatform.core.abox.QuestMaterializer.VirtualTriplePredicateIterator;
import it.unibz.krdb.obda.querymanager.QueryController;

import java.net.URI;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuestDBStatement implements OBDAStatement {

	final QuestStatement st;

	Logger log = LoggerFactory.getLogger(QuestDBStatement.class);

	protected transient OWLOntologyManager man = OWLManager
			.createOWLOntologyManager();

	protected QuestDBStatement(QuestStatement st) {
		this.st = st;
	}

	public int add(Assertion data) throws SQLException {
		return st.insertData(Collections.singleton(data).iterator(), false, -1,
				-1);
	}

	public int add(Iterator<Assertion> data) throws SQLException {
		return st.insertData(data, false, -1, -1);
	}

	/***
	 * As before, but using recreateIndexes = false.
	 * 
	 * @param data
	 * @throws SQLException
	 */
	public int add(Iterator<Assertion> data, int commit, int batch)
			throws SQLException {
		return st.insertData(data, false, commit, batch);
	}

	public int add(URI rdffile) throws OBDAException {
		return load(rdffile, false, -1, -1);
	}

	public int add(URI rdffile, int commit, int batch) throws OBDAException {
		return load(rdffile, false, commit, batch);
	}

	public int addWithTempFile(URI rdffile) throws OBDAException {
		return load(rdffile, true, -1, -1);
	}

	public int addFromOBDA(URI obdaFile) throws OBDAException {
		return loadOBDAModel(obdaFile, false, -1, -1);
	}

	public int addFromOBDA(URI obdaFile, int commitrate, int batchinserts)
			throws OBDAException {
		return loadOBDAModel(obdaFile, false, commitrate, batchinserts);
	}

	public int addFromOBDAWithTempFile(URI obdaFile) throws OBDAException {
		return loadOBDAModel(obdaFile, true, -1, -1);
	}

	/* Move to query time ? */
	private int load(URI rdffile, boolean useFile, int commit, int batch)
			throws OBDAException {
		// checkConnection();
		String pathstr = rdffile.toString();
		int dotidx = pathstr.lastIndexOf('.');
		String ext = pathstr.substring(dotidx);
		int result = -1;

		try {

			if (ext.toLowerCase().equals(".owl")) {

				OWLOntology owlontology = man
						.loadOntologyFromOntologyDocument(IRI.create(rdffile));
				Set<OWLOntology> ontos = man.getImportsClosure(owlontology);

				OWLAPI3ABoxIterator aBoxIter = new OWLAPI3ABoxIterator(ontos,
						st.questInstance.getEquivalenceMap());
				result = st.insertData(aBoxIter, useFile, commit, batch);
			} else if (ext.toLowerCase().equals(".nt")) {
				NTripleAssertionIterator it = new NTripleAssertionIterator(
						rdffile, st.questInstance.getEquivalenceMap());
				result = st.insertData(it, useFile, commit, batch);
			}
			return result;

		} catch (Exception e) {
			throw new OBDAException(e);
		} finally {
			st.close();
		}

	}

	/* Move to query time ? */
	private int loadOBDAModel(URI uri, boolean useFile, int commit, int batch)
			throws OBDAException {
		// checkConnection();
		VirtualTriplePredicateIterator assertionIter = null;
		try {
			OBDAModel obdaModel = OBDADataFactoryImpl.getInstance()
					.getOBDAModel();
			QueryController qcontroller = new QueryController();
			DataManager io = new DataManager(obdaModel, qcontroller);
			io.loadOBDADataFromURI(uri, URI.create(""),
					obdaModel.getPrefixManager());
			QuestMaterializer materializer = new QuestMaterializer(
					obdaModel);
			assertionIter = (VirtualTriplePredicateIterator) materializer
					.getAssertionIterator();
			int result = st.insertData(assertionIter, useFile, commit, batch);
			return result;

		} catch (Exception e) {
			throw new OBDAException(e);
{'fraction_non_alphanumeric': 0.10831721470019343, 'fraction_numerical': 0.004083387062110466, 'mean_word_length': 4.730911330049261, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2375617', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3154, 'n_words': 658}