question

Paula Carneiro Martins avatar image
0 Likes"
Paula Carneiro Martins asked Paula Carneiro Martins commented

How can I check if the string contains a function for executecell?

Hello, I am using the executecell function for a table. As my goal is make my code generic for the user can put information in diferent data types such as number, string or function.

I was trying using the executecell, but when I use the executecell in a normal string as "paula", there is an error

"

FlexscriFlexscript Error MODEL:/Tools/GlobalTables/GlobalTable1>variables/data/Row 1/String Line 1 Undefined variable paula being used.

Flexscript Error MODEL:/Tools/GlobalTables/GlobalTable1>variables/data/Row 1/String Could not resolve correct operator for construct operation. Left side type is Variant&, right type is (invalid)

Flexscript Error MODEL:/Tools/GlobalTables/GlobalTable1>variables/data/Row 1/String

Could not finish parsing because of previous errors.

"


I've tried set the flexscript data type, but the same error occurs.

Is there a possible way to check if the string is executable? We are thinking about put the string to start with an prefix to avoid to execute an undefined variable. Is there a charmier way?

Executecell_example_v01.fsm

FlexSim 21.0.2
flexsim 21.0.2stringexecutecell
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Matthew Gillespie avatar image
1 Like"
Matthew Gillespie answered Paula Carneiro Martins commented

That's what the FlexScript data type is for. If the cell or column has an executable string, then set the data type to be FlexScript. If the cell or column has a string that you don't want to be executed, then set the data type to be string. The table bracket operators check if the cell has FlexScript data before calling execute on the cell, so your code can just be:

//Table reference
Table gt_table = Table("GlobalTable1");
Table gt_tableoutput = Table ("GlobalTable2");

//Loop for all columns
for(int i = 1; i <= gt_table.numCols; i++){
    gt_tableoutput[1][i] = gt_table[1][i];
}

executecell-example-flexscript.fsm


· 5
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Paula Carneiro Martins avatar image Paula Carneiro Martins commented ·

Mathew, I see your point!

My doubt is about how classified the data. Because in this case, I knew that the column 2 is a normal string and the column 3 is a distribution.

But I want to has a code to check if the value is a normal string or not. Because the user will import the table from excel, and the columns can have all data types.
So, I am going to loop for all columns, and I need to know which cell I need to change the type for flexscript (or use execute cell) or not for a general table.

Is there any way for checking that?

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Paula Carneiro Martins commented ·

Are these distributions global or to be shared by many objects? If its the latter how are you specifying the stream? Execute cell will not allow you afaik to pass in the involved object so having getstream(i) within the expression would not be possible - better to use executefsnode in this case.

If you know column 3 is a distribution, why do you need to test it?

If you don't know that, and you're implying that the table could be any format and columns can be in any locaiton, then can you agree a column header name and use that to reference the cell instead of a numeric value?




-1 Like -1 ·
Paula Carneiro Martins avatar image Paula Carneiro Martins commented ·

@jason.lightfoot and Matthew Gillespie , Label_example.fsm

The original idea is about create item labels with the generic proposal. So, the labels values in the table can be string, number, or distributions, for example.

The columns headers are going to be label names and the cell its values. I am trying to have a general code for insert the label value in the item.
So, the user can put all datatype of labels I need to know which I can execute cell for avoiding the error in the model attached


Using this code :

/***tag:snippet*//**/

Table gt_info =Table("GlobalTable1");


//Define row number

int int_row = token.item.Type;


// Assign all labels in the item

for(int j=1;j<=gt_info.numCols;j++){

//Label name

string str_labelname = gt_info.getColHeader(j);

//Set value

//HERE I would like a condition for execute or just copy the value

token.item.labels.assert(str_labelname).value = gt_info.executeCell(int_row,j);


Using a specific prefix in the column header works as a solution, but I was trying to check if there is a function as "objectexist", for example, for checking if the string is or not a function . Or other possibility.

0 Likes 0 ·
1613746452467.png (10.5 KiB)
label-example.fsm (32.8 KiB)
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Paula Carneiro Martins commented ·

@Paula Carneiro Martins

There's not a simple way to know 100% if a string is executable. However, here's an idea that works reasonably well - check if the cell has string data and if it has a parenthesis "(". This will catch cells with commands or FlexScript methods. It can also catch mathematical formulas if you wrap the whole thing in parentheses "(2.2 + 1.7) ".

Table table = Table("GlobalTable1");

//Define row number
int row = token.Item.Type;

// Assign all labels in the item 
for (int i = 1; i <= table.numCols; i++) {
    string labelName = table.getColHeader(i);
    Variant cellValue = table[row][i];
    if (cellValue.type == VAR_TYPE_STRING && cellValue.includes("("))
        cellValue = table.executeCell(row, i);
    token.Item.labels.assert(labelName).value = cellValue;
    
}

CheckParenthesis.fsm

1 Like 1 ·
1613757935512.png (18.8 KiB)
Paula Carneiro Martins avatar image Paula Carneiro Martins Matthew Gillespie ♦♦ commented ·

Thank you Matthew, It works as we wanted!


It solves perfectly for the distribution, usercommands, FlexSim commands, and math calculations. The only precantion that the user needs to avoid the parenthesis in the string value . Really thanks!

0 Likes 0 ·