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

📄 clusteringdataview.java

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

/*
 * Created on 2005-1-22
 *
  */
package eti.bi.alphaminer.patch.standard.operation.result.view;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;


import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Models.Clustering.ClusteringMiningModel;

import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.datamodel.SortingDataGridModel;
import eti.bi.alphaminer.operation.result.export.ExcelExporter;
import eti.bi.alphaminer.operation.result.renderer.DataCellRenderer;
import eti.bi.alphaminer.patch.standard.operation.operator.KMeansOperator;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IBIObject;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.NumberFormatter;

/**
 * Take ClusteringOperator as input. Output a JPanel shown all the
 * records in the operator's MiningStoredData set. The class name
 * and the distance to the class center are also displayed. 
 * 
 * @author TWang	On Jan 22, 2005.
 * 
 */
public class ClusteringDataView extends ResultView {
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// JTable that shows the data
	private JTable m_DataTable;
	private String[] m_DataTableHeader;
	private Object[][] m_DataTableContent;
	private Class[] m_DataTableType; 
	
	// JScrollPane that contains JTable
	private JScrollPane m_ScrollPane;
  
	private KMeansOperator m_ClusteringOperator;
	private ClusteringMiningModel m_ClusteringModel;
	private MiningAttribute[] m_Attributes;  
	private MiningStoredData m_MiningStoredData;
	private MiningDataSpecification m_MetaData;
 	private Vector m_Predicted;
 	private Vector m_Distances;
 	
	/**
	 * @param a_ClusteringModel
	 * @throws SysException
	 * @throws MiningException
	 */
	public ClusteringDataView(KMeansOperator a_ClusteringModel) throws SysException, MiningException{
		super(Resource.srcStr("DataView"));	  
		
		m_ViewType = ResultView.TYPE_DATA; 
		m_ClusteringOperator = a_ClusteringModel;
		
		// The condition actually is also juded 
		IBIObject aBIObject = m_ClusteringOperator.getOutputBIObject();
		if (aBIObject == null || aBIObject.getBIModel() == null
				|| aBIObject.getBIData() == null) {
			
			new SysException("The OutputBIObject in the ClusteringOperator is NULL");

		}
		
		IBIModel aBIModel = aBIObject.getBIModel();
		IBIData aBIData = aBIObject.getBIData();
		m_ClusteringModel = (ClusteringMiningModel) aBIModel.getMiningModel(); 
	 
		// Get the predicted class and the distances vectors
		m_Predicted = m_ClusteringOperator.getPredicted();
		m_Distances = m_ClusteringOperator.getDistances();
		
		m_MetaData = aBIData.getMetaData(); 
		m_MiningStoredData = aBIData.getMiningStoredData(); 
		m_Attributes = m_MetaData.getAttributesArray(); 
		 
		m_ScrollPane = new JScrollPane(); 
		
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
		this.add(m_ScrollPane, BorderLayout.CENTER);
 		createView();
 	}
	
	/* 
	 * Create the view. 
	 */
	public void createView() throws SysException, MiningException
	{
		if (m_ClusteringModel == null){
			throw new SysException("clustering model is null");
		}
		if (m_ClusteringModel != null)
		{   
			createDataTable(m_ScrollPane); 
				
			m_ScrollPane.setPreferredSize(new Dimension(200, 73));
			m_ScrollPane.getViewport().add(m_DataTable);
			m_ScrollPane.getViewport().setBackground(Color.WHITE);  
		}
	}
	
	/**
	 * 
	 * Create the JTable and attach it in the ScrollPane.   
	 *  
	 * @param a_ScrollPane
	 */
	@SuppressWarnings("unchecked")
	private void createDataTable(JScrollPane a_ScrollPane) {
		// Three columns more to display index, class, and distance information.
		int column = m_Attributes.length + 3;
		m_DataTableType = new Class[column];
		m_DataTableHeader = new String[column]; 
		MiningAttribute attribute = null; 
 
		// Create JTable header and JTable class type.
		for (int coloumIndex = 0; coloumIndex < column; coloumIndex++) {
			if (coloumIndex == 0){ 
				m_DataTableType[coloumIndex] = Integer.class;
				m_DataTableHeader[coloumIndex] = Resource.srcStr("CLUSTER_INDEX");
				continue;  
			}
			if (coloumIndex == column - 1) {
				m_DataTableType[coloumIndex] = Double.class;
				m_DataTableHeader[coloumIndex] = Resource.srcStr("CLUSTER_DISTANCE");
				continue;
			}
			if (coloumIndex == column - 2) {
				m_DataTableType[coloumIndex] = String.class;
				m_DataTableHeader[coloumIndex] = Resource.srcStr("CLUSTER_TITLE");
				continue;
			}
			attribute = m_Attributes[coloumIndex-1];
			m_DataTableHeader[coloumIndex] = attribute.getName();
			m_DataTableType[coloumIndex] = String.class; 
			
			if (attribute instanceof NumericAttribute) {
				
				int dataType = ((NumericAttribute) attribute).getDataType();
				
				if (dataType == NumericAttribute.DOUBLE)
					m_DataTableType[coloumIndex] = Double.class;
				else if (dataType == NumericAttribute.FLOAT)
					m_DataTableType[coloumIndex] = Float.class;
				else if (dataType == NumericAttribute.INTEGER)
					m_DataTableType[coloumIndex] = Integer.class;
				else if (dataType == NumericAttribute.BOOLEAN)
					m_DataTableType[coloumIndex] = Boolean.class; 
			} 
			else if (attribute instanceof CategoricalAttribute) {
				
				int dataType = ((CategoricalAttribute) attribute).getDataType();

				if (dataType == CategoricalAttribute.BOOLEAN)
					m_DataTableType[coloumIndex] = Boolean.class; 
				else 
					m_DataTableType[coloumIndex] = String.class; 
			}
		}
		 
		// Fill the JTable content.
		if (m_MiningStoredData != null) {
			ArrayList list = m_MiningStoredData.getMiningVectors();
			Object[][] content = new Object[list.size()][column];
			Vector<Comparable> allVec = null; 
			MiningVector vec = null; 
			
			for (int i = 0; i < list.size(); i++) {
				vec = (MiningVector) list.get(i);
			    allVec = vec.toVector();
				allVec.insertElementAt(new Integer(i+1), 0); 
				allVec.addElement(m_Predicted.elementAt(i).toString()); 
				allVec.addElement( new Double( weka.core.Utils.roundDouble(((Double)m_Distances.elementAt(i)).doubleValue(), NumberFormatter.MAX_FRACTION_DIGIT) ) );
				content[i] =allVec.toArray(); 
			}
			m_DataTableContent = content; 
		}

		// Create Table			
		m_DataTable = new JTable();
		//m_DataTable.setModel(new DataGridModel(m_DataTableContent, m_DataTableHeader, m_DataTableType));
		SortingDataGridModel model = new SortingDataGridModel(m_DataTableContent, m_DataTableHeader, m_DataTableType);
		m_DataTable.setModel(model);
 		m_DataTable.setDefaultRenderer(String.class, new DataCellRenderer(true));
		m_DataTable.setDefaultRenderer(Short.class, new DataCellRenderer(true));
		m_DataTable.setDefaultRenderer(Long.class, new DataCellRenderer(true));
		m_DataTable.setDefaultRenderer(Integer.class, new DataCellRenderer(true));
		m_DataTable.setDefaultRenderer(Double.class, new DataCellRenderer(true));
		m_DataTable.setDefaultRenderer(Float.class, new DataCellRenderer(true)); 
		
		model.addMouseListenerToHeader(m_DataTable); 
		setColumnWidth();
	}
	
	
	public void setColumnWidth() {
			TableColumnModel tcm = m_DataTable.getColumnModel();
		 
			for (int i=1; i<tcm.getColumnCount(); i++)
				tcm.getColumn(i).setMinWidth(60);
			
			m_DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			
			tcm.getColumn(0).setPreferredWidth(60);
			tcm.getColumn(0).setCellRenderer(m_DataTable.getTableHeader().getDefaultRenderer()); 
	}
	
	/**
	 * Exort the data table into an excel file.
	 * 
	 * Called by the OperatorResult class. The subclass of OperatorResult must 
	 * call m_SelectedView.export() explictly if it overwirte the export() function 
	 * of OperatorResult.
	 */
	public void export() throws AppException, SysException {
		// Use user home directory 
		File directory = new File(System.getProperty("user.dir"));

		// Create and initialize file chooser for excel
		JFileChooser chooser = new JFileChooser(directory);
		chooser.setDialogTitle(Resource.srcStr("FileExport"));
		chooser.setFileFilter(ExcelExporter.FILTER);
		chooser.setSelectedFile(ExcelExporter.DEFAULT_FILE);

  		// pop up the file chooser dialog and return the file value
		int returnVal = chooser.showSaveDialog(this);	  
		if (returnVal == JFileChooser.APPROVE_OPTION) 
		{
			File exportFile = chooser.getSelectedFile();
			
			//<<tyleung 20/4/2005 
			if (exportFile.exists()) {
				int option = JOptionPane
						.showConfirmDialog(
								(Component) this,
								"The file  \""
										+ exportFile.getName()
										+ "\""
										+ " already exists. Do you want to replace the existing file?",//						
								"AlphaMiner", JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE);
				if (option != JOptionPane.CANCEL_OPTION) {
					if (option == JOptionPane.YES_OPTION) {
//					  Create the excel exporter with the excel file extension enforced to be .xls
			 		  	ExcelExporter aExporter = new ExcelExporter(m_DataTable, exportFile, true);
						//ExcelExporter aExporter = new ExcelExporter(m_MiningStoredData, exportFile, true);
						aExporter.export();
						
					}else{
						returnVal = chooser.showSaveDialog(this);
					}
				}
			}else{

				// Create the excel exporter with the excel file extension enforced to be .xls
	 		  	ExcelExporter aExporter = new ExcelExporter(m_DataTable, exportFile, true);
				//ExcelExporter aExporter = new ExcelExporter(m_MiningStoredData, exportFile, true);
				aExporter.export();
			}
		}
		//tyleung 20/4/2005>>
	} 

}

⌨️ 快捷键说明

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