MOA 12.03
Real Time Analytics for Data Streams
ClusteringSetupTab.java
Go to the documentation of this file.
00001 /*
00002  *    ClusteringSetupTab.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 
00021 package moa.gui.clustertab;
00022 
00023 import java.awt.event.ActionListener;
00024 import java.io.BufferedReader;
00025 import java.io.File;
00026 import java.io.FileReader;
00027 import javax.swing.JFileChooser;
00028 import moa.clusterers.AbstractClusterer;
00029 import moa.evaluation.MeasureCollection;
00030 import moa.gui.FileExtensionFilter;
00031 import moa.gui.TextViewerPanel;
00032 import moa.streams.clustering.ClusteringStream;
00033 
00034 public class ClusteringSetupTab extends javax.swing.JPanel {
00035     private ClusteringTabPanel clusteringTab;
00036     private String lastfile;
00037 
00039     public ClusteringSetupTab() {
00040         initComponents();
00041         clusteringAlgoPanel0.renderAlgoPanel();
00042     }
00043 
00044     public AbstractClusterer getClusterer0(){
00045         return clusteringAlgoPanel0.getClusterer0();
00046     }
00047 
00048     public AbstractClusterer getClusterer1(){
00049         return clusteringAlgoPanel0.getClusterer1();
00050     }
00051 
00052     public ClusteringStream getStream0(){
00053         return clusteringAlgoPanel0.getStream();
00054     }
00055 
00056     public MeasureCollection[] getMeasures(){
00057         return clusteringEvalPanel1.getSelectedMeasures();
00058     }
00059 
00060     public TextViewerPanel getLogPanel(){
00061         return logPanel;
00062     }
00063 
00064     public void addButtonActionListener(ActionListener l){
00065         buttonWeka.addActionListener(l);
00066         buttonWeka.setActionCommand("weka export");
00067         buttonExport.addActionListener(l);
00068         buttonExport.setActionCommand("csv export");
00069     }
00075     @SuppressWarnings("unchecked")
00076     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00077     private void initComponents() {
00078         java.awt.GridBagConstraints gridBagConstraints;
00079 
00080         clusteringAlgoPanel0 = new moa.gui.clustertab.ClusteringAlgoPanel();
00081         clusteringEvalPanel1 = new moa.gui.clustertab.ClusteringEvalPanel();
00082         buttonStart = new javax.swing.JButton();
00083         buttonStop = new javax.swing.JButton();
00084         buttonExport = new javax.swing.JButton();
00085         buttonWeka = new javax.swing.JButton();
00086         buttonImportSettings = new javax.swing.JButton();
00087         buttonExportSettings = new javax.swing.JButton();
00088         logPanel = new moa.gui.TextViewerPanel();
00089 
00090         setLayout(new java.awt.GridBagLayout());
00091 
00092         clusteringAlgoPanel0.setMinimumSize(new java.awt.Dimension(335, 150));
00093         clusteringAlgoPanel0.setPanelTitle("Cluster Algorithm Setup");
00094         clusteringAlgoPanel0.setPreferredSize(new java.awt.Dimension(335, 150));
00095         gridBagConstraints = new java.awt.GridBagConstraints();
00096         gridBagConstraints.gridx = 0;
00097         gridBagConstraints.gridy = 0;
00098         gridBagConstraints.gridwidth = 4;
00099         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00100         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
00101         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00102         add(clusteringAlgoPanel0, gridBagConstraints);
00103         gridBagConstraints = new java.awt.GridBagConstraints();
00104         gridBagConstraints.gridx = 4;
00105         gridBagConstraints.gridy = 0;
00106         gridBagConstraints.gridwidth = 2;
00107         gridBagConstraints.gridheight = 4;
00108         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00109         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
00110         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00111         add(clusteringEvalPanel1, gridBagConstraints);
00112 
00113         buttonStart.setText("Start");
00114         buttonStart.setPreferredSize(new java.awt.Dimension(80, 23));
00115         buttonStart.addActionListener(new java.awt.event.ActionListener() {
00116             public void actionPerformed(java.awt.event.ActionEvent evt) {
00117                 buttonStartActionPerformed(evt);
00118             }
00119         });
00120         gridBagConstraints = new java.awt.GridBagConstraints();
00121         gridBagConstraints.gridx = 0;
00122         gridBagConstraints.gridy = 4;
00123         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00124         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00125         add(buttonStart, gridBagConstraints);
00126 
00127         buttonStop.setText("Stop");
00128         buttonStop.setEnabled(false);
00129         buttonStop.addActionListener(new java.awt.event.ActionListener() {
00130             public void actionPerformed(java.awt.event.ActionEvent evt) {
00131                 buttonStopActionPerformed(evt);
00132             }
00133         });
00134         gridBagConstraints = new java.awt.GridBagConstraints();
00135         gridBagConstraints.gridx = 1;
00136         gridBagConstraints.gridy = 4;
00137         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00138         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00139         add(buttonStop, gridBagConstraints);
00140 
00141         buttonExport.setText("Export CSV");
00142         buttonExport.setEnabled(false);
00143         gridBagConstraints = new java.awt.GridBagConstraints();
00144         gridBagConstraints.gridx = 5;
00145         gridBagConstraints.gridy = 4;
00146         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00147         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00148         add(buttonExport, gridBagConstraints);
00149 
00150         buttonWeka.setText("Weka Explorer");
00151         buttonWeka.setEnabled(false);
00152         gridBagConstraints = new java.awt.GridBagConstraints();
00153         gridBagConstraints.gridx = 4;
00154         gridBagConstraints.gridy = 4;
00155         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00156         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00157         add(buttonWeka, gridBagConstraints);
00158 
00159         buttonImportSettings.setText("Import");
00160         buttonImportSettings.addActionListener(new java.awt.event.ActionListener() {
00161             public void actionPerformed(java.awt.event.ActionEvent evt) {
00162                 buttonImportSettingsActionPerformed(evt);
00163             }
00164         });
00165         gridBagConstraints = new java.awt.GridBagConstraints();
00166         gridBagConstraints.gridx = 2;
00167         gridBagConstraints.gridy = 4;
00168         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00169         gridBagConstraints.insets = new java.awt.Insets(4, 75, 4, 4);
00170         add(buttonImportSettings, gridBagConstraints);
00171 
00172         buttonExportSettings.setText("Export");
00173         buttonExportSettings.addActionListener(new java.awt.event.ActionListener() {
00174             public void actionPerformed(java.awt.event.ActionEvent evt) {
00175                 buttonExportSettingsActionPerformed(evt);
00176             }
00177         });
00178         gridBagConstraints = new java.awt.GridBagConstraints();
00179         gridBagConstraints.gridx = 3;
00180         gridBagConstraints.gridy = 4;
00181         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00182         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00183         add(buttonExportSettings, gridBagConstraints);
00184         gridBagConstraints = new java.awt.GridBagConstraints();
00185         gridBagConstraints.gridx = 0;
00186         gridBagConstraints.gridy = 5;
00187         gridBagConstraints.gridwidth = 7;
00188         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
00189         gridBagConstraints.weightx = 1.0;
00190         gridBagConstraints.weighty = 1.0;
00191         gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
00192         add(logPanel, gridBagConstraints);
00193     }// </editor-fold>//GEN-END:initComponents
00194 
00195     private void buttonImportSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonImportSettingsActionPerformed
00196 
00197         JFileChooser fileChooser = new JFileChooser();
00198         fileChooser.setAcceptAllFileFilterUsed(true);
00199         fileChooser.addChoosableFileFilter(new FileExtensionFilter("txt"));
00200         if(lastfile!=null)
00201             fileChooser.setSelectedFile(new File(lastfile));
00202         if (fileChooser.showOpenDialog(this.buttonImportSettings) == JFileChooser.APPROVE_OPTION) {
00203             lastfile = fileChooser.getSelectedFile().getPath();
00204             loadOptionsFromFile(fileChooser.getSelectedFile().getPath());
00205         }
00206     }//GEN-LAST:event_buttonImportSettingsActionPerformed
00207 
00208     private void buttonExportSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExportSettingsActionPerformed
00209         StringBuffer sb = new StringBuffer();
00210         sb.append(clusteringAlgoPanel0.getStreamValueAsCLIString()+"\n");
00211         sb.append(clusteringAlgoPanel0.getAlgorithm0ValueAsCLIString()+"\n");
00212         sb.append(clusteringAlgoPanel0.getAlgorithm1ValueAsCLIString()+"\n");
00213         
00214         System.out.println(sb);
00215         logPanel.addText(sb.toString());
00216         
00217     }//GEN-LAST:event_buttonExportSettingsActionPerformed
00218 
00219     private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStartActionPerformed
00220         toggle(true);
00221     }//GEN-LAST:event_buttonStartActionPerformed
00222 
00223     private void buttonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopActionPerformed
00224         stop(true);
00225     }//GEN-LAST:event_buttonStopActionPerformed
00226 
00227 
00228     private void loadOptionsFromFile(String filepath){
00229         try {
00230             BufferedReader in = new BufferedReader(new FileReader(filepath));
00231 
00232             String stream0 = in.readLine();
00233             clusteringAlgoPanel0.setStreamValueAsCLIString(stream0);
00234 
00235             String algo0 = in.readLine();
00236             clusteringAlgoPanel0.setAlgorithm0ValueAsCLIString(algo0);
00237 
00238             String algo1 = in.readLine();
00239             clusteringAlgoPanel0.setAlgorithm1ValueAsCLIString(algo1);
00240 
00241 
00242             System.out.println("Loading settings from "+filepath);
00243             logPanel.addText("Loading settings from "+filepath);
00244 
00245         } catch (Exception e) {
00246             System.out.println("Bad option file:"+e.getMessage());
00247         }
00248     }
00249 
00250     // Variables declaration - do not modify//GEN-BEGIN:variables
00251     private javax.swing.JButton buttonExport;
00252     private javax.swing.JButton buttonExportSettings;
00253     private javax.swing.JButton buttonImportSettings;
00254     private javax.swing.JButton buttonStart;
00255     private javax.swing.JButton buttonStop;
00256     private javax.swing.JButton buttonWeka;
00257     private moa.gui.clustertab.ClusteringAlgoPanel clusteringAlgoPanel0;
00258     private moa.gui.clustertab.ClusteringEvalPanel clusteringEvalPanel1;
00259     private moa.gui.TextViewerPanel logPanel;
00260     // End of variables declaration//GEN-END:variables
00261 
00262     void setClusteringTab(ClusteringTabPanel clusteringTab) {
00263         this.clusteringTab = clusteringTab;
00264     }
00265 
00266     public void toggleRunMode(){
00267         toggle(false);
00268     }
00269 
00270     public void stopRun(){
00271         stop(false);
00272     }
00273 
00274     private void toggle(boolean internal) {
00275         setStateConfigButtons(false);
00276         if(buttonStart.getText().equals("Pause")){
00277             buttonStart.setText("Resume");
00278             buttonWeka.setEnabled(true);
00279             buttonExport.setEnabled(true);
00280         }
00281         else{
00282             buttonStart.setText("Pause");
00283             buttonWeka.setEnabled(false);
00284             buttonExport.setEnabled(false);
00285         }
00286 
00287         //push event forward to the cluster tab
00288         if(internal)
00289             clusteringTab.toggle();
00290     }
00291 
00292 
00293     private void stop(boolean internal) {
00294         buttonStart.setEnabled(true);
00295         buttonStart.setText("Start");
00296         buttonStop.setEnabled(false);
00297         buttonWeka.setEnabled(false);
00298         buttonExport.setEnabled(false);
00299         setStateConfigButtons(true);
00300 
00301         //push event forward to the cluster tab
00302         if(internal)
00303             clusteringTab.stop();
00304     }
00305 
00306     private void setStateConfigButtons(boolean state){
00307         buttonStop.setEnabled(!state);
00308         buttonExportSettings.setEnabled(state);
00309         buttonImportSettings.setEnabled(state);
00310     }
00311 
00312 
00313 }
 All Classes Namespaces Files Functions Variables Enumerations