001 package calhoun.analysis.crf.io;
002
003 import java.io.BufferedReader;
004 import java.io.BufferedWriter;
005 import java.io.File;
006 import java.io.FileReader;
007 import java.io.FileWriter;
008 import java.io.IOException;
009 import java.util.Collections;
010 import java.util.HashMap;
011 import java.util.List;
012 import java.util.Map;
013
014 import calhoun.util.Assert;
015
016 /** base class for <code>InterleavedInputComponent</code>s. Implements the read and write methods
017 * from InputComponentIO by calling the Reader and Writer based methods of InterleavedInputComponent.
018 * This allows an InterleavedInputComponent to implement just the reader and writer methods but be usable
019 * in any input handler.
020 * <p>
021 * Also implements a name field. The name is the name of the input sequence which will be read and written by
022 * the input component. This is the name that will identify the returned input sequence as part of a
023 * composite input. The name defaults to "default".
024 */
025 public abstract class InterleavedInputComponentBase implements InterleavedInputComponent {
026 private static final long serialVersionUID = 2972885935425621520L;
027
028 String name = "default";
029
030 /** returns the name of this component
031 * @return name of the input sequence read by this component */
032 public List<String> getComponentNames() {
033 return Collections.singletonList(name);
034 }
035
036 /** the name of the input sequence read and written by this component
037 * @return returns the name for the input sequence this component reads and writes
038 */
039 public String getName() {
040 return name;
041 }
042
043 /** sets the name of the input sequence read and written by this component
044 * @param name the name for the input sequence this component reads and writes
045 */
046 public void setName(String name) {
047 this.name = name;
048 }
049
050 public void readInputSequences(String location, List<Map<String, InputSequence<?>>> inputs) throws IOException {
051 BufferedReader r = new BufferedReader(new FileReader(new File(location)));
052
053 try {
054 Map<String, InputSequence<?>> seq = new HashMap<String, InputSequence<?>>();
055 if(inputs.size() == 0) {
056 // If the array of inputs is empty, fill it using the reader
057 while(true) {
058 seq = new HashMap<String, InputSequence<?>>();
059 boolean success = read(r, seq);
060 if(success)
061 inputs.add(seq);
062 else
063 break;
064 }
065 }
066 else {
067 // If inputs already exist, add this input to them. Ensure the count remains the same.
068 for(Map<String, InputSequence<?>> input : inputs) {
069 boolean success = read(r, input);
070 Assert.a(success);
071 }
072 Assert.a(!read(r, seq));
073 }
074 }
075 finally {
076 r.close();
077 }
078 }
079
080 public void writeInputSequences(String location, List<? extends Map<String, ? extends InputSequence<?>>> inputComponents) throws IOException {
081 BufferedWriter w = new BufferedWriter(new FileWriter(new File(location)));
082 try {
083 for(Map<String, ? extends InputSequence<?>> seq : inputComponents) {
084 write(w, seq);
085 }
086 }
087 finally {
088 w.close();
089 }
090 }
091 }