source: src/main/java/weka/gui/streams/InstanceSavePanel.java @ 25

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

Import di weka.

File size: 3.9 KB
RevLine 
[4]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 *    InstanceSavePanel.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.streams;
24
25import weka.core.Instance;
26import weka.core.Instances;
27
28import java.awt.BorderLayout;
29import java.awt.Color;
30import java.awt.Label;
31import java.awt.Panel;
32import java.awt.TextField;
33import java.io.FileOutputStream;
34import java.io.PrintWriter;
35
36/**
37 * A bean that saves a stream of instances to a file.
38 *
39 * @author Len Trigg (trigg@cs.waikato.ac.nz)
40 * @version $Revision: 1.5 $
41 */
42public class InstanceSavePanel
43  extends Panel
44  implements InstanceListener {
45
46  /** for serialization */
47  private static final long serialVersionUID = -6061005366989295026L;
48 
49  private Label count_Lab;
50  private int m_Count;
51  private TextField arffFile_Tex;
52  private boolean b_Debug;
53  private PrintWriter outputWriter;
54
55  public void input(Instance instance) throws Exception {
56   
57    if (b_Debug)
58      System.err.println("InstanceSavePanel::input(" + instance +")");
59    m_Count++;
60    count_Lab.setText(""+m_Count+" instances");
61    if (outputWriter != null)
62      outputWriter.println(instance.toString());     
63  }
64 
65  public void inputFormat(Instances instanceInfo) {
66   
67    if (b_Debug)
68      System.err.println("InstanceSavePanel::inputFormat()\n"
69                         +instanceInfo.toString());
70    m_Count = 0;
71    count_Lab.setText(""+m_Count+" instances");
72    try {
73      outputWriter = new PrintWriter(new FileOutputStream(arffFile_Tex.getText()));
74      outputWriter.println(instanceInfo.toString());
75      if (b_Debug)
76        System.err.println("InstanceSavePanel::inputFormat() - written header");
77    } catch (Exception ex) {
78      outputWriter = null;
79      System.err.println("InstanceSavePanel::inputFormat(): "+ex.getMessage());
80    }
81  }
82
83  public void batchFinished() {
84   
85    if (b_Debug)
86      System.err.println("InstanceSavePanel::batchFinished()");
87    if (outputWriter != null)
88      outputWriter.close();
89  }
90
91  public InstanceSavePanel() {
92   
93    setLayout(new BorderLayout());
94    arffFile_Tex = new TextField("arffoutput.arff");
95    add("Center", arffFile_Tex);
96    count_Lab = new Label("0 instances");
97    add("East", count_Lab);
98    //    setSize(60,40);
99    setBackground(Color.lightGray);
100  }
101
102  public void setDebug(boolean debug) {
103    b_Debug = debug;
104  }
105 
106  public boolean getDebug() {
107    return b_Debug;
108  }
109
110  public void setArffFile(String newArffFile) {
111    arffFile_Tex.setText(newArffFile);
112  }
113 
114  public String getArffFile() {
115    return arffFile_Tex.getText();
116  }
117
118  public void instanceProduced(InstanceEvent e) {
119   
120    Object source = e.getSource();
121    if (source instanceof InstanceProducer) { 
122      try {
123        InstanceProducer a = (InstanceProducer) source;
124        switch (e.getID()) {
125        case InstanceEvent.FORMAT_AVAILABLE:
126          inputFormat(a.outputFormat());
127          break;
128        case InstanceEvent.INSTANCE_AVAILABLE:
129          input(a.outputPeek());
130          break;
131        case InstanceEvent.BATCH_FINISHED:
132          batchFinished();
133          break;
134        default:
135          System.err.println("InstanceSavePanel::instanceProduced() - unknown event type");
136          break;
137        }
138      } catch (Exception ex) {
139        System.err.println(ex.getMessage());
140      }
141    } else {
142      System.err.println("InstanceSavePanel::instanceProduced() - Unknown source object type");
143    }
144  }
145}
146
147 
Note: See TracBrowser for help on using the repository browser.