Convert code from "Column Expressions" to "Java Snippet"

Hello, @takbb how are you?
Sorry to bother you.
I would like some help to learn how the code I did in “Column Expressions” would look if it were created in “java snippet” code would be done in java snippet.

For me to understand how the logic works in Java snippet, it’s easier if it’s with some data that I’m familiar with.

But take your time, you can look at it whenever you’re free. It’s just for my learning.

I’m providing a workflow already with the rules executed.
The columns in “blue” are the values ​​from the database, the columns in yellow are the ones created by “column expressions.”

About the file: it’s just an inventory control, product cost, where the initial balance is 57 units, with a total value of 6726.52 and a unit value of each piece of 118.01 (first row).

Then, inventory control is done where code 1409 means a goods entry, so 57 +1 is added, and the value of 146.01 is added to the total.

(In the “@Qtd_Consumo” column, what is negative is stock output and what is positive is stock input)

In the example file, the result is already there, and by generating the flow again, the columns are created again in “orange”.

(I tried to keep it as simple as possible. In fact, it was you who helped me build the “column expressions” formula, but I know you have a lot of knowledge in Java and I had this pending task of using this example and trying to ask for your help to build it in Java, so I can understand and learn how to better understand the node).

You can use any java snippets you have to…


Change_to_java_snippet.knwf (26.5 KB)

1 Like

Hi @Felipereis50 , I will take a longer look when I have some time. And of course if anybody else wants to do the conversion, feel free to give it a go!

I haven’t fully inspected each of the Column Expression nodes, but I think they could probably all be merged into a single Java Snippet, or would you prefer them to remain as three Snippets (one for each Column Expression) ?

One notable difference in the two is that the rowindex of the first row in Column Expressions is 1 whereas for Java Snippet it is 0. Beyond that, for the most part the port from one to the other can be done reasonably methodically.

Here’s the first Column Expression, to be going on with:

var vInitial

switch (rowIndex()) 
{
 case 1:
        vInitial=column("Saldo_Inicial");
        column("Saldo_Inicial");    
 break;
 default:
    vInitial=vInitial + column("@Qtd_Consumo");
}

This could be written as the following Java Snippet

// Your custom variables:
Integer vInitial;

// Enter your code here:


switch (ROWINDEX)
{
	case 0:  // first row in JavaSnippet is 0 whereas first row in CE is 1
		vInitial=c_Saldo_Inicial;
		break;
	default: 
		vInitial += c_Qtd_Consumo;
}

out_qt_saldo_aggreg = vInitial;

I named the output column in this screenshot “qt_saldo_aggreg_js” as there was already the “qt_saldo_aggreg” column.

I’ll work through the others when I have some more spare moments, and upload the flow.

1 Like

Thanks Brian (for your time) :sunglasses:

(for the Java snippet, if you only need 1 node to the task, there’s no problem to use one)

You can see the others columns with no hurry.

(when I get back home, I will analyze it this first part)

:+1: (thanks again)

1 Like

Hi takbb

I’m sorry for being late. I was on vacation.

One interesting point of your code is the difference between this two lines:

default: 
		vInitial +=  c_Qtd_Consumo;

                     versus

default: 
		vInitial =  vInitial + c_Qtd_Consumo;

The result is the same and I google it, and It's only a sytax form.

Analyzing the code. It’s very similar to column expressions.
Some differences are the need to create the columns that will be used in INPUT and OUTPUT.

I need to train more.

In column expressions, I can create several columns in the same node by clicking on the +.
I believe that in the java snippet, it is also possible, using OUTPUT. Right?

One question I have is: is it possible to use the result of a calculation from a column that was created in another that is being created in same node?

Example: in column expressions, I need to use two nodes. Imagine that I use a node, with 1 column created using +, that results are the word “Yes or No” and I need to materialize this result in this new column. Until here, all good.
Then I need to create another column that validates “if it is “yes” then…”
But I can’t create this column on the same node, I need to use a second node column expressions
The second column is dependent “in name” on the result of the first column, and column expression can’t relate. The second column needs the “name” of the first column to find, but the “Col Exp” doesn’t find the created column yet.

In java snippet, can I do this using the same node? What I mean is, can I create two columns in the same node, that the result of the second column, is dependent of the result of the first.
Do you have any example in your mind that can demonstrate this situation?

I have no problem using two nodes. I always do that. But there is only a curiosity for learning.

Hi @Felipereis50 , I was thinking only the other day that I have yet to look at your other code examples with a view to translating to java snippet!

Quick answer to your question is yes, this is a fundamental difference between java snippet and column expressions.

With column expressions , the expression results in a single output and to produce multiple column outputs you require multiple expressions (even though they may be within the same node).

With java snippet, the one node contains a piece of code that can give rise to any number of output columns from the same piece of code and so yes it is possible to calculate these all at the same time and so an output column could indeed be based on another column’s value that is being calculated at the same time.

At the end of the java snippet, you physically assign values to all the output columns as required, whereas at the end of Column Expressions, the output is automatically directed to the single column being created.

I’m not at my pc at the moment (answering this on my mobile) but I’ll upload a simple example later to demonstrate that.

1 Like

@Felipereis50 , take a look at this workflow I’ve put on the hub:

In this workflow, it is demonstrating that the difference of the squares of two numbers a and b can be calculated as (a+b)(a-b).

In this contrived example, the aim is to provide all the columns representing the parts of the calculations in determining the difference of two squares, and produce a message containing values from the other calculated columns.

The rule for this demonstration is that no calculation may be performed more than once!

Using Math Formula, a large number of nodes are required.

Column Expressions reduces the number of nodes but cannot use a column calculated within the same node in the generation of a new column.

Java snippet can generate all output columns in the single node:

1 Like

Hi takbb.

Great example.
Very clear. :white_check_mark:

I made a test and has expected, the order matters.
What I mean is that the order of the code is needed to not break the code.

e.g
This line cannot be before the creation of the columns “a” and “b” because it depends on.

Many thanks for the explanation.
I will try to change the column expressions node to the java snippet, to slowly understanding.
But here comes another question: In column expressions, there is a list of functions that can be used. For me, it helps a lot of knowing some of them and the structure with the examples inside the box (and I realized that there is more function hidden, like “switch”, but I think it doesn’t appear because is not a function.
In java snippet, is there a list of the functions with examples? Maybe the syntax is different. or maybe I will have to deep learn some basic rules of java script

image

Thanks a lot for this starting point.

1 Like

The java snippet is actually using Java rather than Javascript (a language with a similar syntax and a similar name but is actually a very different language).

Column Expressions on the other hand is based on the javascript language.

So the Java Snippet has access to all the features of the java language, so this is widely documented on the Internet but if you’ve never used it can take a bit of getting used to. It is very powerful but would take a lot more explaining than I can do here!

I’ll have a think about what tips and pointers I can maybe come up with, as a kind of Java Snippet 101… Watch this space! :wink:

Oh, and yes the order very much matters in Java Snippet, as you have discovered.

1 Like

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