execute

This function is invoked only after all predecessor nodes have been successfully executed and all data is therefore available at the input ports. Implement this function with your task in the derived model. The input data is available in the given array argument inData and is ensured to be neither null nor contain null elements. The array index refers to the port index, i.e. if the node has two in-ports, then the BufferedDataTable array consists of two elements: first element (position = 0) the input from the first in-port, second element (position = 1) the input from the second in-port. In order to create output data, you need to create objects of class BufferedDataTable. Use the ExecutionContext argument to create BufferedDataTable. The example below demonstrates how to access the data from the in-port.

Example #1:

	protected BufferedDataTable[] execute(finalBufferedDataTable[] inData,
    final ExecutionContext exec) throws Exception {
    BufferedDataTable table = inData[0];
    int rowCount = table.getRowCount();
    int currentRow = 0;
    for (DataRow row : table) {
        // check if the user cancelled the execution
        exec.checkCanceled();
        // report progress
        exec.setProgress((double)currentRow / rowCount, 
               " processing row " + currentRow);
        for (int i = 0; i < row.getNumCells(); i++) {
            DataCell cell = row.getCell(i);
            if (!cell.isMissing()) {
                // do something...
            }
        }
        currentRow++;
    }
    // return here your results in a BufferedDataTable[]
    ...
}

There are two possibilities to create a new BufferedDataTable for the out-port if new data is generated during the execution of the node:

  1. with a BufferedDataContainer (Example#2), or
  2. using the ColumnRearranger (Example#3)

Example #2:

	int nrRows = ...; 
int nrColumns = ...;
BufferedDataContainer buf = exec.createBufferedDataContainer(spec);
for (int j = 0; j < nrRows; j++) {
    DataCell[] cells = new DataCell[nrColumns];
    for (int i = 0; i < nrColumns; i++) {
        cells[i] = new DoubleCell(i * Math.PI);
    }
    DataRow row = new DefaultRow(
    new StringCell(“RowKey_” + j, cells);
    buf.addRowToTable(row);
}
buf.close();
BufferedDataTable table = buf.getTable();

Example #3:

	protected BufferedDataTable[] execute(
		BufferedDataTable[] inData, ExecutionContext exec)
		throws Exception {
   	DataTableSpec inSpec = inData[0].getDataTableSpec();
    	ColumnRearranger rearranger = createColumnRearranger(inSpec);
    	BufferedDataTable outTable = exec.createColumnRearrangeTable(
    		inData[0], rearranger, exec);
    	return new BufferedDataTable[]{outTable};
}

private ColumnRearranger createColumnRearranger(
		DataTableSpec spec) throws InvalidSettingsException {
	// check user settings against input spec here
	// fail with InvalidSettingsException if invalid
    	ColumnRearranger result = new ColumnRearranger(spec);
	// the following code appends a single column
	DataColumnSpecCreator appendSpecCreator = 
		new DataColumnSpecCreator(newName, StringCell.TYPE);
	DataColumnSpec appendSpec = appendSpecCreator.createSpec();
    	result.append(new SingleCellFactory(appendSpec) {
		public DataCell getCell(final DataRow row) {
			// perform calculation based on input row
			DataCell resultCell = new StringCell( ... )
			return resultCell;
		}
    	});
	return result;
}