~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/gui/experiment/SimpleSetupPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 *    SimpleSetupPanel.java
 
19
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.gui.experiment;
 
24
 
 
25
import weka.classifiers.Classifier;
 
26
import weka.core.xml.KOML;
 
27
import weka.experiment.CSVResultListener;
 
28
import weka.experiment.ClassifierSplitEvaluator;
 
29
import weka.experiment.CrossValidationResultProducer;
 
30
import weka.experiment.DatabaseResultListener;
 
31
import weka.experiment.Experiment;
 
32
import weka.experiment.InstancesResultListener;
 
33
import weka.experiment.PropertyNode;
 
34
import weka.experiment.RandomSplitResultProducer;
 
35
import weka.experiment.RegressionSplitEvaluator;
 
36
import weka.experiment.SplitEvaluator;
 
37
import weka.experiment.xml.XMLExperiment;
 
38
import weka.gui.DatabaseConnectionDialog;
 
39
import weka.gui.ExtensionFileFilter;
 
40
 
 
41
import java.awt.BorderLayout;
 
42
import java.awt.GridBagConstraints;
 
43
import java.awt.GridBagLayout;
 
44
import java.awt.GridLayout;
 
45
import java.awt.Insets;
 
46
import java.awt.event.ActionEvent;
 
47
import java.awt.event.ActionListener;
 
48
import java.awt.event.FocusAdapter;
 
49
import java.awt.event.FocusEvent;
 
50
import java.awt.event.KeyAdapter;
 
51
import java.awt.event.KeyEvent;
 
52
import java.awt.event.WindowAdapter;
 
53
import java.awt.event.WindowEvent;
 
54
import java.beans.IntrospectionException;
 
55
import java.beans.PropertyChangeListener;
 
56
import java.beans.PropertyChangeSupport;
 
57
import java.beans.PropertyDescriptor;
 
58
import java.io.BufferedInputStream;
 
59
import java.io.BufferedOutputStream;
 
60
import java.io.File;
 
61
import java.io.FileInputStream;
 
62
import java.io.FileOutputStream;
 
63
import java.io.ObjectInputStream;
 
64
import java.io.ObjectOutputStream;
 
65
 
 
66
import javax.swing.BorderFactory;
 
67
import javax.swing.ButtonGroup;
 
68
import javax.swing.JButton;
 
69
import javax.swing.JComboBox;
 
70
import javax.swing.JFileChooser;
 
71
import javax.swing.JFrame;
 
72
import javax.swing.JLabel;
 
73
import javax.swing.JOptionPane;
 
74
import javax.swing.JPanel;
 
75
import javax.swing.JRadioButton;
 
76
import javax.swing.JScrollPane;
 
77
import javax.swing.JTextArea;
 
78
import javax.swing.JTextField;
 
79
import javax.swing.event.DocumentEvent;
 
80
import javax.swing.event.DocumentListener;
 
81
import javax.swing.filechooser.FileFilter;
 
82
 
 
83
/** 
 
84
 * This panel controls the configuration of an experiment.
 
85
  * <p>
 
86
 * If <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a>
 
87
 * is in the classpath the experiments can also be serialized to XML instead of a
 
88
 * binary format.
 
89
*
 
90
 * @author Richard kirkby (rkirkby@cs.waikato.ac.nz)
 
91
 * @author FracPete (fracpete at waikato dot ac dot nz) 
 
92
 * @version $Revision: 1.14 $
 
93
 */
 
94
public class SimpleSetupPanel
 
95
  extends JPanel {
 
96
 
 
97
  /** for serialization */
 
98
  private static final long serialVersionUID = 5257424515609176509L;
 
99
 
 
100
  /** The experiment being configured */
 
101
  protected Experiment m_Exp;
 
102
 
 
103
  /** The panel which switched between simple and advanced setup modes */
 
104
  protected SetupModePanel m_modePanel = null;
 
105
 
 
106
  /** The database destination URL to store results into */
 
107
  protected String m_destinationDatabaseURL;
 
108
 
 
109
  /** The filename to store results into */
 
110
  protected String m_destinationFilename = "";
 
111
 
 
112
  /** The number of folds for a cross-validation experiment */
 
113
  protected int m_numFolds = 10;
 
114
 
 
115
  /** The training percentage for a train/test split experiment */
 
116
  protected double m_trainPercent = 66;
 
117
 
 
118
  /** The number of times to repeat the sub-experiment */
 
119
  protected int m_numRepetitions = 10;
 
120
 
 
121
  /** Whether or not the user has consented for the experiment to be simplified */
 
122
  protected boolean m_userHasBeenAskedAboutConversion;
 
123
 
 
124
  /** Filter for choosing CSV files */
 
125
  protected ExtensionFileFilter m_csvFileFilter =
 
126
    new ExtensionFileFilter(".csv", "Comma separated value files");
 
127
 
 
128
  /** FIlter for choosing ARFF files */
 
129
  protected ExtensionFileFilter m_arffFileFilter =
 
130
    new ExtensionFileFilter(".arff", "ARFF files");
 
131
 
 
132
  /** Click to load an experiment */
 
133
  protected JButton m_OpenBut = new JButton("Open...");
 
134
 
 
135
  /** Click to save an experiment */
 
136
  protected JButton m_SaveBut = new JButton("Save...");
 
137
 
 
138
  /** Click to create a new experiment with default settings */
 
139
  protected JButton m_NewBut = new JButton("New");
 
140
 
 
141
  /** A filter to ensure only experiment files get shown in the chooser */
 
142
  protected FileFilter m_ExpFilter = 
 
143
    new ExtensionFileFilter(Experiment.FILE_EXTENSION, 
 
144
                            "Experiment configuration files (*" + Experiment.FILE_EXTENSION + ")");
 
145
 
 
146
  /** A filter to ensure only experiment (in KOML format) files get shown in the chooser */
 
147
  protected FileFilter m_KOMLFilter = 
 
148
    new ExtensionFileFilter(KOML.FILE_EXTENSION, 
 
149
                            "Experiment configuration files (*" + KOML.FILE_EXTENSION + ")");
 
150
 
 
151
  /** A filter to ensure only experiment (in XML format) files get shown in the chooser */
 
152
  protected FileFilter m_XMLFilter = 
 
153
    new ExtensionFileFilter(".xml", 
 
154
                            "Experiment configuration files (*.xml)");
 
155
 
 
156
  /** The file chooser for selecting experiments */
 
157
  protected JFileChooser m_FileChooser =
 
158
    new JFileChooser(new File(System.getProperty("user.dir")));
 
159
 
 
160
  /** The file chooser for selecting result destinations */
 
161
  protected JFileChooser m_DestFileChooser =
 
162
    new JFileChooser(new File(System.getProperty("user.dir")));
 
163
 
 
164
  /** Combo box for choosing experiment destination type */
 
165
  protected JComboBox m_ResultsDestinationCBox = new JComboBox();
 
166
 
 
167
  /** Label for destination field */
 
168
  protected JLabel m_ResultsDestinationPathLabel = new JLabel("Filename:");
 
169
 
 
170
  /** Input field for result destination path */ 
 
171
  protected JTextField m_ResultsDestinationPathTField = new JTextField();
 
172
 
 
173
  /** Button for browsing destination files */
 
174
  protected JButton m_BrowseDestinationButton = new JButton("Browse...");
 
175
 
 
176
  /** Combo box for choosing experiment type */
 
177
  protected JComboBox m_ExperimentTypeCBox = new JComboBox();
 
178
 
 
179
  /** Label for parameter field */
 
180
  protected JLabel m_ExperimentParameterLabel = new JLabel("Number of folds:");
 
181
 
 
182
  /** Input field for experiment parameter */
 
183
  protected JTextField m_ExperimentParameterTField = new JTextField(); 
 
184
 
 
185
  /** Radio button for choosing classification experiment */
 
186
  protected JRadioButton m_ExpClassificationRBut = 
 
187
    new JRadioButton("Classification");
 
188
 
 
189
  /** Radio button for choosing regression experiment */
 
190
  protected JRadioButton m_ExpRegressionRBut = 
 
191
    new JRadioButton("Regression");
 
192
 
 
193
  /** Input field for number of repetitions */
 
194
  protected JTextField m_NumberOfRepetitionsTField = new JTextField();  
 
195
 
 
196
  /** Radio button for choosing datasets first in order of execution */ 
 
197
  protected JRadioButton m_OrderDatasetsFirstRBut = 
 
198
    new JRadioButton("Data sets first");
 
199
 
 
200
  /** Radio button for choosing algorithms first in order of execution */ 
 
201
  protected JRadioButton m_OrderAlgorithmsFirstRBut = 
 
202
    new JRadioButton("Algorithms first");
 
203
 
 
204
  /** The strings used to identify the combo box choices */
 
205
  protected static String DEST_DATABASE_TEXT = ("JDBC database");
 
206
  protected static String DEST_ARFF_TEXT = ("ARFF file");
 
207
  protected static String DEST_CSV_TEXT = ("CSV file");
 
208
  protected static String TYPE_CROSSVALIDATION_TEXT = ("Cross-validation");
 
209
  protected static String TYPE_RANDOMSPLIT_TEXT = ("Train/Test Percentage Split (data randomized)");
 
210
  protected static String TYPE_FIXEDSPLIT_TEXT = ("Train/Test Percentage Split (order preserved)");
 
211
 
 
212
  /** The panel for configuring selected datasets */
 
213
  protected DatasetListPanel m_DatasetListPanel = new DatasetListPanel();
 
214
 
 
215
  /** The panel for configuring selected algorithms */
 
216
  protected AlgorithmListPanel m_AlgorithmListPanel = new AlgorithmListPanel();
 
217
 
 
218
  /** A button for bringing up the notes */
 
219
  protected JButton m_NotesButton =  new JButton("Notes");
 
220
 
 
221
  /** Frame for the notes */
 
222
  protected JFrame m_NotesFrame = new JFrame("Notes");
 
223
 
 
224
  /** Area for user notes Default of 10 rows */
 
225
  protected JTextArea m_NotesText = new JTextArea(null, 10, 0);
 
226
 
 
227
  /**
 
228
   * Manages sending notifications to people when we change the experiment,
 
229
   * at this stage, only the resultlistener so the resultpanel can update.
 
230
   */
 
231
  protected PropertyChangeSupport m_Support = new PropertyChangeSupport(this);
 
232
  
 
233
  /**
 
234
   * Creates the setup panel with the supplied initial experiment.
 
235
   *
 
236
   * @param exp a value of type 'Experiment'
 
237
   */
 
238
  public SimpleSetupPanel(Experiment exp) {
 
239
 
 
240
    this();
 
241
    setExperiment(exp);
 
242
  }
 
243
  
 
244
  /**
 
245
   * Creates the setup panel with no initial experiment.
 
246
   */
 
247
  public SimpleSetupPanel() {
 
248
 
 
249
    // everything disabled on startup
 
250
    m_ResultsDestinationCBox.setEnabled(false);
 
251
    m_ResultsDestinationPathLabel.setEnabled(false);
 
252
    m_ResultsDestinationPathTField.setEnabled(false);
 
253
    m_BrowseDestinationButton.setEnabled(false); 
 
254
    m_ExperimentTypeCBox.setEnabled(false);
 
255
    m_ExperimentParameterLabel.setEnabled(false);
 
256
    m_ExperimentParameterTField.setEnabled(false);
 
257
    m_ExpClassificationRBut.setEnabled(false);
 
258
    m_ExpRegressionRBut.setEnabled(false);
 
259
    m_NumberOfRepetitionsTField.setEnabled(false);
 
260
    m_OrderDatasetsFirstRBut.setEnabled(false);
 
261
    m_OrderAlgorithmsFirstRBut.setEnabled(false);
 
262
 
 
263
    // get sensible default database address
 
264
    try {
 
265
      m_destinationDatabaseURL = (new DatabaseResultListener()).getDatabaseURL();
 
266
    } catch (Exception e) {}
 
267
 
 
268
    // create action listeners
 
269
    m_NewBut.setMnemonic('N');
 
270
    m_NewBut.addActionListener(new ActionListener() {
 
271
        public void actionPerformed(ActionEvent e) {
 
272
          Experiment newExp = new Experiment();
 
273
          CrossValidationResultProducer cvrp = new CrossValidationResultProducer();
 
274
          cvrp.setNumFolds(10);
 
275
          cvrp.setSplitEvaluator(new ClassifierSplitEvaluator());
 
276
          newExp.setResultProducer(cvrp);
 
277
          newExp.setPropertyArray(new Classifier[0]);
 
278
          newExp.setUsePropertyIterator(true);
 
279
          setExperiment(newExp);
 
280
 
 
281
          // defaults
 
282
          if (ExperimenterDefaults.getUseClassification())
 
283
            m_ExpClassificationRBut.setSelected(true);
 
284
          else
 
285
            m_ExpRegressionRBut.setSelected(true);
 
286
          
 
287
          setSelectedItem(
 
288
              m_ResultsDestinationCBox, ExperimenterDefaults.getDestination());
 
289
          destinationTypeChanged();
 
290
          
 
291
          setSelectedItem(
 
292
              m_ExperimentTypeCBox, ExperimenterDefaults.getExperimentType());
 
293
          
 
294
          m_numRepetitions = ExperimenterDefaults.getRepetitions();
 
295
          m_NumberOfRepetitionsTField.setText(
 
296
              "" + m_numRepetitions);
 
297
          
 
298
          if (ExperimenterDefaults.getExperimentType().equals(
 
299
                TYPE_CROSSVALIDATION_TEXT)) {
 
300
            m_numFolds = ExperimenterDefaults.getFolds();
 
301
            m_ExperimentParameterTField.setText(
 
302
                "" + m_numFolds);
 
303
          }
 
304
          else {
 
305
            m_trainPercent = ExperimenterDefaults.getTrainPercentage();
 
306
            m_ExperimentParameterTField.setText(
 
307
                "" + m_trainPercent);
 
308
          }
 
309
          
 
310
          if (ExperimenterDefaults.getDatasetsFirst())
 
311
            m_OrderDatasetsFirstRBut.setSelected(true);
 
312
          else
 
313
            m_OrderAlgorithmsFirstRBut.setSelected(true);
 
314
 
 
315
          expTypeChanged();
 
316
        }
 
317
      });
 
318
    m_SaveBut.setEnabled(false);
 
319
    m_SaveBut.setMnemonic('S');
 
320
    m_SaveBut.addActionListener(new ActionListener() {
 
321
        public void actionPerformed(ActionEvent e) {
 
322
          saveExperiment();
 
323
        }
 
324
      });
 
325
    m_OpenBut.setMnemonic('O');
 
326
    m_OpenBut.addActionListener(new ActionListener() {
 
327
        public void actionPerformed(ActionEvent e) {
 
328
          openExperiment();
 
329
        }
 
330
      });
 
331
    m_FileChooser.addChoosableFileFilter(m_ExpFilter);
 
332
    if (KOML.isPresent())
 
333
       m_FileChooser.addChoosableFileFilter(m_KOMLFilter);
 
334
    m_FileChooser.addChoosableFileFilter(m_XMLFilter);
 
335
    if (ExperimenterDefaults.getExtension().equals(".xml"))
 
336
      m_FileChooser.setFileFilter(m_XMLFilter);
 
337
    else if (KOML.isPresent() && ExperimenterDefaults.getExtension().equals(KOML.FILE_EXTENSION))
 
338
      m_FileChooser.setFileFilter(m_KOMLFilter);
 
339
    else
 
340
      m_FileChooser.setFileFilter(m_ExpFilter);
 
341
    m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 
342
    m_DestFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 
343
 
 
344
    m_BrowseDestinationButton.addActionListener(new ActionListener() {
 
345
        public void actionPerformed(ActionEvent e) {
 
346
          //using this button for both browsing file & setting username/password
 
347
          if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT){
 
348
            chooseURLUsername();
 
349
          } else {
 
350
            chooseDestinationFile();
 
351
          }
 
352
        }
 
353
      });
 
354
 
 
355
    m_ExpClassificationRBut.addActionListener(new ActionListener() {
 
356
        public void actionPerformed(ActionEvent e) {
 
357
          expTypeChanged();
 
358
        }
 
359
      });
 
360
  
 
361
    m_ExpRegressionRBut.addActionListener(new ActionListener() {
 
362
        public void actionPerformed(ActionEvent e) {
 
363
          expTypeChanged();
 
364
        }
 
365
      });
 
366
 
 
367
    m_OrderDatasetsFirstRBut.addActionListener(new ActionListener() {
 
368
        public void actionPerformed(ActionEvent e) {
 
369
          if (m_Exp != null) {
 
370
            m_Exp.setAdvanceDataSetFirst(true);
 
371
            m_Support.firePropertyChange("", null, null);
 
372
          }
 
373
        }
 
374
      });
 
375
    
 
376
    m_OrderAlgorithmsFirstRBut.addActionListener(new ActionListener() {
 
377
        public void actionPerformed(ActionEvent e) {
 
378
          if (m_Exp != null) {
 
379
            m_Exp.setAdvanceDataSetFirst(false);
 
380
            m_Support.firePropertyChange("", null, null);
 
381
          }
 
382
        }
 
383
      });
 
384
 
 
385
    m_ResultsDestinationPathTField.getDocument().addDocumentListener(new DocumentListener() {
 
386
        public void insertUpdate(DocumentEvent e) {destinationAddressChanged();}
 
387
        public void removeUpdate(DocumentEvent e) {destinationAddressChanged();}
 
388
        public void changedUpdate(DocumentEvent e) {destinationAddressChanged();}
 
389
      });
 
390
 
 
391
    m_ExperimentParameterTField.getDocument().addDocumentListener(new DocumentListener() {
 
392
        public void insertUpdate(DocumentEvent e) {expParamChanged();}
 
393
        public void removeUpdate(DocumentEvent e) {expParamChanged();}
 
394
        public void changedUpdate(DocumentEvent e) {expParamChanged();}
 
395
      });
 
396
 
 
397
    m_NumberOfRepetitionsTField.getDocument().addDocumentListener(new DocumentListener() {
 
398
        public void insertUpdate(DocumentEvent e) {numRepetitionsChanged();}
 
399
        public void removeUpdate(DocumentEvent e) {numRepetitionsChanged();}
 
400
        public void changedUpdate(DocumentEvent e) {numRepetitionsChanged();}
 
401
      });
 
402
 
 
403
    m_NotesFrame.addWindowListener(new WindowAdapter() {
 
404
        public void windowClosing(WindowEvent e) {
 
405
          m_NotesButton.setEnabled(true);
 
406
        }
 
407
      });
 
408
    m_NotesFrame.getContentPane().add(new JScrollPane(m_NotesText));
 
409
    m_NotesFrame.setSize(600, 400);
 
410
 
 
411
    m_NotesButton.addActionListener(new ActionListener() {
 
412
        public void actionPerformed(ActionEvent e) {
 
413
          m_NotesButton.setEnabled(false);
 
414
          m_NotesFrame.setVisible(true);
 
415
        }
 
416
      });
 
417
    m_NotesButton.setEnabled(false);
 
418
 
 
419
    m_NotesText.setEditable(true);
 
420
    //m_NotesText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
421
    m_NotesText.addKeyListener(new KeyAdapter() {
 
422
        public void keyReleased(KeyEvent e) {
 
423
          m_Exp.setNotes(m_NotesText.getText());
 
424
        }
 
425
      });
 
426
    m_NotesText.addFocusListener(new FocusAdapter() {
 
427
        public void focusLost(FocusEvent e) {
 
428
          m_Exp.setNotes(m_NotesText.getText());
 
429
        }
 
430
      });
 
431
    
 
432
    // Set up the GUI layout
 
433
    JPanel buttons = new JPanel();
 
434
    GridBagLayout gb = new GridBagLayout();
 
435
    GridBagConstraints constraints = new GridBagConstraints();
 
436
    buttons.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
 
437
    buttons.setLayout(gb);
 
438
    constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
 
439
    constraints.fill = GridBagConstraints.HORIZONTAL;
 
440
    constraints.gridwidth=1;constraints.gridheight=1;
 
441
    constraints.insets = new Insets(0,2,0,2);
 
442
    buttons.add(m_OpenBut,constraints);
 
443
    constraints.gridx=1;constraints.gridy=0;constraints.weightx=5;
 
444
    constraints.gridwidth=1;constraints.gridheight=1;
 
445
    buttons.add(m_SaveBut,constraints);
 
446
    constraints.gridx=2;constraints.gridy=0;constraints.weightx=5;
 
447
    constraints.gridwidth=1;constraints.gridheight=1;
 
448
    buttons.add(m_NewBut,constraints);
 
449
 
 
450
    JPanel destName = new JPanel();
 
451
    destName.setLayout(new BorderLayout(5, 5));
 
452
    destName.add(m_ResultsDestinationPathLabel, BorderLayout.WEST);
 
453
    destName.add(m_ResultsDestinationPathTField, BorderLayout.CENTER);
 
454
    
 
455
    m_ResultsDestinationCBox.addItem(DEST_ARFF_TEXT);
 
456
    m_ResultsDestinationCBox.addItem(DEST_CSV_TEXT);
 
457
    m_ResultsDestinationCBox.addItem(DEST_DATABASE_TEXT);
 
458
    
 
459
    m_ResultsDestinationCBox.addActionListener(new ActionListener() {
 
460
        public void actionPerformed(ActionEvent e) {
 
461
          destinationTypeChanged();
 
462
        }
 
463
      });
 
464
 
 
465
    JPanel destInner = new JPanel();
 
466
    destInner.setLayout(new BorderLayout(5, 5));
 
467
    destInner.add(m_ResultsDestinationCBox, BorderLayout.WEST);
 
468
    destInner.add(destName, BorderLayout.CENTER);
 
469
    destInner.add(m_BrowseDestinationButton, BorderLayout.EAST);
 
470
 
 
471
    JPanel dest = new JPanel();
 
472
    dest.setLayout(new BorderLayout());
 
473
    dest.setBorder(BorderFactory.createCompoundBorder(
 
474
                  BorderFactory.createTitledBorder("Results Destination"),
 
475
                  BorderFactory.createEmptyBorder(0, 5, 5, 5)
 
476
                  ));
 
477
    dest.add(destInner, BorderLayout.NORTH);
 
478
 
 
479
    JPanel expParam = new JPanel();
 
480
    expParam.setLayout(new BorderLayout(5, 5));
 
481
    expParam.add(m_ExperimentParameterLabel, BorderLayout.WEST);
 
482
    expParam.add(m_ExperimentParameterTField, BorderLayout.CENTER);
 
483
 
 
484
    ButtonGroup typeBG = new ButtonGroup();
 
485
    typeBG.add(m_ExpClassificationRBut);
 
486
    typeBG.add(m_ExpRegressionRBut);
 
487
    m_ExpClassificationRBut.setSelected(true);
 
488
 
 
489
    JPanel typeRButtons = new JPanel();
 
490
    typeRButtons.setLayout(new GridLayout(1,0));
 
491
    typeRButtons.add(m_ExpClassificationRBut);
 
492
    typeRButtons.add(m_ExpRegressionRBut);
 
493
 
 
494
    m_ExperimentTypeCBox.addItem(TYPE_CROSSVALIDATION_TEXT);
 
495
    m_ExperimentTypeCBox.addItem(TYPE_RANDOMSPLIT_TEXT);
 
496
    m_ExperimentTypeCBox.addItem(TYPE_FIXEDSPLIT_TEXT);
 
497
 
 
498
    m_ExperimentTypeCBox.addActionListener(new ActionListener() {
 
499
        public void actionPerformed(ActionEvent e) {
 
500
          expTypeChanged();
 
501
        }
 
502
      });
 
503
 
 
504
    JPanel typeInner = new JPanel();
 
505
    typeInner.setLayout(new GridLayout(0,1));
 
506
    typeInner.add(m_ExperimentTypeCBox);
 
507
    typeInner.add(expParam);
 
508
    typeInner.add(typeRButtons);
 
509
 
 
510
    JPanel type = new JPanel();
 
511
    type.setLayout(new BorderLayout());
 
512
    type.setBorder(BorderFactory.createCompoundBorder(
 
513
                  BorderFactory.createTitledBorder("Experiment Type"),
 
514
                  BorderFactory.createEmptyBorder(0, 5, 5, 5)
 
515
                  ));
 
516
    type.add(typeInner, BorderLayout.NORTH);
 
517
 
 
518
    ButtonGroup iterBG = new ButtonGroup();
 
519
    iterBG.add(m_OrderDatasetsFirstRBut);
 
520
    iterBG.add(m_OrderAlgorithmsFirstRBut);
 
521
    m_OrderDatasetsFirstRBut.setSelected(true);
 
522
 
 
523
    JPanel numIter = new JPanel();
 
524
    numIter.setLayout(new BorderLayout(5, 5));
 
525
    numIter.add(new JLabel("Number of repetitions:"), BorderLayout.WEST);
 
526
    numIter.add(m_NumberOfRepetitionsTField, BorderLayout.CENTER);
 
527
 
 
528
    JPanel controlInner = new JPanel();
 
529
    controlInner.setLayout(new GridLayout(0,1));
 
530
    controlInner.add(numIter);
 
531
    controlInner.add(m_OrderDatasetsFirstRBut);
 
532
    controlInner.add(m_OrderAlgorithmsFirstRBut);
 
533
 
 
534
    JPanel control = new JPanel();
 
535
    control.setLayout(new BorderLayout());
 
536
    control.setBorder(BorderFactory.createCompoundBorder(
 
537
                  BorderFactory.createTitledBorder("Iteration Control"),
 
538
                  BorderFactory.createEmptyBorder(0, 5, 5, 5)
 
539
                  ));
 
540
    control.add(controlInner, BorderLayout.NORTH);
 
541
 
 
542
    JPanel type_control = new JPanel();
 
543
    type_control.setLayout(new GridLayout(1,0));
 
544
    type_control.add(type);
 
545
    type_control.add(control);
 
546
 
 
547
    JPanel notes = new JPanel();
 
548
    notes.setLayout(new BorderLayout());
 
549
    notes.add(m_NotesButton, BorderLayout.CENTER);
 
550
 
 
551
    JPanel top1 = new JPanel();
 
552
    top1.setLayout(new BorderLayout());
 
553
    top1.add(dest, BorderLayout.NORTH);
 
554
    top1.add(type_control, BorderLayout.CENTER);
 
555
 
 
556
    JPanel top = new JPanel();
 
557
    top.setLayout(new BorderLayout());
 
558
    top.add(buttons, BorderLayout.NORTH);
 
559
    top.add(top1, BorderLayout.CENTER);  
 
560
 
 
561
    JPanel datasets = new JPanel();
 
562
    datasets.setLayout(new BorderLayout());
 
563
    datasets.add(m_DatasetListPanel, BorderLayout.CENTER);
 
564
 
 
565
    JPanel algorithms = new JPanel();
 
566
    algorithms.setLayout(new BorderLayout());
 
567
    algorithms.add(m_AlgorithmListPanel, BorderLayout.CENTER);
 
568
 
 
569
    JPanel schemes = new JPanel();
 
570
    schemes.setLayout(new GridLayout(1,0));
 
571
    schemes.add(datasets);
 
572
    schemes.add(algorithms);
 
573
 
 
574
    setLayout(new BorderLayout());
 
575
    add(top, BorderLayout.NORTH);
 
576
    add(schemes, BorderLayout.CENTER);
 
577
    add(notes, BorderLayout.SOUTH);
 
578
  }
 
579
  
 
580
  /**
 
581
   * Sets the selected item of an combobox, since using setSelectedItem(...)
 
582
   * doesn't work, if one checks object references!
 
583
   *
 
584
   * @param cb      the combobox to set the item for
 
585
   * @param item    the item to set active
 
586
   */
 
587
  protected void setSelectedItem(JComboBox cb, String item) {
 
588
    int       i;
 
589
 
 
590
    for (i = 0; i < cb.getItemCount(); i++) {
 
591
      if (cb.getItemAt(i).toString().equals(item)) {
 
592
        cb.setSelectedIndex(i);
 
593
        break;
 
594
      }
 
595
    }
 
596
  }
 
597
  
 
598
  /**
 
599
   * Deletes the notes frame.
 
600
   */
 
601
  protected void removeNotesFrame() {
 
602
    m_NotesFrame.setVisible(false);
 
603
  }
 
604
 
 
605
  /**
 
606
   * Gets te users consent for converting the experiment to a simpler form.
 
607
   *
 
608
   * @return true if the user has given consent, false otherwise
 
609
   */  
 
610
  private boolean userWantsToConvert() {
 
611
    
 
612
    if (m_userHasBeenAskedAboutConversion) return true;
 
613
    m_userHasBeenAskedAboutConversion = true;
 
614
    return (JOptionPane.showConfirmDialog(this,
 
615
                                          "This experiment has settings that are too advanced\n" +
 
616
                                          "to be represented in the simple setup mode.\n" +
 
617
                                          "Do you want the experiment to be converted,\n" +
 
618
                                          "losing some of the advanced settings?\n",
 
619
                                          "Confirm conversion",
 
620
                                          JOptionPane.YES_NO_OPTION,
 
621
                                          JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION);
 
622
  }
 
623
 
 
624
  /**
 
625
   * Sets the panel used to switch between simple and advanced modes.
 
626
   *
 
627
   * @param modePanel the panel
 
628
   */
 
629
  public void setModePanel(SetupModePanel modePanel) {
 
630
 
 
631
    m_modePanel = modePanel;
 
632
  }
 
633
 
 
634
  /**
 
635
   * Sets the experiment to configure.
 
636
   *
 
637
   * @param exp a value of type 'Experiment'
 
638
   * @return true if experiment could be configured, false otherwise
 
639
   */
 
640
  public boolean setExperiment(Experiment exp) {
 
641
    
 
642
    m_userHasBeenAskedAboutConversion = false;
 
643
    m_Exp = null; // hold off until we are sure we want conversion
 
644
    m_SaveBut.setEnabled(true);
 
645
 
 
646
    if (exp.getResultListener() instanceof DatabaseResultListener) {
 
647
      m_ResultsDestinationCBox.setSelectedItem(DEST_DATABASE_TEXT);
 
648
      m_ResultsDestinationPathLabel.setText("URL:");
 
649
      m_destinationDatabaseURL = ((DatabaseResultListener)exp.getResultListener()).getDatabaseURL();
 
650
      m_ResultsDestinationPathTField.setText(m_destinationDatabaseURL);
 
651
      m_BrowseDestinationButton.setEnabled(true);
 
652
    } else if (exp.getResultListener() instanceof InstancesResultListener) {
 
653
      m_ResultsDestinationCBox.setSelectedItem(DEST_ARFF_TEXT);
 
654
      m_ResultsDestinationPathLabel.setText("Filename:");
 
655
      m_destinationFilename = ((InstancesResultListener)exp.getResultListener()).outputFileName();
 
656
      m_ResultsDestinationPathTField.setText(m_destinationFilename);
 
657
      m_BrowseDestinationButton.setEnabled(true);
 
658
    } else if (exp.getResultListener() instanceof CSVResultListener) {
 
659
      m_ResultsDestinationCBox.setSelectedItem(DEST_CSV_TEXT);
 
660
      m_ResultsDestinationPathLabel.setText("Filename:");
 
661
      m_destinationFilename = ((CSVResultListener)exp.getResultListener()).outputFileName();
 
662
      m_ResultsDestinationPathTField.setText(m_destinationFilename);
 
663
      m_BrowseDestinationButton.setEnabled(true);
 
664
    } else {
 
665
      // unrecognised result listener 
 
666
      System.out.println("SimpleSetup incompatibility: unrecognised result destination");
 
667
      if (userWantsToConvert()) {
 
668
        m_ResultsDestinationCBox.setSelectedItem(DEST_ARFF_TEXT);
 
669
        m_ResultsDestinationPathLabel.setText("Filename:");
 
670
        m_destinationFilename = "";
 
671
        m_ResultsDestinationPathTField.setText(m_destinationFilename);
 
672
        m_BrowseDestinationButton.setEnabled(true);
 
673
      } else {
 
674
        return false;
 
675
      }
 
676
    }
 
677
    m_ResultsDestinationCBox.setEnabled(true);
 
678
    m_ResultsDestinationPathLabel.setEnabled(true);
 
679
    m_ResultsDestinationPathTField.setEnabled(true);
 
680
 
 
681
    if (exp.getResultProducer() instanceof CrossValidationResultProducer) {
 
682
      CrossValidationResultProducer cvrp = (CrossValidationResultProducer) exp.getResultProducer();
 
683
      m_numFolds = cvrp.getNumFolds();
 
684
      m_ExperimentParameterTField.setText("" + m_numFolds);
 
685
      
 
686
      if (cvrp.getSplitEvaluator() instanceof ClassifierSplitEvaluator) {
 
687
        m_ExpClassificationRBut.setSelected(true);
 
688
        m_ExpRegressionRBut.setSelected(false);
 
689
      } else if (cvrp.getSplitEvaluator() instanceof RegressionSplitEvaluator) {
 
690
        m_ExpClassificationRBut.setSelected(false);
 
691
        m_ExpRegressionRBut.setSelected(true);
 
692
      } else {
 
693
        // unknown split evaluator
 
694
        System.out.println("SimpleSetup incompatibility: unrecognised split evaluator");
 
695
        if (userWantsToConvert()) {
 
696
          m_ExpClassificationRBut.setSelected(true);
 
697
          m_ExpRegressionRBut.setSelected(false);
 
698
        } else {
 
699
          return false;
 
700
        }
 
701
      }
 
702
      m_ExperimentTypeCBox.setSelectedItem(TYPE_CROSSVALIDATION_TEXT);
 
703
    } else if (exp.getResultProducer() instanceof RandomSplitResultProducer) {
 
704
      RandomSplitResultProducer rsrp = (RandomSplitResultProducer) exp.getResultProducer();
 
705
      if (rsrp.getRandomizeData()) {
 
706
        m_ExperimentTypeCBox.setSelectedItem(TYPE_RANDOMSPLIT_TEXT);
 
707
      } else {
 
708
        m_ExperimentTypeCBox.setSelectedItem(TYPE_FIXEDSPLIT_TEXT);
 
709
      }
 
710
      if (rsrp.getSplitEvaluator() instanceof ClassifierSplitEvaluator) {
 
711
        m_ExpClassificationRBut.setSelected(true);
 
712
        m_ExpRegressionRBut.setSelected(false);
 
713
      } else if (rsrp.getSplitEvaluator() instanceof RegressionSplitEvaluator) {
 
714
        m_ExpClassificationRBut.setSelected(false);
 
715
        m_ExpRegressionRBut.setSelected(true);
 
716
      } else {
 
717
        // unknown split evaluator
 
718
        System.out.println("SimpleSetup incompatibility: unrecognised split evaluator");
 
719
        if (userWantsToConvert()) {
 
720
          m_ExpClassificationRBut.setSelected(true);
 
721
          m_ExpRegressionRBut.setSelected(false);
 
722
        } else {
 
723
          return false;
 
724
        }
 
725
      }
 
726
      m_trainPercent = rsrp.getTrainPercent();
 
727
      m_ExperimentParameterTField.setText("" + m_trainPercent);
 
728
      
 
729
    } else {
 
730
      // unknown experiment type
 
731
      System.out.println("SimpleSetup incompatibility: unrecognised resultProducer");
 
732
      if (userWantsToConvert()) {
 
733
        m_ExperimentTypeCBox.setSelectedItem(TYPE_CROSSVALIDATION_TEXT);
 
734
        m_ExpClassificationRBut.setSelected(true);
 
735
        m_ExpRegressionRBut.setSelected(false);
 
736
      } else {
 
737
        return false;
 
738
      }
 
739
    }
 
740
 
 
741
    m_ExperimentTypeCBox.setEnabled(true);
 
742
    m_ExperimentParameterLabel.setEnabled(true);
 
743
    m_ExperimentParameterTField.setEnabled(true);
 
744
    m_ExpClassificationRBut.setEnabled(true);
 
745
    m_ExpRegressionRBut.setEnabled(true);
 
746
    
 
747
    if (exp.getRunLower() == 1) {
 
748
      m_numRepetitions = exp.getRunUpper();
 
749
      m_NumberOfRepetitionsTField.setText("" + m_numRepetitions);
 
750
    } else {
 
751
      // unsupported iterations
 
752
      System.out.println("SimpleSetup incompatibility: runLower is not 1");
 
753
      if (userWantsToConvert()) {
 
754
        exp.setRunLower(1);
 
755
        if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_FIXEDSPLIT_TEXT) {
 
756
          exp.setRunUpper(1);
 
757
          m_NumberOfRepetitionsTField.setEnabled(false);
 
758
          m_NumberOfRepetitionsTField.setText("1");
 
759
        } else {
 
760
          exp.setRunUpper(10);
 
761
          m_numRepetitions = 10;
 
762
          m_NumberOfRepetitionsTField.setText("" + m_numRepetitions);
 
763
        }
 
764
        
 
765
      } else {
 
766
        return false;
 
767
      }
 
768
    }
 
769
    m_NumberOfRepetitionsTField.setEnabled(true);
 
770
 
 
771
    m_OrderDatasetsFirstRBut.setSelected(exp.getAdvanceDataSetFirst());
 
772
    m_OrderAlgorithmsFirstRBut.setSelected(!exp.getAdvanceDataSetFirst());
 
773
    m_OrderDatasetsFirstRBut.setEnabled(true);
 
774
    m_OrderAlgorithmsFirstRBut.setEnabled(true);
 
775
 
 
776
    m_NotesText.setText(exp.getNotes());
 
777
    m_NotesButton.setEnabled(true);
 
778
 
 
779
    if (!exp.getUsePropertyIterator() || !(exp.getPropertyArray() instanceof Classifier[])) {
 
780
      // unknown property iteration
 
781
      System.out.println("SimpleSetup incompatibility: unrecognised property iteration");
 
782
      if (userWantsToConvert()) {
 
783
        exp.setPropertyArray(new Classifier[0]);
 
784
        exp.setUsePropertyIterator(true);
 
785
      } else {
 
786
        return false;
 
787
      }
 
788
    }
 
789
 
 
790
    m_DatasetListPanel.setExperiment(exp);
 
791
    m_AlgorithmListPanel.setExperiment(exp);
 
792
    
 
793
    m_Exp = exp;
 
794
    expTypeChanged(); // recreate experiment
 
795
    
 
796
    m_Support.firePropertyChange("", null, null);
 
797
    
 
798
    return true;
 
799
  }
 
800
 
 
801
  /**
 
802
   * Gets the currently configured experiment.
 
803
   *
 
804
   * @return the currently configured experiment.
 
805
   */
 
806
  public Experiment getExperiment() {
 
807
 
 
808
    return m_Exp;
 
809
  }
 
810
  
 
811
  /**
 
812
   * Prompts the user to select an experiment file and loads it.
 
813
   */
 
814
  private void openExperiment() {
 
815
    
 
816
    int returnVal = m_FileChooser.showOpenDialog(this);
 
817
    if (returnVal != JFileChooser.APPROVE_OPTION) {
 
818
      return;
 
819
    }
 
820
    File expFile = m_FileChooser.getSelectedFile();
 
821
    
 
822
    // add extension if necessary
 
823
    if (m_FileChooser.getFileFilter() == m_ExpFilter) {
 
824
      if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION))
 
825
        expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION);
 
826
    }
 
827
    else if (m_FileChooser.getFileFilter() == m_KOMLFilter) {
 
828
      if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION))
 
829
        expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION);
 
830
    }
 
831
    else if (m_FileChooser.getFileFilter() == m_XMLFilter) {
 
832
      if (!expFile.getName().toLowerCase().endsWith(".xml"))
 
833
        expFile = new File(expFile.getParent(), expFile.getName() + ".xml");
 
834
    }
 
835
    
 
836
    try {
 
837
      Experiment exp; 
 
838
      
 
839
      // KOML?
 
840
      if ( (KOML.isPresent()) && (expFile.getAbsolutePath().toLowerCase().endsWith(KOML.FILE_EXTENSION)) ) {
 
841
         exp = (Experiment) KOML.read(expFile.getAbsolutePath());
 
842
      }
 
843
      else
 
844
      // XML?
 
845
      if (expFile.getAbsolutePath().toLowerCase().endsWith(".xml")) {
 
846
         XMLExperiment xml = new XMLExperiment(); 
 
847
         exp = (Experiment) xml.read(expFile);
 
848
      }
 
849
      // binary
 
850
      else {
 
851
         FileInputStream fi = new FileInputStream(expFile);
 
852
         ObjectInputStream oi = new ObjectInputStream(
 
853
                                new BufferedInputStream(fi));
 
854
         exp = (Experiment)oi.readObject();
 
855
         oi.close();
 
856
      }
 
857
      
 
858
      if (!setExperiment(exp)) {
 
859
        if (m_modePanel != null) m_modePanel.switchToAdvanced(exp);
 
860
      }
 
861
      System.err.println("Opened experiment:\n" + exp);
 
862
    } catch (Exception ex) {
 
863
      ex.printStackTrace();
 
864
      JOptionPane.showMessageDialog(this, "Couldn't open experiment file:\n"
 
865
                                    + expFile
 
866
                                    + "\nReason:\n" + ex.getMessage(),
 
867
                                    "Open Experiment",
 
868
                                    JOptionPane.ERROR_MESSAGE);
 
869
      // Pop up error dialog
 
870
    }
 
871
  }
 
872
 
 
873
  /**
 
874
   * Prompts the user for a filename to save the experiment to, then saves
 
875
   * the experiment.
 
876
   */
 
877
  private void saveExperiment() {
 
878
 
 
879
    int returnVal = m_FileChooser.showSaveDialog(this);
 
880
    if (returnVal != JFileChooser.APPROVE_OPTION) {
 
881
      return;
 
882
    }
 
883
    File expFile = m_FileChooser.getSelectedFile();
 
884
    
 
885
    // add extension if necessary
 
886
    if (m_FileChooser.getFileFilter() == m_ExpFilter) {
 
887
      if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION))
 
888
        expFile = new File(expFile.getParent(), expFile.getName() + Experiment.FILE_EXTENSION);
 
889
    }
 
890
    else if (m_FileChooser.getFileFilter() == m_KOMLFilter) {
 
891
      if (!expFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION))
 
892
        expFile = new File(expFile.getParent(), expFile.getName() + KOML.FILE_EXTENSION);
 
893
    }
 
894
    else if (m_FileChooser.getFileFilter() == m_XMLFilter) {
 
895
      if (!expFile.getName().toLowerCase().endsWith(".xml"))
 
896
        expFile = new File(expFile.getParent(), expFile.getName() + ".xml");
 
897
    }
 
898
    
 
899
    try {
 
900
       // KOML?
 
901
       if ( (KOML.isPresent()) && (expFile.getAbsolutePath().toLowerCase().endsWith(KOML.FILE_EXTENSION)) ) {
 
902
          KOML.write(expFile.getAbsolutePath(), m_Exp);
 
903
       }
 
904
       else
 
905
       // XML?
 
906
       if (expFile.getAbsolutePath().toLowerCase().endsWith(".xml")) {
 
907
          XMLExperiment xml = new XMLExperiment(); 
 
908
          xml.write(expFile, m_Exp);
 
909
       }
 
910
       // binary
 
911
       else {
 
912
          FileOutputStream fo = new FileOutputStream(expFile);
 
913
          ObjectOutputStream oo = new ObjectOutputStream(
 
914
                                  new BufferedOutputStream(fo));
 
915
          oo.writeObject(m_Exp);
 
916
          oo.close();
 
917
       }
 
918
      
 
919
      System.err.println("Saved experiment:\n" + m_Exp);
 
920
    } catch (Exception ex) {
 
921
      ex.printStackTrace();
 
922
      JOptionPane.showMessageDialog(this, "Couldn't save experiment file:\n"
 
923
                                    + expFile
 
924
                                    + "\nReason:\n" + ex.getMessage(),
 
925
                                    "Save Experiment",
 
926
                                    JOptionPane.ERROR_MESSAGE);
 
927
    }
 
928
  }
 
929
 
 
930
  /**
 
931
   * Adds a PropertyChangeListener who will be notified of value changes.
 
932
   *
 
933
   * @param l a value of type 'PropertyChangeListener'
 
934
   */
 
935
  public void addPropertyChangeListener(PropertyChangeListener l) {
 
936
    m_Support.addPropertyChangeListener(l);
 
937
  }
 
938
 
 
939
  /**
 
940
   * Removes a PropertyChangeListener.
 
941
   *
 
942
   * @param l a value of type 'PropertyChangeListener'
 
943
   */
 
944
  public void removePropertyChangeListener(PropertyChangeListener l) {
 
945
    m_Support.removePropertyChangeListener(l);
 
946
  }
 
947
 
 
948
  /**
 
949
   * Responds to a change in the destination type.
 
950
   */
 
951
  private void destinationTypeChanged() {
 
952
 
 
953
    if (m_Exp == null) return;
 
954
 
 
955
    String str = "";
 
956
 
 
957
    if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT) {
 
958
      m_ResultsDestinationPathLabel.setText("URL:");
 
959
      str = m_destinationDatabaseURL;
 
960
      m_BrowseDestinationButton.setEnabled(true); //!!!
 
961
      m_BrowseDestinationButton.setText("User...");
 
962
    } else {
 
963
      m_ResultsDestinationPathLabel.setText("Filename:");
 
964
      if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
 
965
        int ind = m_destinationFilename.lastIndexOf(".csv");
 
966
        if (ind > -1) {
 
967
          m_destinationFilename = m_destinationFilename.substring(0, ind) + ".arff";
 
968
        }
 
969
      }
 
970
      if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
 
971
        int ind = m_destinationFilename.lastIndexOf(".arff");
 
972
        if (ind > -1) {
 
973
          m_destinationFilename = m_destinationFilename.substring(0, ind) + ".csv";
 
974
        }
 
975
      }
 
976
      str = m_destinationFilename;
 
977
      if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
 
978
        int ind = str.lastIndexOf(".csv");
 
979
        if (ind > -1) {
 
980
          str = str.substring(0, ind) + ".arff";
 
981
        }
 
982
      }
 
983
      if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
 
984
        int ind = str.lastIndexOf(".arff");
 
985
        if (ind > -1) {
 
986
          str = str.substring(0, ind) + ".csv";
 
987
        }
 
988
      }
 
989
      m_BrowseDestinationButton.setEnabled(true);
 
990
      m_BrowseDestinationButton.setText("Browse...");
 
991
    }
 
992
 
 
993
    if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT) {
 
994
      DatabaseResultListener drl = null;
 
995
      try {
 
996
        drl = new DatabaseResultListener();
 
997
      } catch (Exception e) {
 
998
        e.printStackTrace();
 
999
      }
 
1000
      drl.setDatabaseURL(m_destinationDatabaseURL);
 
1001
      m_Exp.setResultListener(drl);
 
1002
    } else {
 
1003
      if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
 
1004
        InstancesResultListener irl = new InstancesResultListener();
 
1005
        if (!m_destinationFilename.equals("")) {
 
1006
          irl.setOutputFile(new File(m_destinationFilename));
 
1007
        }
 
1008
        m_Exp.setResultListener(irl);
 
1009
      } else if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
 
1010
        CSVResultListener crl = new CSVResultListener();
 
1011
        if (!m_destinationFilename.equals("")) {
 
1012
          crl.setOutputFile(new File(m_destinationFilename));
 
1013
        }
 
1014
        m_Exp.setResultListener(crl);
 
1015
      }
 
1016
    }
 
1017
 
 
1018
    m_ResultsDestinationPathTField.setText(str);
 
1019
 
 
1020
    m_Support.firePropertyChange("", null, null);
 
1021
  }
 
1022
 
 
1023
  /**
 
1024
   * Responds to a change in the destination address.
 
1025
   */
 
1026
  private void destinationAddressChanged() {
 
1027
 
 
1028
    if (m_Exp == null) return;
 
1029
 
 
1030
    if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT) {
 
1031
      m_destinationDatabaseURL = m_ResultsDestinationPathTField.getText();
 
1032
      if (m_Exp.getResultListener() instanceof DatabaseResultListener) {
 
1033
        ((DatabaseResultListener)m_Exp.getResultListener()).setDatabaseURL(m_destinationDatabaseURL);
 
1034
      }
 
1035
    } else {
 
1036
      File resultsFile = null;
 
1037
      m_destinationFilename = m_ResultsDestinationPathTField.getText();
 
1038
 
 
1039
      // Use temporary file if no file name is provided
 
1040
      if (m_destinationFilename.equals("")) {
 
1041
        try {
 
1042
          if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
 
1043
            resultsFile = File.createTempFile("weka_experiment", ".arff");
 
1044
          }
 
1045
          if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
 
1046
            resultsFile = File.createTempFile("weka_experiment", ".csv");
 
1047
          }
 
1048
          resultsFile.deleteOnExit();
 
1049
        } catch (Exception e) {
 
1050
          System.err.println("Cannot create temp file, writing to standard out.");
 
1051
          resultsFile = new File("-");
 
1052
        }
 
1053
      } else {
 
1054
        if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
 
1055
          if (!m_destinationFilename.endsWith(".arff")) {
 
1056
            m_destinationFilename += ".arff";
 
1057
          }
 
1058
        }
 
1059
        if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
 
1060
          if (!m_destinationFilename.endsWith(".csv")) {
 
1061
            m_destinationFilename += ".csv";
 
1062
          }
 
1063
        }
 
1064
        resultsFile = new File(m_destinationFilename);
 
1065
      }
 
1066
      ((CSVResultListener)m_Exp.getResultListener()).setOutputFile(resultsFile);
 
1067
      ((CSVResultListener)m_Exp.getResultListener()).setOutputFileName(m_destinationFilename);
 
1068
    }
 
1069
 
 
1070
    m_Support.firePropertyChange("", null, null);
 
1071
  }
 
1072
 
 
1073
  /**
 
1074
   * Responds to a change in the experiment type.
 
1075
   */
 
1076
  private void expTypeChanged() {
 
1077
 
 
1078
    if (m_Exp == null) return;
 
1079
 
 
1080
    // update parameter ui
 
1081
    if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
 
1082
      m_ExperimentParameterLabel.setText("Number of folds:");
 
1083
      m_ExperimentParameterTField.setText("" + m_numFolds);
 
1084
    } else {
 
1085
      m_ExperimentParameterLabel.setText("Train percentage:");
 
1086
      m_ExperimentParameterTField.setText("" + m_trainPercent);
 
1087
    }
 
1088
 
 
1089
    // update iteration ui
 
1090
    if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_FIXEDSPLIT_TEXT) {
 
1091
      m_NumberOfRepetitionsTField.setEnabled(false);
 
1092
      m_NumberOfRepetitionsTField.setText("1");
 
1093
      m_Exp.setRunLower(1);
 
1094
      m_Exp.setRunUpper(1);
 
1095
    } else {
 
1096
      m_NumberOfRepetitionsTField.setText("" + m_numRepetitions);
 
1097
      m_NumberOfRepetitionsTField.setEnabled(true);
 
1098
      m_Exp.setRunLower(1);
 
1099
      m_Exp.setRunUpper(m_numRepetitions);
 
1100
    }
 
1101
 
 
1102
    SplitEvaluator se = null;
 
1103
    Classifier sec = null;
 
1104
    if (m_ExpClassificationRBut.isSelected()) {
 
1105
      se = new ClassifierSplitEvaluator();
 
1106
      sec = ((ClassifierSplitEvaluator)se).getClassifier();
 
1107
    } else {
 
1108
      se = new RegressionSplitEvaluator();
 
1109
      sec = ((RegressionSplitEvaluator)se).getClassifier();
 
1110
    }
 
1111
    
 
1112
    // build new ResultProducer
 
1113
    if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
 
1114
      CrossValidationResultProducer cvrp = new CrossValidationResultProducer();
 
1115
      cvrp.setNumFolds(m_numFolds);
 
1116
      cvrp.setSplitEvaluator(se);
 
1117
      
 
1118
      PropertyNode[] propertyPath = new PropertyNode[2];
 
1119
      try {
 
1120
        propertyPath[0] = new PropertyNode(se, new PropertyDescriptor("splitEvaluator",
 
1121
                                                                      CrossValidationResultProducer.class),
 
1122
                                           CrossValidationResultProducer.class);
 
1123
        propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier",
 
1124
                                                                       se.getClass()),
 
1125
                                           se.getClass());
 
1126
      } catch (IntrospectionException e) {
 
1127
        e.printStackTrace();
 
1128
      }
 
1129
      
 
1130
      m_Exp.setResultProducer(cvrp);
 
1131
      m_Exp.setPropertyPath(propertyPath);
 
1132
 
 
1133
    } else {
 
1134
      RandomSplitResultProducer rsrp = new RandomSplitResultProducer();
 
1135
      rsrp.setRandomizeData(m_ExperimentTypeCBox.getSelectedItem() == TYPE_RANDOMSPLIT_TEXT);
 
1136
      rsrp.setTrainPercent(m_trainPercent);
 
1137
      rsrp.setSplitEvaluator(se);
 
1138
 
 
1139
      PropertyNode[] propertyPath = new PropertyNode[2];
 
1140
      try {
 
1141
        propertyPath[0] = new PropertyNode(se, new PropertyDescriptor("splitEvaluator",
 
1142
                                                                      RandomSplitResultProducer.class),
 
1143
                                           RandomSplitResultProducer.class);
 
1144
        propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier",
 
1145
                                                                       se.getClass()),
 
1146
                                           se.getClass());
 
1147
      } catch (IntrospectionException e) {
 
1148
        e.printStackTrace();
 
1149
      }
 
1150
 
 
1151
      m_Exp.setResultProducer(rsrp);
 
1152
      m_Exp.setPropertyPath(propertyPath);
 
1153
 
 
1154
    }
 
1155
 
 
1156
    m_Exp.setUsePropertyIterator(true);
 
1157
    m_Support.firePropertyChange("", null, null);
 
1158
  }
 
1159
 
 
1160
  /**
 
1161
   * Responds to a change in the experiment parameter.
 
1162
   */
 
1163
  private void expParamChanged() {
 
1164
 
 
1165
    if (m_Exp == null) return;
 
1166
 
 
1167
    if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
 
1168
      try {
 
1169
        m_numFolds = Integer.parseInt(m_ExperimentParameterTField.getText());
 
1170
      } catch (NumberFormatException e) {
 
1171
        return;
 
1172
      }
 
1173
    } else {
 
1174
      try {
 
1175
        m_trainPercent = Double.parseDouble(m_ExperimentParameterTField.getText());
 
1176
      } catch (NumberFormatException e) {
 
1177
        return;
 
1178
      }
 
1179
    }
 
1180
 
 
1181
    if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
 
1182
 
 
1183
      if (m_Exp.getResultProducer() instanceof CrossValidationResultProducer) {
 
1184
        CrossValidationResultProducer cvrp = (CrossValidationResultProducer) m_Exp.getResultProducer();
 
1185
        cvrp.setNumFolds(m_numFolds);
 
1186
      } else {
 
1187
        return;
 
1188
      }
 
1189
 
 
1190
    } else {
 
1191
      
 
1192
      if (m_Exp.getResultProducer() instanceof RandomSplitResultProducer) {
 
1193
        RandomSplitResultProducer rsrp = (RandomSplitResultProducer) m_Exp.getResultProducer();
 
1194
        rsrp.setRandomizeData(m_ExperimentTypeCBox.getSelectedItem() == TYPE_RANDOMSPLIT_TEXT);
 
1195
        rsrp.setTrainPercent(m_trainPercent);
 
1196
      } else {
 
1197
        //System.err.println("not rsrp");
 
1198
        return;
 
1199
      }
 
1200
    }
 
1201
 
 
1202
    m_Support.firePropertyChange("", null, null);
 
1203
  }
 
1204
 
 
1205
  /**
 
1206
   * Responds to a change in the number of repetitions.
 
1207
   */
 
1208
  private void numRepetitionsChanged() {
 
1209
 
 
1210
    if (m_Exp == null || !m_NumberOfRepetitionsTField.isEnabled()) return;
 
1211
 
 
1212
    try {
 
1213
      m_numRepetitions = Integer.parseInt(m_NumberOfRepetitionsTField.getText());
 
1214
    } catch (NumberFormatException e) {
 
1215
      return;
 
1216
    }
 
1217
 
 
1218
    m_Exp.setRunLower(1);
 
1219
    m_Exp.setRunUpper(m_numRepetitions);
 
1220
 
 
1221
    m_Support.firePropertyChange("", null, null);
 
1222
  }
 
1223
 
 
1224
  /**
 
1225
   * Lets user enter username/password/URL.
 
1226
   */
 
1227
  private void chooseURLUsername() {
 
1228
    String dbaseURL=((DatabaseResultListener)m_Exp.getResultListener()).getDatabaseURL();
 
1229
    String username=((DatabaseResultListener)m_Exp.getResultListener()).getUsername();
 
1230
    DatabaseConnectionDialog dbd= new DatabaseConnectionDialog(null,dbaseURL,username);
 
1231
    dbd.setVisible(true);
 
1232
      
 
1233
    //if (dbaseURL == null) {
 
1234
    if (dbd.getReturnValue()==JOptionPane.CLOSED_OPTION) {
 
1235
      return;
 
1236
    }
 
1237
 
 
1238
    ((DatabaseResultListener)m_Exp.getResultListener()).setUsername(dbd.getUsername());
 
1239
    ((DatabaseResultListener)m_Exp.getResultListener()).setPassword(dbd.getPassword());
 
1240
    ((DatabaseResultListener)m_Exp.getResultListener()).setDatabaseURL(dbd.getURL());
 
1241
    ((DatabaseResultListener)m_Exp.getResultListener()).setDebug(dbd.getDebug());
 
1242
    m_ResultsDestinationPathTField.setText(dbd.getURL());
 
1243
  }
 
1244
  /**
 
1245
   * Lets user browse for a destination file..
 
1246
   */
 
1247
  private void chooseDestinationFile() {
 
1248
 
 
1249
    FileFilter fileFilter = null;
 
1250
    if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
 
1251
      fileFilter = m_csvFileFilter;
 
1252
    } else {
 
1253
      fileFilter = m_arffFileFilter;
 
1254
    }
 
1255
    m_DestFileChooser.setFileFilter(fileFilter);
 
1256
    int returnVal = m_DestFileChooser.showSaveDialog(this);
 
1257
    if (returnVal != JFileChooser.APPROVE_OPTION) {
 
1258
      return;
 
1259
    }
 
1260
    m_ResultsDestinationPathTField.setText(m_DestFileChooser.getSelectedFile().toString());
 
1261
  }
 
1262
}