⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miningsqlstream.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    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.
 */

/**
 * Title: XELOPES Data Mining Library
 * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
 * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
 * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
 * @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
 * @version 1.0
 */

package com.prudsys.pdm.Input.Relational;

import java.sql.*;

import com.prudsys.pdm.Core.*;
import com.prudsys.pdm.Input.*;

/**
 * Access to SQL data source. Could be extended or replaced by more
 * sophisticated implementations.
 */
public class MiningSqlStream extends MiningInputStream
{
    //<<Frank Xu, 01/12/2004
    //Add functionality to identify the type of categorical data.
    //protected int m_BoundedThreshold = 20;
    //>>Frank Xu, 01/12/2004

    // -----------------------------------------------------------------------
    //  Variables declarations
    // -----------------------------------------------------------------------
    /** Object defining SQL source. */
    protected MiningSqlSource dataSource;

    /** Result set of JDBC. */
    protected ResultSet rs;

    /** Supported cursor type. */
    protected int cursorType;

    /** SQL statement to be executed. */
    protected String request;

    /** Contains the objects used as missing values. */
    protected Object[] usedAsMissingValues = null;

    // -----------------------------------------------------------------------
    //  Constructors
    // -----------------------------------------------------------------------
    /**
     * Empty constructor.
     */
    public MiningSqlStream()
    {
    }

    /**
     * MiningSqlStream from data dource and SQL statement.
     * 
     * @param dataSource
     *            JDBC address of data source
     * @param request
     *            SQL statement to be executed
     * @exception MiningException
     *                could not recognize format
     * @exception IllegalArgumentException
     *                wrong specification of data source
     */
    public MiningSqlStream(MiningSqlSource dataSource, String request)
            throws MiningException, IllegalArgumentException
    {
        this.dataSource = dataSource;
        this.request = request;
        reset();
    }

    // -----------------------------------------------------------------------
    //  Getter and setter methods
    // -----------------------------------------------------------------------
    /**
     * Returns supported stream methods.
     * 
     * @return supported stream methods
     */
    public java.util.Enumeration getSupportedStreamMethods()
    {

        java.util.Vector suppmeth = new java.util.Vector();
        suppmeth.addElement("recognize");
        suppmeth.addElement("reset");
        suppmeth.addElement("move");
        return suppmeth.elements();
    }

    /**
     * Returns physical database model (CWM Resource Package "Relational").
     * 
     * @exception MiningException
     *                couldn't obtain physical model
     */
    public void findPhysicalModel() throws MiningException
    {

        physicalModel = dataSource.getPhysicalModel();
    }

    /**
     * Returns the CWM mapping from the physical to the logical data model.
     * 
     * @return transformation of physical to logical data model
     * @throws MiningException
     *             couldn't get transformation
     */
    public org.omg.cwm.analysis.transformation.TransformationMap getPhysicalToLogicalModelTransformation()
            throws MiningException
    {
        throw new MiningException("not supported");
    }

    /**
     * Returns all values used as missing values. The missing values are kept in
     * {@link #usedAsMissingValues}.
     * 
     * @return missing values as array
     */
    public Object[] getMissingValues()
    {
        return usedAsMissingValues;
    }

    /**
     * Sets the string values used as missing values.
     * 
     * @param values
     *            missing values as String array.
     */
    public void setMissingValues(Object[] values)
    {
        this.usedAsMissingValues = values;
    }

    // -----------------------------------------------------------------------
    //  General stream methods
    // -----------------------------------------------------------------------
    /**
     * Open mining file stream. This method can be left because does nothing.
     * 
     * @exception MiningException
     *                if a mining source access error occurs
     */
    public void open() throws MiningException
    {
    }

    /**
     * Close mining sql stream by closing result set.
     * 
     * @exception MiningException
     *                if a mining source access error occurs
     */
    public void close() throws MiningException
    {
        try
        {
            rs.close();
        } catch (Exception ex)
        {
            throw new MiningException("Can't close reader from result set: "
                    + rs);
        }
        ;
    }

    /**
     * Parses result set to obtain meta data.
     * 
     * @return meta data recognized
     * @throws MiningException
     *             cannot create meta data
     */
    public MiningDataSpecification recognize() throws MiningException
    {
        try
        {
            int cursorType = rs.getType();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();

            MiningDataSpecification metaData = new MiningDataSpecification();
            metaData.setRelationName("sql");
            String name = null;
            for (int i = 1; i <= columnCount; i++)
            {
                name = rsmd.getColumnName(i);

                int type = rsmd.getColumnType(i);
                int numerictype = -1;
                switch (type)
                {
	                case Types.BIGINT:
	                	//numerictype = NumericAttribute.INTEGER; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.DECIMAL:
	                	//numerictype = NumericAttribute.DOUBLE; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.DOUBLE:
	                	//numerictype = NumericAttribute.DOUBLE; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.FLOAT:
	                	//numerictype = NumericAttribute.FLOAT; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.INTEGER:
	                	//numerictype = NumericAttribute.INTEGER; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.NUMERIC:
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.REAL:
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.SMALLINT:
	                	//numerictype = NumericAttribute.INTEGER; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                case Types.TINYINT:
	                	//numerictype = NumericAttribute.INTEGER; remarked this phrase by Joyce 2005/03/09
	                	numerictype = NumericAttribute.DOUBLE;
	                	break;
	                default:
	                	break;
                }
            	// If it's not numeric data, numerictype should be -1
                if (numerictype==NumericAttribute.INTEGER || numerictype==NumericAttribute.FLOAT || 
                	numerictype==NumericAttribute.DOUBLE)
                {
                	NumericAttribute numAtt = new NumericAttribute(name);
                    numAtt.setDataType(numerictype);
                    metaData.addMiningAttribute(numAtt);
                }else
                {
                    CategoricalAttribute catAtt = new CategoricalAttribute(name);
                    //<<Frank Xu, 01/12/2004
                    //Add functionality to identify the type of categorical
                    // data.
                    //catAtt.setUnboundedCategories(true);
                    //int distinctCount = 0;

                    //<<Franky Chan, 16/02/2005
                    String sql;
                    if (rsmd.getTableName(1).indexOf("&")>= 0){
                    	  sql = "select distinct(" + name + ") from \""
                         + rsmd.getTableName(1) + "\""; 
                    }
                    else{
                         sql = "select distinct(" + name + ") from "
                            + rsmd.getTableName(1) ; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -