source: src/main/java/weka/core/Check.java @ 6

Last change on this file since 6 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.2 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * CheckScheme.java
19 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.core;
24
25import java.util.Enumeration;
26import java.util.Vector;
27
28/**
29 * Abstract general class for testing in Weka.
30 *
31 * @author FracPete (fracpete at waikato dot ac dot nz)
32 * @version $Revision: 5953 $
33 */
34public abstract class Check
35  implements OptionHandler, RevisionHandler {
36 
37  /** Debugging mode, gives extra output if true */
38  protected boolean m_Debug = false;
39 
40  /** Silent mode, for no output at all to stdout */
41  protected boolean m_Silent = false;
42 
43  /**
44   * Returns an enumeration describing the available options.
45   *
46   * @return an enumeration of all the available options.
47   */
48  public Enumeration listOptions() {
49    Vector<Option> result = new Vector<Option>();
50   
51    result.addElement(new Option(
52        "\tTurn on debugging output.",
53        "D", 0, "-D"));
54   
55    result.addElement(new Option(
56        "\tSilent mode - prints nothing to stdout.",
57        "S", 0, "-S"));
58   
59    return result.elements();
60  }
61 
62  /**
63   * Parses a given list of options.
64   *
65   * @param options the list of options as an array of strings
66   * @throws Exception if an option is not supported
67   */
68  public void setOptions(String[] options) throws Exception {
69    setDebug(Utils.getFlag('D', options));
70
71    setSilent(Utils.getFlag('S', options));
72  }
73 
74  /**
75   * Gets the current settings of the CheckClassifier.
76   *
77   * @return an array of strings suitable for passing to setOptions
78   */
79  public String[] getOptions() {
80    Vector<String>        result;
81   
82    result = new Vector<String>();
83   
84    if (getDebug())
85      result.add("-D");
86   
87    if (getSilent())
88      result.add("-S");
89   
90    return (String[]) result.toArray(new String[result.size()]);
91  }
92 
93  /**
94   * Tries to instantiate a new instance of the given class and checks whether
95   * it is an instance of the specified class. For convenience one can also
96   * specify a classname prefix (e.g., "weka.classifiers") to avoid long
97   * classnames and then instantiate it with the shortened classname (e.g.,
98   * "trees.J48").
99   *
100   * @param prefix      the classname prefix (without trailing dot)
101   * @param cls         the class to check whether the generated object is an
102   *                    instance of
103   * @param classname   the classname to instantiate
104   * @param options     optional options for the object
105   * @return            the configured object
106   * @throws Exception  if instantiation fails
107   */
108  protected Object forName(String prefix, Class cls, String classname, 
109      String[] options) throws Exception {
110
111    Object      result;
112   
113    result = null;
114
115    try {
116      result = Utils.forName(cls, classname, options);
117    }
118    catch (Exception e) {
119      // shall we try with prefix?
120      if (e.getMessage().toLowerCase().indexOf("can't find") > -1) {
121        try {
122          result = Utils.forName(cls, prefix + "." + classname, options);
123        }
124        catch (Exception ex) {
125          if (e.getMessage().toLowerCase().indexOf("can't find") > -1) {
126            throw new Exception(
127                "Can't find class called '" + classname
128                + "' or '" + prefix + "." + classname + "'!");
129          }
130          else {
131            throw new Exception(ex);
132          }
133        }
134      }
135      else {
136        throw new Exception(e);
137      }
138    }
139   
140    return result;
141  }
142 
143  /**
144   * Begin the tests, reporting results to System.out
145   */
146  public abstract void doTests();
147 
148  /**
149   * Set debugging mode
150   *
151   * @param debug true if debug output should be printed
152   */
153  public void setDebug(boolean debug) {
154    m_Debug = debug;
155    // disable silent mode, if necessary
156    if (getDebug())
157      setSilent(false);
158  }
159 
160  /**
161   * Get whether debugging is turned on
162   *
163   * @return true if debugging output is on
164   */
165  public boolean getDebug() {
166    return m_Debug;
167  }
168 
169  /**
170   * Set slient mode, i.e., no output at all to stdout
171   *
172   * @param value whether silent mode is active or not
173   */
174  public void setSilent(boolean value) {
175    m_Silent = value;
176  }
177 
178  /**
179   * Get whether silent mode is turned on
180   *
181   * @return true if silent mode is on
182   */
183  public boolean getSilent() {
184    return m_Silent;
185  }
186 
187  /**
188   * prints the given message to stdout, if not silent mode
189   *
190   * @param msg         the text to print to stdout
191   */
192  protected void print(Object msg) {
193    if (!getSilent())
194      System.out.print(msg);
195  }
196 
197  /**
198   * prints the given message (+ LF) to stdout, if not silent mode
199   *
200   * @param msg         the message to println to stdout
201   */
202  protected void println(Object msg) {
203    print(msg + "\n");
204  }
205 
206  /**
207   * prints a LF to stdout, if not silent mode
208   */
209  protected void println() {
210    print("\n");
211  }
212 
213  /**
214   * runs the CheckScheme with the given options
215   *
216   * @param check       the checkscheme to setup and run
217   * @param options     the commandline parameters to use
218   */
219  protected static void runCheck(Check check, String[] options) {
220    try {
221      try {
222        check.setOptions(options);
223        Utils.checkForRemainingOptions(options);
224      }
225      catch (Exception ex) {
226        String result = ex.getMessage() + "\n\n" + check.getClass().getName().replaceAll(".*\\.", "") + " Options:\n\n";
227        Enumeration enm = check.listOptions();
228        while (enm.hasMoreElements()) {
229          Option option = (Option) enm.nextElement();
230          result += option.synopsis() + "\n" + option.description() + "\n";
231        }
232        throw new Exception(result);
233      }
234     
235      check.doTests();
236    }
237    catch (Exception ex) {
238      System.err.println(ex.getMessage());
239    }
240  }
241}
Note: See TracBrowser for help on using the repository browser.