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.
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.
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.
19
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
23
package weka.associations;
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;
31
import java.io.Serializable;
34
* Abstract scheme for learning associations. All schemes for learning
35
* associations implemement this class
37
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
38
* @version $Revision: 1.9 $
40
public abstract class Associator
41
implements Cloneable, Serializable, CapabilitiesHandler {
43
/** for serialization */
44
private static final long serialVersionUID = -3017644543382432070L;
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
52
* @param data set of instances serving as training data
53
* @exception Exception if the associator has not been
54
* generated successfully
56
public abstract void buildAssociations(Instances data) throws Exception;
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.
64
* @param associatorName the fully qualified class name of the associator
65
* @param options an array of options suitable for passing to setOptions. May
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
71
public static Associator forName(String associatorName,
72
String [] options) throws Exception {
74
return (Associator)Utils.forName(Associator.class,
80
* Creates a deep copy of the given associator using serialization.
82
* @param model the associator to copy
83
* @return a deep copy of the associator
84
* @exception Exception if an error occurs
86
public static Associator makeCopy(Associator model) throws Exception {
87
return (Associator) new SerializedObject(model).getObject();
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.
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
101
public static Associator[] makeCopies(Associator model,
102
int num) throws Exception {
105
throw new Exception("No model associator set");
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();
116
* Returns the Capabilities of this associator. Derived associators have to
117
* override this method to enable capabilities.
119
* @return the capabilities of this object
122
public Capabilities getCapabilities() {
123
return new Capabilities(this);
127
* runs the associator with the given commandline options
129
* @param associator the associator to run
130
* @param options the commandline options
132
protected static void runAssociator(Associator associator, String[] options) {
135
AssociatorEvaluation.evaluate(associator, options));
137
catch (Exception e) {
138
if ( (e.getMessage() != null)
139
&& (e.getMessage().indexOf("General options") == -1) )
142
System.err.println(e.getMessage());