package de.lmu.ifi.dbs.elki.datasource;
/*
This file is part of ELKI:
Environment for Developing KDD-Applications Supported by Index-Structures
Copyright (C) 2011
Ludwig-Maximilians-Universität München
Lehr- und Forschungseinheit für Datenbanksysteme
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 .
*/
import java.io.InputStream;
import java.util.List;
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.DoubleVectorLabelParser;
import de.lmu.ifi.dbs.elki.datasource.parser.Parser;
import de.lmu.ifi.dbs.elki.logging.Logging;
import de.lmu.ifi.dbs.elki.utilities.documentation.Description;
import de.lmu.ifi.dbs.elki.utilities.documentation.Title;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ObjectParameter;
/**
* Provides a database connection expecting input from an input stream such as
* stdin.
*
* @author Arthur Zimek
*
* @apiviz.uses Parser oneway - - runs
*/
@Title("Input-Stream based database connection")
@Description("Parse an input stream such as STDIN into a database.")
public class InputStreamDatabaseConnection extends AbstractDatabaseConnection {
/**
* The logger for this class.
*/
private static final Logging logger = Logging.getLogger(InputStreamDatabaseConnection.class);
/**
* Parameter to specify the parser to provide a database.
*
* Key: {@code -dbc.parser}
*
*/
public static final OptionID PARSER_ID = OptionID.getOrCreateOptionID("dbc.parser", "Parser to provide the database.");
/**
* Holds the instance of the parser.
*/
Parser parser;
/**
* The input stream to parse from.
*/
InputStream in = System.in;
/**
* Constructor.
*
* @param filters Filters to use
* @param parser the parser to provide a database
*/
public InputStreamDatabaseConnection(List filters, Parser parser) {
super(filters);
this.parser = parser;
}
@Override
public MultipleObjectsBundle loadData() {
// Run parser
if(logger.isDebugging()) {
logger.debugFine("Invoking parsers.");
}
MultipleObjectsBundle parsingResult = parser.parse(in);
// normalize objects and transform labels
if(logger.isDebugging()) {
logger.debugFine("Invoking filters.");
}
MultipleObjectsBundle objects = invokeFilters(parsingResult);
return objects;
}
@Override
protected Logging getLogger() {
return logger;
}
/**
* Parameterization class.
*
* @author Erich Schubert
*
* @apiviz.exclude
*/
public static class Parameterizer extends AbstractDatabaseConnection.Parameterizer {
Parser parser = null;
@Override
protected void makeOptions(Parameterization config) {
super.makeOptions(config);
configParser(config, Parser.class, DoubleVectorLabelParser.class);
configFilters(config);
}
protected void configParser(Parameterization config, Class> parserRestrictionClass, Class> parserDefaultValueClass) {
ObjectParameter parserParam = new ObjectParameter(PARSER_ID, parserRestrictionClass, parserDefaultValueClass);
if(config.grab(parserParam)) {
parser = parserParam.instantiateClass(config);
}
}
@Override
protected InputStreamDatabaseConnection makeInstance() {
return new InputStreamDatabaseConnection(filters, parser);
}
}
}