Example usage for weka.experiment ResultProducer getKeyTypes

List of usage examples for weka.experiment ResultProducer getKeyTypes

Introduction

In this page you can find the example usage for weka.experiment ResultProducer getKeyTypes.

Prototype

Object[] getKeyTypes() throws Exception;

Source Link

Document

Gets the data types of each of the key columns produced for a single run.

Usage

From source file:py.fpuna.lib.ExtendedDatabaseUtils.java

License:Open Source License

/**
 * Creates a results table for the supplied result producer.
 *
 * @param rp        the ResultProducer generating the results
 * @param tableName the name of the resultsTable
 * @return the name of the created results table
 * @throws Exception if an error occurs.
 *//*w  w  w .ja va 2 s  .co  m*/
public String createResultsTable(ResultProducer rp, String tableName) throws Exception {

    if (m_Debug) {
        System.err.println("Creating results table " + tableName + "...");
    }
    String query = "CREATE TABLE " + tableName + " ( ";
    // Loop over the key fields
    String[] names = rp.getKeyNames();
    Object[] types = rp.getKeyTypes();
    if (names.length != types.length) {
        throw new Exception("key names types differ in length");
    }
    for (int i = 0; i < names.length; i++) {
        query += "Key_" + names[i] + " ";
        if (types[i] instanceof Double) {
            query += m_doubleType;
        } else if (types[i] instanceof String) {

            // Workaround for MySQL (doesn't support LONGVARCHAR)
            // Also for InstantDB which attempts to interpret numbers when storing
            // in LONGVARBINARY
            /*
             * if (m_Connection.getMetaData().getDriverName().
             * equals("Mark Matthews' MySQL Driver") ||
             * (m_Connection.getMetaData().getDriverName().
             * indexOf("InstantDB JDBC Driver")) != -1) { query += "TEXT "; } else {
             */
            // query += "LONGVARCHAR ";
            query += m_stringType + " ";
            // }
        } else {
            throw new Exception("Unknown/unsupported field type in key");
        }
        query += ", ";
    }
    // Loop over the result fields
    names = rp.getResultNames();
    types = rp.getResultTypes();
    if (names.length != types.length) {
        throw new Exception("result names and types differ in length");
    }
    for (int i = 0; i < names.length; i++) {
        query += names[i] + " ";
        if (types[i] instanceof Double) {
            query += m_doubleType;
        } else if (types[i] instanceof String) {

            // Workaround for MySQL (doesn't support LONGVARCHAR)
            // Also for InstantDB which attempts to interpret numbers when storing
            // in LONGVARBINARY
            /*
             * if (m_Connection.getMetaData().getDriverName().
             * equals("Mark Matthews' MySQL Driver") ||
             * (m_Connection.getMetaData().getDriverName().
             * equals("InstantDB JDBC Driver"))) { query += "TEXT "; } else {
             */
            // query += "LONGVARCHAR ";
            query += m_stringType + " ";
            // }
        } else {
            throw new Exception("Unknown/unsupported field type in key");
        }
        if (i < names.length - 1) {
            query += ", ";
        }
    }
    query += " )";

    update(query);
    if (m_Debug) {
        System.err.println("table created");
    }
    close();

    if (m_createIndex) {
        query = "CREATE UNIQUE INDEX Key_IDX ON " + tableName + " (";

        String[] keyNames = rp.getKeyNames();

        boolean first = true;
        for (String keyName : keyNames) {
            if (keyName != null) {
                if (first) {
                    first = false;
                    query += "Key_" + keyName;
                } else {
                    query += ",Key_" + keyName;
                }
            }
        }
        query += ")";

        update(query);
    }
    return tableName;
}