MOA 12.03
Real Time Analytics for Data Streams
ClusteringAlgoPanel.java
Go to the documentation of this file.
00001 /*
00002  *    ClusteringAlgoPanel.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen (moa@cs.rwth-aachen.de)
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.BorderLayout;
00024 
00025 import java.awt.GridBagConstraints;
00026 import java.awt.GridBagLayout;
00027 import java.awt.Insets;
00028 import java.awt.event.ActionEvent;
00029 import java.awt.event.ActionListener;
00030 import java.util.ArrayList;
00031 import java.util.LinkedList;
00032 import java.util.List;
00033 import java.util.logging.Level;
00034 import java.util.logging.Logger;
00035 import javax.swing.JButton;
00036 import javax.swing.JComponent;
00037 import javax.swing.JLabel;
00038 import javax.swing.JPanel;
00039 
00040 import moa.clusterers.AbstractClusterer;
00041 import moa.clusterers.Clusterer;
00042 import moa.gui.GUIUtils;
00043 import moa.gui.OptionEditComponent;
00044 import moa.options.ClassOption;
00045 import moa.options.Option;
00046 import moa.streams.clustering.ClusteringStream;
00047 import moa.streams.generators.RandomRBFGenerator;
00048 
00049 public class ClusteringAlgoPanel extends javax.swing.JPanel implements ActionListener{
00050 
00051     protected List<OptionEditComponent> editComponents = new LinkedList<OptionEditComponent>();
00052 
00053     private ClassOption streamOption = new ClassOption("Stream", 's',
00054                 "", ClusteringStream.class,
00055                 "RandomRBFGeneratorEvents"); 
00056 
00057    private ClassOption algorithmOption0 = new ClassOption("Algorithm0", 'a',
00058                "Algorithm to use.", Clusterer.class, "ClusterGenerator");
00059 
00060    private ClassOption algorithmOption1 = new ClassOption("Algorithm1", 'c',
00061                "Comparison algorithm", Clusterer.class, "", "clustream.Clustream");
00062    
00063 
00064     public ClusteringAlgoPanel() {
00065         initComponents();
00066     }
00067 
00068     public void renderAlgoPanel(){
00069 
00070         setLayout(new BorderLayout());
00071 
00072         ArrayList<Option> options = new ArrayList<Option>();
00073         options.add(streamOption);
00074         options.add(algorithmOption0);
00075         options.add(algorithmOption1);
00076 
00077         JPanel optionsPanel = new JPanel();
00078         GridBagLayout gbLayout = new GridBagLayout();
00079         optionsPanel.setLayout(gbLayout);
00080         
00081         //Create generic label constraints
00082         GridBagConstraints gbcLabel = new GridBagConstraints();
00083         gbcLabel.gridx = 0;
00084         gbcLabel.fill = GridBagConstraints.NONE;
00085         gbcLabel.anchor = GridBagConstraints.EAST;
00086         gbcLabel.weightx = 0;
00087         gbcLabel.insets = new Insets(5, 5, 5, 5);
00088         
00089         //Create generic editor constraints
00090         GridBagConstraints gbcOption = new GridBagConstraints();
00091         gbcOption.gridx = 1;
00092         gbcOption.fill = GridBagConstraints.HORIZONTAL;
00093         gbcOption.anchor = GridBagConstraints.CENTER;
00094         gbcOption.weightx = 1;
00095         gbcOption.insets = new Insets(5, 5, 5, 0);
00096 
00097         //Stream Option
00098         JLabel labelStream = new JLabel("Stream");
00099         labelStream.setToolTipText("Stream to use.");
00100         optionsPanel.add(labelStream, gbcLabel);
00101         JComponent editorStream = streamOption.getEditComponent();
00102         labelStream.setLabelFor(editorStream);
00103         editComponents.add((OptionEditComponent) editorStream);
00104         optionsPanel.add(editorStream, gbcOption);
00105 
00106         //Algorithm0 Option
00107         JLabel labelAlgo0 = new JLabel("Algorithm1");
00108         labelAlgo0.setToolTipText("Algorithm to use.");
00109         optionsPanel.add(labelAlgo0, gbcLabel);
00110         JComponent editorAlgo0 = algorithmOption0.getEditComponent();
00111         labelAlgo0.setLabelFor(editorAlgo0);
00112         editComponents.add((OptionEditComponent) editorAlgo0);
00113         optionsPanel.add(editorAlgo0, gbcOption);
00114 
00115         //Algorithm1 Option
00116         JLabel labelAlgo1 = new JLabel("Algorithm2");
00117         labelAlgo1.setToolTipText("Comparison algorithm to use.");
00118         optionsPanel.add(labelAlgo1, gbcLabel);
00119         JComponent editorAlgo1 = algorithmOption1.getEditComponent();
00120         labelAlgo1.setLabelFor(editorAlgo1);
00121         editComponents.add((OptionEditComponent) editorAlgo1);
00122         optionsPanel.add(editorAlgo1, gbcOption);
00123 
00124         //use comparison Algorithm Option
00125         GridBagConstraints gbcClearButton = new GridBagConstraints();
00126         gbcClearButton.gridx = 2;
00127         gbcClearButton.gridy = 2;
00128         gbcClearButton.fill = GridBagConstraints.NONE;
00129         gbcClearButton.anchor = GridBagConstraints.CENTER;
00130         gbcClearButton.insets = new Insets(5, 0, 5, 5);
00131 
00132         JButton clearButton = new JButton("Clear");
00133         clearButton.addActionListener(this);
00134         clearButton.setActionCommand("clear");
00135         optionsPanel.add(clearButton, gbcClearButton);      
00136         
00137             
00138         add(optionsPanel);
00139     }
00140 
00141     public void actionPerformed(ActionEvent e) {
00142         if(e.getActionCommand().equals("clear")){
00143             algorithmOption1.setValueViaCLIString("None");
00144             editComponents.get(2).setEditState("None");
00145         }
00146     }    
00147     
00148     public AbstractClusterer getClusterer0(){
00149         AbstractClusterer c = null;
00150         applyChanges();
00151         try {
00152             c = (AbstractClusterer) ClassOption.cliStringToObject(algorithmOption0.getValueAsCLIString(), Clusterer.class, null);
00153         } catch (Exception ex) {
00154             Logger.getLogger(ClusteringAlgoPanel.class.getName()).log(Level.SEVERE, null, ex);
00155         }
00156         return c;
00157     }
00158     
00159     public AbstractClusterer getClusterer1(){
00160         AbstractClusterer c = null;
00161         applyChanges();
00162         if(!algorithmOption1.getValueAsCLIString().equals("None")){
00163             try {
00164                 c = (AbstractClusterer) ClassOption.cliStringToObject(algorithmOption1.getValueAsCLIString(), Clusterer.class, null);
00165             } catch (Exception ex) {
00166                 Logger.getLogger(ClusteringAlgoPanel.class.getName()).log(Level.SEVERE, null, ex);
00167             }
00168         }
00169         return c;
00170     }
00171     
00172     public ClusteringStream getStream(){
00173         ClusteringStream s = null;
00174         applyChanges();
00175         try {
00176             s = (ClusteringStream) ClassOption.cliStringToObject(streamOption.getValueAsCLIString(), ClusteringStream.class, null);
00177         } catch (Exception ex) {
00178             Logger.getLogger(ClusteringAlgoPanel.class.getName()).log(Level.SEVERE, null, ex);
00179         }
00180         return s;
00181     }
00182 
00183     public String getStreamValueAsCLIString(){
00184         applyChanges();
00185         return streamOption.getValueAsCLIString();
00186     }
00187 
00188     public String getAlgorithm0ValueAsCLIString(){
00189         applyChanges();
00190         return algorithmOption0.getValueAsCLIString();
00191     }
00192     
00193     public String getAlgorithm1ValueAsCLIString(){
00194         applyChanges();
00195         return algorithmOption1.getValueAsCLIString();
00196     }
00197     
00198     /* We need to fetch the right item from editComponents list, index needs to match GUI order */
00199     public void setStreamValueAsCLIString(String s){
00200         streamOption.setValueViaCLIString(s);
00201         editComponents.get(0).setEditState(streamOption.getValueAsCLIString());
00202     }
00203 
00204     public void setAlgorithm0ValueAsCLIString(String s){
00205         algorithmOption0.setValueViaCLIString(s);
00206         editComponents.get(1).setEditState(algorithmOption0.getValueAsCLIString());
00207     }
00208 
00209     public void setAlgorithm1ValueAsCLIString(String s){
00210         algorithmOption1.setValueViaCLIString(s);
00211         editComponents.get(2).setEditState(algorithmOption1.getValueAsCLIString());
00212     }    
00213 
00214     public void applyChanges() {
00215             for (OptionEditComponent editor : this.editComponents) {
00216                     try {
00217                             editor.applyState();
00218                     } catch (Exception ex) {
00219                             GUIUtils.showExceptionDialog(this, "Problem with option "
00220                                             + editor.getEditedOption().getName(), ex);
00221                     }
00222             }
00223     }
00224 
00225     public void setPanelTitle(String title){
00226         setBorder(javax.swing.BorderFactory.createTitledBorder(null,title, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11)));
00227     }
00228 
00234     @SuppressWarnings("unchecked")
00235     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00236     private void initComponents() {
00237 
00238         setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cluster Algorithm Setup", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
00239         setLayout(new java.awt.GridBagLayout());
00240     }// </editor-fold>//GEN-END:initComponents
00241 
00242 
00243     // Variables declaration - do not modify//GEN-BEGIN:variables
00244     // End of variables declaration//GEN-END:variables
00245 
00246 }
 All Classes Namespaces Files Functions Variables Enumerations