~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/associations/Associator.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *    Associator.java
 
19
 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.associations;
 
24
 
 
25
import weka.core.Capabilities;
 
26
import weka.core.CapabilitiesHandler;
 
27
import weka.core.Instances;
 
28
import weka.core.SerializedObject;
 
29
import weka.core.Utils;
 
30
 
 
31
import java.io.Serializable;
 
32
 
 
33
/** 
 
34
 * Abstract scheme for learning associations. All schemes for learning
 
35
 * associations implemement this class
 
36
 *
 
37
 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
 
38
 * @version $Revision: 1.9 $ 
 
39
 */
 
40
public abstract class Associator 
 
41
  implements Cloneable, Serializable, CapabilitiesHandler {
 
42
 
 
43
  /** for serialization */
 
44
  private static final long serialVersionUID = -3017644543382432070L;
 
45
  
 
46
  /**
 
47
   * Generates an associator. Must initialize all fields of the associator
 
48
   * that are not being set via options (ie. multiple calls of buildAssociator
 
49
   * must always lead to the same result). Must not change the dataset
 
50
   * in any way.
 
51
   *
 
52
   * @param data set of instances serving as training data 
 
53
   * @exception Exception if the associator has not been 
 
54
   * generated successfully
 
55
   */
 
56
  public abstract void buildAssociations(Instances data) throws Exception;
 
57
 
 
58
  /**
 
59
   * Creates a new instance of a associator given it's class name and
 
60
   * (optional) arguments to pass to it's setOptions method. If the
 
61
   * associator implements OptionHandler and the options parameter is
 
62
   * non-null, the associator will have it's options set.
 
63
   *
 
64
   * @param associatorName the fully qualified class name of the associator
 
65
   * @param options an array of options suitable for passing to setOptions. May
 
66
   * be null.
 
67
   * @return the newly created associator, ready for use.
 
68
   * @exception Exception if the associator name is invalid, or the options
 
69
   * supplied are not acceptable to the associator
 
70
   */
 
71
  public static Associator forName(String associatorName,
 
72
                                   String [] options) throws Exception {
 
73
 
 
74
    return (Associator)Utils.forName(Associator.class,
 
75
                                     associatorName,
 
76
                                     options);
 
77
  }
 
78
 
 
79
  /**
 
80
   * Creates a deep copy of the given associator using serialization.
 
81
   *
 
82
   * @param model the associator to copy
 
83
   * @return a deep copy of the associator
 
84
   * @exception Exception if an error occurs
 
85
   */
 
86
  public static Associator makeCopy(Associator model) throws Exception {
 
87
    return (Associator) new SerializedObject(model).getObject();
 
88
  }
 
89
 
 
90
  /**
 
91
   * Creates copies of the current associator. Note that this method
 
92
   * now uses Serialization to perform a deep copy, so the Associator
 
93
   * object must be fully Serializable. Any currently built model will
 
94
   * now be copied as well.
 
95
   *
 
96
   * @param model an example associator to copy
 
97
   * @param num the number of associators copies to create.
 
98
   * @return an array of associators.
 
99
   * @exception Exception if an error occurs 
 
100
   */
 
101
  public static Associator[] makeCopies(Associator model,
 
102
                                         int num) throws Exception {
 
103
 
 
104
    if (model == null) {
 
105
      throw new Exception("No model associator set");
 
106
    }
 
107
    Associator [] associators = new Associator [num];
 
108
    SerializedObject so = new SerializedObject(model);
 
109
    for(int i = 0; i < associators.length; i++) {
 
110
      associators[i] = (Associator) so.getObject();
 
111
    }
 
112
    return associators;
 
113
  }
 
114
 
 
115
  /** 
 
116
   * Returns the Capabilities of this associator. Derived associators have to
 
117
   * override this method to enable capabilities.
 
118
   *
 
119
   * @return            the capabilities of this object
 
120
   * @see               Capabilities
 
121
   */
 
122
  public Capabilities getCapabilities() {
 
123
    return new Capabilities(this);
 
124
  }
 
125
  
 
126
  /**
 
127
   * runs the associator with the given commandline options
 
128
   * 
 
129
   * @param associator  the associator to run
 
130
   * @param options     the commandline options
 
131
   */
 
132
  protected static void runAssociator(Associator associator, String[] options) {
 
133
    try {
 
134
      System.out.println(
 
135
          AssociatorEvaluation.evaluate(associator, options));
 
136
    }
 
137
    catch (Exception e) {
 
138
      if (    (e.getMessage() != null)
 
139
           && (e.getMessage().indexOf("General options") == -1) )
 
140
        e.printStackTrace();
 
141
      else
 
142
        System.err.println(e.getMessage());
 
143
    }
 
144
  }
 
145
}