source: src/main/java/weka/gui/explorer/AssociationsPanel.java @ 11

Last change on this file since 11 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 19.1 KB
Line 
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 *    AssociationsPanel.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.explorer;
24
25import weka.associations.Associator;
26import weka.core.Attribute;
27import weka.core.Capabilities;
28import weka.core.CapabilitiesHandler;
29import weka.core.Instances;
30import weka.core.OptionHandler;
31import weka.core.Utils;
32import weka.gui.GenericObjectEditor;
33import weka.gui.Logger;
34import weka.gui.PropertyPanel;
35import weka.gui.ResultHistoryPanel;
36import weka.gui.SaveBuffer;
37import weka.gui.SysErrLog;
38import weka.gui.TaskLogger;
39import weka.gui.explorer.Explorer.CapabilitiesFilterChangeEvent;
40import weka.gui.explorer.Explorer.CapabilitiesFilterChangeListener;
41import weka.gui.explorer.Explorer.ExplorerPanel;
42import weka.gui.explorer.Explorer.LogHandler;
43
44import java.awt.BorderLayout;
45import java.awt.Font;
46import java.awt.GridBagConstraints;
47import java.awt.GridBagLayout;
48import java.awt.GridLayout;
49import java.awt.Point;
50import java.awt.event.ActionEvent;
51import java.awt.event.ActionListener;
52import java.awt.event.InputEvent;
53import java.awt.event.MouseAdapter;
54import java.awt.event.MouseEvent;
55import java.beans.PropertyChangeEvent;
56import java.beans.PropertyChangeListener;
57import java.text.SimpleDateFormat;
58import java.util.Date;
59
60import javax.swing.BorderFactory;
61import javax.swing.JButton;
62import javax.swing.JMenuItem;
63import javax.swing.JPanel;
64import javax.swing.JPopupMenu;
65import javax.swing.JScrollPane;
66import javax.swing.JTextArea;
67import javax.swing.JViewport;
68import javax.swing.event.ChangeEvent;
69import javax.swing.event.ChangeListener;
70
71/**
72 * This panel allows the user to select, configure, and run a scheme
73 * that learns associations.
74 *
75 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
76 * @version $Revision: 5704 $
77 */
78public class AssociationsPanel 
79  extends JPanel
80  implements CapabilitiesFilterChangeListener, ExplorerPanel, LogHandler {
81 
82  /** for serialization */
83  static final long serialVersionUID = -6867871711865476971L;
84
85  /** the parent frame */
86  protected Explorer m_Explorer = null;
87
88  /** Lets the user configure the associator */
89  protected GenericObjectEditor m_AssociatorEditor =
90    new GenericObjectEditor();
91
92  /** The panel showing the current associator selection */
93  protected PropertyPanel m_CEPanel = new PropertyPanel(m_AssociatorEditor);
94 
95  /** The output area for associations */
96  protected JTextArea m_OutText = new JTextArea(20, 40);
97
98  /** The destination for log/status messages */
99  protected Logger m_Log = new SysErrLog();
100
101  /** The buffer saving object for saving output */
102  protected SaveBuffer m_SaveOut = new SaveBuffer(m_Log, this);
103
104  /** A panel controlling results viewing */
105  protected ResultHistoryPanel m_History = new ResultHistoryPanel(m_OutText);
106
107  /** Click to start running the associator */
108  protected JButton m_StartBut = new JButton("Start");
109
110  /** Click to stop a running associator */
111  protected JButton m_StopBut = new JButton("Stop");
112 
113  /** The main set of instances we're playing with */
114  protected Instances m_Instances;
115
116  /** The user-supplied test set (if any) */
117  protected Instances m_TestInstances;
118 
119  /** A thread that associator runs in */
120  protected Thread m_RunThread;
121
122  /* Register the property editors we need */
123  static {
124     GenericObjectEditor.registerEditors();
125  }
126 
127  /**
128   * Creates the associator panel
129   */
130  public AssociationsPanel() {
131
132    // Connect / configure the components
133    m_OutText.setEditable(false);
134    m_OutText.setFont(new Font("Monospaced", Font.PLAIN, 12));
135    m_OutText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
136    m_OutText.addMouseListener(new MouseAdapter() {
137      public void mouseClicked(MouseEvent e) {
138        if ((e.getModifiers() & InputEvent.BUTTON1_MASK)
139            != InputEvent.BUTTON1_MASK) {
140          m_OutText.selectAll();
141        }
142      }
143    });
144    m_History.setBorder(BorderFactory.createTitledBorder("Result list (right-click for options)"));
145    m_History.setHandleRightClicks(false);
146    // see if we can popup a menu for the selected result
147    m_History.getList().addMouseListener(new MouseAdapter() {
148        public void mouseClicked(MouseEvent e) {
149          if (((e.getModifiers() & InputEvent.BUTTON1_MASK)
150               != InputEvent.BUTTON1_MASK) || e.isAltDown()) {
151            int index = m_History.getList().locationToIndex(e.getPoint());
152            if (index != -1) {
153              String name = m_History.getNameAtIndex(index);
154              historyRightClickPopup(name, e.getX(), e.getY());
155            } else {
156              historyRightClickPopup(null, e.getX(), e.getY());
157            }
158          }
159        }
160      });
161
162    m_AssociatorEditor.setClassType(Associator.class);
163    m_AssociatorEditor.setValue(ExplorerDefaults.getAssociator());
164    m_AssociatorEditor.addPropertyChangeListener(new PropertyChangeListener() {
165      public void propertyChange(PropertyChangeEvent e) {
166        m_StartBut.setEnabled(true);
167        // Check capabilities
168        Capabilities currentFilter = m_AssociatorEditor.getCapabilitiesFilter();
169        Associator associator = (Associator) m_AssociatorEditor.getValue();
170        Capabilities currentSchemeCapabilities =  null;
171        if (associator != null && currentFilter != null && 
172            (associator instanceof CapabilitiesHandler)) {
173          currentSchemeCapabilities = ((CapabilitiesHandler)associator).getCapabilities();
174         
175          if (!currentSchemeCapabilities.supportsMaybe(currentFilter) &&
176              !currentSchemeCapabilities.supports(currentFilter)) {
177            m_StartBut.setEnabled(false);
178          }
179        }
180        repaint();
181      }
182    });
183
184    m_StartBut.setToolTipText("Starts the associator");
185    m_StopBut.setToolTipText("Stops the associator");
186    m_StartBut.setEnabled(false);
187    m_StopBut.setEnabled(false);
188    m_StartBut.addActionListener(new ActionListener() {
189      public void actionPerformed(ActionEvent e) {
190        startAssociator();
191      }
192    });
193    m_StopBut.addActionListener(new ActionListener() {
194      public void actionPerformed(ActionEvent e) {
195        stopAssociator();
196      }
197    });
198
199    // Layout the GUI
200    JPanel p1 = new JPanel();
201    p1.setBorder(BorderFactory.createCompoundBorder(
202                 BorderFactory.createTitledBorder("Associator"),
203                 BorderFactory.createEmptyBorder(0, 5, 5, 5)
204                 ));
205    p1.setLayout(new BorderLayout());
206    p1.add(m_CEPanel, BorderLayout.NORTH);
207
208    JPanel buttons = new JPanel();
209    buttons.setLayout(new GridLayout(1,2));
210    JPanel ssButs = new JPanel();
211    ssButs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
212    ssButs.setLayout(new GridLayout(1, 2, 5, 5));
213    ssButs.add(m_StartBut);
214    ssButs.add(m_StopBut);
215    buttons.add(ssButs);
216
217    JPanel p3 = new JPanel();
218    p3.setBorder(BorderFactory.createTitledBorder("Associator output"));
219    p3.setLayout(new BorderLayout());
220    final JScrollPane js = new JScrollPane(m_OutText);
221    p3.add(js, BorderLayout.CENTER);
222    js.getViewport().addChangeListener(new ChangeListener() {
223      private int lastHeight;
224      public void stateChanged(ChangeEvent e) {
225        JViewport vp = (JViewport)e.getSource();
226        int h = vp.getViewSize().height; 
227        if (h != lastHeight) { // i.e. an addition not just a user scrolling
228          lastHeight = h;
229          int x = h - vp.getExtentSize().height;
230          vp.setViewPosition(new Point(0, x));
231        }
232      }
233    });
234   
235    GridBagLayout gbL = new GridBagLayout();
236    GridBagConstraints gbC = new GridBagConstraints();
237    JPanel mondo = new JPanel();
238    gbL = new GridBagLayout();
239    mondo.setLayout(gbL);
240    gbC = new GridBagConstraints();
241    gbC.anchor = GridBagConstraints.NORTH;
242    gbC.fill = GridBagConstraints.HORIZONTAL;
243    gbC.gridy = 1;     gbC.gridx = 0;
244    gbL.setConstraints(buttons, gbC);
245    mondo.add(buttons);
246    gbC = new GridBagConstraints();
247    gbC.fill = GridBagConstraints.BOTH;
248    gbC.gridy = 2;     gbC.gridx = 0; gbC.weightx = 0;
249    gbL.setConstraints(m_History, gbC);
250    mondo.add(m_History);
251    gbC = new GridBagConstraints();
252    gbC.fill = GridBagConstraints.BOTH;
253    gbC.gridy = 0;     gbC.gridx = 1;
254    gbC.gridheight = 3;
255    gbC.weightx = 100; gbC.weighty = 100;
256    gbL.setConstraints(p3, gbC);
257    mondo.add(p3);
258
259    setLayout(new BorderLayout());
260    add(p1, BorderLayout.NORTH);
261    add(mondo, BorderLayout.CENTER);
262  }
263 
264  /**
265   * Sets the Logger to receive informational messages
266   *
267   * @param newLog the Logger that will now get info messages
268   */
269  public void setLog(Logger newLog) {
270
271    m_Log = newLog;
272  }
273 
274  /**
275   * Tells the panel to use a new set of instances.
276   *
277   * @param inst a set of Instances
278   */
279  public void setInstances(Instances inst) {
280   
281    m_Instances = inst;
282    String [] attribNames = new String [m_Instances.numAttributes()];
283    for (int i = 0; i < attribNames.length; i++) {
284      String type = "";
285      switch (m_Instances.attribute(i).type()) {
286      case Attribute.NOMINAL:
287        type = "(Nom) ";
288        break;
289      case Attribute.NUMERIC:
290        type = "(Num) ";
291        break;
292      case Attribute.STRING:
293        type = "(Str) ";
294        break;
295      case Attribute.DATE:
296        type = "(Dat) ";
297        break;
298      case Attribute.RELATIONAL:
299        type = "(Rel) ";
300        break;
301      default:
302        type = "(???) ";
303      }
304      attribNames[i] = type + m_Instances.attribute(i).name();
305    }
306    m_StartBut.setEnabled(m_RunThread == null);
307    m_StopBut.setEnabled(m_RunThread != null);
308  }
309 
310  /**
311   * Starts running the currently configured associator with the current
312   * settings. This is run in a separate thread, and will only start if
313   * there is no associator already running. The associator output is sent
314   * to the results history panel.
315   */
316  protected void startAssociator() {
317
318    if (m_RunThread == null) {
319      m_StartBut.setEnabled(false);
320      m_StopBut.setEnabled(true);
321      m_RunThread = new Thread() {
322        public void run() {
323          // Copy the current state of things
324          m_Log.statusMessage("Setting up...");
325          Instances inst = new Instances(m_Instances);
326
327          Associator associator = (Associator) m_AssociatorEditor.getValue();
328          StringBuffer outBuff = new StringBuffer();
329          String name = (new SimpleDateFormat("HH:mm:ss - "))
330          .format(new Date());
331          String cname = associator.getClass().getName();
332          if (cname.startsWith("weka.associations.")) {
333            name += cname.substring("weka.associations.".length());
334          } else {
335            name += cname;
336          }
337          String cmd = m_AssociatorEditor.getValue().getClass().getName();
338          if (m_AssociatorEditor.getValue() instanceof OptionHandler)
339            cmd += " " + Utils.joinOptions(((OptionHandler) m_AssociatorEditor.getValue()).getOptions());
340          try {
341
342            // Output some header information
343            m_Log.logMessage("Started " + cname);
344            m_Log.logMessage("Command: " + cmd);
345            if (m_Log instanceof TaskLogger) {
346              ((TaskLogger)m_Log).taskStarted();
347            }
348            outBuff.append("=== Run information ===\n\n");
349            outBuff.append("Scheme:       " + cname);
350            if (associator instanceof OptionHandler) {
351              String [] o = ((OptionHandler) associator).getOptions();
352              outBuff.append(" " + Utils.joinOptions(o));
353            }
354            outBuff.append("\n");
355            outBuff.append("Relation:     " + inst.relationName() + '\n');
356            outBuff.append("Instances:    " + inst.numInstances() + '\n');
357            outBuff.append("Attributes:   " + inst.numAttributes() + '\n');
358            if (inst.numAttributes() < 100) {
359              for (int i = 0; i < inst.numAttributes(); i++) {
360                outBuff.append("              " + inst.attribute(i).name()
361                               + '\n');
362              }
363            } else {
364              outBuff.append("              [list of attributes omitted]\n");
365            }
366            m_History.addResult(name, outBuff);
367            m_History.setSingle(name);
368           
369            // Build the model and output it.
370            m_Log.statusMessage("Building model on training data...");
371            associator.buildAssociations(inst);
372            outBuff.append("=== Associator model (full training set) ===\n\n");
373            outBuff.append(associator.toString() + '\n');
374            m_History.updateResult(name);
375            m_Log.logMessage("Finished " + cname);
376            m_Log.statusMessage("OK");
377          } catch (Exception ex) {
378            m_Log.logMessage(ex.getMessage());
379            m_Log.statusMessage("See error log");
380          } finally {
381            if (isInterrupted()) {
382              m_Log.logMessage("Interrupted " + cname);
383              m_Log.statusMessage("See error log");
384            }
385            m_RunThread = null;
386            m_StartBut.setEnabled(true);
387            m_StopBut.setEnabled(false);
388            if (m_Log instanceof TaskLogger) {
389              ((TaskLogger)m_Log).taskFinished();
390            }
391          }
392        }
393      };
394      m_RunThread.setPriority(Thread.MIN_PRIORITY);
395      m_RunThread.start();
396    }
397  }
398 
399  /**
400   * Stops the currently running Associator (if any).
401   */
402  protected void stopAssociator() {
403
404    if (m_RunThread != null) {
405      m_RunThread.interrupt();
406     
407      // This is deprecated (and theoretically the interrupt should do).
408      m_RunThread.stop();
409     
410    }
411  }
412
413  /**
414   * Save the currently selected associator output to a file.
415   * @param name the name of the buffer to save
416   */
417  protected void saveBuffer(String name) {
418    StringBuffer sb = m_History.getNamedBuffer(name);
419    if (sb != null) {
420      if (m_SaveOut.save(sb)) {
421        m_Log.logMessage("Save successful.");
422      }
423    }
424  }
425   
426  /**
427   * Handles constructing a popup menu with visualization options.
428   * @param name the name of the result history list entry clicked on by
429   * the user
430   * @param x the x coordinate for popping up the menu
431   * @param y the y coordinate for popping up the menu
432   */
433  protected void historyRightClickPopup(String name, int x, int y) {
434    final String selectedName = name;
435    JPopupMenu resultListMenu = new JPopupMenu();
436   
437    JMenuItem visMainBuffer = new JMenuItem("View in main window");
438    if (selectedName != null) {
439      visMainBuffer.addActionListener(new ActionListener() {
440          public void actionPerformed(ActionEvent e) {
441            m_History.setSingle(selectedName);
442          }
443        });
444    } else {
445      visMainBuffer.setEnabled(false);
446    }
447    resultListMenu.add(visMainBuffer);
448   
449    JMenuItem visSepBuffer = new JMenuItem("View in separate window");
450    if (selectedName != null) {
451      visSepBuffer.addActionListener(new ActionListener() {
452        public void actionPerformed(ActionEvent e) {
453          m_History.openFrame(selectedName);
454        }
455      });
456    } else {
457      visSepBuffer.setEnabled(false);
458    }
459    resultListMenu.add(visSepBuffer);
460   
461    JMenuItem saveOutput = new JMenuItem("Save result buffer");
462    if (selectedName != null) {
463      saveOutput.addActionListener(new ActionListener() {
464          public void actionPerformed(ActionEvent e) {
465            saveBuffer(selectedName);
466          }
467        });
468    } else {
469      saveOutput.setEnabled(false);
470    }
471    resultListMenu.add(saveOutput);
472
473    JMenuItem deleteOutput = new JMenuItem("Delete result buffer");
474    if (selectedName != null) {
475      deleteOutput.addActionListener(new ActionListener() {
476        public void actionPerformed(ActionEvent e) {
477          m_History.removeResult(selectedName);
478        }
479      });
480    } else {
481      deleteOutput.setEnabled(false);
482    }
483    resultListMenu.add(deleteOutput);
484
485    resultListMenu.show(m_History.getList(), x, y);
486  }
487 
488  /**
489   * updates the capabilities filter of the GOE
490   *
491   * @param filter      the new filter to use
492   */
493  protected void updateCapabilitiesFilter(Capabilities filter) {
494    Instances           tempInst;
495    Capabilities        filterClass;
496   
497    if (filter == null) {
498      m_AssociatorEditor.setCapabilitiesFilter(new Capabilities(null));
499      return;
500    }
501   
502    if (!ExplorerDefaults.getInitGenericObjectEditorFilter())
503      tempInst = new Instances(m_Instances, 0);
504    else
505      tempInst = new Instances(m_Instances);
506    tempInst.setClassIndex(-1);
507   
508    try {
509      filterClass = Capabilities.forInstances(tempInst);
510    }
511    catch (Exception e) {
512      filterClass = new Capabilities(null);
513    }
514   
515    m_AssociatorEditor.setCapabilitiesFilter(filterClass);
516   
517    m_StartBut.setEnabled(true);
518    // Check capabilities
519    Capabilities currentFilter = m_AssociatorEditor.getCapabilitiesFilter();
520    Associator associator = (Associator) m_AssociatorEditor.getValue();
521    Capabilities currentSchemeCapabilities =  null;
522    if (associator != null && currentFilter != null && 
523        (associator instanceof CapabilitiesHandler)) {
524      currentSchemeCapabilities = ((CapabilitiesHandler)associator).getCapabilities();
525     
526      if (!currentSchemeCapabilities.supportsMaybe(currentFilter) &&
527          !currentSchemeCapabilities.supports(currentFilter)) {
528        m_StartBut.setEnabled(false);
529      }
530    }
531  }
532 
533  /**
534   * method gets called in case of a change event
535   *
536   * @param e           the associated change event
537   */
538  public void capabilitiesFilterChanged(CapabilitiesFilterChangeEvent e) {
539    if (e.getFilter() == null)
540      updateCapabilitiesFilter(null);
541    else
542      updateCapabilitiesFilter((Capabilities) e.getFilter().clone());
543  }
544
545  /**
546   * Sets the Explorer to use as parent frame (used for sending notifications
547   * about changes in the data)
548   *
549   * @param parent      the parent frame
550   */
551  public void setExplorer(Explorer parent) {
552    m_Explorer = parent;
553  }
554 
555  /**
556   * returns the parent Explorer frame
557   *
558   * @return            the parent
559   */
560  public Explorer getExplorer() {
561    return m_Explorer;
562  }
563 
564  /**
565   * Returns the title for the tab in the Explorer
566   *
567   * @return            the title of this tab
568   */
569  public String getTabTitle() {
570    return "Associate";
571  }
572 
573  /**
574   * Returns the tooltip for the tab in the Explorer
575   *
576   * @return            the tooltip of this tab
577   */
578  public String getTabTitleToolTip() {
579    return "Discover association rules";
580  }
581
582  /**
583   * Tests out the Associator panel from the command line.
584   *
585   * @param args may optionally contain the name of a dataset to load.
586   */
587  public static void main(String [] args) {
588
589    try {
590      final javax.swing.JFrame jf =
591        new javax.swing.JFrame("Weka Explorer: Associator");
592      jf.getContentPane().setLayout(new BorderLayout());
593      final AssociationsPanel sp = new AssociationsPanel();
594      jf.getContentPane().add(sp, BorderLayout.CENTER);
595      weka.gui.LogPanel lp = new weka.gui.LogPanel();
596      sp.setLog(lp);
597      jf.getContentPane().add(lp, BorderLayout.SOUTH);
598      jf.addWindowListener(new java.awt.event.WindowAdapter() {
599        public void windowClosing(java.awt.event.WindowEvent e) {
600          jf.dispose();
601          System.exit(0);
602        }
603      });
604      jf.pack();
605      jf.setVisible(true);
606      if (args.length == 1) {
607        System.err.println("Loading instances from " + args[0]);
608        java.io.Reader r = new java.io.BufferedReader(
609                           new java.io.FileReader(args[0]));
610        Instances i = new Instances(r);
611        sp.setInstances(i);
612      }
613    } catch (Exception ex) {
614      ex.printStackTrace();
615      System.err.println(ex.getMessage());
616    }
617  }
618}
Note: See TracBrowser for help on using the repository browser.