001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * ArffSaver.java
019: * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.core.converters;
024:
025: import weka.core.Capabilities;
026: import weka.core.Instance;
027: import weka.core.Instances;
028: import weka.core.Capabilities.Capability;
029:
030: import java.io.IOException;
031: import java.io.PrintWriter;
032:
033: /**
034: * Writes to a destination in arff text format. <p/>
035: *
036: <!-- options-start -->
037: * Valid options are: <p/>
038: *
039: * <pre> -i <the input file>
040: * The input file</pre>
041: *
042: * <pre> -o <the output file>
043: * The output file</pre>
044: *
045: <!-- options-end -->
046: *
047: * @author Stefan Mutter (mutter@cs.waikato.ac.nz)
048: * @version $Revision: 1.6 $
049: * @see Saver
050: */
051: public class ArffSaver extends AbstractFileSaver implements
052: BatchConverter, IncrementalConverter {
053:
054: /** for serialization */
055: static final long serialVersionUID = 2223634248900042228L;
056:
057: /** Constructor */
058: public ArffSaver() {
059:
060: resetOptions();
061: }
062:
063: /**
064: * Returns a string describing this Saver
065: * @return a description of the Saver suitable for
066: * displaying in the explorer/experimenter gui
067: */
068: public String globalInfo() {
069: return "Writes to a destination that is in arff (attribute relation file format) "
070: + "format. ";
071: }
072:
073: /**
074: * Returns a description of the file type.
075: *
076: * @return a short file description
077: */
078: public String getFileDescription() {
079: return "Arff data files";
080: }
081:
082: /**
083: * Resets the Saver
084: */
085: public void resetOptions() {
086:
087: super .resetOptions();
088: setFileExtension(".arff");
089: }
090:
091: /**
092: * Returns the Capabilities of this saver.
093: *
094: * @return the capabilities of this object
095: * @see Capabilities
096: */
097: public Capabilities getCapabilities() {
098: Capabilities result = super .getCapabilities();
099:
100: // attributes
101: result.enableAllAttributes();
102: result.enable(Capability.MISSING_VALUES);
103:
104: // class
105: result.enableAllClasses();
106: result.enable(Capability.MISSING_CLASS_VALUES);
107: result.enable(Capability.NO_CLASS);
108:
109: return result;
110: }
111:
112: /** Saves an instances incrementally. Structure has to be set by using the
113: * setStructure() method or setInstances() method.
114: * @param inst the instance to save
115: * @throws IOException throws IOEXception if an instance cannot be saved incrementally.
116: */
117: public void writeIncremental(Instance inst) throws IOException {
118:
119: int writeMode = getWriteMode();
120: Instances structure = getInstances();
121: PrintWriter outW = null;
122:
123: if (getRetrieval() == BATCH || getRetrieval() == NONE)
124: throw new IOException(
125: "Batch and incremental saving cannot be mixed.");
126: if (getWriter() != null)
127: outW = new PrintWriter(getWriter());
128:
129: if (writeMode == WAIT) {
130: if (structure == null) {
131: setWriteMode(CANCEL);
132: if (inst != null)
133: System.err
134: .println("Structure(Header Information) has to be set in advance");
135: } else
136: setWriteMode(STRUCTURE_READY);
137: writeMode = getWriteMode();
138: }
139: if (writeMode == CANCEL) {
140: if (outW != null)
141: outW.close();
142: cancel();
143: }
144: if (writeMode == STRUCTURE_READY) {
145: setWriteMode(WRITE);
146: //write header
147: Instances header = new Instances(structure, 0);
148: if (retrieveFile() == null || outW == null)
149: System.out.println(header.toString());
150: else {
151: outW.print(header.toString());
152: outW.print("\n");
153: outW.flush();
154: }
155: writeMode = getWriteMode();
156: }
157: if (writeMode == WRITE) {
158: if (structure == null)
159: throw new IOException(
160: "No instances information available.");
161: if (inst != null) {
162: //write instance
163: if (retrieveFile() == null || outW == null)
164: System.out.println(inst);
165: else {
166: outW.println(inst);
167: m_incrementalCounter++;
168: //flush every 100 instances
169: if (m_incrementalCounter > 100) {
170: m_incrementalCounter = 0;
171: outW.flush();
172: }
173: }
174: } else {
175: //close
176: if (outW != null) {
177: outW.flush();
178: outW.close();
179: }
180: m_incrementalCounter = 0;
181: resetStructure();
182: outW = null;
183: resetWriter();
184: }
185: }
186: }
187:
188: /** Writes a Batch of instances
189: * @throws IOException throws IOException if saving in batch mode is not possible
190: */
191: public void writeBatch() throws IOException {
192:
193: if (getInstances() == null)
194: throw new IOException("No instances to save");
195: if (getRetrieval() == INCREMENTAL)
196: throw new IOException(
197: "Batch and incremental saving cannot be mixed.");
198: setRetrieval(BATCH);
199: setWriteMode(WRITE);
200: if (retrieveFile() == null || getWriter() == null) {
201: System.out.println((getInstances()).toString());
202: setWriteMode(WAIT);
203: return;
204: }
205: PrintWriter outW = new PrintWriter(getWriter());
206: outW.print((getInstances()).toString());
207: outW.flush();
208: outW.close();
209: setWriteMode(WAIT);
210: outW = null;
211: resetWriter();
212: setWriteMode(CANCEL);
213: }
214:
215: /**
216: * Main method.
217: *
218: * @param args should contain the options of a Saver.
219: */
220: public static void main(String[] args) {
221: runFileSaver(new ArffSaver(), args);
222: }
223: }
|