New FlowVariable API suggestion

The new FlowVariable API has some great features, but I would like to make a suggestion, which is probably only relevant in a small number of cases where the VariableType is not known until either in the code, e.g. an abstract NodeModel implementation which implementations will add a variable but the type will be determined in the subclass.

It is good practice in the NodeModel#configure() method to push place-holder variables with default values, so that downstream nodes know that the variable will exist.

In the legacy API, this was achieved with:

pushFlowVariableDouble("A double", 0.0);
pushFlowVariableInt("An int", 0);
pushFlowVariableString("A String", "");

The new API makes this more flexible, via the method pushFlowVariable(String name, VariableType<T> type, T value), but if we don’t know T then a default value is difficult to come by generically. As a simple example:

public abstract class MyNodeModel<T> extends NodeModel {

    ....

    public DataTableSpec[] configure(DataTableSpec[] inSpecs) {
        // Do some configure stuff
        // ...

       // Push a default value
       pushFlowVariable("My Variable", getType(), getDefaultValue());
    }

    /** @return the variable type */
    public abstract VariableType<T> getType();

    /** @return the default value *?
    public abstract T getDefaultValue();

}

Well, that’s OK, but VariableType already knows it’s default value -

for example, booleans default to false…

Making this method public would allow a much simpler #configure method in the above snippet:

       // Push a default value
       pushFlowVariable("My Variable", getType(), getType().defaultValue().get());

Actually, there are some alternative workarounds too, all of which might have their uses:

  • In NodeModel, pushFlowVariable(String name, VariableType<T> type, T value) does a few sanity checks and then calls pushFlowVariable(new FlowVariable(name, type, value)); This latter method is package private - making it protected visibility would mean that pushFlowVariable(new FlowVariable("My Variable", getType())); was an alternative route to default as the 2-argument FlowVariable constructor creates a variable with the default value
  • In NodeModel, a new method pushFlowVariable(String name, VariableType<T> type) which pushed the variable onto the stack with the default value

Of course, there is an alternative workaround at the moment…

T defaultValue = new FlowVariable("My Variable", getType()).getValue(getType());

But that does feel a bit… convoluted! (Rather like this post has become - sorry!)

Steve

4 Likes

Hi @Vernalis,

Cool Idea, I let the core devs know about it, maybe they include it in the next release.

best,
Gabriel

3 Likes

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