001/** 002 * Copyright (c) 2012, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.ml.feature.transform; 025 026import java.io.File; 027import java.io.FileInputStream; 028import java.io.FileNotFoundException; 029import java.io.IOException; 030import java.io.ObjectInputStream; 031import java.io.Serializable; 032import java.net.URI; 033import java.util.NoSuchElementException; 034 035import org.cleartk.ml.Instance; 036 037/** 038 * Copyright (c) 2012, Regents of the University of Colorado <br> 039 * All rights reserved. 040 * <p> 041 * This class wraps the data written out by an InstanceDataWriter into an Iterable collection 042 * 043 * @author Lee Becker 044 */ 045public class InstanceStream<OUTCOME_T> implements Iterable<Instance<OUTCOME_T>> { 046 047 private URI uri; 048 049 public static <OUTCOME_T> Iterable<Instance<OUTCOME_T>> loadFromDirectory(File dir) { 050 051 File instancesFile = new File(dir, InstanceDataWriter.INSTANCES_OUTPUT_FILENAME); 052 InstanceStream<OUTCOME_T> instanceStream = new InstanceStream<OUTCOME_T>(instancesFile.toURI()); 053 return instanceStream; 054 } 055 056 // public static java.util.Iterator<Instance> loadFromURI(URI uri) { 057 public static <OUTCOME_T> Iterable<Instance<OUTCOME_T>> loadFromURI(URI uri) { 058 059 InstanceStream<OUTCOME_T> instanceStream = new InstanceStream<OUTCOME_T>(uri); 060 return instanceStream; 061 } 062 063 public static class Terminator<OUTCOME_T> extends Instance<OUTCOME_T> implements Serializable { 064 065 private static final long serialVersionUID = 1L; 066 067 public Terminator() { 068 } 069 } 070 071 /** 072 * 073 * @author Lee Becker 074 */ 075 public static class Iterator<OUTCOME_T> implements java.util.Iterator<Instance<OUTCOME_T>> { 076 077 private URI instancesURI; 078 079 private ObjectInputStream input; 080 081 private Instance<OUTCOME_T> lastRead; 082 083 private boolean done; 084 085 public Iterator(URI uri) { 086 this.instancesURI = uri; 087 this.lastRead = null; 088 this.done = false; 089 090 try { 091 FileInputStream fis = new FileInputStream(this.instancesURI.getPath()); 092 this.input = new ObjectInputStream(fis); 093 } catch (FileNotFoundException e) { 094 e.printStackTrace(); 095 } catch (IOException e) { 096 e.printStackTrace(); 097 } 098 } 099 100 @Override 101 public boolean hasNext() { 102 if (this.done) { 103 return false; 104 } 105 106 try { 107 @SuppressWarnings("unchecked") 108 Instance<OUTCOME_T> inst = (Instance<OUTCOME_T>) this.input.readObject(); 109 if (inst instanceof InstanceStream.Terminator) { 110 this.done = true; 111 } else { 112 this.lastRead = inst; 113 this.done = false; 114 } 115 } catch (IOException e) { 116 this.done = true; 117 } catch (ClassNotFoundException e) { 118 this.done = true; 119 } 120 121 return !this.done; 122 123 } 124 125 @SuppressWarnings("unchecked") 126 @Override 127 public Instance<OUTCOME_T> next() { 128 if (this.lastRead != null) { 129 Instance<OUTCOME_T> nextInst = this.lastRead; 130 this.lastRead = null; 131 return nextInst; 132 } 133 134 try { 135 this.lastRead = null; 136 return (Instance<OUTCOME_T>) this.input.readObject(); 137 } catch (IOException e) { 138 throw new NoSuchElementException(); 139 } catch (ClassNotFoundException e) { 140 throw new NoSuchElementException(); 141 } 142 } 143 144 @Override 145 public void remove() { 146 throw new UnsupportedOperationException(); 147 } 148 149 } 150 151 public InstanceStream(URI uri) { 152 this.uri = uri; 153 } 154 155 @Override 156 public java.util.Iterator<Instance<OUTCOME_T>> iterator() { 157 return new InstanceStream.Iterator<OUTCOME_T>(this.uri); 158 } 159 160}