/*
* This file is part of ELKI:
* Environment for Developing KDD-Applications Supported by Index-Structures
*
* Copyright (C) 2017
* ELKI Development Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.lmu.ifi.dbs.elki.datasource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import de.lmu.ifi.dbs.elki.data.DoubleVector;
import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle;
import de.lmu.ifi.dbs.elki.datasource.filter.ObjectFilter;
import de.lmu.ifi.dbs.elki.datasource.parser.NumberVectorLabelParser;
/**
* Abstract base class for testing data sources.
*
* @author Erich Schubert
*/
public abstract class AbstractDataSourceTest {
/**
* Base path for unit test files.
*/
public final static String UNITTEST = "elki/testdata/input/";
/**
* Open a resource input stream. Use gzip if the name ends with .gz.
* (Autodetection currently does not work on resource streams.)
*
* @param filename resource name
* @return Input stream
* @throws IOException
*/
public static InputStream open(String filename) throws IOException {
InputStream is = AbstractDataSourceTest.class.getClassLoader().getResourceAsStream(filename);
if(is == null) {
throw new IOException("Resource not found: " + filename);
}
return filename.endsWith(".gz") ? new GZIPInputStream(is) : is;
}
/**
* Read data as a bundle, as we do not yet have Database objects in this
* module. Bundles don't have nearest neighbor search, so usually you will
* want to use Database or Relation objects instead in real code.
*
* @param filename Resource filename to load.
* @param filterlist Filters
* @return Bundle
*/
protected static MultipleObjectsBundle readBundle(String filename, ObjectFilter... filterlist) {
NumberVectorLabelParser<DoubleVector> parser = new NumberVectorLabelParser<>(DoubleVector.FACTORY);
try (InputStream is = open(filename);
InputStreamDatabaseConnection dbc = new InputStreamDatabaseConnection(is, Arrays.asList(filterlist), parser)) {
return dbc.loadData();
}
catch(IOException e) {
throw new RuntimeException(e); // Fail the test, no need to catch this.
}
}
}