SerializedInstancesLoader.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.  *    SerializedInstancesLoader.java
  18.  *    Copyright (C) 2000 Webmind Corp.
  19.  *
  20.  */
  21. package weka.core.converters;
  22. import java.io.BufferedInputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.ObjectInputStream;
  29. import weka.core.Instance;
  30. import weka.core.Instances;
  31. /**
  32.  * Reads a source that contains serialized Instances.
  33.  *
  34.  * @author <a href="mailto:len@webmind.com">Len Trigg</a>
  35.  * @version $Revision: 1.3 $
  36.  * @see Loader
  37.  */
  38. public class SerializedInstancesLoader extends AbstractLoader {
  39.   /** Holds the structure (header) of the data set. */
  40.   protected Instances m_Dataset = null;
  41.   /** The current index position for incremental reading */
  42.   protected int m_IncrementalIndex = 0;
  43.   /** Resets the Loader ready to read a new data set */
  44.   public void reset() {
  45.     m_Dataset = null;
  46.     m_IncrementalIndex = 0;
  47.   }
  48.   /**
  49.    * Resets the Loader object and sets the source of the data set to be 
  50.    * the supplied File object.
  51.    *
  52.    * @param file the source file.
  53.    * @exception IOException if an error occurs
  54.    */
  55.   public void setSource(File file) throws IOException {
  56.     reset();
  57.     if (file == null) {
  58.       throw new IOException("Source file object is null!");
  59.     }
  60.     try {
  61.       setSource(new FileInputStream(file));
  62.     } catch (FileNotFoundException ex) {
  63.       throw new IOException("File not found");
  64.     }
  65.   }
  66.   /**
  67.    * Resets the Loader object and sets the source of the data set to be 
  68.    * the supplied InputStream.
  69.    *
  70.    * @param in the source InputStream.
  71.    * @exception IOException if there is a problem with IO
  72.    */
  73.   public void setSource(InputStream in) throws IOException {
  74.     ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(in));
  75.     try {
  76.       m_Dataset = (Instances)oi.readObject();
  77.     } catch (ClassNotFoundException ex) {
  78.       throw new IOException("Could not deserialize instances from this source.");
  79.     }
  80.   }
  81.   /**
  82.    * Determines and returns (if possible) the structure (internally the 
  83.    * header) of the data set as an empty set of instances.
  84.    *
  85.    * @return the structure of the data set as an empty set of Instances
  86.    * @exception IOException if an error occurs
  87.    */
  88.   public Instances getStructure() throws IOException {
  89.     if (m_Dataset == null) {
  90.       throw new IOException("No source has been specified");
  91.     }
  92.     // We could cache a structure-only if getStructure is likely to be called
  93.     // many times.
  94.     return new Instances(m_Dataset, 0);
  95.   }
  96.   /**
  97.    * Return the full data set. If the structure hasn't yet been determined
  98.    * by a call to getStructure then method should do so before processing
  99.    * the rest of the data set.
  100.    *
  101.    * @return the structure of the data set as an empty set of Instances
  102.    * @exception IOException if there is no source or parsing fails
  103.    */
  104.   public Instances getDataSet() throws IOException {
  105.     if (m_Dataset == null) {
  106.       throw new IOException("No source has been specified");
  107.     }
  108.     return m_Dataset;
  109.   }
  110.   /**
  111.    * Read the data set incrementally---get the next instance in the data 
  112.    * set or returns null if there are no
  113.    * more instances to get. If the structure hasn't yet been 
  114.    * determined by a call to getStructure then method should do so before
  115.    * returning the next instance in the data set.
  116.    *
  117.    * @return the next instance in the data set as an Instance object or null
  118.    * if there are no more instances to be read
  119.    * @exception IOException if there is an error during parsing
  120.    */
  121.   public Instance getNextInstance() throws IOException {
  122.     if (m_Dataset == null) {
  123.       throw new IOException("No source has been specified");
  124.     }
  125.     // We have to fake this method, since we can only deserialize an entire
  126.     // dataset at a time.
  127.     if (m_IncrementalIndex == m_Dataset.numInstances()) {
  128.       return null;
  129.     }
  130.  
  131.     return m_Dataset.instance(m_IncrementalIndex++);
  132.   }
  133.   /**
  134.    * Main method.
  135.    *
  136.    * @param args should contain the name of an input file.
  137.    */
  138.   public static void main(String [] args) {
  139.     if (args.length > 0) {
  140.       File inputfile;
  141.       inputfile = new File(args[0]);
  142.       try {
  143. SerializedInstancesLoader lo = new SerializedInstancesLoader();
  144. lo.setSource(inputfile);
  145. System.out.println(lo.getStructure());
  146. Instance temp;
  147. do {
  148.   temp = lo.getNextInstance();
  149.   if (temp != null) {
  150.     System.out.println(temp);
  151.   }
  152. } while (temp != null);
  153.       } catch (Exception ex) {
  154. ex.printStackTrace();
  155.       }
  156.     } else {
  157.       System.err.println("Usage:ntSerializedInstancesLoader <file>n");
  158.     }
  159.   }
  160. }