using Java IF node

Hi!

I'm trying to use the Java IF node to pass some data to one of the two output ports. I created an integer flow variable (it correctly shows up in the left window of the configuration dialogue of the java IF node) in my workflow that I want to use to create an expression that can be entered as java code in the Method body window of that IF node. So if my variable is 0 the expression should be 0 as well and the data should go to output port 1. Likewise if the variable is not zero the expression should be 1 and the input data should be passed to the output 2. Since I'm completely new to java code I wanted to ask if you could help me with some small code example. Whatever I've tried so far always gave some error message during the compiling.

The flow variable is of Integer type

Cheers!

Christian

The Java code in the Java IF node should look similar to 'return $${Iout}$$;' where out is the name of the variable (type: integer) which is either 0 or 1.

Hi Gabriel,

Thanks for your help. I'm afraid that your reply didn't help me much so far. I guess I also didn't explain the problem well.

The variable that I want to use in the IF node counts objects in an image. It can be either 0 (if there are no objects) or it can have any other value (4, 46 , 57 or whatever). I want that the expression in the IF node returns 0 if the value of the counting variable is 0 and 1 for any other value. So I assume that I have to include some kind of IF statement in the method body window of that node.

I thought of something like (sorry for my poor coding skills):

if ($$myCounter$$ = 0)  {

    int val = 0; // val would be just some internal integer variable to return 0 or 1

    }

else  {

  val = 1;

    }

return val;

Unfortunately this doesn't work. I'm sure what I write is probably just creepy or something is totally wrong with the syntax. It would be nice if you could help me out here.

 

Thanks again!

Christian

 

You are almost there: you just need to move the variable declaration outside the if-else clause or immediately return 0 or 1:

if ($${ImyCount}$$ <= 0)  {

  return 0;

} else {

  return 1;

}

Hello Christian,

 

You control the flow by setting your variable to 'top' or 'bottom'.

 

HTH,

Fred

Great, thanks! I knew it must be rather simple. It works perfectly now.

Christian

Hello,

I tried this and it doesn't work (myVal is an integer):

if ($${ImyVal}$$ = 100) {
 return 1;
}
else {
 return 0;
}

I have a type mismatch error between Integer and Boolean. Any Idea?

Equality tests in Java are done with "==" (two equal signs).

Thanks! BTW, the error line referred by KNIME was wrong (it says a line number above the number of Java lines in my code, I guess because of additional code generated by KNIME). It is thus difficult to find where does the error come from.

Hi,

I have 2 columns “LUCE” and  “LUCENum” that represent the cycle of the light (a cycle always start with one "ON", then there are a random numbers of "LIGHT", and always finish with one "OFF").

LUCE LUCENum

ON         1

LIGHT    2

LIGHT    2

OFF        3

ON         1

LIGHT    2

LIGHT    2

LIGHT    2

LIGHT    2

OFF        3

ON         1

LIGHT    2

OFF        3

("LuceNum" is equal to the first column where 1 =ON;      2 = LIGHT;     3 = OFF).

 

Using Loops I would like to create a new column called RESULT with a unique ID for each cycle (a cycle always start with one "ON", then there are a random numbers of "LIGHT", and always finish with one "OFF").

In other words the column Result will contain a counter that increase each time LUCENum  =  1.

This is the desired result:

LUCE  LUCENum  RESULT

ON         1             1

LIGHT    2             1

LIGHT    2             1

OFF        3             1

ON         1             2

LIGHT    2             2

LIGHT    2             2

LIGHT    2             2

LIGHT    2             2

OFF        3             2

ON         1             3

LIGHT    2             3

OFF        3             3

 

Attached you will find an example with several attempts of what I tried to do, but it doesn’t work.

Please could you help me? Thanks in advance