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: * CheckScheme.java
019: * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.core;
024:
025: import java.util.Enumeration;
026: import java.util.Vector;
027:
028: /**
029: * Abstract general class for testing in Weka.
030: *
031: * @author FracPete (fracpete at waikato dot ac dot nz)
032: * @version $Revision: 1.1 $
033: */
034: public abstract class Check implements OptionHandler {
035:
036: /** Debugging mode, gives extra output if true */
037: protected boolean m_Debug = false;
038:
039: /** Silent mode, for no output at all to stdout */
040: protected boolean m_Silent = false;
041:
042: /**
043: * Returns an enumeration describing the available options.
044: *
045: * @return an enumeration of all the available options.
046: */
047: public Enumeration listOptions() {
048: Vector result = new Vector();
049:
050: result.addElement(new Option("\tTurn on debugging output.",
051: "D", 0, "-D"));
052:
053: result.addElement(new Option(
054: "\tSilent mode - prints nothing to stdout.", "S", 0,
055: "-S"));
056:
057: return result.elements();
058: }
059:
060: /**
061: * Parses a given list of options.
062: *
063: * @param options the list of options as an array of strings
064: * @throws Exception if an option is not supported
065: */
066: public void setOptions(String[] options) throws Exception {
067: setDebug(Utils.getFlag('D', options));
068:
069: setSilent(Utils.getFlag('S', options));
070: }
071:
072: /**
073: * Gets the current settings of the CheckClassifier.
074: *
075: * @return an array of strings suitable for passing to setOptions
076: */
077: public String[] getOptions() {
078: Vector result;
079:
080: result = new Vector();
081:
082: if (getDebug())
083: result.add("-D");
084:
085: if (getSilent())
086: result.add("-S");
087:
088: return (String[]) result.toArray(new String[result.size()]);
089: }
090:
091: /**
092: * Tries to instantiate a new instance of the given class and checks whether
093: * it is an instance of the specified class. For convenience one can also
094: * specify a classname prefix (e.g., "weka.classifiers") to avoid long
095: * classnames and then instantiate it with the shortened classname (e.g.,
096: * "trees.J48").
097: *
098: * @param prefix the classname prefix (without trailing dot)
099: * @param cls the class to check whether the generated object is an
100: * instance of
101: * @param classname the classname to instantiate
102: * @param options optional options for the object
103: * @return the configured object
104: * @throws Exception if instantiation fails
105: */
106: protected Object forName(String prefix, Class cls,
107: String classname, String[] options) throws Exception {
108:
109: Object result;
110:
111: result = null;
112:
113: try {
114: result = Utils.forName(cls, classname, options);
115: } catch (Exception e) {
116: // shall we try with prefix?
117: if (e.getMessage().toLowerCase().indexOf("can't find") > -1) {
118: try {
119: result = Utils.forName(cls, prefix + "."
120: + classname, options);
121: } catch (Exception ex) {
122: if (e.getMessage().toLowerCase().indexOf(
123: "can't find") > -1) {
124: throw new Exception("Can't find class called '"
125: + classname + "' or '" + prefix + "."
126: + classname + "'!");
127: } else {
128: throw new Exception(ex);
129: }
130: }
131: } else {
132: throw new Exception(e);
133: }
134: }
135:
136: return result;
137: }
138:
139: /**
140: * Begin the tests, reporting results to System.out
141: */
142: public abstract void doTests();
143:
144: /**
145: * Set debugging mode
146: *
147: * @param debug true if debug output should be printed
148: */
149: public void setDebug(boolean debug) {
150: m_Debug = debug;
151: // disable silent mode, if necessary
152: if (getDebug())
153: setSilent(false);
154: }
155:
156: /**
157: * Get whether debugging is turned on
158: *
159: * @return true if debugging output is on
160: */
161: public boolean getDebug() {
162: return m_Debug;
163: }
164:
165: /**
166: * Set slient mode, i.e., no output at all to stdout
167: *
168: * @param value whether silent mode is active or not
169: */
170: public void setSilent(boolean value) {
171: m_Silent = value;
172: }
173:
174: /**
175: * Get whether silent mode is turned on
176: *
177: * @return true if silent mode is on
178: */
179: public boolean getSilent() {
180: return m_Silent;
181: }
182:
183: /**
184: * prints the given message to stdout, if not silent mode
185: *
186: * @param msg the text to print to stdout
187: */
188: protected void print(Object msg) {
189: if (!getSilent())
190: System.out.print(msg);
191: }
192:
193: /**
194: * prints the given message (+ LF) to stdout, if not silent mode
195: *
196: * @param msg the message to println to stdout
197: */
198: protected void println(Object msg) {
199: print(msg + "\n");
200: }
201:
202: /**
203: * prints a LF to stdout, if not silent mode
204: */
205: protected void println() {
206: print("\n");
207: }
208:
209: /**
210: * runs the CheckScheme with the given options
211: *
212: * @param check the checkscheme to setup and run
213: * @param options the commandline parameters to use
214: */
215: protected static void runCheck(Check check, String[] options) {
216: try {
217: try {
218: check.setOptions(options);
219: Utils.checkForRemainingOptions(options);
220: } catch (Exception ex) {
221: String result = ex.getMessage()
222: + "\n\n"
223: + check.getClass().getName().replaceAll(
224: ".*\\.", "") + " Options:\n\n";
225: Enumeration enm = check.listOptions();
226: while (enm.hasMoreElements()) {
227: Option option = (Option) enm.nextElement();
228: result += option.synopsis() + "\n"
229: + option.description() + "\n";
230: }
231: throw new Exception(result);
232: }
233:
234: check.doTests();
235: } catch (Exception ex) {
236: System.err.println(ex.getMessage());
237: }
238: }
239: }
|