InstanceSavePanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

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