~ubuntu-branches/ubuntu/oneiric/weka/oneiric

« back to all changes in this revision

Viewing changes to weka/associations/AbstractAssociator.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner, Soeren Sonnenburg, Torsten Werner
  • Date: 2008-08-10 21:27:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080810212705-tr8etpnkdx2ziktp
Tags: 3.5.8-1
[ Soeren Sonnenburg ]
* Bump Standards Version to 3.8.0.
* Remove references to non-free Java in debian/copyright.

[ Torsten Werner ]
* new upstream release
* Switch to openjdk-6.
* Move package to main.

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