| 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 | *    PMMLClassifier.java | 
|---|
| 19 | *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand | 
|---|
| 20 | * | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | package weka.classifiers.pmml.consumer; | 
|---|
| 24 |  | 
|---|
| 25 | import java.io.Serializable; | 
|---|
| 26 | import org.w3c.dom.Document; | 
|---|
| 27 | import org.w3c.dom.Element; | 
|---|
| 28 | import org.w3c.dom.Node; | 
|---|
| 29 | import org.w3c.dom.NodeList; | 
|---|
| 30 |  | 
|---|
| 31 | import weka.classifiers.Classifier; | 
|---|
| 32 | import weka.classifiers.AbstractClassifier; | 
|---|
| 33 | import weka.core.Instances; | 
|---|
| 34 | import weka.core.pmml.*; | 
|---|
| 35 | import weka.gui.Logger; | 
|---|
| 36 |  | 
|---|
| 37 | /** | 
|---|
| 38 | * Abstract base class for all PMML classifiers. | 
|---|
| 39 | * | 
|---|
| 40 | * @author Mark Hall (mhall{[at]}pentaho{[dot]}com) | 
|---|
| 41 | * @version $Revision: 5928 $ | 
|---|
| 42 | */ | 
|---|
| 43 | public abstract class PMMLClassifier extends AbstractClassifier | 
|---|
| 44 | implements Serializable, PMMLModel { | 
|---|
| 45 |  | 
|---|
| 46 | /** For serialization */ | 
|---|
| 47 | private static final long serialVersionUID = -5371600590320702971L; | 
|---|
| 48 |  | 
|---|
| 49 | /** PMML version */ | 
|---|
| 50 | protected String m_pmmlVersion = "?"; | 
|---|
| 51 |  | 
|---|
| 52 | /** Creator application */ | 
|---|
| 53 | protected String m_creatorApplication = "?"; | 
|---|
| 54 |  | 
|---|
| 55 | /** Logger */ | 
|---|
| 56 | protected Logger m_log = null; | 
|---|
| 57 |  | 
|---|
| 58 | /** The data dictionary */ | 
|---|
| 59 | protected Instances m_dataDictionary; | 
|---|
| 60 |  | 
|---|
| 61 | /** The fields and meta data used by the model */ | 
|---|
| 62 | protected MiningSchema m_miningSchema; | 
|---|
| 63 |  | 
|---|
| 64 | /** The mapping between mining schema fields and incoming instance | 
|---|
| 65 | attributes */ | 
|---|
| 66 | protected transient MappingInfo m_fieldsMap; | 
|---|
| 67 |  | 
|---|
| 68 | /** Has the classifier been initialized (i.e. have we established | 
|---|
| 69 | a mapping between the mining schema and the incoming instances)? */ | 
|---|
| 70 | protected transient boolean m_initialized = false; | 
|---|
| 71 |  | 
|---|
| 72 | /** | 
|---|
| 73 | * Constructor. | 
|---|
| 74 | * | 
|---|
| 75 | * @param dataDictionary the data dictionary | 
|---|
| 76 | * @param miningSchema the mining schema | 
|---|
| 77 | */ | 
|---|
| 78 | PMMLClassifier(Instances dataDictionary, | 
|---|
| 79 | MiningSchema miningSchema) { | 
|---|
| 80 | m_dataDictionary = dataDictionary; | 
|---|
| 81 | m_miningSchema = miningSchema; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 | * Set the version of PMML used for this model. | 
|---|
| 86 | * | 
|---|
| 87 | * @param doc the Document encapsulating the pmml | 
|---|
| 88 | */ | 
|---|
| 89 | public void setPMMLVersion(Document doc) { | 
|---|
| 90 | NodeList tempL = doc.getElementsByTagName("PMML"); | 
|---|
| 91 | Node pmml = tempL.item(0); | 
|---|
| 92 | if (pmml.getNodeType() == Node.ELEMENT_NODE) { | 
|---|
| 93 | String version = ((Element)pmml).getAttribute("version"); | 
|---|
| 94 | if (version.length() > 0) { | 
|---|
| 95 | m_pmmlVersion = version; | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * Set the name of the application (if specified) that created this | 
|---|
| 102 | * model | 
|---|
| 103 | * | 
|---|
| 104 | * @param doc the Document encapsulating the pmml | 
|---|
| 105 | */ | 
|---|
| 106 | public void setCreatorApplication(Document doc) { | 
|---|
| 107 | NodeList tempL = doc.getElementsByTagName("Header"); | 
|---|
| 108 | Node header = tempL.item(0); | 
|---|
| 109 | if (header.getNodeType() == Node.ELEMENT_NODE) { | 
|---|
| 110 | NodeList appL = ((Element)header).getElementsByTagName("Application"); | 
|---|
| 111 | if (appL.getLength() > 0) { | 
|---|
| 112 | Node app = appL.item(0); | 
|---|
| 113 | if (app.getNodeType() == Node.ELEMENT_NODE) { | 
|---|
| 114 | String appName = ((Element)app).getAttribute("name"); | 
|---|
| 115 | if (appName != null && appName.length() > 0) { | 
|---|
| 116 | String version = ((Element)app).getAttribute("version"); | 
|---|
| 117 | if (version != null && version.length() > 0) { | 
|---|
| 118 | appName += " v. " + version; | 
|---|
| 119 | } | 
|---|
| 120 | m_creatorApplication = appName; | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /** | 
|---|
| 128 | * Get the data dictionary. | 
|---|
| 129 | * | 
|---|
| 130 | * @return the data dictionary | 
|---|
| 131 | */ | 
|---|
| 132 | public Instances getDataDictionary() { | 
|---|
| 133 | return m_dataDictionary; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | /** | 
|---|
| 137 | * Get the mining schema for this model. | 
|---|
| 138 | * | 
|---|
| 139 | * @return the mining schema | 
|---|
| 140 | */ | 
|---|
| 141 | public MiningSchema getMiningSchema() { | 
|---|
| 142 | return m_miningSchema; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | /** | 
|---|
| 146 | * Get the PMML version used for this model. | 
|---|
| 147 | * | 
|---|
| 148 | * @return the PMML version | 
|---|
| 149 | */ | 
|---|
| 150 | public String getPMMLVersion() { | 
|---|
| 151 | return m_pmmlVersion; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /** | 
|---|
| 155 | * Get the name of the application that created this model | 
|---|
| 156 | * | 
|---|
| 157 | * @return the name of the creating application or null | 
|---|
| 158 | * if not specified in the pmml. | 
|---|
| 159 | */ | 
|---|
| 160 | public String getCreatorApplication() { | 
|---|
| 161 | return m_creatorApplication; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | /** | 
|---|
| 165 | * Set a logger to use. | 
|---|
| 166 | * | 
|---|
| 167 | * @param log the logger to use | 
|---|
| 168 | */ | 
|---|
| 169 | public void setLog(Logger log) { | 
|---|
| 170 | m_log = log; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | /** | 
|---|
| 174 | * Get the logger. | 
|---|
| 175 | * | 
|---|
| 176 | * @return the logger (or null if none is being used) | 
|---|
| 177 | */ | 
|---|
| 178 | public Logger getLog() { | 
|---|
| 179 | return m_log; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | /** | 
|---|
| 183 | * Throw an exception - PMML models are pre-built. | 
|---|
| 184 | * | 
|---|
| 185 | * @param data the Instances to learn from | 
|---|
| 186 | * @throws Exception if something goes wrong | 
|---|
| 187 | */ | 
|---|
| 188 | public void buildClassifier(Instances data) throws Exception { | 
|---|
| 189 | throw new Exception("[PMMLClassifier] PMML models are pre-built " | 
|---|
| 190 | + "and static!"); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | /** | 
|---|
| 194 | * Signal that a scoring run has been completed. Resets | 
|---|
| 195 | * the initialized state to false so that a subsequent | 
|---|
| 196 | * scoring run will trigger the mapping of the mining | 
|---|
| 197 | * schema to incoming instances. If not called after a | 
|---|
| 198 | * scoring run, then the classifier will assume that | 
|---|
| 199 | * the current mapping is still valid. | 
|---|
| 200 | */ | 
|---|
| 201 | public void done() { | 
|---|
| 202 | m_initialized = false; | 
|---|
| 203 | m_fieldsMap = null; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | /** | 
|---|
| 207 | * Map mining schema to incoming instances. | 
|---|
| 208 | * | 
|---|
| 209 | * @param dataSet the structure of the incoming Instances | 
|---|
| 210 | * @throws Exception if something goes wrong | 
|---|
| 211 | */ | 
|---|
| 212 | public void mapToMiningSchema(Instances dataSet) throws Exception { | 
|---|
| 213 | if (m_fieldsMap == null) { | 
|---|
| 214 | // PMMLUtils.mapToMiningSchema(dataSet, m_miningSchema); | 
|---|
| 215 | m_fieldsMap = new MappingInfo(dataSet, m_miningSchema, m_log); | 
|---|
| 216 | m_initialized = true; | 
|---|
| 217 | } | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | /** | 
|---|
| 221 | * Get a textual description of the mapping between mining schema | 
|---|
| 222 | * fields and incoming data fields. | 
|---|
| 223 | * | 
|---|
| 224 | * @return a description of the fields mapping as a String or null if | 
|---|
| 225 | * no mapping has been constructed yet. | 
|---|
| 226 | */ | 
|---|
| 227 | public String getFieldsMappingString() { | 
|---|
| 228 | if (!m_initialized) { | 
|---|
| 229 | return null; | 
|---|
| 230 | } | 
|---|
| 231 | return m_fieldsMap.getFieldsMappingString(); | 
|---|
| 232 | } | 
|---|
| 233 | } | 
|---|