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