HowTo Limit SettingsModel value to the number of input rows

Hi
My custom developed node has a setting that needs to be limited to the number of rows in the input column. But I am having some issues with this:

  1. I tried to detect the number of rows when connecting the node. However, using breakpoints I did not find a function that is triggered when this happens. And even if it would be triggered, I think that the nr of rows are only known in the execute function, correct?
  2. Assuming my previous assumption is correct (the nr of rows are only known in the execute function) I tried to add a changeListener to the SettingsModelInteger that I want to limit. I followed the procedure as described here: https://www.knime.com/developer/settingsmodellistener. However, it doesnt work. Perhaps the listeners only work within the Dialog (not between dialog and model?) In the execute function I can change the SettingsModelInteger, that triggers the stateChanged function and I can see it has changed the value. But I cannot see the updated value when opening the dialog (with F6). I put a breakpoint in the Dialog code and noticed that the SettingsModelInteger value did not change here. Below are some snippets of the parts that I think are essential for this issue.

Thanks Ashgard

The Dialog:

   public class MyNodeDialog extends DefaultNodeSettingsPane  {
     SettingsModelInteger mySettings;

     protected MyNodeDialog() {
       super();

	   mySettings= MyNodeModel.createMySettingsModel();
	   mySettings.addChangeListener(new MyListener());
	   this.getPanel().repaint();
       new DialogComponentNumberEdit(mySettings, "mySettingValue", 10);
   }

     private void Repaint() { // Not sure if this is needed. But in here the value is back to the unchanged value
       this.getPanel().repaint();
     }

     class MyListener implements ChangeListener {
       @Override
       public void stateChanged(ChangeEvent e) { // This works fine
         mySettings.setIntValue(mySettings.getIntValue());
         MyNodeDialog.this.Repaint();
       }
     }
   }

and the model

   public class MyNodeModel extends NodeModel {
     public static final SettingsModelInteger mySettings = createMySettingsModel();
     
     static SettingsModelInteger createMySettingsModel() {
       return new SettingsModelInteger(KEY_RANDOM_ROW_COUNT, 0);
     }

     protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) {
       BufferedDataTable inputTable = inData[0];
       mySettings.setIntValue((int) Math.min(inputTable.size(), mySettings.getIntValue()));
     }
   }

As far as I know, there is no way to know directly from the Node Dialog how many rows you have in the input, as that is not part of the input spec, which is all you have access to. If you need the full table take a look at subclassing DataAwareNodeDialogPane:

(There are some examples in e.g. the Python scripting nodes at https://github.com/knime/knime-python/tree/master/org.knime.python.nodes/src/org/knime/python/nodes/script1in2out)

The ChangeListeners only apply in the node dialog or node model - not both - the settings are only transferred from dialog to model when you click on ‘Apply’ or ‘OK’

Steve

3 Likes

Hi @s.roughley

Thanks for your response.
I now understand why the ChangeListener is not working.
And the DataAwareNodeDialogPane indeed looks as the solution to my issue.

KR
Ashgard

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.