📄 assoicationrulesview.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-14
*
* $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.io.File;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import weka.core.Utils;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Models.AssociationRules.AssociationRulesMiningModel;
import com.prudsys.pdm.Models.AssociationRules.AssociationRulesSettings;
import com.prudsys.pdm.Models.AssociationRules.ItemSet;
import com.prudsys.pdm.Models.AssociationRules.RuleSet;
import eti.bi.alphaminer.operation.result.OperatorResult;
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.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.NumberFormatter;
/**
*
* Take the AssociationRulesMiningModel rule as input. Output a JPanel withe the
* a JScrollPane in the BorderLayout.CENTER. The rules details are stored in a
* JTable attached in the JScrollPane
*
* @author TWang Jan 21, 2005
*
*/
public class AssoicationRulesView extends ResultView {
/**
*
*/
private static final long serialVersionUID = 1L;
private JScrollPane m_RulesScrollPane = new JScrollPane();
private JTable m_RuleTable = new JTable();
private String[] m_RuleTableHeaderAdvance = { Resource.srcStr("ASSOCIATION_RULE_NO"),
Resource.srcStr("ASSOCIATION_RULE"), Resource.srcStr("ASSOCIATION_ITEM"),
Resource.srcStr("ASSOCIATION_SUPPORT"), Resource.srcStr("ASSOCIATION_CONFIDENCE"),
Resource.srcStr("ASSOCIATION_LIFT"), Resource.srcStr("ASSOCIATION_COVERAGE"),
Resource.srcStr("ASSOCIATION_COSINE") };
private Object[][] m_RuleTableContentAdvance;
private Class[] m_RuleTableTypeAdvance;
private String[] m_RuleTableHeaderSimple = { Resource.srcStr("ASSOCIATION_RULE_NO"),
Resource.srcStr("ASSOCIATION_RULE"), Resource.srcStr("ASSOCIATION_ITEM"),
Resource.srcStr("ASSOCIATION_SUPPORT"), Resource.srcStr("ASSOCIATION_CONFIDENCE"), };// ,
// "Lift",
// "Coverage(%)",
// "Cosine"
// };
private Object[][] m_RuleTableContentSimple;
private Class[] m_RuleTableTypeSimple;
SortingDataGridModel m_DataGridModelSimple = null;
SortingDataGridModel m_DataGridModelAdvance = null;
private AssociationRulesMiningModel m_AssociationModel;
public AssoicationRulesView(AssociationRulesMiningModel a_model, OperatorResult a_OperatorResult)
throws MiningException {
super(Resource.srcStr("RulesView"), a_OperatorResult);
// viewType is used to enable menu.
m_ViewType = ResultView.TYPE_TABLE;
m_AssociationModel = a_model;
this.setLayout(new BorderLayout());
this.add(m_RulesScrollPane, BorderLayout.CENTER);
m_RulesScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
m_RulesScrollPane.getViewport().setBackground(Color.white);
m_RulesScrollPane.getViewport().add(m_RuleTable, null);
createView();
}
private void createView() throws MiningException {
// Get rules and large itemsets from model:
Vector rules = m_AssociationModel.getAssociationRules();
// String[] ruleTableHeader = null;
// ruleTableHeader = m_RuleTableHeaderDemo;
// ruleTableHeader = m_RuleTableHeaderOrig;
// Get item and transaction attributes:
CategoricalAttribute itemId = (CategoricalAttribute) ((AssociationRulesSettings) m_AssociationModel
.getMiningSettings()).getItemId();
int nRules = rules.size();
// Show all association rules for advance and simple view:
m_RuleTableContentAdvance = new Object[nRules][m_RuleTableHeaderAdvance.length];
m_RuleTableTypeAdvance = new Class[m_RuleTableHeaderAdvance.length];
m_RuleTableContentSimple = new Object[nRules][m_RuleTableHeaderSimple.length];
m_RuleTableTypeSimple = new Class[m_RuleTableHeaderSimple.length];
for (int i = 0; i < nRules; i++) {
// Get and show rule:
RuleSet rs = (RuleSet) rules.elementAt(i);
String ruleString = new String();
int itemSize = rs.getSize();
// Premise part of rule:
ItemSet is = rs.getPremise();
int nprem = rs.getPremise().getSize();
// get the first item in the premmise
int firstItem = is.getItemAt(0);
ruleString = ruleString.concat(((Category) itemId.getCategory(firstItem)).getValue() + "");
// get the rest items in the premmise
for (int j = 1; j < nprem; j++) {
int pN = is.getItemAt(j);
Category cat = (Category) itemId.getCategory(pN);
ruleString = ruleString.concat(", " + cat.getValue());
}
;
ruleString = ruleString.concat(" => ");
// Conclusion part of rule:
// get the first item in the conclusion
int firstConItem = rs.getConclusion().getItemAt(0);
ruleString = ruleString.concat(((Category) itemId.getCategory(firstConItem)).getValue() + "");
// get the rest items in the conclusion
for (int j = nprem + 1; j < itemSize; j++) {
int pN = rs.getConclusion().getItemAt(j - nprem);
Category cat = (Category) itemId.getCategory(pN);
ruleString = ruleString.concat(", " + cat.getValue());
}
// Show the rule index for advance and simple view
m_RuleTableContentAdvance[i][0] = new Integer(i + 1);
m_RuleTableTypeAdvance[0] = Integer.class;
m_RuleTableContentSimple[i][0] = new Integer(i + 1);
m_RuleTableTypeSimple[0] = Integer.class;
// Show the rule content for advance and simple view
m_RuleTableContentAdvance[i][1] = new String(ruleString);
m_RuleTableTypeAdvance[1] = String.class;
m_RuleTableContentSimple[i][1] = new String(ruleString);
m_RuleTableTypeSimple[1] = String.class;
// Show support and confidence of rule for advance and simple view:
double Support = rs.getSupport() * 100.0;
double Confidence = rs.getConfidence() * 100.0;
m_RuleTableContentAdvance[i][2] = new Integer(rs.getSize());
m_RuleTableTypeAdvance[2] = Integer.class;
m_RuleTableContentAdvance[i][3] = new Double(Utils.roundDouble(Support, NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeAdvance[3] = Double.class;
m_RuleTableContentAdvance[i][4] = new Double(Utils.roundDouble(Confidence,
NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeAdvance[4] = Double.class;
m_RuleTableContentSimple[i][2] = new Integer(rs.getSize());
m_RuleTableTypeSimple[2] = Integer.class;
m_RuleTableContentSimple[i][3] = new Double(Utils.roundDouble(Support, NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeSimple[3] = Double.class;
m_RuleTableContentSimple[i][4] = new Double(Utils.roundDouble(Confidence,
NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeSimple[4] = Double.class;
// Additional measures for advance view only:
m_AssociationModel.buildLargeItemSets();
double Coverage = m_AssociationModel.coverage(rs) * 100.0;
double Lift = m_AssociationModel.lift(rs);
double Cosine = m_AssociationModel.cosine(rs);
m_RuleTableContentAdvance[i][5] = new Double(Utils.roundDouble(Lift, NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeAdvance[5] = Double.class;
m_RuleTableContentAdvance[i][6] = new Double(Utils
.roundDouble(Coverage, NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeAdvance[6] = Double.class;
m_RuleTableContentAdvance[i][7] = new Double(Utils.roundDouble(Cosine, NumberFormatter.MAX_FRACTION_DIGIT));
m_RuleTableTypeAdvance[7] = Double.class;
}
// m_RuleTable.setModel(new DataGridModel(m_RuleTableContent,
// ruleTableHeader, m_RuleTableType));
SortingDataGridModel m_DataGridModelSimple = new SortingDataGridModel(m_RuleTableContentSimple,
m_RuleTableHeaderSimple, m_RuleTableTypeSimple);
m_DataGridModelSimple.addMouseListenerToHeader(m_RuleTable);
SortingDataGridModel m_DataGridModelAdvance = new SortingDataGridModel(m_RuleTableContentAdvance,
m_RuleTableHeaderAdvance, m_RuleTableTypeAdvance);
m_DataGridModelAdvance.addMouseListenerToHeader(m_RuleTable);
// Set simple model by default
m_RuleTable.setModel(m_DataGridModelSimple);
m_RuleTable.setCellSelectionEnabled(true);
m_RuleTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setColumnWidth();
showSimple();
}
public void setColumnWidth() {
int stand_width = 80;
int rule_width = 300;
m_RuleTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnModel tcm = m_RuleTable.getColumnModel();
// set width
for (int i = 1; i < tcm.getColumnCount(); i++) {
TableColumn col = tcm.getColumn(i);
if (i == 1) {
col.setPreferredWidth(rule_width);
col.setMinWidth(rule_width);
} else {
col.setPreferredWidth(stand_width);
col.setMinWidth(stand_width);
}
}
}
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_RuleTable, 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_RuleTable, exportFile, true);
aExporter.export();
}
}
// tyleung 20/4/2005>>
}
public void showAdvance() {
m_DataGridModelAdvance = new SortingDataGridModel(m_RuleTableContentAdvance, m_RuleTableHeaderAdvance,
m_RuleTableTypeAdvance);
m_RuleTable.setModel(m_DataGridModelAdvance);
m_DataGridModelAdvance.addMouseListenerToHeader(m_RuleTable);
setColumnWidth();
}
public void showSimple() {
SortingDataGridModel m_DataGridModelSimple = new SortingDataGridModel(m_RuleTableContentSimple,
m_RuleTableHeaderSimple, m_RuleTableTypeSimple);
m_RuleTable.setModel(m_DataGridModelSimple);
m_DataGridModelSimple.addMouseListenerToHeader(m_RuleTable);
setColumnWidth();
}
}
class MyTableCellRenderer implements TableCellRenderer {
/*
* (non-Javadoc)
*
* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
* java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
// TODO Auto-generated method stub
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -