Source Code Cross Referenced for ArffSaver.java in  » Science » weka » weka » core » converters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » weka » weka.core.converters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 &lt;the input file&gt;
040:         * The input file</pre>
041:         * 
042:         * <pre> -o &lt;the output file&gt;
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.