How to set default value of a variable


I have a branch where a variable may be not exists, if not exists it must be set to 0.

How can i do this job with a java edit variable? i have the problem of the replace flag

This is a fun one...

So it sounds like you already discovered that the Java Edit Variable (but only the non-simple variant) offers methods to help you, namely the getFlowVariable(name, type) and the flowVariableExists(name) methods and that the only thing missing is a way to set the "replace" flag. But this flag is a setting like any other setting, so you can set it on the Flow Variables tab! To do that you obviously need another "boolean" flow variable first that you can use. (Boolean is quoted because, as always, you actually need a String value containing "true" or "false".) And you can get this second variable ... in a second Java Edit Variable! (again with the flowVariableExists(name) method)

But I'm curious: Under what circumstances do you need this behaviour? The catch nodes should be fixed by now (at least in this regard), so are there other broken nodes? Or are your variables extracted from tables? In the latter case, something like Table Validator and Missing Value together might be a simpler, more visible, and more "natural" way than juggling flow variables.

Thanks, your solution works fine.

My use case consist of create a metanode that all of the beginner users of knime can copy without to imperatively define workflow variables.

That sounds reasonable. Although I suspect that Quick Form nodes may be a more user friendly alternative.

I've also thought of a simpler solution in the meantime: you can define different variables with the same name in different branches without conflict. If you then merge those branches, only one variable will be actually used. So just add another "dangling" branch and merge it into your main branch in a clever way to exploit the overwrite-rules. E.g. later definition overwrite earlier ones, so just let an optional outside definition come in after your default definition. Or: Merge Variable nodes will prefer the upper inputs, so put your definition below an outside definition.

@Edit: I just found that the upper-before-lower rule of Merge Variables only works with respect to the topmost port, not between the lower ones. Probably a bug. So if you want to exploit this trick, maybe go with the later-overwrites-earlier rule, as that is probably far more reliable.