InstanceJoiner.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 9k
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.  *    InstanceJoiner.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui.streams;
  22. import weka.core.Instance;
  23. import weka.core.Instances;
  24. import java.util.Vector;
  25. import java.io.Serializable;
  26. /** 
  27.  * A bean that joins two streams of instances into one.
  28.  *
  29.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  30.  * @version $Revision: 1.3 $
  31.  */
  32. public class InstanceJoiner implements Serializable, InstanceProducer, 
  33.   SerialInstanceListener {
  34.   /** The listeners */
  35.   private Vector listeners;
  36.   /** Debugging mode */
  37.   private boolean b_Debug;
  38.   /** The input format for instances */
  39.   protected Instances m_InputFormat;
  40.   /** The current output instance */
  41.   private Instance m_OutputInstance;
  42.   /** Whether the first input batch has finished */
  43.   private boolean b_FirstInputFinished;
  44.   private boolean b_SecondInputFinished;
  45.   /** Setup the initial states of the member variables */
  46.   public InstanceJoiner() {
  47.     
  48.     listeners = new Vector();
  49.     m_InputFormat = null;
  50.     m_OutputInstance = null;
  51.     b_Debug = false;
  52.     b_FirstInputFinished = false;
  53.     b_SecondInputFinished = false;
  54.   }
  55.   /**
  56.    * Sets the format of the input instances. If the filter is able to determine
  57.    * the output format before seeing any input instances, it does so here. This
  58.    * default implementation assumes the output format is determined when 
  59.    * batchFinished() is called.
  60.    *
  61.    * @param instanceInfo an Instances object containing the input instance
  62.    * structure (any instances contained in the object are ignored - only the
  63.    * structure is required).
  64.    * @return true if the outputFormat may be collected immediately
  65.    */
  66.   public boolean inputFormat(Instances instanceInfo) {
  67.     
  68.     m_InputFormat = new Instances(instanceInfo,0);
  69.     notifyInstanceProduced(new InstanceEvent(this, InstanceEvent.FORMAT_AVAILABLE));
  70.     b_FirstInputFinished = false;
  71.     b_SecondInputFinished = false;
  72.     return true;
  73.   }
  74.   /**
  75.    * Gets the format of the output instances. This should only be called
  76.    * after input() or batchFinished() has returned true.
  77.    *
  78.    * @return an Instances object containing the output instance
  79.    * structure only.
  80.    * @exception Exception if no input structure has been defined (or the output
  81.    * format hasn't been determined yet)
  82.    */
  83.   public Instances outputFormat() throws Exception {
  84.     
  85.     if (m_InputFormat == null) {
  86.       throw new Exception("No output format defined.");
  87.     }
  88.     return new Instances(m_InputFormat,0);
  89.   }
  90.   
  91.   public boolean input(Instance instance) throws Exception {
  92.     
  93.     if (m_InputFormat == null) {
  94.       throw new Exception("No input instance format defined");
  95.     }
  96.     if (instance != null) {
  97.       m_OutputInstance = (Instance)instance.copy();
  98.       notifyInstanceProduced(new InstanceEvent(this,
  99. InstanceEvent.INSTANCE_AVAILABLE));
  100.       return true;
  101.     }
  102.     return false;
  103.   }
  104.   /**
  105.    * Signify that this batch of input to the filter is finished. If the filter
  106.    * requires all instances prior to filtering, output() may now be called
  107.    * to retrieve the filtered instances. Any subsequent instances filtered
  108.    * should be filtered based on setting obtained from the first batch
  109.    * (unless the inputFormat has been re-assigned or new options have been
  110.    * set). This default implementation assumes all instance processing occurs
  111.    * during inputFormat() and input().
  112.    *
  113.    * @return true if there are instances pending output
  114.    * @exception Exception if no input structure has been defined
  115.    */
  116.   public void batchFinished() throws Exception {
  117.     
  118.     if (m_InputFormat == null) {
  119.       throw new Exception("No input instance format defined");
  120.     }
  121.     notifyInstanceProduced(new InstanceEvent(this,
  122.      InstanceEvent.BATCH_FINISHED));
  123.   }
  124.   /**
  125.    * Output an instance after filtering but do not remove from the output
  126.    * queue.
  127.    *
  128.    * @return the instance that has most recently been filtered (or null if
  129.    * the queue is empty).
  130.    * @exception Exception if no input structure has been defined
  131.    */
  132.   public Instance outputPeek() throws Exception {
  133.     
  134.     if (m_InputFormat == null) {
  135.       throw new Exception("No output instance format defined");
  136.     }
  137.     if (m_OutputInstance == null) {
  138.       return null;
  139.     }
  140.     return (Instance)m_OutputInstance.copy();
  141.   }
  142.   public void setDebug(boolean debug) {
  143.     
  144.     b_Debug = debug;
  145.   }
  146.   
  147.   public boolean getDebug() {
  148.     
  149.     return b_Debug;
  150.   }
  151.   public synchronized void addInstanceListener(InstanceListener ipl) {
  152.     
  153.     listeners.addElement(ipl);
  154.   }
  155.   
  156.   public synchronized void removeInstanceListener(InstanceListener ipl) {
  157.     
  158.     listeners.removeElement(ipl);
  159.   }
  160.   
  161.   protected void notifyInstanceProduced(InstanceEvent e) {
  162.     
  163.     if (listeners.size() > 0) {
  164.       if (b_Debug) {
  165. System.err.println(this.getClass().getName()
  166.    + "::notifyInstanceProduced()");
  167.       }
  168.       Vector l;
  169.       synchronized (this) {
  170. l = (Vector)listeners.clone();
  171.       }
  172.       for(int i = 0; i < l.size(); i++) {
  173. ((InstanceListener)l.elementAt(i)).instanceProduced(e);
  174.       }
  175.       // If there are any listeners, and the event is an INSTANCE_AVAILABLE,
  176.       // they should have retrieved the instance with outputPeek();
  177.       try {
  178. if (e.getID() == InstanceEvent.INSTANCE_AVAILABLE) {
  179.   m_OutputInstance = null;
  180. }
  181.       } catch (Exception ex) {
  182. System.err.println("Problem: notifyInstanceProduced() wasn"
  183.    + "called with INSTANCE_AVAILABLE, but output()n"
  184.    + "threw an exception: " + ex.getMessage());
  185.       }
  186.     }
  187.   }
  188.   public void instanceProduced(InstanceEvent e) {
  189.     
  190.     Object source = e.getSource();
  191.     if (source instanceof InstanceProducer) { 
  192.       try {
  193. InstanceProducer a = (InstanceProducer) source;
  194. switch (e.getID()) {
  195. case InstanceEvent.FORMAT_AVAILABLE:
  196.   if (b_Debug) {
  197.     System.err.println(this.getClass().getName()
  198. + "::firstInstanceProduced() - Format available");
  199.   }
  200.   inputFormat(a.outputFormat());
  201.   break;
  202. case InstanceEvent.INSTANCE_AVAILABLE:
  203.   if (b_Debug) {
  204.     System.err.println(this.getClass().getName()
  205. + "::firstInstanceProduced() - Instance available");
  206.   }
  207.   input(a.outputPeek());
  208.   break;
  209. case InstanceEvent.BATCH_FINISHED:
  210.   if (b_Debug) {
  211.     System.err.println(this.getClass().getName()
  212. + "::firstInstanceProduced() - End of instance batch");
  213.   }
  214.   batchFinished();
  215.   b_FirstInputFinished = true;
  216.   break;
  217. default:
  218.   System.err.println(this.getClass().getName()
  219.        + "::firstInstanceProduced() - unknown event type");
  220.   break;
  221. }
  222.       } catch (Exception ex) {
  223. System.err.println(ex.getMessage());
  224.       }
  225.     } else {
  226.       System.err.println(this.getClass().getName()
  227.      + "::firstInstanceProduced() - Unknown source object type");
  228.     }
  229.   }
  230.   public void secondInstanceProduced(InstanceEvent e) {
  231.     
  232.     Object source = e.getSource();
  233.     if (source instanceof InstanceProducer) { 
  234.       try {
  235. if (!b_FirstInputFinished) {
  236.   throw new Exception(this.getClass().getName()
  237.   + "::secondInstanceProduced() - Input received from"
  238.   + " second stream before first stream finished");
  239. }
  240. InstanceProducer a = (InstanceProducer) source;
  241. switch (e.getID()) {
  242. case InstanceEvent.FORMAT_AVAILABLE:
  243.   if (b_Debug) {
  244.     System.err.println(this.getClass().getName()
  245.     + "::secondInstanceProduced() - Format available");
  246.   }
  247.   // Check the formats are compatible
  248.   if (!(a.outputFormat()).equalHeaders(outputFormat())) {
  249.     throw new Exception(this.getClass().getName()
  250.     + "::secondInstanceProduced() - incompatible instance streams");
  251.   }
  252.   break;
  253. case InstanceEvent.INSTANCE_AVAILABLE:
  254.   if (b_Debug) {
  255.     System.err.println(this.getClass().getName()
  256.     + "::secondInstanceProduced() - Instance available");
  257.   }
  258.   input(a.outputPeek());
  259.   break;
  260. case InstanceEvent.BATCH_FINISHED:
  261.   if (b_Debug) {
  262.     System.err.println(this.getClass().getName()
  263.     + "::secondInstanceProduced() - End of instance batch");
  264.   }
  265.   batchFinished();
  266.   break;
  267. default:
  268.   System.err.println(this.getClass().getName()
  269. + "::secondInstanceProduced() - unknown event type");
  270.   break;
  271. }
  272.       } catch (Exception ex) {
  273. System.err.println(ex.getMessage());
  274.       }
  275.     } else {
  276.       System.err.println(this.getClass().getName()
  277.   + "::secondInstanceProduced() - Unknown source object type");
  278.     }
  279.   }
  280. }