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

📄 decisiontreetreeview.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/13
 *
 * $Author$
 * $Date$
 * $Revision$ 
 */
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.awt.Label;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import weka.classifiers.trees.J48;
import weka.classifiers.trees.j48.ClassifierTree;

import com.prudsys.pdm.Core.MiningException;

import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.export.TextExporter;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;

/**
 * 
 * Take J48 classifier as input. Build the label of each tree node recursively in the 
 * ClassifierTree.toString() method. Modifications are made in J48, ClassifierTree 
 * classess accordingly. Afterwards, build the JTree recursively from the ClassifierTree
 * class. Output a JPanel (JTree) shown all results. 
 *  
 * @author TWang On Jan 26, 2005.
 * 
 */
public class DecisionTreeTreeView extends ResultView{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTree m_Tree;  
	private JScrollPane m_ScrollPane; 
	// The tree content string
	private String m_outPutString;
	 
	private J48 m_J48Classifier;  
	
	public DecisionTreeTreeView(J48 a_classifier) throws SysException, MiningException{
		super(Resource.srcStr("DECISIONTREE_VIEW_NAME")); 
		m_ViewType = ResultView.TYPE_TEXT;
		
		if (a_classifier == null) { 
			throw new SysException("The J48 Classifier in the DecisionOperator is NULL"); 
		}	 
		m_J48Classifier = a_classifier;	
	 
		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. 
	 * @throws SysException
	 * @throws MiningException
	 */
	public void createView() throws SysException, MiningException
	{ 
		    ClassifierTree classifierTree = m_J48Classifier.getRoot();
		    
		    // Recusively build the label of each tree node. Many modifications 
		    // are made in ClassifierTree class. Pls refer for details.
		    m_outPutString = classifierTree.toString();
		    
		    if (classifierTree != null){
		     	DefaultMutableTreeNode root = new DefaultMutableTreeNode(Resource.srcStr("DECISIONTREE_ROOT"));
 		     	createTreeRecursively(root, classifierTree);
		     	
		    	m_Tree = new JTree(root);	
  		    	m_Tree.setCellRenderer(new DecisionTreeTreeViewCellRenderer());
  		    	m_Tree.setRowHeight(40); 
		    	
		    	m_ScrollPane.setPreferredSize(new Dimension(200, 70));
		    	m_ScrollPane.getViewport().add(m_Tree);
		    }else {
		    	m_ScrollPane.getViewport().add(new Label("The tree is empty."));
		    }
		    
			m_ScrollPane.getViewport().setBackground(Color.WHITE); 
	} 

	
	/**
	 * Build the JTree recursively from the ClassifierTree class.
	 * 
	 * @param a_Father
	 * @param a_ClassifierTree
	 */
	private void createTreeRecursively(DefaultMutableTreeNode a_Father, ClassifierTree a_ClassifierTree ){
		ClassifierTree[] childClassifierTrees = a_ClassifierTree.getClassifierTreeChildren();
		if(childClassifierTrees != null){
			for(int i=0; i<childClassifierTrees.length; i++){
				if (childClassifierTrees[i].isLeaf()){
//					JLabel nodeLabel = new JLabel(childClassifierTrees[i].getM_NodeContent()+" DIS" + childClassifierTrees[i].getM_localModel().distribution().getClassDist());
					DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(childClassifierTrees[i]);
					a_Father.add(treeNode);
					
					//modify the color change
//					    StyledDocument d = treeNode.getStyledDocument(); 
//				       MutableAttributeSet a = new SimpleAttributeSet(); 
//				       StyleConstants.setForeground(a, Color.red);  
//				       d.setCharacterAttributes(0, 5, a, false); 
//				       StyleConstants.setForeground(a, Color.blue); 
//				       d.setCharacterAttributes(6, 9, a, false);
					//change
				}
				else{
					DefaultMutableTreeNode node = new DefaultMutableTreeNode(childClassifierTrees[i]); 
					a_Father.add(node);
					createTreeRecursively(node, childClassifierTrees[i]);				
				}
			}			
		}
	}
	
	public void export() throws SysException, AppException
 	{
		// Use user home directory 
		File directory = new File(System.getProperty("user.dir"));
		
		// Create and initialize file chooser for pmml
		JFileChooser chooser = new JFileChooser(directory);
		chooser.setDialogTitle(Resource.srcStr("DECISIONTREE_TILTLE_EXPORT"));
		chooser.setFileFilter(TextExporter.FILTER);
		chooser.setSelectedFile(TextExporter.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?",//						
								"KnowBiz", JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE);
				if (option != JOptionPane.CANCEL_OPTION) {
					if (option == JOptionPane.YES_OPTION) {
					    if (m_outPutString!=null)
						{
							TextExporter aExporter = new TextExporter(m_outPutString, exportFile, true);
							aExporter.export();
						}
						
					}else{
						returnVal = chooser.showSaveDialog(this);
					}
				}
			}else {

				if (m_outPutString!=null)
				{
					TextExporter aExporter = new TextExporter(m_outPutString, exportFile, true);
					aExporter.export();
				}
			}
			//tyleung 20/4/2005>>
		}		
 	}
}

⌨️ 快捷键说明

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