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/>. */ /** * WekaEvaluationInfo.java * Copyright (C) 2017 University of Waikato, Hamilton, NZ */ package adams.flow.transformer; import adams.core.QuickInfoHelper; import adams.flow.container.WekaEvaluationContainer; import adams.flow.core.DataInfoActor; import adams.flow.core.Token; import weka.classifiers.Evaluation; import weka.core.Instances; /** <!-- globalinfo-start --> * Outputs information about a Weka weka.classifiers.Evaluation object. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * weka.classifiers.Evaluation<br> * adams.flow.container.WekaEvaluationContainer<br> * - generates:<br> * java.lang.String<br> * <br><br> * Container information:<br> * - adams.flow.container.WekaEvaluationContainer: Evaluation, Model, Prediction output, Original indices * <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: WekaEvaluationInfo * </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 execution at this level gets stopped in case this * actor encounters an error; the error gets propagated; useful for critical * actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console; Note: the enclosing * actor handler must have this enabled as well. * default: false * </pre> * * <pre>-type <RELATION_NAME|HEADER|CLASS_ATTRIBUTE_NAME|PREDICTIONS_RECORDED|NUM_PREDICTIONS> (property: type) * The type of information to generate. * default: RELATION_NAME * </pre> * <!-- options-end --> * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class WekaEvaluationInfo extends AbstractTransformer implements DataInfoActor { private static final long serialVersionUID = -4898802545222942458L; /** * The type of information to output. */ public enum InfoType { RELATION_NAME, HEADER, CLASS_ATTRIBUTE_NAME, PREDICTIONS_RECORDED, NUM_PREDICTIONS, } /** 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 about a Weka " + Evaluation.class.getName() + " object."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("type", "type", InfoType.RELATION_NAME); } /** * 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 the Class of objects that can be processed */ @Override public Class[] accepts() { return new Class[] { Evaluation.class, WekaEvaluationContainer.class }; } /** * Returns the class of objects that it generates. * * @return the Class of the generated tokens */ @Override public Class[] generates() { switch (m_Type) { case RELATION_NAME: case CLASS_ATTRIBUTE_NAME: return new Class[] { String.class }; case HEADER: return new Class[] { Instances.class }; case PREDICTIONS_RECORDED: return new Class[] { Boolean.class }; case NUM_PREDICTIONS: 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() { Evaluation eval; if (m_InputToken.getPayload() instanceof Evaluation) eval = (Evaluation) m_InputToken.getPayload(); else eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); switch (m_Type) { case RELATION_NAME: m_OutputToken = new Token(eval.getHeader().relationName()); break; case CLASS_ATTRIBUTE_NAME: m_OutputToken = new Token(eval.getHeader().classAttribute().name()); break; case HEADER: m_OutputToken = new Token(eval.getHeader()); break; case PREDICTIONS_RECORDED: m_OutputToken = new Token(eval.predictions() != null); break; case NUM_PREDICTIONS: if (eval.predictions() == null) m_OutputToken = new Token(-1); else m_OutputToken = new Token(eval.predictions().size()); break; default: throw new IllegalStateException("Unhandled info type: " + m_Type); } return null; } }