source: branches/MetisMQI/src/main/java/weka/gui/explorer/DataGeneratorPanel.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.0 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 * DataGeneratorPanel.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.explorer;
24
25import weka.core.Instances;
26import weka.core.OptionHandler;
27import weka.core.Utils;
28import weka.datagenerators.DataGenerator;
29import weka.gui.GenericObjectEditor;
30import weka.gui.Logger;
31import weka.gui.PropertyPanel;
32import weka.gui.SysErrLog;
33
34import java.awt.BorderLayout;
35import java.beans.PropertyChangeEvent;
36import java.beans.PropertyChangeListener;
37import java.io.PrintWriter;
38import java.io.StringReader;
39import java.io.StringWriter;
40
41import javax.swing.JOptionPane;
42import javax.swing.JPanel;
43
44/**
45 * A panel for generating artificial data via DataGenerators.
46 *
47 * @author FracPete (fracpete at waikato dot ac dot nz)
48 * @version $Revision: 1.4 $
49 */
50public class DataGeneratorPanel
51  extends JPanel {
52
53  /** for serialization */
54  private static final long serialVersionUID = -2520408165350629380L;
55
56  /** the GOE for the generators */
57  protected GenericObjectEditor m_GeneratorEditor = new GenericObjectEditor();
58
59  /** the generated Instances */
60  protected Instances m_Instances = null;
61
62  /** the generated output (as text) */
63  protected StringWriter m_Output = new StringWriter();
64
65  /** The destination for log/status messages */
66  protected Logger m_Log = new SysErrLog();
67
68  /** register the classes */
69  static {
70     GenericObjectEditor.registerEditors();
71  }
72 
73  /**
74   * creates the panel
75   */
76  public DataGeneratorPanel() {
77    setLayout(new BorderLayout());
78   
79    add(new PropertyPanel(m_GeneratorEditor), BorderLayout.CENTER);
80
81    // editor
82    m_GeneratorEditor.setClassType(DataGenerator.class);
83    m_GeneratorEditor.addPropertyChangeListener(new PropertyChangeListener() {
84      public void propertyChange(PropertyChangeEvent e) {
85        repaint();
86      }
87    });
88   
89    // set default generator
90    setGenerator(null);
91  }
92
93  /**
94   * Sets the Logger to receive informational messages
95   *
96   * @param value       the Logger that will now get info messages
97   */
98  public void setLog(Logger value) {
99    m_Log = value;
100  }
101
102  /**
103   * returns the generated instances, null if the process was cancelled.
104   *
105   * @return the generated Instances
106   */
107  public Instances getInstances() {
108    return m_Instances;
109  }
110
111  /**
112   * returns the generated output as text
113   *
114   * @return            the generated output
115   */
116  public String getOutput() {
117    return m_Output.toString();
118  }
119
120  /**
121   * sets the generator to use initially
122   *
123   * @param value       the data generator to use
124   */
125  public void setGenerator(DataGenerator value) {
126    if (value != null)
127      m_GeneratorEditor.setValue(value);
128    else
129      m_GeneratorEditor.setValue(
130          new weka.datagenerators.classifiers.classification.RDG1());
131  }
132
133  /**
134   * returns the currently selected DataGenerator
135   *
136   * @return            the current data generator
137   */
138  public DataGenerator getGenerator() {
139    return (DataGenerator) m_GeneratorEditor.getValue();
140  }
141
142  /**
143   * generates the instances, returns TRUE if successful
144   *
145   * @return            TRUE if successful
146   * @see #getInstances()
147   */
148  public boolean execute() {
149    DataGenerator     generator;
150    boolean           result;
151    String            relName;
152    String            cname;
153    String            cmd;
154   
155    result    = true;
156    generator = (DataGenerator) m_GeneratorEditor.getValue();
157    relName   = generator.getRelationName();
158
159    cname = generator.getClass().getName().replaceAll(".*\\.", "");
160    cmd = generator.getClass().getName();
161    if (generator instanceof OptionHandler)
162      cmd += " " + Utils.joinOptions(((OptionHandler) generator).getOptions());
163   
164    try {
165      m_Log.logMessage("Started " + cname);
166      m_Log.logMessage("Command: " + cmd);
167      m_Output = new StringWriter();
168      generator.setOutput(new PrintWriter(m_Output));
169      DataGenerator.makeData(generator, generator.getOptions());
170      m_Instances = new Instances(new StringReader(getOutput()));
171      m_Log.logMessage("Finished " + cname);
172    }
173    catch (Exception e) {
174      e.printStackTrace();
175      JOptionPane.showMessageDialog(
176          this, "Error generating data:\n" + e.getMessage(), 
177          "Error", JOptionPane.ERROR_MESSAGE);
178      m_Instances = null;
179      m_Output    = new StringWriter();
180      result      = false;
181    }
182
183    generator.setRelationName(relName);
184
185    return result;
186  }
187}
Note: See TracBrowser for help on using the repository browser.