I’m not very conversant in Java, so I need some help with syntax and possibly which node would be best to use.
At the beginning of my workflow I have a Single Selection Configuration that defines a string variable called ‘PlateFormat’. It’s possible values are ‘96w’, ‘384w’, and 1536w’. At the end of my workflow I need to write a csv file with a header that needs to incorporate information based on this ‘PlateFormat’ variable. But instead of the strings above, the variable content needs to be changed as follows depending on the content (in sort-of pseudo code):
CASE PlateFormat OF
96w: replace “96w” with “# PlateFormat\t 8\t 12”
384w: replace “384w” with# PlateFormat\t 16\t 24
1536w: replace “1536w” with # PlateFormat\t 32\t 48
ENDCASE
There are probably many ways to go about it, and I had some partial success using the String Manipulation (Variable) node using this syntax:
But adding other lines for the remaining two cases leads to an error (maybe not too surprisingly). I assume, the Java Edit Variable node is probably most appropriate here, but I read somewhere that case statements with variables are not allowed in Java. So, does this have to be accomplished with nested if-then statements? In either case, I don’t know the proper syntax to implement this and would appreciate some help.
Hi @pipetman, as @rfeigel suggests, the Column Expressions node (or for variables, its Variable Expressions counterpart) is better suited for writing programmatic scripts and may be more straightforward, or at least easier to write!
String Manipulation and String Manipulation (Variable) can only accept a single statement, but you can achieve what you need using string replacement by “nesting” the replace statements:
The downside here is that because it is a replace statement, it will replace substrings too, and if it found the value “196w” it would still end up replacing the “96w” part. This may be undesirable.
Alternatively, String Manipulation (Variable) does have a conditional if-then-else syntax, but it looks a little odd. In this example it would replace only exact matches for the whole value:
Thanks a lot! This is fantastic and works like a charm.
I actually opted for the String Manipulation (Variable) node, as it doesn’t require an extra extension (I’m behind a corporate firewall and installing updates and extensions is a bit of a process, so whenever possible I opt for the ‘pure’ Knime experience).
The potential substring match is not really an issue, as these microtiter plates only come in limited formats.
You’re welcome @pipetman.
What I should have mentioned, the simpler option for this would generally have been the Rule Engine (Variable) node, except that I don’t think the tab characters would work for you, assuming you want actual tabs!
But the rules themselves would be more straightforward:
To get the tabs corrected, I think this could be followed by a String Manipulation (Variable) node with the following (odd looking) script:
regexReplace($${SPlateFormat}$$,"\\\\t","\t")
So it’s an additional node, but if you had a lot of replacements to do, it may be beneficial.
Finally, you could also do this using a lookup table of replacements, and use the Value Lookup node to pick them. Again this can be beneficial with a larger number of replacements, and can be easier to maintain.
I had an inkling that there were a myriad ways to archive this, and that the Rule Engine would be among them (thanks again!). I could have probably figured out the syntax for the replacement, but the last line with the TRUE statement would have probably eluded me (I assume, it updates the PlateFormat variable).
I have used Knime for a quite a few years now and could intuitively or via tutorials figure out many things, but as your examples above show, the syntax to do more advanced things can be to be somewhat arcane. Is there a comprehensive documentation that one could digest to get a better understanding of those nodes that require some code input, or does that ‘just’ require a deep familiarity with Java?
Hi @pipetman , the difficulty I think that many people face with the syntax is that there isn’t a common syntax across all the “script” style nodes, so basically you have to learn each one. Some nodes have a java or javascript-style syntax, others (like Rule Engine) have a syntax all their own.
The free ebook “Beginner’s Luck” (updated for KNIME 5.2) does have some examples and explanation of some syntax in it:
But much of the detailed learning for KNIME comes from taking a look at the example workflows such as
and taking a look at courses,(both free self-paced, and instructor-led) at
One day I might actually complete one of those courses myself, and who knows maybe actually do an exam to see if I understand any of this stuff!
The Rule Engine syntax that you mention here is basically a statement, with each line representing a condition and a simple outcome. (I say “simple” because the outcome of Rule Engine cannot be anything “fancy”, like string conversions, or concatenations. The outcome is always a literal value, or a single column or variable value.)
It can be thought of as this:
IF ... THEN ...
ELSE IF ... THEN...
ELSE IF ... THEN...
ELSE IF ... THEN...
What the rule engine actually does is evaluate the conditional statement that is to the left of each => in turn, and the first one that equates to TRUE is used to determine the outcome as being the value to the right of that =>
So, if none of the conditions prior to the last statement is TRUE, then the condition TRUE on the final line is inspected and of course this equates to TRUE and so it returns the value found in $${SPlateFormat}$$ unchanged.
Had that final TRUE line not been present, then if the Rule Engine got to the end of the set of rules with no line equating to TRUE it would have no value to return and so it would return “missing value”.
This can trip people up, but can also be a useful feature to make use of. Suppose you wanted to replace the value 0 in a column with “missing value” (also known as null)…