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/>. */ /* * WekaDatasetsMerge.java * Copyright (C) 2019 University of Waikato, Hamilton, NZ */ package adams.flow.transformer; import adams.core.QuickInfoHelper; import adams.flow.core.Token; import adams.flow.transformer.wekadatasetsmerge.AbstractMerge; import adams.flow.transformer.wekadatasetsmerge.Simple; import weka.core.Instance; import weka.core.Instances; /** <!-- globalinfo-start --> * Merges 2 or more datasets into a single dataset, under a selectable merge method. * <br><br> <!-- globalinfo-end --> * <p> <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * weka.core.Instances[]<br> * weka.core.Instance[]<br> * - generates:<br> * weka.core.Instances<br> * <br><br> <!-- flow-summary-end --> * <p> <!-- 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: WekaDatasetsMerge * </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>-method <adams.flow.transformer.wekadatasetsmerge.AbstractMerge> (property: mergeMethod) * The method that should be used to perform the merge. * default: adams.flow.transformer.wekadatasetsmerge.Simple * </pre> * <p> <!-- options-end --> * <p> * Merges two or more datasets into a single dataset using the specified merge method. * <p> * * @author Corey Sterling (csterlin at waikato dot ac dot nz) */ public class WekaDatasetsMerge extends AbstractTransformer implements WekaMergeInstancesActor { private static final long serialVersionUID = 433833069085059906L; /** The method to use to perform the merge. */ protected AbstractMerge m_MergeMethod; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Merges 2 or more datasets into a single dataset, using a selectable merge method."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("method", "mergeMethod", new Simple()); } /** * Gets the currently-set merge method. * * @return The merge method being used currently. */ public AbstractMerge getMergeMethod() { return m_MergeMethod; } /** * Sets the merge method to use to perform the merge. * * @param mergeMethod The merge method to use. */ public void setMergeMethod(AbstractMerge mergeMethod) { m_MergeMethod = mergeMethod; reset(); } /** * Gets the tip-text for the merge method option. * * @return The tip-text as a String. */ public String mergeMethodTipText() { return "The method that should be used to perform the merge."; } /** * Returns a quick info about the actor, which will be displayed in the GUI. * <br><br> * Default implementation returns null. * * @return null if no info available, otherwise short string */ @Override public String getQuickInfo() { return QuickInfoHelper.toString(this, "mergeMethod", m_MergeMethod, "method: "); } /** * Gets the input Instances to merge. * * @return The input Instances objects to merge. */ protected Instances[] getInput() { // The input to return Instances[] datasets; // Get the payload from the input token Object payload = m_InputToken.getPayload(); // See what type of payload we have if (payload instanceof Instances[]) { // Payload in correct form already, just return it datasets = (Instances[]) payload; } else if (payload instanceof Instance[]) { // Payload is a set of single Instance objects. Instance[] instancePayload = (Instance[]) payload; // Wrap the single instances into a dataset each of size one datasets = new Instances[instancePayload.length]; for (int i = 0; i < instancePayload.length; i++) { datasets[i] = datasetForSingleInstance(instancePayload[i]); } } else { // Unsupported input type throw new IllegalStateException("Unhandled input type: " + m_InputToken.getPayload().getClass()); } // Return the input return datasets; } /** * Creates an Instances dataset, containing a copy of the single instance * provided. * * @param instance The instance to create a dataset for. * @return The created dataset. */ protected Instances datasetForSingleInstance(Instance instance) { // Create a copy of the instance's original dataset Instances dataset = new Instances(instance.dataset(), 1); // Add a copy of the provided instance dataset.add((Instance) instance.copy()); // Return the dataset return dataset; } /** * Sets the payload of the output token for this transformer. * * @param output The output payload. */ protected void setOutput(Instances output) { m_OutputToken = new Token(); m_OutputToken.setPayload(output); } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { try { // Get the input token Instances[] datasetsToMerge = getInput(); // Perform the merge operation Instances joinedDataset = m_MergeMethod.merge(datasetsToMerge); // Set the result against the output token setOutput(joinedDataset); return null; } catch (Exception e) { return e.getMessage(); } } /** * Returns the class that the consumer accepts. * * @return the Class of objects that can be processed */ @Override public Class[] accepts() { return new Class[] { Instances[].class, Instance[].class }; } /** * Returns the class of objects that it generates. * * @return the Class of the generated tokens */ @Override public Class[] generates() { return new Class[] { Instances.class }; } }