详解KNIME自定义节点的NodeModel类

Knime新建自定义节点官网有教程
新建自定义节点教程
新建好了目录应该是下图这样
详解KNIME自定义节点的NodeModel类_第1张图片
NodeModel.java

package org.knime.examples.test1;

import java.io.File;
import java.io.IOException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IllegalFormatException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.knime.core.data.DataCell;
import org.knime.core.data.DataColumnSpec;
import org.knime.core.data.DataColumnSpecCreator;
import org.knime.core.data.DataRow;
import org.knime.core.data.DataTableSpec;
import org.knime.core.data.container.CloseableRowIterator;
import org.knime.core.data.def.DefaultRow;
import org.knime.core.data.def.DoubleCell;
import org.knime.core.data.def.StringCell;
import org.knime.core.node.BufferedDataContainer;
import org.knime.core.node.BufferedDataTable;
import org.knime.core.node.CanceledExecutionException;
import org.knime.core.node.ExecutionContext;
import org.knime.core.node.ExecutionMonitor;
import org.knime.core.node.InvalidSettingsException;
import org.knime.core.node.NodeLogger;
import org.knime.core.node.NodeModel;
import org.knime.core.node.NodeSettingsRO;
import org.knime.core.node.NodeSettingsWO;
import org.knime.core.node.defaultnodesettings.SettingsModelString;


/**
 * This is an example implementation of the node model of the
 * "Test1" node.
 * 
 * This example node performs simple number formatting
 * ({@link String#format(String, Object...)}) using a user defined format string
 * on all double columns of its input table.
 *
 * @author zaj
 */
public class Test1NodeModel extends NodeModel {
    
    /**
	 * The logger is used to print info/warning/error messages to the KNIME console
	 * and to the KNIME log file. Retrieve it via 'NodeLogger.getLogger' providing
	 * the class of this node model.
	 */
	private static final NodeLogger LOGGER = NodeLogger.getLogger(Test1NodeModel.class);

	/**
	 * The settings key to retrieve and store settings shared between node dialog
	 * and node model. In this case, the key for the number format String that
	 * should be entered by the user in the dialog.
	 */
	private static final String KEY_NUMBER_FOMAT = "number_format";

	/**
	 * The default number format String. This default will round to three decimal
	 * places. For an explanation of the format String specification please refer to
	 * https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
	 */
	private static final String DEFAULT_NUMBER_FORMAT = "%.3f";

	/**
	 * The settings model to manage the shared settings. This model will hold the
	 * value entered by the user in the dialog and will update once the user changes
	 * the value. Furthermore, it provides methods to easily load and save the value
	 * to and from the shared settings (see:
	 * 
* {@link #loadValidatedSettingsFrom(NodeSettingsRO)}, * {@link #saveSettingsTo(NodeSettingsWO)}). *
* Here, we use a SettingsModelString as the number format is a String. * There are models for all common data types. Also have a look at the comments * in the constructor of the {@link Test1NodeDialog} as the settings * models are also used to create simple dialogs. */
private final SettingsModelString m_numberFormatSettings = createNumberFormatSettingsModel(); /** * Constructor for the node model. */ protected Test1NodeModel() { /** * Here we specify how many data input and output tables the node should have. * In this case its one input and one output table. */ super(1, 1); } /** * A convenience method to create a new settings model used for the number * format String. This method will also be used in the {@link Test1NodeDialog}. * The settings model will sync via the above defined key. * * @return a new SettingsModelString with the key for the number format String */ static SettingsModelString createNumberFormatSettingsModel() { String s = "你好"; //System.out.println(s); if(s.equals("你好")) { Map<String, String> map = new HashMap(); map.put("1", "70%"); map.put("3", "30%"); System.out.println("******推荐节点列表********"); System.out.println(map); Iterator<String> it = map.keySet().iterator(); while(it.hasNext()) { Object key = it.next(); Object val = map.get(key); System.out.println("编号:" +key+ ",概率: "+val); } } return new SettingsModelString(KEY_NUMBER_FOMAT, DEFAULT_NUMBER_FORMAT); } /** * * {@inheritDoc} */ @Override protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception { /* * The functionality of the node is implemented in the execute method. This * implementation will format each double column of the input table using a user * provided format String. The output will be one String column for each double * column of the input containing the formatted number from the input table. For * simplicity, all other columns are ignored in this example. * * Some example log output. This will be printed to the KNIME console and KNIME * log. */ LOGGER.info("This is an example info."); /* * The input data table to work with. The "inData" array will contain as many * input tables as specified in the constructor. In this case it can only be one * (see constructor). */ BufferedDataTable inputTable = inData[0]; /* * Create the spec of the output table, for each double column of the input * table we will create one formatted String column in the output. See the * javadoc of the "createOutputSpec(...)" for more information. */ DataTableSpec outputSpec = createOutputSpec(inputTable.getDataTableSpec()); /* * The execution context provides storage capacity, in this case a * data container to which we will add rows sequentially. Note, this container * can handle arbitrary big data tables, it will buffer to disc if necessary. * The execution context is provided as an argument to the execute method by the * framework. Have a look at the methods of the "exec". There is a lot of * functionality to create and change data tables. */ BufferedDataContainer container = exec.createDataContainer(outputSpec); /* * Get the row iterator over the input table which returns each row one-by-one * from the input table. */ CloseableRowIterator rowIterator = inputTable.iterator(); /* * A counter for how many rows have already been processed. This is used to * calculate the progress of the node, which is displayed as a loading bar under * the node icon. */ int currentRowCounter = 0; // Iterate over the rows of the input table. while (rowIterator.hasNext()) { DataRow currentRow = rowIterator.next(); int numberOfCells = currentRow.getNumCells(); /* * A list to collect the cells to output for the current row. The type and * amount of cells must match the DataTableSpec we used when creating the * DataContainer. */ List<DataCell> cells = new ArrayList<>(); // Iterate over the cells of the current row. for (int i = 0; i < numberOfCells; i++) { DataCell cell = currentRow.getCell(i); /* * We only care about double cells. Hence, we check if the current cell equals * DoubleCell.class. All other cells in the input table will be ignored. */ if (cell.getType().getCellClass().equals((DoubleCell.class))) { // Cast the cell as we know is must be a DoubleCell. DoubleCell doubleCell = (DoubleCell) cell; /* * Format the double value using the user defined number format. The format is * retrieved from the settings model member that we created above. */ String format = m_numberFormatSettings.getStringValue(); String formatedValue = String.format(format, doubleCell.getDoubleValue()); // Create a new StringCell and add it to our cell list. cells.add(new StringCell(formatedValue)); } /* * In this example we do not check for missing cells. If there are missing cells * in a row, the node will throw an Exception because we try to create a row * with less cells than specified in the table specification we used to create * the data container above. Hence, for your node implementation keep in mind to * check for missing cells in the input table. Then create missing cells with an * appropriate message or throw an Exception with a nice error message in case * missing cells are not allowed at all. Here, this could be done in an 'else * if' clause checking 'cell.isMissing()'. Then, add a new MissingCell to the * list of cells. */ } // Add the new row to the output data container DataRow row = new DefaultRow(currentRow.getKey(), cells); container.addRowToTable(row); // We finished processing one row, hence increase the counter currentRowCounter++; /* * Here we check if a user triggered a cancel of the node. If so, this call will * throw an exception and the execution will stop. This should be done * frequently during execution, e.g. after the processing of one row if * possible. */ exec.checkCanceled(); /* * Calculate the percentage of execution progress and inform the * ExecutionMonitor. Additionally, we can set a message what the node is * currently doing (the message will be displayed as a tooltip when hovering * over the progress bar of the node). This is especially useful to inform the * user about the execution status for long running nodes. */ exec.setProgress(currentRowCounter / (double) inputTable.size(), "Formatting row " + currentRowCounter); } /* * Once we are done, we close the container and return its table. Here we need * to return as many tables as we specified in the constructor. This node has * one output, hence return one table (wrapped in an array of tables). */ container.close(); BufferedDataTable out = container.getTable(); return new BufferedDataTable[] { out }; } /** * {@inheritDoc} */ @Override protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException { /* * Check if the node is executable, e.g. all required user settings are * available and valid, or the incoming types are feasible for the node to * execute. In case the node can execute in its current configuration with the * current input, calculate and return the table spec that would result of the * execution of this node. I.e. this method precalculates the table spec of the * output table. * * Here we perform a sanity check on the entered number format String. In this * case we just try to apply it to some dummy double number. If there is a * problem, an IllegalFormatException will be thrown. We catch the exception and * wrap it in a InvalidSettingsException with an informative message for the * user. The message should make clear what the problem is and how it can be * fixed if this information is available. This will be displayed in the KNIME * console and printed to the KNIME log. The log will also contain the stack * trace. */ String format = m_numberFormatSettings.getStringValue(); try { String.format(format, 0.0123456789); } catch (IllegalFormatException e) { throw new InvalidSettingsException( "The entered format is not a valid pattern String! Reason: " + e.getMessage(), e); } /* * Similar to the return type of the execute method, we need to return an array * of DataTableSpecs with the length of the number of outputs ports of the node * (as specified in the constructor). The resulting table created in the execute * methods must match the spec created in this method. As we will need to * calculate the output table spec again in the execute method in order to * create a new data container, we create a new method to do that. */ DataTableSpec inputTableSpec = inSpecs[0]; return new DataTableSpec[] { createOutputSpec(inputTableSpec) }; } /** * Creates the output table spec from the input spec. For each double column in * the input, one String column will be created containing the formatted double * value as String. * * @param inputTableSpec * @return */ private DataTableSpec createOutputSpec(DataTableSpec inputTableSpec) { List<DataColumnSpec> newColumnSpecs = new ArrayList<>(); // Iterate over the input column specs for (int i = 0; i < inputTableSpec.getNumColumns(); i++) { DataColumnSpec columnSpec = inputTableSpec.getColumnSpec(i); /* * If the column is a double column (hence there are double cells), we create a * new DataColumnSpec with column type String and a new column name. Here, we * wrap the original column name with 'Formatted(...)'. */ if (columnSpec.getType().getCellClass().equals(DoubleCell.class)) { String newName = "Formatted(" + columnSpec.getName() + ")"; DataColumnSpecCreator specCreator = new DataColumnSpecCreator(newName, StringCell.TYPE); newColumnSpecs.add(specCreator.createSpec()); } } // Create and return a new DataTableSpec from the list of DataColumnSpecs. DataColumnSpec[] newColumnSpecsArray = newColumnSpecs.toArray(new DataColumnSpec[newColumnSpecs.size()]); return new DataTableSpec(newColumnSpecsArray); } /** * {@inheritDoc} */ @Override protected void saveSettingsTo(final NodeSettingsWO settings) { /* * Save user settings to the NodeSettings object. SettingsModels already know how to * save them self to a NodeSettings object by calling the below method. In general, * the NodeSettings object is just a key-value store and has methods to write * all common data types. Hence, you can easily write your settings manually. * See the methods of the NodeSettingsWO. */ m_numberFormatSettings.saveSettingsTo(settings); } /** * {@inheritDoc} */ @Override protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws InvalidSettingsException { /* * Load (valid) settings from the NodeSettings object. It can be safely assumed that * the settings are validated by the method below. * * The SettingsModel will handle the loading. After this call, the current value * (from the view) can be retrieved from the settings model. */ m_numberFormatSettings.loadSettingsFrom(settings); } /** * {@inheritDoc} */ @Override protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException { /* * Check if the settings could be applied to our model e.g. if the user provided * format String is empty. In this case we do not need to check as this is * already handled in the dialog. Do not actually set any values of any member * variables. */ m_numberFormatSettings.validateSettings(settings); } @Override protected void loadInternals(File nodeInternDir, ExecutionMonitor exec) throws IOException, CanceledExecutionException { /* * Advanced method, usually left empty. Everything that is * handed to the output ports is loaded automatically (data returned by the execute * method, models loaded in loadModelContent, and user settings set through * loadSettingsFrom - is all taken care of). Only load the internals * that need to be restored (e.g. data used by the views). */ } @Override protected void saveInternals(File nodeInternDir, ExecutionMonitor exec) throws IOException, CanceledExecutionException { /* * Advanced method, usually left empty. Everything * written to the output ports is saved automatically (data returned by the execute * method, models saved in the saveModelContent, and user settings saved through * saveSettingsTo - is all taken care of). Save only the internals * that need to be preserved (e.g. data used by the views). */ } @Override protected void reset() { /* * Code executed on a reset of the node. Models built during execute are cleared * and the data handled in loadInternals/saveInternals will be erased. */ } }

上面是源码,我们一步步分析,看每个方法怎么执行的。
我在每个方法都写了一个输出语句,当调用此方法时会打印该方法的名字,下面是我修改过的源码。

package org.knime.exxamples.test;

import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IllegalFormatException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

import org.knime.core.data.DataCell;
import org.knime.core.data.DataColumnSpec;
import org.knime.core.data.DataColumnSpecCreator;
import org.knime.core.data.DataRow;
import org.knime.core.data.DataTableSpec;
import org.knime.core.data.container.CloseableRowIterator;
import org.knime.core.data.def.DefaultRow;
import org.knime.core.data.def.DoubleCell;
import org.knime.core.data.def.StringCell;
import org.knime.core.node.BufferedDataContainer;
import org.knime.core.node.BufferedDataTable;
import org.knime.core.node.CanceledExecutionException;
import org.knime.core.node.ExecutionContext;
import org.knime.core.node.ExecutionMonitor;
import org.knime.core.node.InvalidSettingsException;
import org.knime.core.node.NodeInfo;
import org.knime.core.node.NodeLogger;
import org.knime.core.node.NodeModel;
import org.knime.core.node.NodeSettingsRO;
import org.knime.core.node.NodeSettingsWO;
import org.knime.core.node.defaultnodesettings.SettingsModelString;
import org.knime.core.util.Version;
import org.knime.core.util.workflowalizer.NodeAndBundleInformation;


/**
 * This is an example implementation of the node model of the
 * "Test" node.
 * 
 * This example node performs simple number formatting
 * ({@link String#format(String, Object...)}) using a user defined format string
 * on all double columns of its input table.
 *
 * @author zaj
 */
public class TestNodeModel extends NodeModel implements DragSourceListener{
	public Optional<Version> recommendation() {
		ArrayList<Object> list = new ArrayList<>();
		NodeAndBundleInformation a = new NodeAndBundleInformation(null, null, null, null, null, null, null, null, null, null);
		return a.getBundleVersion();
	}
	
	public static void nodeInfo() {
		NodeInfo nodeInfo = new NodeInfo("org.knime.exxamples.test.TestNodeFactory","TestNodeFactory");
		System.out.println(nodeInfo.getName());
	}
    
    /**
	 * The logger is used to print info/warning/error messages to the KNIME console
	 * and to the KNIME log file. Retrieve it via 'NodeLogger.getLogger' providing
	 * the class of this node model.
	 */
	private static final NodeLogger LOGGER = NodeLogger.getLogger(TestNodeModel.class);

	/**
	 * The settings key to retrieve and store settings shared between node dialog
	 * and node model. In this case, the key for the number format String that
	 * should be entered by the user in the dialog.
	 */
	private static final String KEY_NUMBER_FOMAT = "number_format";

	/**
	 * The default number format String. This default will round to three decimal
	 * places. For an explanation of the format String specification please refer to
	 * https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
	 */
	private static final String DEFAULT_NUMBER_FORMAT = "%.3f";

	/**
	 * The settings model to manage the shared settings. This model will hold the
	 * value entered by the user in the dialog and will update once the user changes
	 * the value. Furthermore, it provides methods to easily load and save the value
	 * to and from the shared settings (see:
	 * 
* {@link #loadValidatedSettingsFrom(NodeSettingsRO)}, * {@link #saveSettingsTo(NodeSettingsWO)}). *
* Here, we use a SettingsModelString as the number format is a String. * There are models for all common data types. Also have a look at the comments * in the constructor of the {@link TestNodeDialog} as the settings * models are also used to create simple dialogs. */
private final SettingsModelString m_numberFormatSettings = createNumberFormatSettingsModel(); /** * Constructor for the node model. */ protected TestNodeModel() { /** * Here we specify how many data input and output tables the node should have. * In this case its one input and one output table. */ super(1, 1); System.out.println("TestNodeModel"); } /** * A convenience method to create a new settings model used for the number * format String. This method will also be used in the {@link TestNodeDialog}. * The settings model will sync via the above defined key. * * @return a new SettingsModelString with the key for the number format String */ static SettingsModelString createNumberFormatSettingsModel() { System.out.println("createNumberFormatSettingsModel"); //Logger.getLogger("createNumberFormatSettingsModel"); String s = "你好"; //System.out.println(s); if(s.equals("你好")) { Map<String, String> map = new HashMap(); map.put("2", "50%"); map.put("1", "30%"); map.put("3", "20%"); System.out.println("******推荐节点列表********"); System.out.println(map); Iterator<String> it = map.keySet().iterator(); while(it.hasNext()) { Object key = it.next(); Object val = map.get(key); System.out.println("编号:" +key+ ",概率: "+val); } } nodeInfo(); return new SettingsModelString(KEY_NUMBER_FOMAT, DEFAULT_NUMBER_FORMAT); } /** * * {@inheritDoc} */ @Override protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception { /* * The functionality of the node is implemented in the execute method. This * implementation will format each double column of the input table using a user * provided format String. The output will be one String column for each double * column of the input containing the formatted number from the input table. For * simplicity, all other columns are ignored in this example. * * Some example log output. This will be printed to the KNIME console and KNIME * log. */ LOGGER.info("This is an example info."); /* * The input data table to work with. The "inData" array will contain as many * input tables as specified in the constructor. In this case it can only be one * (see constructor). */ BufferedDataTable inputTable = inData[0]; /* * Create the spec of the output table, for each double column of the input * table we will create one formatted String column in the output. See the * javadoc of the "createOutputSpec(...)" for more information. */ DataTableSpec outputSpec = createOutputSpec(inputTable.getDataTableSpec()); /* * The execution context provides storage capacity, in this case a * data container to which we will add rows sequentially. Note, this container * can handle arbitrary big data tables, it will buffer to disc if necessary. * The execution context is provided as an argument to the execute method by the * framework. Have a look at the methods of the "exec". There is a lot of * functionality to create and change data tables. */ BufferedDataContainer container = exec.createDataContainer(outputSpec); /* * Get the row iterator over the input table which returns each row one-by-one * from the input table. */ CloseableRowIterator rowIterator = inputTable.iterator(); /* * A counter for how many rows have already been processed. This is used to * calculate the progress of the node, which is displayed as a loading bar under * the node icon. */ int currentRowCounter = 0; // Iterate over the rows of the input table. while (rowIterator.hasNext()) { DataRow currentRow = rowIterator.next(); int numberOfCells = currentRow.getNumCells(); /* * A list to collect the cells to output for the current row. The type and * amount of cells must match the DataTableSpec we used when creating the * DataContainer. */ List<DataCell> cells = new ArrayList<>(); // Iterate over the cells of the current row. for (int i = 0; i < numberOfCells; i++) { DataCell cell = currentRow.getCell(i); /* * We only care about double cells. Hence, we check if the current cell equals * DoubleCell.class. All other cells in the input table will be ignored. */ if (cell.getType().getCellClass().equals((DoubleCell.class))) { // Cast the cell as we know is must be a DoubleCell. DoubleCell doubleCell = (DoubleCell) cell; /* * Format the double value using the user defined number format. The format is * retrieved from the settings model member that we created above. */ String format = m_numberFormatSettings.getStringValue(); String formatedValue = String.format(format, doubleCell.getDoubleValue()); // Create a new StringCell and add it to our cell list. cells.add(new StringCell(formatedValue)); } /* * In this example we do not check for missing cells. If there are missing cells * in a row, the node will throw an Exception because we try to create a row * with less cells than specified in the table specification we used to create * the data container above. Hence, for your node implementation keep in mind to * check for missing cells in the input table. Then create missing cells with an * appropriate message or throw an Exception with a nice error message in case * missing cells are not allowed at all. Here, this could be done in an 'else * if' clause checking 'cell.isMissing()'. Then, add a new MissingCell to the * list of cells. */ } // Add the new row to the output data container DataRow row = new DefaultRow(currentRow.getKey(), cells); container.addRowToTable(row); // We finished processing one row, hence increase the counter currentRowCounter++; /* * Here we check if a user triggered a cancel of the node. If so, this call will * throw an exception and the execution will stop. This should be done * frequently during execution, e.g. after the processing of one row if * possible. */ exec.checkCanceled(); /* * Calculate the percentage of execution progress and inform the * ExecutionMonitor. Additionally, we can set a message what the node is * currently doing (the message will be displayed as a tooltip when hovering * over the progress bar of the node). This is especially useful to inform the * user about the execution status for long running nodes. */ exec.setProgress(currentRowCounter / (double) inputTable.size(), "Formatting row " + currentRowCounter); } /* * Once we are done, we close the container and return its table. Here we need * to return as many tables as we specified in the constructor. This node has * one output, hence return one table (wrapped in an array of tables). */ container.close(); BufferedDataTable out = container.getTable(); return new BufferedDataTable[] { out }; } /** * {@inheritDoc} */ @Override protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException { /* * Check if the node is executable, e.g. all required user settings are * available and valid, or the incoming types are feasible for the node to * execute. In case the node can execute in its current configuration with the * current input, calculate and return the table spec that would result of the * execution of this node. I.e. this method precalculates the table spec of the * output table. * * Here we perform a sanity check on the entered number format String. In this * case we just try to apply it to some dummy double number. If there is a * problem, an IllegalFormatException will be thrown. We catch the exception and * wrap it in a InvalidSettingsException with an informative message for the * user. The message should make clear what the problem is and how it can be * fixed if this information is available. This will be displayed in the KNIME * console and printed to the KNIME log. The log will also contain the stack * trace. */ System.out.println("DataTableSpec[]"); String format = m_numberFormatSettings.getStringValue(); try { String.format(format, 0.0123456789); } catch (IllegalFormatException e) { throw new InvalidSettingsException( "The entered format is not a valid pattern String! Reason: " + e.getMessage(), e); } /* * Similar to the return type of the execute method, we need to return an array * of DataTableSpecs with the length of the number of outputs ports of the node * (as specified in the constructor). The resulting table created in the execute * methods must match the spec created in this method. As we will need to * calculate the output table spec again in the execute method in order to * create a new data container, we create a new method to do that. */ DataTableSpec inputTableSpec = inSpecs[0]; return new DataTableSpec[] { createOutputSpec(inputTableSpec) }; } /** * Creates the output table spec from the input spec. For each double column in * the input, one String column will be created containing the formatted double * value as String. * * @param inputTableSpec * @return */ private DataTableSpec createOutputSpec(DataTableSpec inputTableSpec) { System.out.println("DataTableSpec"); List<DataColumnSpec> newColumnSpecs = new ArrayList<>(); // Iterate over the input column specs for (int i = 0; i < inputTableSpec.getNumColumns(); i++) { DataColumnSpec columnSpec = inputTableSpec.getColumnSpec(i); /* * If the column is a double column (hence there are double cells), we create a * new DataColumnSpec with column type String and a new column name. Here, we * wrap the original column name with 'Formatted(...)'. */ if (columnSpec.getType().getCellClass().equals(DoubleCell.class)) { String newName = "Formatted(" + columnSpec.getName() + ")"; DataColumnSpecCreator specCreator = new DataColumnSpecCreator(newName, StringCell.TYPE); newColumnSpecs.add(specCreator.createSpec()); } } // Create and return a new DataTableSpec from the list of DataColumnSpecs. DataColumnSpec[] newColumnSpecsArray = newColumnSpecs.toArray(new DataColumnSpec[newColumnSpecs.size()]); return new DataTableSpec(newColumnSpecsArray); } /** * {@inheritDoc} */ @Override protected void saveSettingsTo(final NodeSettingsWO settings) { /* * Save user settings to the NodeSettings object. SettingsModels already know how to * save them self to a NodeSettings object by calling the below method. In general, * the NodeSettings object is just a key-value store and has methods to write * all common data types. Hence, you can easily write your settings manually. * See the methods of the NodeSettingsWO. */ System.out.println("saveSettingsTo"); m_numberFormatSettings.saveSettingsTo(settings); } /** * {@inheritDoc} */ @Override protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws InvalidSettingsException { /* * Load (valid) settings from the NodeSettings object. It can be safely assumed that * the settings are validated by the method below. * * The SettingsModel will handle the loading. After this call, the current value * (from the view) can be retrieved from the settings model. */ System.out.println("loadValidatedSettingsFrom"); m_numberFormatSettings.loadSettingsFrom(settings); } /** * {@inheritDoc} */ @Override protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException { /* * Check if the settings could be applied to our model e.g. if the user provided * format String is empty. In this case we do not need to check as this is * already handled in the dialog. Do not actually set any values of any member * variables. */ System.out.println("validateSettings"); m_numberFormatSettings.validateSettings(settings); } @Override protected void loadInternals(File nodeInternDir, ExecutionMonitor exec) throws IOException, CanceledExecutionException { /* * Advanced method, usually left empty. Everything that is * handed to the output ports is loaded automatically (data returned by the execute * method, models loaded in loadModelContent, and user settings set through * loadSettingsFrom - is all taken care of). Only load the internals * that need to be restored (e.g. data used by the views). */ System.out.println("loadInternals"); } @Override protected void saveInternals(File nodeInternDir, ExecutionMonitor exec) throws IOException, CanceledExecutionException { /* * Advanced method, usually left empty. Everything * written to the output ports is saved automatically (data returned by the execute * method, models saved in the saveModelContent, and user settings saved through * saveSettingsTo - is all taken care of). Save only the internals * that need to be preserved (e.g. data used by the views). */ System.out.println("saveInternals"); } @Override protected void reset() { /* * Code executed on a reset of the node. Models built during execute are cleared * and the data handled in loadInternals/saveInternals will be erased. */ System.out.println("reset"); } @Override public void dragDropEnd(DragSourceDropEvent arg0) { // TODO Auto-generated method stub System.out.println("你好"); } @Override public void dragEnter(DragSourceDragEvent arg0) { // TODO Auto-generated method stub System.out.println("你好"); } @Override public void dragExit(DragSourceEvent arg0) { // TODO Auto-generated method stub System.out.println("你好"); } @Override public void dragOver(DragSourceDragEvent arg0) { // TODO Auto-generated method stub Logger.getLogger("你好"); } @Override public void dropActionChanged(DragSourceDragEvent arg0) { System.out.println("你好"); } }

第一步
把Test节点拖到工作区
详解KNIME自定义节点的NodeModel类_第2张图片
后台显示
在这里插入图片描述
首先调用createNumberFormatSettingsModel,创建一个数字格式化视图。
详解KNIME自定义节点的NodeModel类_第3张图片
第二步
新增节点,组成流程
详解KNIME自定义节点的NodeModel类_第4张图片
调用节点配置方法
在这里插入图片描述

第三步
执行流程
详解KNIME自定义节点的NodeModel类_第5张图片
在这里插入图片描述

createNumberFormatSettingsModel
ERROR	 main TestNodeFactory	 CODING PROBLEM	Missing or surplus view description
******推荐节点列表********
{1=30%, 2=50%, 3=20%}
编号:1,概率: 30%
编号:2,概率: 50%
编号:3,概率: 20%
TestNodeFactory
TestNodeModel
configure
createOutputSpec
configure
createOutputSpec
createOutputSpec
WARN 	 main Node	 No columns to visualize are available! Please refer to the NodeDescription to find out why!
reset
configure
createOutputSpec
WARN 	 main Node	 No columns to visualize are available! Please refer to the NodeDescription to find out why!
createOutputSpec
WARN 	 KNIME-Worker-2 Node	 No columns to visualize are available! Please refer to the NodeDescription to find out why!

你可能感兴趣的:(knime,java,eclipse,jar)