source: src/main/java/weka/associations/SingleAssociatorEnhancer.java @ 22

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

Import di weka.

File size: 5.6 KB
RevLine 
[4]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 *    SingleAssociatorEnhancer.java
19 *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.associations;
24
25import weka.core.Capabilities;
26import weka.core.Option;
27import weka.core.OptionHandler;
28import weka.core.Utils;
29import weka.core.Capabilities.Capability;
30
31import java.util.Enumeration;
32import java.util.Vector;
33
34/**
35 * Abstract utility class for handling settings common to meta
36 * associators that use a single base associator. 
37 *
38 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
39 * @author FracPete (fracpete at waikato dot ac dot nz)
40 * @version $Revision: 1.2 $
41 */
42public abstract class SingleAssociatorEnhancer
43  extends AbstractAssociator
44  implements OptionHandler {
45
46  /** for serialization */
47  private static final long serialVersionUID = -3665885256363525164L;
48
49  /** The base associator to use */
50  protected Associator m_Associator = new Apriori();
51
52  /**
53   * String describing default Associator.
54   *
55   * @return            default classname
56   */
57  protected String defaultAssociatorString() {
58    return Apriori.class.getName();
59  }
60
61  /**
62   * Returns an enumeration describing the available options.
63   *
64   * @return            an enumeration of all the available options.
65   */
66  public Enumeration listOptions() {
67    Vector result = new Vector();
68
69    result.addElement(new Option(
70              "\tFull name of base associator.\n"
71              + "\t(default: " + defaultAssociatorString() +")",
72              "W", 1, "-W"));
73
74    if (m_Associator instanceof OptionHandler) {
75      result.addElement(new Option(
76          "",
77          "", 0, "\nOptions specific to associator "
78          + m_Associator.getClass().getName() + ":"));
79
80      Enumeration enm = ((OptionHandler) m_Associator).listOptions();
81      while (enm.hasMoreElements())
82        result.addElement(enm.nextElement());
83    }
84
85    return result.elements();
86  }
87
88  /**
89   * Parses a given list of options. Valid options are:<p>
90   *
91   * -W classname <br>
92   * Specify the full class name of the base associator.<p>
93   *
94   * Options after -- are passed to the designated associator.<p>
95   *
96   * @param options     the list of options as an array of strings
97   * @throws Exception  if an option is not supported
98   */
99  public void setOptions(String[] options) throws Exception {
100    String      tmpStr;
101   
102    tmpStr = Utils.getOption('W', options);
103    if (tmpStr.length() > 0) { 
104      // This is just to set the associator in case the option
105      // parsing fails.
106      setAssociator(AbstractAssociator.forName(tmpStr, null));
107      setAssociator(AbstractAssociator.forName(tmpStr, Utils.partitionOptions(options)));
108    }
109    else {
110      // This is just to set the associator in case the option
111      // parsing fails.
112      setAssociator(AbstractAssociator.forName(defaultAssociatorString(), null));
113      setAssociator(AbstractAssociator.forName(defaultAssociatorString(), Utils.partitionOptions(options)));
114    }
115  }
116
117  /**
118   * Gets the current settings of the associator.
119   *
120   * @return            an array of strings suitable for passing to setOptions
121   */
122  public String[] getOptions() {
123    int                 i;
124    Vector<String>      result;
125    String[]            options;
126   
127    result = new Vector<String>();
128
129    result.add("-W");
130    result.add(getAssociator().getClass().getName());
131
132    if (getAssociator() instanceof OptionHandler) {
133      options = ((OptionHandler) getAssociator()).getOptions();
134      result.add("--");
135      for (i = 0; i < options.length; i++)
136        result.add(options[i]);
137    }
138
139    return result.toArray(new String[result.size()]);
140  }
141 
142  /**
143   * Returns the tip text for this property
144   *
145   * @return            tip text for this property suitable for
146   *                    displaying in the explorer/experimenter gui
147   */
148  public String associatorTipText() {
149    return "The base associator to be used.";
150  }
151
152  /**
153   * Set the base associator.
154   *
155   * @param value       the associator to use.
156   */
157  public void setAssociator(Associator value) {
158    m_Associator = value;
159  }
160
161  /**
162   * Get the associator used as the base associator.
163   *
164   * @return            the currently used associator
165   */
166  public Associator getAssociator() {
167    return m_Associator;
168  }
169 
170  /**
171   * Gets the associator specification string, which contains the class name of
172   * the associator and any options to the associator
173   *
174   * @return the associator string
175   */
176  protected String getAssociatorSpec() {
177    Associator c = getAssociator();
178    return c.getClass().getName() + " "
179      + Utils.joinOptions(((OptionHandler)c).getOptions());
180  }
181
182  /**
183   * Returns default capabilities of the base associator.
184   *
185   * @return      the capabilities of the base associator
186   */
187  public Capabilities getCapabilities() {
188    Capabilities        result;
189
190    if (getAssociator() != null)
191      result = getAssociator().getCapabilities();
192    else
193      result = new Capabilities(this);
194   
195    // set dependencies
196    for (Capability cap: Capability.values())
197      result.enableDependency(cap);
198   
199    result.setOwner(this);
200   
201    return result;
202  }
203}
Note: See TracBrowser for help on using the repository browser.