Example usage for weka.experiment InstanceQuery retrieveInstances

List of usage examples for weka.experiment InstanceQuery retrieveInstances

Introduction

In this page you can find the example usage for weka.experiment InstanceQuery retrieveInstances.

Prototype

public Instances retrieveInstances(String query) throws Exception 

Source Link

Document

Makes a database query to convert a table into a set of instances

Usage

From source file:adams.flow.transformer.WekaExperimentEvaluation.java

License:Open Source License

/**
 * Loads the experimental results.// w  ww .j  a v  a  2  s . c  o  m
 *
 * @param exp      the experiment to evaluate
 * @return      the results
 * @throws Exception    If reading fails.
 */
protected Instances getData(weka.experiment.Experiment exp) throws Exception {
    Instances result;
    String tmpStr;
    DatabaseResultListener dbListener;
    InstanceQuery query;

    // load experiment data
    if (exp.getResultListener() instanceof CSVResultListener) {
        try {
            result = DataSource
                    .read(((CSVResultListener) exp.getResultListener()).getOutputFile().getAbsolutePath());
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Failed to get instances from CSV:", e);
            throw new Exception("Error reading experiment from file:\n" + e);
        }
    } else if (exp.getResultListener() instanceof DatabaseResultListener) {
        dbListener = (DatabaseResultListener) exp.getResultListener();
        try {
            tmpStr = dbListener.getResultsTableName(exp.getResultProducer());
            query = new InstanceQuery();
            query.setDatabaseURL(dbListener.getDatabaseURL());
            query.setUsername(dbListener.getUsername());
            query.setPassword(dbListener.getPassword());
            query.setDebug(dbListener.getDebug());
            query.connectToDatabase();
            result = query.retrieveInstances("SELECT * FROM " + tmpStr);
            query.disconnectFromDatabase();
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Failed to get instances from database:", e);
            throw new Exception("Error reading experiment from database:\n" + e);
        }
    } else {
        throw new Exception(
                "Unsupported ResultListener '" + exp.getResultListener().getClass().getName() + "'!");
    }

    return result;
}