source: branches/MetisMQI/src/main/java/weka/gui/beans/InstanceStreamToBatchMaker.java @ 29

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

Taggata versione per la demo e aggiunto branch.

File size: 8.1 KB
Line 
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 *    InstanceStreamToBatchMaker.java
19 *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import java.awt.BorderLayout;
26import java.beans.EventSetDescriptor;
27import java.util.ArrayList;
28
29import javax.swing.JPanel;
30
31import weka.core.Instance;
32import weka.core.Instances;
33import weka.gui.Logger;
34
35/**
36 * Bean that converts an instance stream into a (batch) data set.
37 * Useful in conjunction with the Reservoir sampling filter.
38 *
39 * @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
40 * @version $Revision: 5563 $
41 */
42public class InstanceStreamToBatchMaker extends JPanel
43  implements BeanCommon, Visible, InstanceListener, 
44  EventConstraints, DataSource {
45 
46  /**
47   * For serialization
48   */
49  private static final long serialVersionUID = -7037141087208627799L;
50
51  protected BeanVisual m_visual = 
52    new BeanVisual("InstanceStreamToBatchMaker",
53                   BeanVisual.ICON_PATH+"InstanceStreamToBatchMaker.gif",
54                   BeanVisual.ICON_PATH+"InstanceStreamToBatchMaker_animated.gif");
55 
56  /**
57   * The log.
58   */
59  private transient Logger m_log;
60 
61  /**
62   * Component connected to us.
63   */
64  private Object m_listenee;
65 
66  private ArrayList<DataSourceListener> m_dataListeners = 
67    new ArrayList<DataSourceListener>();
68 
69  /**
70   * Collects up the instances.
71   */
72  private ArrayList<Instance> m_batch;
73 
74  private Instances m_structure;
75 
76  public InstanceStreamToBatchMaker() {
77    setLayout(new BorderLayout());
78    add(m_visual, BorderLayout.CENTER);
79  }
80 
81  /**
82   * Accept an instance to add to the batch.
83   *
84   * @param e an <code>InstanceEvent</code> value
85   */
86  public void acceptInstance(InstanceEvent e) {
87    if (e.getStatus() == InstanceEvent.FORMAT_AVAILABLE) {
88      m_batch = new ArrayList<Instance>();
89      m_structure = e.getStructure();
90     
91      // notify dataset listeners of structure available
92      if (m_log != null) {
93        m_log.logMessage("[InstanceStreamToBatch] passing on structure.");
94      }
95      DataSetEvent dse = new DataSetEvent(this, m_structure);
96      notifyDataListeners(dse);
97    } else if (e.getStatus() == InstanceEvent.INSTANCE_AVAILABLE) {
98      m_batch.add(e.getInstance());
99    } else {
100      // batch finished
101     
102      // add the last instance
103      m_batch.add(e.getInstance());
104     
105      // create the new Instances
106      Instances dataSet = new Instances(m_structure, m_batch.size());
107      for (Instance i : m_batch) {
108        dataSet.add(i);
109      }
110      dataSet.compactify();
111     
112      // save memory
113      m_batch = null;
114     
115      if (m_log != null) {
116        m_log.logMessage("[InstanceStreamToBatch] sending batch to listeners.");
117      }
118     
119      // notify dataset listeners
120      DataSetEvent dse = new DataSetEvent(this, dataSet);
121      notifyDataListeners(dse);
122    }
123  }
124
125  /**
126   * Returns true if, at this time,
127   * the object will accept a connection according to the supplied
128   * EventSetDescriptor
129   *
130   * @param esd the EventSetDescriptor
131   * @return true if the object will accept a connection
132   */
133  public boolean connectionAllowed(EventSetDescriptor esd) {
134    return connectionAllowed(esd.getName());
135  }
136
137  /**
138   * Returns true if, at this time,
139   * the object will accept a connection with respect to the named event
140   *
141   * @param eventName the event
142   * @return true if the object will accept a connection
143   */
144  public boolean connectionAllowed(String eventName) {
145    if (m_listenee != null || !eventName.equals("instance")) {
146      return false;
147    }
148    return true;
149  }
150
151  /**
152   * Notify this object that it has been registered as a listener with
153   * a source with respect to the named event
154   *
155   * @param eventName the event
156   * @param source the source with which this object has been registered as
157   * a listener
158   */
159  public void connectionNotification(String eventName, Object source) {
160    if (connectionAllowed(eventName)) {
161      m_listenee = source;
162    }
163  }
164
165  /**
166   * Notify this object that it has been deregistered as a listener with
167   * a source with respect to the supplied event name
168   *
169   * @param eventName the event
170   * @param source the source with which this object has been registered as
171   * a listener
172   */
173  public void disconnectionNotification(String eventName, Object source) {
174    m_listenee = null;
175  }
176 
177  /**
178   * Returns true if, at the current time, the named event could be
179   * generated.
180   *
181   * @param eventName the name of the event in question
182   * @return true if the named event could be generated
183   */
184  public boolean eventGeneratable(String eventName) {
185    if (!eventName.equals("dataSet")) {
186      return false;
187    }
188   
189    if (m_listenee == null) {
190      return false;
191    }
192   
193    if (m_listenee instanceof EventConstraints) {
194      if (!((EventConstraints)m_listenee).eventGeneratable("instance")) {
195        return false;
196      }
197    }
198   
199    return true;
200  }
201
202  /**
203   * Get the custom (descriptive) name for this bean (if one has been set)
204   *
205   * @return the custom name (or the default name)
206   */
207  public String getCustomName() {
208    return m_visual.getText();
209  }
210
211  /**
212   * Set a custom (descriptive) name for this bean
213   *
214   * @param name the name to use
215   */
216  public void setCustomName(String name) {
217    m_visual.setText(name);
218  }
219
220  /**
221   * Set a logger
222   *
223   * @param logger a <code>Logger</code> value
224   */
225  public void setLog(Logger logger) {
226    m_log = logger;
227  }
228 
229  /**
230   * Returns true if. at this time, the bean is busy with some
231   * (i.e. perhaps a worker thread is performing some calculation).
232   *
233   * @return true if the bean is busy.
234   */
235  public boolean isBusy() {
236    return false;
237  }
238
239  /**
240   * Stop any action (if possible).
241   */
242  public void stop() {
243    // not much we can do. Stopping depends on upstream components.
244  }
245
246  /**
247   * Gets the visual appearance of this wrapper bean
248   */
249  public BeanVisual getVisual() {
250    return m_visual;
251  }
252
253  /**
254   * Sets the visual appearance of this wrapper bean
255   *
256   * @param newVisual a <code>BeanVisual</code> value
257   */
258  public void setVisual(BeanVisual newVisual) {
259    m_visual = newVisual;
260  }
261
262  /**
263   * Use the default visual appearance for this bean
264   */
265  public void useDefaultVisual() {
266    m_visual.loadIcons(BeanVisual.ICON_PATH+"InstanceStreamToBatchMaker.gif",
267        BeanVisual.ICON_PATH+"InstanceStreamToBatchMaker_animated.gif");
268  }
269 
270  /**
271   * Notify all data source listeners.
272   *
273   * @param tse the DataSetEvent to pass on.
274   */
275  protected void notifyDataListeners(DataSetEvent tse) {
276    ArrayList<DataSourceListener> l;
277    synchronized (this) {
278      l = (ArrayList<DataSourceListener>)m_dataListeners.clone();
279    }
280    if (l.size() > 0) {
281      for(int i = 0; i < l.size(); i++) {
282        l.get(i).acceptDataSet(tse);
283      }
284    }
285  }
286
287  public synchronized void addDataSourceListener(DataSourceListener tsl) {
288    m_dataListeners.add(tsl);
289    // pass on any format that we might know about
290    if (m_structure != null) {
291      DataSetEvent e = new DataSetEvent(this, m_structure);
292      tsl.acceptDataSet(e);
293    }
294  }
295
296  public synchronized void removeDataSourceListener(DataSourceListener tsl) {
297    m_dataListeners.remove(tsl);
298  }
299 
300  public synchronized void addInstanceListener(InstanceListener il) {
301    // we don't produce instance events
302  }
303 
304  public synchronized void removeInstanceListener(InstanceListener il) {
305    // we don't produce instance events
306  }
307}
Note: See TracBrowser for help on using the repository browser.