Hi all,
I'm unsure how data grids are supposed to be initialised in Intelligent Decisioning. Working in a ds2 code file, I declare my data grid in the variables tab and define the columns as such:
We have 3 columns defined: `str1`, `dec1`, and `int1`.
My ds2 code is as follows:
package "${PACKAGE_NAME}" /inline;
method execute(in_out package datagrid my_datagrid);
dcl varchar col_name;
dcl int num_cols;
dcl int num_rows;
dcl int i;
num_cols = DATAGRID_COLUMNCOUNT(my_datagrid) ;
put num_cols=;
do i = 1 to num_cols;
col_name = DATAGRID_COLUMNNAME(my_datagrid, i);
put col_name=;
end;
DATAGRID_ADDROW(my_datagrid, 1);
num_rows = DATAGRID_COUNT(my_datagrid);
DATAGRID_SET(my_datagrid, 'str1', num_rows, 'abc');
DATAGRID_SET(my_datagrid, 'dec1', num_rows, 3.14);
DATAGRID_SET(my_datagrid, 'int1', num_rows, 123);
end;
endpackage;
When I score this as a scenario test I expect to see `num_cols=3` and the three column names printed to the log.
What I actually see is num_cols=0 and the following error repeated for each column:
ERROR: Line 30: Datagrid my_datagrid_grid.columnIndex(): Datagrid Column Name not found: 'xxx'
I understand that I can use the various Data Grid functions to initialise the columns, so I could add the following to my code:
DATAGRID_ADDCOLUMN(my_datagrid, 'str1', 'string');
DATAGRID_ADDCOLUMN(my_datagrid, 'dec1', 'decimal');
DATAGRID_ADDCOLUMN(my_datagrid, 'int1', 'integer');
But if I have to do this, then what is the point of defining the columns in the variables tab? I feel like defining the columns in the variables tab should negate the need to define them a second time in the code file, right?
Additionally, If we define the data grid as both an input and an output then we get inconsistent behavior depending on if the data grid passed as input has it's columns defined or not. So to solve that you first need to check if each and every column exists and then add the column if it doesn't. Like so:
if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'str1')) then do;
DATAGRID_ADDCOLUMN(my_datagrid, 'str1', 'string');
end;
if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'dec1')) then do;
DATAGRID_ADDCOLUMN(my_datagrid, 'dec1', 'decimal');
end;
if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'int1')) then do;
DATAGRID_ADDCOLUMN(my_datagrid, 'int1', 'integer');
end;
This does not seem intuitive at all. What am I missing here?