Knime - If Switch (Conditions)

Hi! Is it possible to set-up a condition where it allows switches to flow on both sides? Thanks!

Hi @JohnAndehl and welcome to the forum.

The “both” option in the dialog will allow for both output ports to remain open. Is this what you need?

2 Likes

What I’m actually trying to do is to set-up a rule where if it satisfies this one condition, it would only open one port. Otherwise, the both output ports would remain open.

image

Hi @JohnAndehl

I believe that Scott is hinting at the If-Switch rather than the CASE switch since the If-Switch has the option to go top,bottom or both:

image

Maybe below setup can give you some inspiration:

Let’s assume the following data:

image

By applying a rule engine, I determine whether each row should go in the switch:

image

If you only want to go top or both, then use

$PARAM1$ = "AA" => "top" 
TRUE => "both"

With the direction now established, I pass this ‘direction’ value as flow variable to an If-Switch (that will control the chosen Port Index.

image

For this example, I just apply a static value to verify if the row went top, bottom or both (via the Constant Value Column).

After the rows have been processed, it can be observed that “CC” was processed by top and bottom individually in accordance with it’s PortIndex being set to Both.

image

See WF:
if switch loop.knwf (44.9 KB)

Hopefully this helps in any way.

3 Likes

Hi @ArjenEX , this is very helpful. However, I’m trying to create a switch for paths. Is it possible to have path ports (instead of data ports) in the “If-Switch” Node ? Thank you!

Hi @JohnAndehl

Not to my knowledge. But you can circumnavigate this quite easy. I’m still not sure what your full use case is but assumingly you want to process files in a certain way that fulfill a particular condition based on the path.

Changes I made:

  • Get all files from a directory (random example).
  • Convert the Path to a string equivalent.
  • Apply a rule to the string path (for example: all rows that have a csv file extension should go top, the rest goes bottom).

The rest is similar as before. At the end, all csv’s went top and the rest (meta,sqlite,xls and table) went bottom. You still have the original Paths available as well.

3 Likes

Got it! Thank you for this.

2 Likes