| 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 | * CheckGOE.java |
|---|
| 19 | * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.core; |
|---|
| 24 | |
|---|
| 25 | import java.beans.BeanInfo; |
|---|
| 26 | import java.beans.Introspector; |
|---|
| 27 | import java.beans.PropertyDescriptor; |
|---|
| 28 | import java.util.Collections; |
|---|
| 29 | import java.util.Enumeration; |
|---|
| 30 | import java.util.HashSet; |
|---|
| 31 | import java.util.Iterator; |
|---|
| 32 | import java.util.Vector; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Simple command line checking of classes that are editable in the GOE.<p/> |
|---|
| 36 | * |
|---|
| 37 | * Usage: <p/> |
|---|
| 38 | * <code> |
|---|
| 39 | * CheckGOE -W classname -- test options |
|---|
| 40 | * </code> <p/> |
|---|
| 41 | * |
|---|
| 42 | <!-- options-start --> |
|---|
| 43 | * Valid options are: <p/> |
|---|
| 44 | * |
|---|
| 45 | * <pre> -D |
|---|
| 46 | * Turn on debugging output.</pre> |
|---|
| 47 | * |
|---|
| 48 | * <pre> -S |
|---|
| 49 | * Silent mode - prints nothing to stdout.</pre> |
|---|
| 50 | * |
|---|
| 51 | * <pre> -ignored <comma-separated list of properties> |
|---|
| 52 | * Skipped properties. |
|---|
| 53 | * (default: capabilities,options)</pre> |
|---|
| 54 | * |
|---|
| 55 | * <pre> -W |
|---|
| 56 | * Full name of the class analysed. |
|---|
| 57 | * eg: weka.classifiers.rules.ZeroR |
|---|
| 58 | * (default weka.classifiers.rules.ZeroR)</pre> |
|---|
| 59 | * |
|---|
| 60 | <!-- options-end --> |
|---|
| 61 | * |
|---|
| 62 | * @author FracPete (fracpete at waikato dot ac dot nz) |
|---|
| 63 | * @version $Revision: 5953 $ |
|---|
| 64 | */ |
|---|
| 65 | public class CheckGOE |
|---|
| 66 | extends Check { |
|---|
| 67 | |
|---|
| 68 | /** the object to test */ |
|---|
| 69 | protected Object m_Object = new weka.classifiers.rules.ZeroR(); |
|---|
| 70 | |
|---|
| 71 | /** whether the tests were successful */ |
|---|
| 72 | protected boolean m_Success; |
|---|
| 73 | |
|---|
| 74 | /** properties that are skipped in the checkToolTips method |
|---|
| 75 | * @see #checkToolTips() */ |
|---|
| 76 | protected HashSet<String> m_IgnoredProperties = new HashSet<String>(); |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * default constructor |
|---|
| 80 | */ |
|---|
| 81 | public CheckGOE() { |
|---|
| 82 | super(); |
|---|
| 83 | |
|---|
| 84 | // set default options |
|---|
| 85 | try { |
|---|
| 86 | setOptions(new String[0]); |
|---|
| 87 | } |
|---|
| 88 | catch (Exception e) { |
|---|
| 89 | e.printStackTrace(); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Returns an enumeration describing the available options. |
|---|
| 95 | * |
|---|
| 96 | * @return an enumeration of all the available options. |
|---|
| 97 | */ |
|---|
| 98 | public Enumeration listOptions() { |
|---|
| 99 | Vector<Option> result = new Vector<Option>(); |
|---|
| 100 | |
|---|
| 101 | Enumeration en = super.listOptions(); |
|---|
| 102 | while (en.hasMoreElements()) |
|---|
| 103 | result.addElement((Option)en.nextElement()); |
|---|
| 104 | |
|---|
| 105 | result.addElement(new Option( |
|---|
| 106 | "\tSkipped properties.\n" |
|---|
| 107 | + "\t(default: capabilities,options)", |
|---|
| 108 | "ignored", 1, "-ignored <comma-separated list of properties>")); |
|---|
| 109 | |
|---|
| 110 | result.addElement(new Option( |
|---|
| 111 | "\tFull name of the class analysed.\n" |
|---|
| 112 | +"\teg: weka.classifiers.rules.ZeroR\n" |
|---|
| 113 | + "\t(default weka.classifiers.rules.ZeroR)", |
|---|
| 114 | "W", 1, "-W")); |
|---|
| 115 | |
|---|
| 116 | return result.elements(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Parses a given list of options. <p/> |
|---|
| 121 | * |
|---|
| 122 | <!-- options-start --> |
|---|
| 123 | * Valid options are: <p/> |
|---|
| 124 | * |
|---|
| 125 | * <pre> -D |
|---|
| 126 | * Turn on debugging output.</pre> |
|---|
| 127 | * |
|---|
| 128 | * <pre> -S |
|---|
| 129 | * Silent mode - prints nothing to stdout.</pre> |
|---|
| 130 | * |
|---|
| 131 | * <pre> -ignored <comma-separated list of properties> |
|---|
| 132 | * Skipped properties. |
|---|
| 133 | * (default: capabilities,options)</pre> |
|---|
| 134 | * |
|---|
| 135 | * <pre> -W |
|---|
| 136 | * Full name of the class analysed. |
|---|
| 137 | * eg: weka.classifiers.rules.ZeroR |
|---|
| 138 | * (default weka.classifiers.rules.ZeroR)</pre> |
|---|
| 139 | * |
|---|
| 140 | <!-- options-end --> |
|---|
| 141 | * |
|---|
| 142 | * @param options the list of options as an array of strings |
|---|
| 143 | * @throws Exception if an option is not supported |
|---|
| 144 | */ |
|---|
| 145 | public void setOptions(String[] options) throws Exception { |
|---|
| 146 | String tmpStr; |
|---|
| 147 | |
|---|
| 148 | super.setOptions(options); |
|---|
| 149 | |
|---|
| 150 | tmpStr = Utils.getOption('W', options); |
|---|
| 151 | if (tmpStr.length() == 0) |
|---|
| 152 | tmpStr = weka.classifiers.rules.ZeroR.class.getName(); |
|---|
| 153 | setObject(Utils.forName(Object.class, tmpStr, null)); |
|---|
| 154 | |
|---|
| 155 | tmpStr = Utils.getOption("ignored", options); |
|---|
| 156 | if (tmpStr.length() == 0) |
|---|
| 157 | tmpStr = "capabilities,options"; |
|---|
| 158 | setIgnoredProperties(tmpStr); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Gets the current settings of the object. |
|---|
| 163 | * |
|---|
| 164 | * @return an array of strings suitable for passing to setOptions |
|---|
| 165 | */ |
|---|
| 166 | public String[] getOptions() { |
|---|
| 167 | Vector<String> result; |
|---|
| 168 | String[] options; |
|---|
| 169 | int i; |
|---|
| 170 | |
|---|
| 171 | result = new Vector<String>(); |
|---|
| 172 | |
|---|
| 173 | options = super.getOptions(); |
|---|
| 174 | for (i = 0; i < options.length; i++) |
|---|
| 175 | result.add(options[i]); |
|---|
| 176 | |
|---|
| 177 | result.add("-ignored"); |
|---|
| 178 | result.add(getIgnoredProperties()); |
|---|
| 179 | |
|---|
| 180 | if (getObject() != null) { |
|---|
| 181 | result.add("-W"); |
|---|
| 182 | result.add(getObject().getClass().getName()); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | return (String[]) result.toArray(new String[result.size()]); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * Set the object to work on.. |
|---|
| 190 | * |
|---|
| 191 | * @param value the object to use. |
|---|
| 192 | */ |
|---|
| 193 | public void setObject(Object value) { |
|---|
| 194 | m_Object = value; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Get the object used in the tests. |
|---|
| 199 | * |
|---|
| 200 | * @return the object used in the tests. |
|---|
| 201 | */ |
|---|
| 202 | public Object getObject() { |
|---|
| 203 | return m_Object; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * Sets the properties to ignore in checkToolTips(). Comma-separated list. |
|---|
| 208 | * |
|---|
| 209 | * @param value the list of properties |
|---|
| 210 | * @see #checkToolTips() |
|---|
| 211 | */ |
|---|
| 212 | public void setIgnoredProperties(String value) { |
|---|
| 213 | String[] props; |
|---|
| 214 | int i; |
|---|
| 215 | |
|---|
| 216 | m_IgnoredProperties.clear(); |
|---|
| 217 | props = value.split(","); |
|---|
| 218 | for (i = 0; i < props.length; i++) |
|---|
| 219 | m_IgnoredProperties.add(props[i]); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /** |
|---|
| 223 | * Get the ignored properties used in checkToolTips() as comma-separated |
|---|
| 224 | * list (sorted). |
|---|
| 225 | * |
|---|
| 226 | * @return the ignored properties |
|---|
| 227 | * @see #checkToolTips() |
|---|
| 228 | */ |
|---|
| 229 | public String getIgnoredProperties() { |
|---|
| 230 | String result; |
|---|
| 231 | Vector<String> list; |
|---|
| 232 | Iterator iter; |
|---|
| 233 | int i; |
|---|
| 234 | |
|---|
| 235 | list = new Vector<String>(); |
|---|
| 236 | iter = m_IgnoredProperties.iterator(); |
|---|
| 237 | while (iter.hasNext()) |
|---|
| 238 | list.add((String) iter.next()); |
|---|
| 239 | |
|---|
| 240 | // sort |
|---|
| 241 | if (list.size() > 1) |
|---|
| 242 | Collections.sort(list); |
|---|
| 243 | |
|---|
| 244 | result = ""; |
|---|
| 245 | for (i = 0; i < list.size(); i++) { |
|---|
| 246 | if (i > 0) |
|---|
| 247 | result += ","; |
|---|
| 248 | result += list.get(i); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | return result; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * returns the success of the tests |
|---|
| 256 | * |
|---|
| 257 | * @return true if the tests were successful |
|---|
| 258 | */ |
|---|
| 259 | public boolean getSuccess() { |
|---|
| 260 | return m_Success; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | /** |
|---|
| 264 | * checks whether the object declares a globalInfo method. |
|---|
| 265 | * |
|---|
| 266 | * @return true if the test was passed |
|---|
| 267 | */ |
|---|
| 268 | public boolean checkGlobalInfo() { |
|---|
| 269 | boolean result; |
|---|
| 270 | Class<?> cls; |
|---|
| 271 | |
|---|
| 272 | print("Global info..."); |
|---|
| 273 | |
|---|
| 274 | result = true; |
|---|
| 275 | cls = getObject().getClass(); |
|---|
| 276 | |
|---|
| 277 | // test for globalInfo method |
|---|
| 278 | try { |
|---|
| 279 | cls.getMethod("globalInfo", (Class[]) null); |
|---|
| 280 | } |
|---|
| 281 | catch (Exception e) { |
|---|
| 282 | result = false; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | if (result) |
|---|
| 286 | println("yes"); |
|---|
| 287 | else |
|---|
| 288 | println("no"); |
|---|
| 289 | |
|---|
| 290 | return result; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | /** |
|---|
| 294 | * checks whether the object declares tip text method for all its |
|---|
| 295 | * properties. |
|---|
| 296 | * |
|---|
| 297 | * @return true if the test was passed |
|---|
| 298 | */ |
|---|
| 299 | public boolean checkToolTips() { |
|---|
| 300 | boolean result; |
|---|
| 301 | Class<?> cls; |
|---|
| 302 | BeanInfo info; |
|---|
| 303 | PropertyDescriptor[] desc; |
|---|
| 304 | int i; |
|---|
| 305 | Vector<String> missing; |
|---|
| 306 | String suffix; |
|---|
| 307 | |
|---|
| 308 | print("Tool tips..."); |
|---|
| 309 | |
|---|
| 310 | result = true; |
|---|
| 311 | suffix = "TipText"; |
|---|
| 312 | cls = getObject().getClass(); |
|---|
| 313 | |
|---|
| 314 | // get properties |
|---|
| 315 | try { |
|---|
| 316 | info = Introspector.getBeanInfo(cls, Object.class); |
|---|
| 317 | desc = info.getPropertyDescriptors(); |
|---|
| 318 | } |
|---|
| 319 | catch (Exception e) { |
|---|
| 320 | e.printStackTrace(); |
|---|
| 321 | desc = null; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | // test for TipText methods |
|---|
| 325 | if (desc != null) { |
|---|
| 326 | missing = new Vector<String>(); |
|---|
| 327 | |
|---|
| 328 | for (i = 0; i < desc.length; i++) { |
|---|
| 329 | // skip property? |
|---|
| 330 | if (m_IgnoredProperties.contains(desc[i].getName())) |
|---|
| 331 | continue; |
|---|
| 332 | if ((desc[i].getReadMethod() == null) || (desc[i].getWriteMethod() == null)) |
|---|
| 333 | continue; |
|---|
| 334 | |
|---|
| 335 | try { |
|---|
| 336 | cls.getMethod(desc[i].getName() + suffix, (Class[]) null); |
|---|
| 337 | } |
|---|
| 338 | catch (Exception e) { |
|---|
| 339 | result = false; |
|---|
| 340 | missing.add(desc[i].getName() + suffix); |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | if (result) |
|---|
| 345 | println("yes"); |
|---|
| 346 | else |
|---|
| 347 | println("no (missing: " + missing + ")"); |
|---|
| 348 | |
|---|
| 349 | } |
|---|
| 350 | else { |
|---|
| 351 | println("maybe"); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | return result; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | /** |
|---|
| 358 | * Runs some diagnostic tests on the object. Output is |
|---|
| 359 | * printed to System.out (if not silent). |
|---|
| 360 | */ |
|---|
| 361 | public void doTests() { |
|---|
| 362 | println("Object: " + m_Object.getClass().getName() + "\n"); |
|---|
| 363 | |
|---|
| 364 | println("--> Tests"); |
|---|
| 365 | |
|---|
| 366 | m_Success = checkGlobalInfo(); |
|---|
| 367 | |
|---|
| 368 | if (m_Success) |
|---|
| 369 | m_Success = checkToolTips(); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | /** |
|---|
| 373 | * Returns the revision string. |
|---|
| 374 | * |
|---|
| 375 | * @return the revision |
|---|
| 376 | */ |
|---|
| 377 | public String getRevision() { |
|---|
| 378 | return RevisionUtils.extract("$Revision: 5953 $"); |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | /** |
|---|
| 382 | * Main method for using the CheckGOE. |
|---|
| 383 | * |
|---|
| 384 | * @param args the options to the CheckGOE |
|---|
| 385 | */ |
|---|
| 386 | public static void main(String[] args) { |
|---|
| 387 | CheckGOE check = new CheckGOE(); |
|---|
| 388 | runCheck(check, args); |
|---|
| 389 | if (check.getSuccess()) |
|---|
| 390 | System.exit(0); |
|---|
| 391 | else |
|---|
| 392 | System.exit(1); |
|---|
| 393 | } |
|---|
| 394 | } |
|---|