InstanceLoader.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
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.  *    InstanceLoader.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui.streams;
  22. import javax.swing.JPanel;
  23. import javax.swing.JButton;
  24. import javax.swing.JTextField;
  25. import java.awt.Color;
  26. import java.awt.BorderLayout;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.ActionEvent;
  29. import java.util.Vector;
  30. import java.io.Serializable;
  31. import java.io.Reader;
  32. import java.io.BufferedReader;
  33. import java.io.FileReader;
  34. import weka.core.Instance;
  35. import weka.core.Instances;
  36. /** 
  37.  * A bean that produces a stream of instances from a file.
  38.  *
  39.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  40.  * @version $Revision: 1.3 $
  41.  */
  42. public class InstanceLoader extends JPanel 
  43.   implements Serializable, ActionListener, InstanceProducer {
  44.   
  45.   private Vector m_Listeners;
  46.   private Thread m_LoaderThread;
  47.   private Instance m_OutputInstance;
  48.   private Instances m_OutputInstances;
  49.   private boolean m_Debug;
  50.   private JButton m_StartBut;
  51.   private JTextField m_FileNameTex;
  52.   private class LoadThread extends Thread {
  53.     
  54.     private InstanceProducer m_IP;
  55.     public LoadThread(InstanceProducer ip) {
  56.       
  57.       m_IP = ip;
  58.     }
  59.     public void run() {
  60.       
  61.       try {
  62. m_StartBut.setText("Stop");
  63. m_StartBut.setBackground(Color.red);
  64. if (m_Debug) {
  65.   System.err.println("InstanceLoader::LoadThread::run()");
  66. }
  67. // Load the instances one at a time and pass them on to the listener
  68. Reader input = new BufferedReader(
  69.        new FileReader(m_FileNameTex.getText()));
  70. m_OutputInstances = new Instances(input, 1);
  71. if (m_Debug) {
  72.   System.err.println("InstanceLoader::LoadThread::run()"
  73.      + " - Instances opened from: "
  74.      + m_FileNameTex.getText());
  75. }
  76. InstanceEvent ie = new InstanceEvent(m_IP,
  77.      InstanceEvent.FORMAT_AVAILABLE);
  78. notifyInstanceProduced(ie);
  79. while (m_OutputInstances.readInstance(input)) {
  80.   if (m_LoaderThread != this) {
  81.     return;
  82.   }
  83.   if (m_Debug) {
  84.     System.err.println("InstanceLoader::LoadThread::run()"
  85.        + " - read instance");
  86.   }
  87.   // put the instance into a queue?
  88.   m_OutputInstance = m_OutputInstances.instance(0);
  89.   m_OutputInstances.delete(0);
  90.   ie = new InstanceEvent(m_IP, InstanceEvent.INSTANCE_AVAILABLE);
  91.   notifyInstanceProduced(ie);
  92. }
  93. ie = new InstanceEvent(m_IP, InstanceEvent.BATCH_FINISHED);
  94. notifyInstanceProduced(ie);
  95.       } catch (Exception ex) {
  96. System.err.println(ex.getMessage());
  97.       } finally {
  98. m_LoaderThread = null;
  99. m_StartBut.setText("Start");
  100. m_StartBut.setBackground(Color.green);
  101.       }
  102.     }
  103.   }
  104.   public InstanceLoader() {
  105.     setLayout(new BorderLayout());
  106.     m_StartBut = new JButton("Start");
  107.     m_StartBut.setBackground(Color.green);
  108.     add("West",m_StartBut);
  109.     m_StartBut.addActionListener(this);
  110.     m_FileNameTex = new JTextField("/home/trigg/datasets/UCI/iris.arff");
  111.     add("Center",m_FileNameTex);
  112.     m_Listeners = new Vector();
  113.     //    setSize(60,40);
  114.   }
  115.   public void setDebug(boolean debug) {
  116.     
  117.     m_Debug = debug;
  118.   }
  119.   
  120.   public boolean getDebug() {
  121.     
  122.     return m_Debug;
  123.   }
  124.   public void setArffFile(String newArffFile) {
  125.     
  126.     m_FileNameTex.setText(newArffFile);
  127.   }
  128.   
  129.   public String getArffFile() {
  130.     return m_FileNameTex.getText();
  131.   }
  132.   public synchronized void addInstanceListener(InstanceListener ipl) {
  133.     
  134.     m_Listeners.addElement(ipl);
  135.   }
  136.   
  137.   public synchronized void removeInstanceListener(InstanceListener ipl) {
  138.     
  139.     m_Listeners.removeElement(ipl);
  140.   }
  141.   
  142.   protected void notifyInstanceProduced(InstanceEvent e) {
  143.     
  144.     if (m_Debug) {
  145.       System.err.println("InstanceLoader::notifyInstanceProduced()");
  146.     }
  147.     Vector l;
  148.     synchronized (this) {
  149.       l = (Vector)m_Listeners.clone();
  150.     }
  151.     if (l.size() > 0) {
  152.       for(int i = 0; i < l.size(); i++) {
  153. ((InstanceListener)l.elementAt(i)).instanceProduced(e);
  154.       }
  155.       if (e.getID() == InstanceEvent.INSTANCE_AVAILABLE) {
  156. m_OutputInstance = null;
  157.       }
  158.     }
  159.   }
  160.   public Instances outputFormat() throws Exception {
  161.     
  162.     if (m_OutputInstances == null) {
  163.       throw new Exception("No output format defined.");
  164.     }
  165.     return new Instances(m_OutputInstances,0);
  166.   }
  167.   
  168.   public Instance outputPeek() throws Exception {
  169.     
  170.     if ((m_OutputInstances == null)
  171. || (m_OutputInstance == null)) {
  172.       return null;
  173.     }
  174.     return (Instance)m_OutputInstance.copy();
  175.   }
  176.   public void actionPerformed(ActionEvent e) {
  177.     
  178.     Object source = e.getSource();
  179.     if (source == m_StartBut) {
  180.       // load the arff file and send the instances out to the listener
  181.       if (m_LoaderThread == null) {
  182. m_LoaderThread = new LoadThread(this);
  183. m_LoaderThread.setPriority(Thread.MIN_PRIORITY);
  184. m_LoaderThread.start();
  185.       } else {
  186. m_LoaderThread = null;
  187.       }
  188.     }
  189.   }
  190. }