All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.experiment.DatabaseResultListener Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This is the stable version. Apart from bugfixes, this version does not receive any other updates.

There is a newer version: 3.8.6
Show newest version
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *    DatabaseResultListener.java
 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
 *
 */


package weka.experiment;

import weka.core.FastVector;
import weka.core.RevisionUtils;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

/**
 
 * Takes results from a result producer and sends them to a database.
 * 

* * @author Len Trigg ([email protected]) * @version $Revision: 5125 $ */ public class DatabaseResultListener extends DatabaseUtils implements ResultListener { /** for serialization */ static final long serialVersionUID = 7388014746954652818L; /** The ResultProducer to listen to */ protected ResultProducer m_ResultProducer; /** The name of the current results table */ protected String m_ResultsTableName; /** True if debugging output should be printed */ protected boolean m_Debug = true; /** Holds the name of the key field to cache upon, or null if no caching */ protected String m_CacheKeyName = ""; /** Stores the index of the key column holding the cache key data */ protected int m_CacheKeyIndex; /** Stores the key for which the cache is valid */ protected Object [] m_CacheKey; /** Stores the cached values */ protected FastVector m_Cache = new FastVector(); /** * Returns a string describing this result listener * @return a description of the result listener suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Takes results from a result producer and sends them to a " +"database."; } /** * Sets up the database drivers * * @throws Exception if an error occurs */ public DatabaseResultListener() throws Exception { super(); } /** * Prepare for the results to be received. * * @param rp the ResultProducer that will generate the results * @throws Exception if an error occurs during preprocessing. */ public void preProcess(ResultProducer rp) throws Exception { m_ResultProducer = rp; // Connect to the database and find out what table corresponds to this // ResultProducer updateResultsTableName(m_ResultProducer); } /** * Perform any postprocessing. When this method is called, it indicates * that no more results will be sent that need to be grouped together * in any way. * * @param rp the ResultProducer that generated the results * @throws Exception if an error occurs */ public void postProcess(ResultProducer rp) throws Exception { if (m_ResultProducer != rp) { throw new Error("Unrecognized ResultProducer calling postProcess!!"); } disconnectFromDatabase(); } /** * Determines if there are any constraints (imposed by the * destination) on any additional measures produced by * resultProducers. Null should be returned if there are NO * constraints, otherwise a list of column names should be * returned as an array of Strings. In the case of * DatabaseResultListener, the structure of an existing database * will impose constraints. * @param rp the ResultProducer to which the constraints will apply * @return an array of column names to which resutltProducer's * results will be restricted. * @throws Exception if an error occurs. */ public String [] determineColumnConstraints(ResultProducer rp) throws Exception { FastVector cNames = new FastVector(); updateResultsTableName(rp); DatabaseMetaData dbmd = m_Connection.getMetaData(); ResultSet rs; // gets a result set where each row is info on a column if (m_checkForUpperCaseNames) { rs = dbmd.getColumns(null, null, m_ResultsTableName.toUpperCase(), null); } else { rs = dbmd.getColumns(null, null, m_ResultsTableName, null); } boolean tableExists=false; int numColumns = 0; while (rs.next()) { tableExists = true; // column four contains the column name String name = rs.getString(4); if (name.toLowerCase().startsWith("measure")) { numColumns++; cNames.addElement(name); } } // no constraints on any additional measures if the table does not exist if (!tableExists) { return null; } // a zero element array indicates maximum constraint String [] columnNames = new String [numColumns]; for (int i=0;i





© 2015 - 2025 Weber Informatics LLC | Privacy Policy