Java tutorial
/* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * WekaClustererInfo.java * Copyright (C) 2015 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer; import adams.core.QuickInfoHelper; import adams.core.Utils; import adams.flow.container.WekaModelContainer; import adams.flow.core.DataInfoActor; import adams.flow.core.Token; import weka.classifiers.Classifier; import weka.clusterers.Clusterer; import weka.core.Drawable; /** <!-- globalinfo-start --> * Outputs information of a trained weka.clusterers.Clusterer object. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * weka.classifiers.Classifier<br> * adams.flow.container.WekaModelContainer<br> * - generates:<br> * java.lang.String<br> * <br><br> * Container information:<br> * - adams.flow.container.WekaModelContainer: Model, Header, Dataset * <br><br> <!-- flow-summary-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-name <java.lang.String> (property: name) * The name of the actor. * default: WekaClustererInfo * </pre> * * <pre>-annotation <adams.core.base.BaseAnnotation> (property: annotations) * The annotations to attach to this actor. * default: * </pre> * * <pre>-skip <boolean> (property: skip) * If set to true, transformation is skipped and the input token is just forwarded * as it is. * default: false * </pre> * * <pre>-stop-flow-on-error <boolean> (property: stopFlowOnError) * If set to true, the flow gets stopped in case this actor encounters an error; * useful for critical actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console. * default: false * </pre> * * <pre>-type <MODEL|NUM_CLUSTERS|GRAPH> (property: type) * The type of information to generate. * default: MODEL * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision: 9523 $ */ public class WekaClustererInfo extends AbstractTransformer implements DataInfoActor { /** for serialization. */ private static final long serialVersionUID = -3019442578354930841L; /** * The type of information to generate. * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision: 9523 $ */ public enum InfoType { /** model. */ MODEL, /** number of clusters. */ NUM_CLUSTERS, /** graph (if available). */ GRAPH, } /** the type of information to generate. */ protected InfoType m_Type; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Outputs information of a trained weka.clusterers.Clusterer object."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("type", "type", InfoType.MODEL); } /** * Returns a quick info about the actor, which will be displayed in the GUI. * * @return null if no info available, otherwise short string */ @Override public String getQuickInfo() { return QuickInfoHelper.toString(this, "type", m_Type); } /** * Sets the type of information to generate. * * @param value the type */ public void setType(InfoType value) { m_Type = value; reset(); } /** * Returns the type of information to generate. * * @return the type */ public InfoType getType() { return m_Type; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String typeTipText() { return "The type of information to generate."; } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->weka.classifiers.Classifier.class, adams.flow.container.WekaModelContainer.class<!-- flow-accepts-end --> */ public Class[] accepts() { return new Class[] { Classifier.class, WekaModelContainer.class }; } /** * Returns the class of objects that it generates. * * @return <!-- flow-generates-start -->java.lang.String.class<!-- flow-generates-end --> */ public Class[] generates() { switch (m_Type) { case MODEL: case GRAPH: return new Class[] { String.class }; case NUM_CLUSTERS: return new Class[] { Integer.class }; default: throw new IllegalStateException("Unhandled info type: " + m_Type); } } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Clusterer cls; result = null; cls = null; if (m_InputToken.getPayload() instanceof Classifier) cls = (Clusterer) m_InputToken.getPayload(); else if (m_InputToken.getPayload() instanceof WekaModelContainer) cls = (Clusterer) ((WekaModelContainer) m_InputToken.getPayload()) .getValue(WekaModelContainer.VALUE_MODEL); else result = "Unhandled class: " + Utils.classToString(m_InputToken.getPayload()); if (result == null) { switch (m_Type) { case MODEL: m_OutputToken = new Token(cls.toString()); break; case NUM_CLUSTERS: try { m_OutputToken = new Token(cls.numberOfClusters()); } catch (Exception e) { result = handleException("Failed to obtain number of clusters!", e); } break; case GRAPH: try { if (cls instanceof Drawable) m_OutputToken = new Token(((Drawable) cls).graph()); } catch (Exception e) { result = handleException("Failed to obtain graph from clusterer!", e); } break; default: result = "Unhandled info type: " + m_Type; } } return result; } }