Creating NodeFactories at runtime

I need to create several nodes which can share plenty of common functionality and I would like to avoid creating dozens of "dumb" NodeFactory implementations. The structure could be abstracted so far, that I can share one common NodeFactory for all nodes and move all my moving parts to an IAlgorithm strategy. Basically, I have something like this:

public class MyGenericNodeFactory extends NodeFactory<MyGenericNodeModel> {
    private final IAlgorithm algorithm;

    public MyGenericNodeFactory(IAlgorithm algorithm) {
        this.algorithm = algorithm;
    }

    @Override
    public MyNodeModel createNodeModel() {
        return new MyGenericNodeModel(algorithm);
    }

    // …
}

Further, I discovered that the org.knime.core.node.NodeSetFactory interface and extension point allow me, to dynamically add nodes without having to register each of them at the extension point. Looks good so far ... the problem is, that NodeSetFactory expects me to return the NodeFactory as Class type (and not an instance):

public Class<? extends NodeFactory<? extends NodeModel>> getNodeFactory(String id);

Which is obviously not feasable in my use-case, because I need to parametrize it usign the IAlgorithm dependency injected via constructor, as shown above. Is there a mechanism which could solve my issue? As stated, I would really, relly like to avoid having to create a NodeFactory implemention for each single node.

Node factories are instantiated via reflection therefore they must have a non-arg constructor. If the factory needs to be parameterized you can override NodeFactory.loadAdditionalFactorySettings and try to set the IAlgorithm there.

1 Like

Thank you, that worked!

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