/* * RapidMiner * * Copyright (C) 2001-2008 by Rapid-I and the contributors * * Complete list of developers available at our web site: * * http://rapid-i.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ package com.rapidminer.operator.learner.weka; import java.util.LinkedList; import java.util.List; import com.rapidminer.example.ExampleSet; import com.rapidminer.operator.IOObject; import com.rapidminer.operator.Operator; import com.rapidminer.operator.OperatorDescription; import com.rapidminer.operator.OperatorException; import com.rapidminer.operator.UserError; import com.rapidminer.parameter.ParameterType; import com.rapidminer.tools.WekaInstancesAdaptor; import com.rapidminer.tools.WekaTools; import weka.associations.Associator; import weka.core.Instances; import weka.core.OptionHandler; import weka.core.TechnicalInformation; import weka.core.TechnicalInformationHandler; /** * Performs the Weka association rule learner with the same name. The operator * returns a result object containing the rules found by the association * learner. In contrast to models generated by normal learners, the association * rules cannot be applied to an example set. Hence, there is no way to evaluate * the performance of association rules yet. See the Weka javadoc for further * operator and parameter descriptions. * * @author Ingo Mierswa * @version $Id: GenericWekaAssociationLearner.java,v 1.17 2006/04/05 09:42:01 * ingomierswa Exp $ */ public class GenericWekaAssociationLearner extends Operator implements TechnicalInformationHandler { public static final String[] WEKA_ASSOCIATORS = WekaTools.getWekaClasses(weka.associations.Associator.class); /** The list with the weka parameters. */ private List<ParameterType> wekaParameters = new LinkedList<ParameterType>(); public GenericWekaAssociationLearner(OperatorDescription description) { super(description); } public IOObject[] apply() throws OperatorException { ExampleSet exampleSet = getInput(ExampleSet.class); Associator associator = getWekaAssociator(getOperatorClassName(), WekaTools.getWekaParametersFromTypes(this, wekaParameters)); log("Converting to Weka instances."); Instances instances = WekaTools.toWekaInstances(exampleSet, "MiningInstances", WekaInstancesAdaptor.ASSOCIATION_RULE_MINING); try { log("Building Weka associator."); associator.buildAssociations(instances); } catch (Exception e) { throw new UserError(this, e, 905, new Object[] { getOperatorClassName(), e }); } return new IOObject[] { new WekaAssociator(getOperatorClassName(), associator) }; } /** Returns the Weka associator based on the subtype of this operator. */ private Associator getWekaAssociator(String prefixName, String[] parameters) throws OperatorException { String actualName = prefixName.substring(WekaTools.WEKA_OPERATOR_PREFIX.length()); String associatorName = null; for (int i = 0; i < WEKA_ASSOCIATORS.length; i++) { if (WEKA_ASSOCIATORS[i].endsWith(actualName)) { associatorName = WEKA_ASSOCIATORS[i]; break; } } Associator associator = null; try { Class clazz = Class.forName(associatorName); associator = (Associator) clazz.newInstance(); if (associator instanceof OptionHandler) { OptionHandler optionHandler = (OptionHandler)associator; optionHandler.setOptions(parameters); } } catch (Exception e) { throw new UserError(this, e, 904, new Object[] { associatorName, e }); } return associator; } public TechnicalInformation getTechnicalInformation() { try { Associator associator = getWekaAssociator(getOperatorClassName(), null); if (associator instanceof TechnicalInformationHandler) return ((TechnicalInformationHandler)associator).getTechnicalInformation(); else return null; } catch (OperatorException e) { return null; } } public Class<?>[] getInputClasses() { return new Class[] { ExampleSet.class }; } public Class<?>[] getOutputClasses() { return new Class[] { WekaAssociator.class }; } public List<ParameterType> getParameterTypes() { List<ParameterType> types = super.getParameterTypes(); Associator associator = null; try { // parameters must be null, not an empty String[0] array! associator = getWekaAssociator(getOperatorClassName(), null); } catch (OperatorException e) { throw new RuntimeException("Cannot instantiate Weka associator " + getOperatorClassName()); } wekaParameters = new LinkedList<ParameterType>(); if ((associator != null) && (associator instanceof OptionHandler)) { WekaTools.addParameterTypes((OptionHandler) associator, types, wekaParameters, false, null); } return types; } }