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.
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.
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.
18
* DataGeneratorPanel.java
19
* Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
23
package weka.gui.explorer;
25
import weka.core.Instances;
26
import weka.core.OptionHandler;
27
import weka.core.Utils;
28
import weka.datagenerators.DataGenerator;
29
import weka.gui.GenericObjectEditor;
30
import weka.gui.Logger;
31
import weka.gui.PropertyPanel;
32
import weka.gui.SysErrLog;
34
import java.awt.BorderLayout;
35
import java.beans.PropertyChangeEvent;
36
import java.beans.PropertyChangeListener;
37
import java.io.PrintWriter;
38
import java.io.StringReader;
39
import java.io.StringWriter;
41
import javax.swing.JOptionPane;
42
import javax.swing.JPanel;
45
* A panel for generating artificial data via DataGenerators.
47
* @author FracPete (fracpete at waikato dot ac dot nz)
48
* @version $Revision: 1.4 $
50
public class DataGeneratorPanel
53
/** for serialization */
54
private static final long serialVersionUID = -2520408165350629380L;
56
/** the GOE for the generators */
57
protected GenericObjectEditor m_GeneratorEditor = new GenericObjectEditor();
59
/** the generated Instances */
60
protected Instances m_Instances = null;
62
/** the generated output (as text) */
63
protected StringWriter m_Output = new StringWriter();
65
/** The destination for log/status messages */
66
protected Logger m_Log = new SysErrLog();
68
/** register the classes */
70
GenericObjectEditor.registerEditors();
76
public DataGeneratorPanel() {
77
setLayout(new BorderLayout());
79
add(new PropertyPanel(m_GeneratorEditor), BorderLayout.CENTER);
82
m_GeneratorEditor.setClassType(DataGenerator.class);
83
m_GeneratorEditor.addPropertyChangeListener(new PropertyChangeListener() {
84
public void propertyChange(PropertyChangeEvent e) {
89
// set default generator
94
* Sets the Logger to receive informational messages
96
* @param value the Logger that will now get info messages
98
public void setLog(Logger value) {
103
* returns the generated instances, null if the process was cancelled.
105
* @return the generated Instances
107
public Instances getInstances() {
112
* returns the generated output as text
114
* @return the generated output
116
public String getOutput() {
117
return m_Output.toString();
121
* sets the generator to use initially
123
* @param value the data generator to use
125
public void setGenerator(DataGenerator value) {
127
m_GeneratorEditor.setValue(value);
129
m_GeneratorEditor.setValue(
130
new weka.datagenerators.classifiers.classification.RDG1());
134
* returns the currently selected DataGenerator
136
* @return the current data generator
138
public DataGenerator getGenerator() {
139
return (DataGenerator) m_GeneratorEditor.getValue();
143
* generates the instances, returns TRUE if successful
145
* @return TRUE if successful
146
* @see #getInstances()
148
public boolean execute() {
149
DataGenerator generator;
156
generator = (DataGenerator) m_GeneratorEditor.getValue();
157
relName = generator.getRelationName();
159
cname = generator.getClass().getName().replaceAll(".*\\.", "");
160
cmd = generator.getClass().getName();
161
if (generator instanceof OptionHandler)
162
cmd += " " + Utils.joinOptions(((OptionHandler) generator).getOptions());
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);
173
catch (Exception e) {
175
JOptionPane.showMessageDialog(
176
this, "Error generating data:\n" + e.getMessage(),
177
"Error", JOptionPane.ERROR_MESSAGE);
179
m_Output = new StringWriter();
183
generator.setRelationName(relName);