The above described methods are only one step to check whether the node is executable with the current settings. It is also very important to check whether or not it might work with the incoming data table. This is accomplished by the configure method. The configure method is called as soon as the inport has been connected. In the small example of our numeric binner, a check is performed to see if at least one numeric column is available and if the incoming data table contains a column with the selected column name. otherwise the node is not executable. The DataTableSpec contains the required information and is passed to the configure method.
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs)
throws InvalidSettingsException {
boolean hasNumericColumn = false;
boolean containsName = false;
for (int i = 0; i < inSpecs[IN_PORT].getNumColumns(); i++) {
DataColumnSpec columnSpec = inSpecs[IN_PORT].getColumnSpec(i);
if (columnSpec.getType().isCompatible(DoubleValue.class)) {
hasNumericColumn = true;
}
if (m_column != null
&& columnSpec.getName().equals(
m_column.getStringValue())) {
containsName = true;
}
}
if (!hasNumericColumn) {
throw new InvalidSettingsException(
"Input table must contain at least "
+ "one numeric column");
}
if (!containsName) {
throw new InvalidSettingsException(
"Input table contains not the column "
+ m_column.getStringValue()
+ " . Please (re-)configure the node.");
}
...
Just as we rely on the incoming specification of the data, the successor nodes also require information about the data format, which is provided after execution. For this reason, a specification for the output of our node must also be created in the configure method.
...
DataColumnSpec newColumnSpec = createOutputColumnSpec();
DataTableSpec appendedSpec = new DataTableSpec(newColumnSpec);
DataTableSpec outputSpec = new DataTableSpec(inSpecs[IN_PORT],
appendedSpec);
return new DataTableSpec[]{outputSpec};
...
Since a DataColumnSpec must be created for the newly appended column in both the configure and the execute method, the code for the creation of the DataColumnSpec is extracted in a separate method:
private DataColumnSpec createOutputColumnSpec() {
DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(
"Bin Number", IntCell.TYPE);
DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(
new IntCell(0), new IntCell(m_numberOfBins.getIntValue() - 1));
colSpecCreator.setDomain(domainCreator.createDomain());
DataColumnSpec newColumnSpec = colSpecCreator.createSpec();
return newColumnSpec;
}