source: src/main/java/weka/core/FindWithCapabilities.java @ 26

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

Import di weka.

File size: 28.4 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 * FindWithCapabilities.java
19 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.core;
23
24import weka.core.Capabilities.Capability;
25import weka.gui.GenericPropertiesCreator;
26
27import java.io.BufferedReader;
28import java.io.FileReader;
29import java.util.Enumeration;
30import java.util.Iterator;
31import java.util.Properties;
32import java.util.StringTokenizer;
33import java.util.Vector;
34
35/**
36 * Locates all classes with certain capabilities. One should keep in mind,
37 * that works only with the default capabilities of a scheme and doesn't
38 * take dependencies into account. E.g., a meta-classifier that could have
39 * a base classifier handling numeric classes, but by default uses one with
40 * a nominal class, will never show up in a search for schemes that handle
41 * numeric classes.<p/>
42 *
43 <!-- options-start -->
44 * Valid options are: <p/>
45 *
46 * <pre> All class and attribute options can be prefixed with 'not',
47 * e.g., '-not-numeric-class'. This makes sure that the returned
48 * schemes 'cannot' handle numeric classes.
49 * </pre>
50 *
51 * <pre> -num-instances &lt;num&gt;
52 *  The minimum number of instances (default 1).</pre>
53 *
54 * <pre> -unary-class
55 *  Must handle unray classes.</pre>
56 *
57 * <pre> -binary-class
58 *  Must handle binary classes.</pre>
59 *
60 * <pre> -nominal-class
61 *  Must handle nominal classes.</pre>
62 *
63 * <pre> -numeric-class
64 *  Must handle numeric classes.</pre>
65 *
66 * <pre> -string-class
67 *  Must handle string classes.</pre>
68 *
69 * <pre> -date-class
70 *  Must handle date classes.</pre>
71 *
72 * <pre> -relational-class
73 *  Must handle relational classes.</pre>
74 *
75 * <pre> -missing-class-values
76 *  Must handle missing class values.</pre>
77 *
78 * <pre> -no-class
79 *  Doesn't need a class.</pre>
80 *
81 * <pre> -unary-atts
82 *  Must handle unary attributes.</pre>
83 *
84 * <pre> -binary-atts
85 *  Must handle binary attributes.</pre>
86 *
87 * <pre> -nominal-atts
88 *  Must handle nominal attributes.</pre>
89 *
90 * <pre> -numeric-atts
91 *  Must handle numeric attributes.</pre>
92 *
93 * <pre> -string-atts
94 *  Must handle string attributes.</pre>
95 *
96 * <pre> -date-atts
97 *  Must handle date attributes.</pre>
98 *
99 * <pre> -relational-atts
100 *  Must handle relational attributes.</pre>
101 *
102 * <pre> -missing-att-values
103 *  Must handle missing attribute values.</pre>
104 *
105 * <pre> -only-multiinstance
106 *  Must handle multi-instance data.</pre>
107 *
108 * <pre> -W &lt;classname&gt;
109 *  The Capabilities handler to base the handling on.
110 *  The other parameters can be used to override the ones
111 *  determined from the handler. Additional parameters for
112 *  handler can be passed on after the '--'.
113 *  Either '-W' or '-t' can be used.</pre>
114 *
115 * <pre> -t &lt;file&gt;
116 *  The dataset to base the capabilities on.
117 *  The other parameters can be used to override the ones
118 *  determined from the handler.
119 *  Either '-t' or '-W' can be used.</pre>
120 *
121 * <pre> -c &lt;num&gt;
122 *  The index of the class attribute, -1 for none.
123 *  'first' and 'last' are also valid.
124 *  Only in conjunction with option '-t'.</pre>
125 *
126 * <pre> -superclass
127 *  Superclass to look for in the packages.
128 * </pre>
129 *
130 * <pre> -packages
131 *  Comma-separated list of packages to search in.</pre>
132 *
133 * <pre> -generic
134 *  Retrieves the package list from the GenericPropertiesCreator
135 *  for the given superclass. (overrides -packages &lt;list&gt;).</pre>
136 *
137 * <pre> -misses
138 *  Also prints the classname that didn't match the criteria.</pre>
139 *
140 <!-- options-end -->
141 *
142 * @author  fracpete (fracpete at waikato dot ac dot nz)
143 * @version $Revision: 5953 $
144 * @see Capabilities
145 * @see Capabilities.Capability
146 * @see GenericPropertiesCreator
147 */
148public class FindWithCapabilities 
149implements OptionHandler, CapabilitiesHandler, RevisionHandler {
150
151  /** the capabilities to look for. */
152  protected Capabilities m_Capabilities = new Capabilities(this);
153
154  /** the capabilities to look for to "not have". */
155  protected Capabilities m_NotCapabilities = new Capabilities(this);
156
157  /** the packages to search in. */
158  protected Vector<String> m_Packages = new Vector<String>();
159
160  /** a capabilities handler to retrieve the capabilities from. */
161  protected CapabilitiesHandler m_Handler = null;
162
163  /** a file the capabilities can be based on. */
164  protected String m_Filename = "";
165
166  /** the class index, in case the capabilities are based on a file. */
167  protected SingleIndex m_ClassIndex = new SingleIndex();
168
169  /** the superclass from the GenericPropertiesCreator to retrieve the packages from. */
170  protected String m_Superclass = "";
171
172  /** whether to use the GenericPropertiesCreator with the superclass. */
173  protected boolean m_GenericPropertiesCreator = false;
174
175  /** the classes that matched. */
176  protected Vector<String> m_Matches = new Vector<String>();
177
178  /** the class that didn't match. */
179  protected Vector<String> m_Misses = new Vector<String>();
180
181  /**
182   * Returns an enumeration describing the available options.
183   *
184   * @return an enumeration of all the available options.
185   */
186  public Enumeration listOptions() {
187    Vector<Option> result = new Vector<Option>();
188
189    result.addElement(new Option(
190        "", "", 0, 
191        "All class and attribute options can be prefixed with 'not',\n"
192        + "e.g., '-not-numeric-class'. This makes sure that the returned\n"
193        + "schemes 'cannot' handle numeric classes."));
194
195    result.addElement(new Option(
196        "\tThe minimum number of instances (default 1).",
197        "num-instances", 1, "-num-instances <num>"));
198
199    result.addElement(new Option(
200        "\tMust handle unray classes.",
201        "unary-class", 0, "-unary-class"));
202
203    result.addElement(new Option(
204        "\tMust handle binary classes.",
205        "binary-class", 0, "-binary-class"));
206
207    result.addElement(new Option(
208        "\tMust handle nominal classes.",
209        "nominal-class", 0, "-nominal-class"));
210
211    result.addElement(new Option(
212        "\tMust handle numeric classes.",
213        "numeric-class", 0, "-numeric-class"));
214
215    result.addElement(new Option(
216        "\tMust handle string classes.",
217        "string-class", 0, "-string-class"));
218
219    result.addElement(new Option(
220        "\tMust handle date classes.",
221        "date-class", 0, "-date-class"));
222
223    result.addElement(new Option(
224        "\tMust handle relational classes.",
225        "relational-class", 0, "-relational-class"));
226
227    result.addElement(new Option(
228        "\tMust handle missing class values.",
229        "missing-class-values", 0, "-missing-class-values"));
230
231    result.addElement(new Option(
232        "\tDoesn't need a class.",
233        "no-class", 0, "-no-class"));
234
235    result.addElement(new Option(
236        "\tMust handle unary attributes.",
237        "unary-atts", 0, "-unary-atts"));
238
239    result.addElement(new Option(
240        "\tMust handle binary attributes.",
241        "binary-atts", 0, "-binary-atts"));
242
243    result.addElement(new Option(
244        "\tMust handle nominal attributes.",
245        "nominal-atts", 0, "-nominal-atts"));
246
247    result.addElement(new Option(
248        "\tMust handle numeric attributes.",
249        "numeric-atts", 0, "-numeric-atts"));
250
251    result.addElement(new Option(
252        "\tMust handle string attributes.",
253        "string-atts", 0, "-string-atts"));
254
255    result.addElement(new Option(
256        "\tMust handle date attributes.",
257        "date-atts", 0, "-date-atts"));
258
259    result.addElement(new Option(
260        "\tMust handle relational attributes.",
261        "relational-atts", 0, "-relational-atts"));
262
263    result.addElement(new Option(
264        "\tMust handle missing attribute values.",
265        "missing-att-values", 0, "-missing-att-values"));
266
267    result.addElement(new Option(
268        "\tMust handle multi-instance data.",
269        "only-multiinstance", 0, "-only-multiinstance"));
270
271    result.addElement(new Option(
272        "\tThe Capabilities handler to base the handling on.\n"
273        + "\tThe other parameters can be used to override the ones\n"
274        + "\tdetermined from the handler. Additional parameters for\n"
275        + "\thandler can be passed on after the '--'.\n"
276        + "\tEither '-W' or '-t' can be used.",
277        "W", 1, "-W <classname>"));
278
279    result.addElement(new Option(
280        "\tThe dataset to base the capabilities on.\n"
281        + "\tThe other parameters can be used to override the ones\n"
282        + "\tdetermined from the handler.\n"
283        + "\tEither '-t' or '-W' can be used.",
284        "t", 1, "-t <file>"));
285
286    result.addElement(new Option(
287        "\tThe index of the class attribute, -1 for none.\n"
288        + "\t'first' and 'last' are also valid.\n"
289        + "\tOnly in conjunction with option '-t'.",
290        "c", 1, "-c <num>"));
291
292    result.addElement(new Option(
293        "\tSuperclass to look for in the packages.\n",
294        "superclass", 1, "-superclass"));
295
296    result.addElement(new Option(
297        "\tComma-separated list of packages to search in.",
298        "packages", 1, "-packages"));
299
300    result.addElement(new Option(
301        "\tRetrieves the package list from the GenericPropertiesCreator\n"
302        + "\tfor the given superclass. (overrides -packages <list>).",
303        "generic", 1, "-generic"));
304
305    result.addElement(new Option(
306        "\tAlso prints the classname that didn't match the criteria.",
307        "misses", 0, "-misses"));
308
309    return result.elements();
310  }
311
312  /**
313   * Parses a given list of options.
314   *
315   * @param options the list of options as an array of strings
316   * @throws Exception if an option is not supported
317   */
318  public void setOptions(String[] options) throws Exception {
319    String                      tmpStr;
320    Class                       cls;
321    CapabilitiesHandler         handler;
322    boolean                     initialized;
323    StringTokenizer             tok;
324    GenericPropertiesCreator    creator;
325    Properties                  props;
326
327    m_Capabilities = new Capabilities(this);
328    initialized    = false;
329
330    tmpStr = Utils.getOption('W', options);
331    if (tmpStr.length() != 0) {
332      cls = Class.forName(tmpStr);
333      if (ClassDiscovery.hasInterface(CapabilitiesHandler.class, cls)) {
334        initialized = true;
335        handler = (CapabilitiesHandler) cls.newInstance();
336        if (handler instanceof OptionHandler)
337          ((OptionHandler) handler).setOptions(Utils.partitionOptions(options));
338        setHandler(handler);
339      }
340      else {
341        throw new IllegalArgumentException("Class '" + tmpStr + "' is not a CapabilitiesHandler!");
342      }
343    }
344    else {
345      tmpStr = Utils.getOption('c', options);
346      if (tmpStr.length() != 0)
347        setClassIndex(tmpStr);
348      else
349        setClassIndex("last");
350
351      tmpStr = Utils.getOption('t', options);
352      setFilename(tmpStr);
353    }
354
355    tmpStr = Utils.getOption("num-instances", options);
356    if (tmpStr.length() != 0)
357      m_Capabilities.setMinimumNumberInstances(Integer.parseInt(tmpStr));
358    else if (!initialized)
359      m_Capabilities.setMinimumNumberInstances(1);
360
361    // allowed
362    if (Utils.getFlag("no-class", options))
363      enable(Capability.NO_CLASS);
364    // not allowed
365    if (Utils.getFlag("not-no-class", options))
366      enableNot(Capability.NO_CLASS);
367
368    if (!m_Capabilities.handles(Capability.NO_CLASS)) {
369      // allowed
370      if (Utils.getFlag("nominal-class", options)) {
371        enable(Capability.NOMINAL_CLASS);
372        disable(Capability.BINARY_CLASS);
373      }
374      if (Utils.getFlag("binary-class", options)) {
375        enable(Capability.BINARY_CLASS);
376        disable(Capability.UNARY_CLASS);
377      }
378      if (Utils.getFlag("unary-class", options))
379        enable(Capability.UNARY_CLASS);
380      if (Utils.getFlag("numeric-class", options))
381        enable(Capability.NUMERIC_CLASS);
382      if (Utils.getFlag("string-class", options))
383        enable(Capability.STRING_CLASS);
384      if (Utils.getFlag("date-class", options))
385        enable(Capability.DATE_CLASS);
386      if (Utils.getFlag("relational-class", options))
387        enable(Capability.RELATIONAL_CLASS);
388      if (Utils.getFlag("missing-class-values", options))
389        enable(Capability.MISSING_CLASS_VALUES);
390    }
391    // not allowed
392    if (Utils.getFlag("not-nominal-class", options)) {
393      enableNot(Capability.NOMINAL_CLASS);
394      disableNot(Capability.BINARY_CLASS);
395    }
396    if (Utils.getFlag("not-binary-class", options)) {
397      enableNot(Capability.BINARY_CLASS);
398      disableNot(Capability.UNARY_CLASS);
399    }
400    if (Utils.getFlag("not-unary-class", options))
401      enableNot(Capability.UNARY_CLASS);
402    if (Utils.getFlag("not-numeric-class", options))
403      enableNot(Capability.NUMERIC_CLASS);
404    if (Utils.getFlag("not-string-class", options))
405      enableNot(Capability.STRING_CLASS);
406    if (Utils.getFlag("not-date-class", options))
407      enableNot(Capability.DATE_CLASS);
408    if (Utils.getFlag("not-relational-class", options))
409      enableNot(Capability.RELATIONAL_CLASS);
410    if (Utils.getFlag("not-relational-class", options))
411      enableNot(Capability.RELATIONAL_CLASS);
412    if (Utils.getFlag("not-missing-class-values", options))
413      enableNot(Capability.MISSING_CLASS_VALUES);
414
415    // allowed
416    if (Utils.getFlag("nominal-atts", options)) {
417      enable(Capability.NOMINAL_ATTRIBUTES);
418      disable(Capability.BINARY_ATTRIBUTES);
419    }
420    if (Utils.getFlag("binary-atts", options)) {
421      enable(Capability.BINARY_ATTRIBUTES);
422      disable(Capability.UNARY_ATTRIBUTES);
423    }
424    if (Utils.getFlag("unary-atts", options))
425      enable(Capability.UNARY_ATTRIBUTES);
426    if (Utils.getFlag("numeric-atts", options))
427      enable(Capability.NUMERIC_ATTRIBUTES);
428    if (Utils.getFlag("string-atts", options))
429      enable(Capability.STRING_ATTRIBUTES);
430    if (Utils.getFlag("date-atts", options))
431      enable(Capability.DATE_ATTRIBUTES);
432    if (Utils.getFlag("relational-atts", options))
433      enable(Capability.RELATIONAL_ATTRIBUTES);
434    if (Utils.getFlag("missing-att-values", options))
435      enable(Capability.MISSING_VALUES);
436    // not allowed
437    if (Utils.getFlag("not-nominal-atts", options)) {
438      enableNot(Capability.NOMINAL_ATTRIBUTES);
439      disableNot(Capability.BINARY_ATTRIBUTES);
440    }
441    if (Utils.getFlag("not-binary-atts", options)) {
442      enableNot(Capability.BINARY_ATTRIBUTES);
443      disableNot(Capability.UNARY_ATTRIBUTES);
444    }
445    if (Utils.getFlag("not-unary-atts", options))
446      enableNot(Capability.UNARY_ATTRIBUTES);
447    if (Utils.getFlag("not-numeric-atts", options))
448      enableNot(Capability.NUMERIC_ATTRIBUTES);
449    if (Utils.getFlag("not-string-atts", options))
450      enableNot(Capability.STRING_ATTRIBUTES);
451    if (Utils.getFlag("not-date-atts", options))
452      enableNot(Capability.DATE_ATTRIBUTES);
453    if (Utils.getFlag("not-relational-atts", options))
454      enableNot(Capability.RELATIONAL_ATTRIBUTES);
455    if (Utils.getFlag("not-missing-att-values", options))
456      enableNot(Capability.MISSING_VALUES);
457
458    if (Utils.getFlag("only-multiinstance", options))
459      enable(Capability.ONLY_MULTIINSTANCE);
460
461    tmpStr = Utils.getOption("superclass", options);
462    if (tmpStr.length() != 0)
463      m_Superclass = tmpStr;
464    else
465      throw new IllegalArgumentException("A superclass has to be specified!");
466
467    tmpStr = Utils.getOption("packages", options);
468    if (tmpStr.length() != 0) {
469      tok        = new StringTokenizer(tmpStr, ",");
470      m_Packages = new Vector<String>();
471      while (tok.hasMoreTokens())
472        m_Packages.add(tok.nextToken());
473    }
474
475    if (Utils.getFlag("generic", options)) {
476      creator    = new GenericPropertiesCreator();
477      creator.execute(false);
478      props      = creator.getInputProperties();
479      tok        = new StringTokenizer(props.getProperty(m_Superclass), ",");
480      m_Packages = new Vector<String>();
481      while (tok.hasMoreTokens())
482        m_Packages.add(tok.nextToken());
483    }
484  }
485
486  /**
487   * Gets the current settings of this object.
488   *
489   * @return an array of strings suitable for passing to setOptions
490   */
491  public String[] getOptions() {
492    Vector<String>      result;
493    String[]    options;
494    int         i;
495
496    result = new Vector<String>();
497
498    result.add("-num-instances");
499    result.add("" + m_Capabilities.getMinimumNumberInstances());
500
501    if (isEnabled(Capability.NO_CLASS)) {
502      result.add("-no-class");
503    }
504    else {
505      if (isEnabled(Capability.UNARY_CLASS))
506        result.add("-unary-class");
507      if (isEnabled(Capability.BINARY_CLASS))
508        result.add("-binary-class");
509      if (isEnabled(Capability.NOMINAL_CLASS))
510        result.add("-nominal-class");
511      if (isEnabled(Capability.NUMERIC_CLASS))
512        result.add("-numeric-class");
513      if (isEnabled(Capability.STRING_CLASS))
514        result.add("-string-class");
515      if (isEnabled(Capability.DATE_CLASS))
516        result.add("-date-class");
517      if (isEnabled(Capability.RELATIONAL_CLASS))
518        result.add("-relational-class");
519      if (isEnabled(Capability.MISSING_CLASS_VALUES))
520        result.add("-missing-class-values");
521    }
522
523    if (isEnabled(Capability.UNARY_ATTRIBUTES))
524      result.add("-unary-atts");
525    if (isEnabled(Capability.BINARY_ATTRIBUTES))
526      result.add("-binary-atts");
527    if (isEnabled(Capability.NOMINAL_ATTRIBUTES))
528      result.add("-nominal-atts");
529    if (isEnabled(Capability.NUMERIC_ATTRIBUTES))
530      result.add("-numeric-atts");
531    if (isEnabled(Capability.STRING_ATTRIBUTES))
532      result.add("-string-atts");
533    if (isEnabled(Capability.DATE_ATTRIBUTES))
534      result.add("-date-atts");
535    if (isEnabled(Capability.RELATIONAL_ATTRIBUTES))
536      result.add("-relational-atts");
537    if (isEnabled(Capability.MISSING_VALUES))
538      result.add("-missing-att-values");
539
540    // not allowed
541    if (isEnabledNot(Capability.NO_CLASS))
542      result.add("-not-no-class");
543    if (isEnabledNot(Capability.UNARY_CLASS))
544      result.add("-not-unary-class");
545    if (isEnabledNot(Capability.BINARY_CLASS))
546      result.add("-not-binary-class");
547    if (isEnabledNot(Capability.NOMINAL_CLASS))
548      result.add("-not-nominal-class");
549    if (isEnabledNot(Capability.NUMERIC_CLASS))
550      result.add("-not-numeric-class");
551    if (isEnabledNot(Capability.STRING_CLASS))
552      result.add("-not-string-class");
553    if (isEnabledNot(Capability.DATE_CLASS))
554      result.add("-not-date-class");
555    if (isEnabledNot(Capability.RELATIONAL_CLASS))
556      result.add("-not-relational-class");
557    if (isEnabledNot(Capability.MISSING_CLASS_VALUES))
558      result.add("-not-missing-class-values");
559
560    if (isEnabledNot(Capability.UNARY_ATTRIBUTES))
561      result.add("-not-unary-atts");
562    if (isEnabledNot(Capability.BINARY_ATTRIBUTES))
563      result.add("-not-binary-atts");
564    if (isEnabledNot(Capability.NOMINAL_ATTRIBUTES))
565      result.add("-not-nominal-atts");
566    if (isEnabledNot(Capability.NUMERIC_ATTRIBUTES))
567      result.add("-not-numeric-atts");
568    if (isEnabledNot(Capability.STRING_ATTRIBUTES))
569      result.add("-not-string-atts");
570    if (isEnabledNot(Capability.DATE_ATTRIBUTES))
571      result.add("-not-date-atts");
572    if (isEnabledNot(Capability.RELATIONAL_ATTRIBUTES))
573      result.add("-not-relational-atts");
574    if (isEnabledNot(Capability.MISSING_VALUES))
575      result.add("-not-missing-att-values");
576
577    if (isEnabled(Capability.ONLY_MULTIINSTANCE))
578      result.add("-only-multi-instance");
579
580    if (getHandler() != null) {
581      result.add("-W");
582      result.add(getHandler().getClass().getName());
583      if (getHandler() instanceof OptionHandler) {
584        result.add("--");
585        options = ((OptionHandler) getHandler()).getOptions();
586        for (i = 0; i < options.length; i++)
587          result.add(options[i]);
588      }
589    }
590    else if (getFilename().length() != 0) {
591      result.add("-t");
592      result.add(getFilename());
593      result.add("-c");
594      result.add(m_ClassIndex.getSingleIndex());
595    }
596
597    if (m_Superclass.length() != 0) {
598      result.add("-superclass");
599      result.add(m_Superclass);
600    }
601    else {
602      result.add("-packages");
603      result.add(m_Packages.toString().replaceAll("\\[", "").replaceAll("\\]", ""));
604    }
605
606    return (String[]) result.toArray(new String[result.size()]);
607  }
608
609  /**
610   * sets the Capabilities handler to generate the data for.
611   *
612   * @param value       the handler
613   */
614  public void setHandler(CapabilitiesHandler value) {
615    m_Handler = value;
616    setCapabilities(m_Handler.getCapabilities());
617  }
618
619  /**
620   * returns the current set CapabilitiesHandler to generate the dataset
621   * for, can be null.
622   *
623   * @return            the handler
624   */
625  public CapabilitiesHandler getHandler() {
626    return m_Handler;
627  }
628
629  /**
630   * Sets the dataset filename to base the capabilities on. It immediately
631   * loads the dataset and retrieves the capabilities from it.
632   *
633   * @param value       the filename of the dataset
634   */
635  public void setFilename(String value) {
636    Instances           insts;
637
638    m_Filename = value;
639
640    if (m_Filename.length() != 0) {
641      try {
642        insts  = new Instances(new BufferedReader(new FileReader(m_Filename)));
643        m_ClassIndex.setUpper(insts.numAttributes());
644        insts.setClassIndex(Integer.parseInt(getClassIndex()) - 1);
645
646        setCapabilities(Capabilities.forInstances(insts));
647      }
648      catch (Exception e) {
649        e.printStackTrace();
650      }
651    }
652  }
653
654  /**
655   * returns the current filename for the dataset to base the capabilities on.
656   *
657   * @return            the filename of the dataset
658   */
659  public String getFilename() {
660    return m_Filename;
661  }
662
663  /**
664   * sets the class index, -1 for none, first and last are also valid.
665   *
666   * @param value       the class index
667   */
668  public void setClassIndex(String value) {
669    if (value.equals("-1"))
670      m_ClassIndex = null;
671    else
672      m_ClassIndex = new SingleIndex(value);
673  }
674
675  /**
676   * returns the current current class index, -1 if no class attribute.
677   *
678   * @return            the class index
679   */
680  public String getClassIndex() {
681    if (m_ClassIndex == null)
682      return "-1";
683    else
684      return "" + m_ClassIndex.getIndex();
685  }
686
687  /**
688   * enables the given capability.
689   *
690   * @param c           the capability to enable
691   */
692  public void enable(Capability c) {
693    m_Capabilities.enable(c);
694  }
695
696  /**
697   * whether the given capability is enabled.
698   *
699   * @param c           the capability to enable
700   * @return            true if the capability is enabled
701   */
702  public boolean isEnabled(Capability c) {
703    return m_Capabilities.handles(c);
704  }
705
706  /**
707   * disables the given capability.
708   *
709   * @param c           the capability to disable
710   */
711  public void disable(Capability c) {
712    m_Capabilities.disable(c);
713  }
714
715  /**
716   * enables the given "not to have" capability.
717   *
718   * @param c           the capability to enable
719   */
720  public void enableNot(Capability c) {
721    m_NotCapabilities.enable(c);
722  }
723
724  /**
725   * whether the given "not to have" capability is enabled.
726   *
727   * @param c           the capability to enable
728   * @return            true if the capability is enabled
729   */
730  public boolean isEnabledNot(Capability c) {
731    return m_NotCapabilities.handles(c);
732  }
733
734  /**
735   * disables the given "not to have" capability.
736   *
737   * @param c           the capability to disable
738   */
739  public void disableNot(Capability c) {
740    m_NotCapabilities.disable(c);
741  }
742
743  /**
744   * returns true if the given capability can be handled.
745   *
746   * @param c           the capability to check
747   * @return            true if the capability can be handled
748   */
749  public boolean handles(Capability c) {
750    return m_Capabilities.handles(c);
751  }
752
753  /**
754   * The capabilities to search for.
755   *
756   * @return            the capabilities to search for
757   * @see               Capabilities
758   */
759  public Capabilities getCapabilities() {
760    return m_Capabilities;
761  }
762
763  /**
764   * Uses the given Capabilities for the search.
765   *
766   * @param c           the capabilities to use for the search
767   */
768  public void setCapabilities(Capabilities c) {
769    m_Capabilities = (Capabilities) c.clone();
770  }
771
772  /**
773   * The "not to have" capabilities to search for.
774   *
775   * @return            the capabilities to search for
776   * @see               Capabilities
777   */
778  public Capabilities getNotCapabilities() {
779    return m_NotCapabilities;
780  }
781
782  /**
783   * Uses the given "not to have" Capabilities for the search.
784   *
785   * @param c           the capabilities to use for the search
786   */
787  public void setNotCapabilities(Capabilities c) {
788    m_NotCapabilities = (Capabilities) c.clone();
789  }
790
791  /**
792   * returns the matches from the last find call.
793   *
794   * @return            the matching classname from the last find run
795   */
796  public Vector<String> getMatches() {
797    return m_Matches;
798  }
799
800  /**
801   * returns the misses from the last find call.
802   *
803   * @return            the classnames that didn't match from the last find run
804   */
805  public Vector<String> getMisses() {
806    return m_Misses;
807  }
808
809  /**
810   * returns a list with all the classnames that fit the criteria.
811   *
812   * @return            contains all classnames that fit the criteria
813   */
814  public Vector<String> find() {
815    Vector<String>              list;
816    int                 i;
817    Class               cls;
818    Object              obj;
819    CapabilitiesHandler handler;
820    boolean             fits;
821    Capabilities        caps;
822
823    m_Matches = new Vector<String>();
824    m_Misses  = new Vector<String>();
825
826    list = ClassDiscovery.find(m_Superclass, (String[]) m_Packages.toArray(new String[m_Packages.size()]));
827    for (i = 0; i < list.size(); i++) {
828      try {
829        cls = Class.forName((String) list.get(i));
830        obj = cls.newInstance();
831
832        // exclude itself
833        if (cls == this.getClass())
834          continue;
835
836        // really a CapabilitiesHandler?
837        if (!(obj instanceof CapabilitiesHandler))
838          continue;
839
840        // check capabilities enumeration
841        handler = (CapabilitiesHandler) obj;
842        caps    = handler.getCapabilities();
843        fits    = true;
844        for (Capability cap: Capability.values()) {
845          if (m_Capabilities.handles(cap)) {
846            if (!(caps.handles(cap))) {
847              fits = false;
848              break;
849            }
850          }
851        }
852        if (!fits) {
853          m_Misses.add(list.get(i));
854          continue;
855        }
856
857        // check "not" list
858        for (Capability cap: Capability.values()) {
859          if (m_NotCapabilities.handles(cap)) {
860            if ((caps.handles(cap))) {
861              fits = false;
862              break;
863            }
864          }
865        }
866        if (!fits) {
867          m_Misses.add(list.get(i));
868          continue;
869        }
870
871        // other stuff
872        if (caps.getMinimumNumberInstances() > m_Capabilities.getMinimumNumberInstances()) {
873          m_Misses.add(list.get(i));
874          continue;
875        }
876
877        // matches all criteria!
878        m_Matches.add(list.get(i));
879      }
880      catch (Exception e) {
881        // ignore
882      }
883    }
884
885    return m_Matches;
886  }
887 
888  /**
889   * Returns the revision string.
890   *
891   * @return            the revision
892   */
893  public String getRevision() {
894    return RevisionUtils.extract("$Revision: 5953 $");
895  }
896
897  /**
898   * Executes the location of classes with parameters from the commandline.
899   *
900   * @param args        the commandline parameters
901   */
902  public static void main(String[] args) {
903    FindWithCapabilities        find;
904    Vector<String>                      list;
905    String                      result;
906    int                         i;
907    boolean                     printMisses;
908    Iterator                    iter;
909    boolean                     first;
910
911    printMisses = false;
912
913    try {
914      find = new FindWithCapabilities();
915
916      try {
917        printMisses = Utils.getFlag("misses", args);
918        find.setOptions(args);
919        Utils.checkForRemainingOptions(args);
920      } 
921      catch (Exception ex) {
922        result = ex.getMessage() + "\n\n" + find.getClass().getName().replaceAll(".*\\.", "") + " Options:\n\n";
923        Enumeration enm = find.listOptions();
924        while (enm.hasMoreElements()) {
925          Option option = (Option) enm.nextElement();
926          result += option.synopsis() + "\n" + option.description() + "\n";
927        }
928        throw new Exception(result);
929      }
930
931      System.out.println("\nSearching for the following Capabilities:");
932      // allowed
933      System.out.print("- allowed: ");
934      iter  = find.getCapabilities().capabilities();
935      first = true;
936      while (iter.hasNext()) {
937        if (!first)
938          System.out.print(", ");
939        first = false;
940        System.out.print(iter.next());
941      }
942      System.out.println();
943      // not allowed
944      System.out.print("- not allowed: ");
945      iter  = find.getNotCapabilities().capabilities();
946      first = true;
947      if (iter.hasNext()) {
948        while (iter.hasNext()) {
949          if (!first)
950            System.out.print(", ");
951          first = false;
952          System.out.print(iter.next());
953        }
954        System.out.println();
955      }
956      else {
957        System.out.println("-");
958      }
959
960      find.find();
961
962      // matches
963      list = find.getMatches();
964      if (list.size() == 1)
965        System.out.println("\nFound " + list.size() + " class that matched the criteria:\n");
966      else
967        System.out.println("\nFound " + list.size() + " classes that matched the criteria:\n");
968      for (i = 0; i < list.size(); i++)
969        System.out.println(list.get(i));
970
971      // misses
972      if (printMisses) {
973        list = find.getMisses();
974        if (list.size() == 1)
975          System.out.println("\nFound " + list.size() + " class that didn't match the criteria:\n");
976        else
977          System.out.println("\nFound " + list.size() + " classes that didn't match the criteria:\n");
978        for (i = 0; i < list.size(); i++)
979          System.out.println(list.get(i));
980      }
981     
982      System.out.println();
983    } 
984    catch (Exception ex) {
985      System.err.println(ex.getMessage());
986    }
987  }
988}
Note: See TracBrowser for help on using the repository browser.