Check if flow variable exists - Part 2

Hi,

I try to check if a Variable exists (which it does depending on the data input) in the workflow via the Java Edit Variable node using the “flowVariableExists(var)” method as suggested here:

But I dont get it to work, so obviously I am missing something. Here is the script of the “Java Edit Variable”:

if (flowVariableExists(v_Row0)){
	out_var = "NA";
}
else{
	out_var = v_Row0;
}

I also tried the typeof operator (function - JavaScript check if variable exists (is defined/initialized) - Stack Overflow) with the same result - not working.

I may have a solution using the “Python Edit Variable” node that is working so far:

try:
	flow_variables['Row0']
except:
	flow_variables['var'] = "NA"
else:
		flow_variables['var'] = flow_variables['Row0']

I was just wondering what is wrong with the Jave code.

Would be nice if somebody could provide a solution for the Java Edit Variable Node.

Best

A.

Hi @Anjo , what do you mean by “I dont get it to work”? Is there any error, or does it work by you get unexpected results?

You probably want to reverse your logic, in that it should be more like:

if (flowVariableExists(v_Row0)){
	out_var = v_Row0;
}
else{
	out_var = "NA";
}

I put something together to test this. I created 3 variables:
image

variable_1, variable_2, and variable_4

And I want to check if any of these flow variables exist:
image

This is the result:
image

This is what I used in the Java Edit Variable:

if (flowVariableExists(v_variable_to_test)){
	out_var = "Exist";
}
else{
	out_var = "NA";
}

You can also do individual check without a loop with hardcoded values:

if (flowVariableExists("variable_1")){
	out_var = "Exist";
}
else{
	out_var = "NA";
}

My sample workflow looks like this:
image

Here’s the workflow: Check if flow variable exist.knwf (17.8 KB)

5 Likes

Your python solution works. Is there any problem using that?
br

Well, for the rest of the workflow I used the Java Edit Variable Node to modify variables. So consistency would be one thing.

And there is a recommended solution for this particular problem for the Java Edit node that does not work for me and I am curious why.

But yes I am using the Python node instead.

Hi @Anjo , the function flowVariableExists() takes the name of the variable to check. It takes the name as a string.

So, in your case, you want to check for:
flowVariableExists("v_Row0")

You can refer to the “Individual variable check” part of my workflow for that.

If you want to pass a variable to it, the variable should contain the name of the variable to check.

For example, you can define a variable called variable_to_check and it can hold “v_Row0”.

So, if you have variable_to_check = "v_Row0", you can then pass it to the function:
flowVariableExists(variable_to_check)

You can refer to my “Multiple variable check” part for that. In my loop, I am checking for each of the values that’s coming from that Table Creator, which are “variable_1”, “variable_2”, …, “variable_5”.

You are seeing “variable_5” because it’s at the end of the loop, so that’s the value of the last iteration. But for sure it went through all of them, “variable_1”, and then “variable_2”, etc…, to “variable_5”, and it checked if each of them existed or not.

That is how it produced this result:
image

And you saw from this window that only variable_1, variable_2 and variable_4 existed:
image

So, the results match the reality.

I’m not sure I understand what’s the issue you are facing.

If you were to look at how the loop worked, let’s first understand that this function is always executing flowVariableExists(variable_to_check) within the loop.

On the 1st iteration of the loop, variable_to_check will be assigned “variable_1”, so
flowVariableExists(variable_to_check) will in fact run flowVariableExists("variable_1")

On the 2nd iteration of the loop, variable_to_check will be assigned “variable_2”, so
flowVariableExists(variable_to_check) will in fact run flowVariableExists("variable_2")

etc

And on the 5th iteration of the loop, variable_to_check will be assigned “variable_5”, so
flowVariableExists(variable_to_check) will in fact run flowVariableExists("variable_5") (hence why yuo see “variable_5” at the end of the loop)

3 Likes

Hi @Anjo , ok now I understand what you are trying to do and what the issue is.

You won’t be able to assign this value in Java, since it is still referring to the variable inside the node. You need to do this in 2 steps with an if switch node. With the if switch, you can still reference a non-existing variable in the sense that the if switch will decide whether or not to execute that node.

So basically we still use Java to check if the variable exists. After that we split into 2 routes:
Route 1: var = v_Row0
Route 2: var = “NA”

If the java code returns that variable v_Row0 exists, then take Route 1, else take Route 2.

So, in the event that v_Row0 does not exist, it will take Route 2, hence never executing Route 1.

I put something together for you for this:
image

I have set some values to my variables so we can see some values in the results:
image

image

I’ve done 2 cases in the workflow, one where the variable exist, and one where the variable does not exist. And I applied the rule that you want, that is if variable exist, var = the value of the variable, else var = “NA”.

Workflow looks like this:

In the first scenario, I’m checking for variable_3, which does not exist. So we expect var = “NA”. Results as expected:
image

In the second scenario, I’m checking for variable_1, which exists. So we expect var = variable_1 = “value_of_var_1”. Results as expected:
image

Here’s the workflow: If variable exists then use it else use NA.knwf (24.6 KB)

EDIT: Some additional information for you. Both scenarios are using the same logic, they’re just 2 different test cases. So, in both cases, the top route of the if switch (Node 14 and Node 12) references to the variable, and the bottom route (Node 11 and Node 13) assigns “NA”.

As you can see in the first scenario, the top node (Node 14) is “disabled”, so it is never executed. The node references to variable_3 which does not exist, but it does not matter as it is never executed.

1 Like

I just realized something. Since we’re using Variable Expressions already, and that Variable Expressions does not point directly to a variable, but rather retrieves the values of a variable via the variable() function, we can do all the if operations in the Variable Expressions directly without any issue.

The workflow becomes as simple as this:
image

As usual, I create 2 scenarios:
image

And we get the same results as before - we’re using the same logic, just doing everything via the Variable Expressions.
First scenario:
image

Second scenario:
image

Here’s the new workflow: If variable exists then use it else use NA.knwf (12.8 KB)

3 Likes

(sorry had to delete the original post because of minor conidential info). The following is the response to: Check if flow variable exists - Part 2 - #6 by bruno29a

Hi Bruno,

thanks a lot for your clarification. I think I get what your workflow is doing in both cases.

I think my problem is the following (see my workflow Variable_Exists.knwf (48.4 KB)): I can assign v_Row0 to the flowVariableExists() as a string (“v_Row0”) as you said (flowVariableExists(“v_Row0”) but the Java Edit Variable Node still complains because I am trying or better need to assign v_Row0 (or the value that v_Row0 holds) to out_var in the if part of the statement (the v_Row0 variable contains a value that changes so I cannot hard code it in the if statement part) even if the else part is executed.

In your case you assign the string (“Exist”) that of course always exists. Try to replace “Exist” with v_variable_5.

I tried to assign “Row0” to v_variable_to_check and the the value of var Row0 to v_variable_to_check_value but then the error ocurse in the “Variable Creator” node where I assign the value of var Row0 to v_variable_to_check_value if var Row0 does not exist.

Variable Row0 exists:

Variable Row0 does not exist:

Sorry, I seem to be stuck a little bit. I will think about it.

It is not that import but maybe nice for anybody else who may faces a similar knot in the head.

Again thanks a lot Bruno. I think your solution brought me closer to what the actual problem is.

Best

A

Hi Bruno,

yes great. I think that is the solution to my question! I haven’t used the “Variable Expressions” node before.

Thanks for the explanations.

Best

Andreas

Hi @Anjo , no problem.

The Variable Expressions, as well as the Column Expressions (Variable Expressions for manipulating Variables, and Column Expressions for manipulating table Columns) is mainly based on Javascript (with some additional functionalities), and they allow you to implement logic/rules with manipulation.

Rule Engine nodes (Rule Engine, Rule-based filter/splitter, etc) allow you to apply logic/rules, but with no manipulation, and String Manipulation or Math Formula allows manipulation but you cannot apply logic/rules.

The Variable/Column Expressions allows you to do both. Moreover, as you saw, you don’t reference/point directly to a variable/column, so there is no complaining at run-time by Knime in your case for what you need to do.

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