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