9.2.1.1. Example 1

<< Click to Display Table of Contents >>

Navigation:  9. Anatella for the Expert Users > 9.2. About the integration of Javascript, R, Python inside Anatella > 9.2.1. JavaScript Integration inside Anatella >

9.2.1.1. Example 1

 

This is the simplest JavaScript that you can write:

 

AN76E5~1_img107

 

This script simply "passes by" the rows from the input pin to the output pin without doing any treatment. This script is simply a "Basic Template" that you can "tweak" to create your own, complex scripts.

 

The script returned to the Anatella Engine must have at least two functions: “init()” and “run()”, otherwise the JavaScript won’t work. The “parallelRun()” function is facultative: if It’s missing, Anatella assumes the following: “function parallelRun(v) { return false; }”. See section 9.9.1. for more information about this subject.

 

The “init()” function is called only once, to allow you to initialize the variables used inside your script. The “init()” function must return 0 if the initialization went fine. If the initialization failed (for whatever reason), the “init()” function must return 1. Once the script is initialized properly, Anatella will execute the “run” function several times. Anatella will keep executing again and again the “run” function while the “run” function returns 0. If the “run” function returns 1, it means that the transformation script completed successfully. You can also stop the execution of your script with an error message, using the “throw(“message”)” function that throws an error to Anatella.
 

Basically, a JavaScript Action reads rows from its input pins (on the left of the “box”) and writes rows to its output pins (on the right side of the “box”). The two global functions to “read rows” from the input pins are:
 

1.getCurrentRow(inputPinIndex)

2.getNextRow(inputPinIndex)

 
These two functions are returning “row” objects. You can thereafter write these “rows” to any output pin, using the method “write(outputPinIndex)” of the row object.

 

The syntax-highlight-system of Anatella allows you to directly check the correct syntax of your scripts, before even compiling them. A keyword that has the proper syntax will turn dark blue. If a keyword stays black, it’s a syntax error.

 

Before writing anything on an output pin X (before any call to the “write()” method of the “row” object), you must define the size of the output pin X (i.e. the number of columns inside the table associated with the output pin X). To define the size of a specific pin, use the global function “setOuputRowSize(outputPinIndex ,number_of_column)”.

 

Let’s go back to the above example. We have, inside the “initialization” function:

 

AN76E5~1_img108

 

 

On line 3: We get the first row available on input pin 0. (if no parameters are given to the “getCurrentRow()” function, then it’s is assumed that we want a row from pin 0). This first row is stored into an object named “r”.

 

One line 4: If the table on input pin 0, does not contain any row, then the “getCurrentRow()” function returned a row of the “null”-type, to signal that no additional rows are available. Thus, we test if we get a valid row (i.e. a non-NULL row) and use the global function “setOuputRowSize” to signal to Anatella that the number of columns on the output pin 0 is “r.nColumn”.  “nColumn” is a property of the “row” object that give you the number of columns that the “row” contains.

 

To summarize, in the “initialization” function, we simple “say” that the size of the output pin 0 (i.e. the number of columns of the table associated to the output pin 0) is equal to the size of the input pin 0.

 

Let’s now examine in more detail the “run” function:

 

 

AN76E5~1_img109

 

 

On line 10: We get the next row available on input pin 0. (if no parameters are given to the “getNextRow()” function, then it’s is assumed that we want a row from input pin 0). All the rows of an input table are accessible sequentially, one row-at-a-time, using the “getNextRow()” function. This new row is stored into an object named “r”.

 

On line 11: If the table on input pin 0, does not contain any more row (i.e. we have already read all the rows and we have now arrived at the “bottom” of the input table), then the “getNextRow()” function returned a row of the “null”-type, to signal that no additional rows are available. If now more rows on input pin 0 are available, than we return 1, to indicate to the Anatella Engine that this script completed successfully.

 

On line 12: We use the method “write()” of the object “r” to write our row “r” on the output pin 0 (If no parameters are given to the “write()” method, then it’s is assumed that we want to write to the output pin 0).

 

On line 13: We use the global function “writeEOL(outputPinIndex)” to signal to Anatella that the computation of the row for the output pin 0 is complete: Anatella can take the “completed” row on the output 0 and send it to the next Action. At each execution of the “run()” function, you can call maximum one time the function “writeEOL()” for each output pin. In other words, each execution of the “run()” function produces zero or one row on each output pin. The name of the “writeEOL()” function is the contraction of ‘”Write-End-Of-Line”. In opposition, at each execution of the “run()” function, you can call as many times as you want the “getNextRow()” function (to read from the input pins as many rows as you want).

 

When Anatella executes the “writeEOL(0)” function, Anatella checks if the row on the output pin 0 has been properly initialized. For example, if you did setup an output pin of size 10 and you did only “write” to this pin 9 columns, then Anatella will stop the execution of the script with an error message. This “sanity check” allows easily detection of coding errors and it’s also important to ensure consistency of the output table.

 

On line 14: We return 0 to indicate to Anatella that the transformation must continue because there are (most certainly) more rows to process.