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/>. */ /* * WekaClusterAssignments.java * Copyright (C) 2016 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer; import adams.data.spreadsheet.DefaultSpreadSheet; import adams.data.spreadsheet.SpreadSheet; import adams.flow.container.WekaClusterEvaluationContainer; import adams.flow.core.Token; import weka.clusterers.ClusterEvaluation; /** <!-- globalinfo-start --> * Outputs the cluster assignments from the evaluation. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * weka.clusterers.ClusterEvaluation<br> * adams.flow.container.WekaClusterEvaluationContainer<br> * - generates:<br> * adams.data.spreadsheet.SpreadSheet<br> * <br><br> * Container information:<br> * - adams.flow.container.WekaClusterEvaluationContainer: Evaluation, Model, Log-likelohood * <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: WekaClusterAssignments * </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> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class WekaClusterAssignments extends AbstractTransformer { /** for serialization. */ private static final long serialVersionUID = 8082115424369061977L; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Outputs the cluster assignments from the evaluation."; } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->weka.clusterers.ClusterEvaluation.class, adams.flow.container.WekaClusterEvaluationContainer.class<!-- flow-accepts-end --> */ @Override public Class[] accepts() { return new Class[] { ClusterEvaluation.class, WekaClusterEvaluationContainer.class }; } /** * Returns the class of objects that it generates. * * @return <!-- flow-generates-start -->adams.data.spreadsheet.SpreadSheet.class<!-- flow-generates-end --> */ @Override public Class[] generates() { return new Class[] { SpreadSheet.class }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; ClusterEvaluation eval; SpreadSheet sheet; result = null; if (m_InputToken.getPayload() instanceof WekaClusterEvaluationContainer) eval = (ClusterEvaluation) ((WekaClusterEvaluationContainer) m_InputToken.getPayload()) .getValue(WekaClusterEvaluationContainer.VALUE_EVALUATION); else eval = (ClusterEvaluation) m_InputToken.getPayload(); if ((eval != null) && (eval.getClusterAssignments() != null)) { sheet = new DefaultSpreadSheet(); // header sheet.getHeaderRow().addCell("A").setContent("Assignment"); // data for (double assignment : eval.getClusterAssignments()) sheet.addRow().addCell("A").setContent(assignment); m_OutputToken = new Token(sheet); } else { getLogger().warning("No evaluation or cluster assignments available!"); } return result; } }