question

Julio R avatar image
1 Like"
Julio R asked Julio R answered

Add variables in Opquest by code

Is there anyway to add variables to the experimenters (first tab) without doing it manually?.

My final objetive is to import it from an excel, but I guess that if I can add them from a table I can import that table from excel, so if there is anyway to add and modify those variables without doing it manually I hope I can find the way to import it.

FlexSim 17.2.2
excelimportopquest
· 1
5 |100000

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

Jeff Nordgren avatar image Jeff Nordgren commented ·
@Julio R

Did you get a satisfactory answer to your question?

Thanks.

0 Likes 0 ·
Julio R avatar image
0 Likes"
Julio R answered

Yes, I called in and @Logan Gold help me build a code:

string tabla = "GlobalTable1";
int rows = gettablerows(tabla);
int i = 1;
int num = model().find("Tools/Experimenter/ExperimentVariables").subnodes.length;
for (int i = 1; i < num; i++) {
    destroyobject(rank(model().find("Tools/Experimenter/ExperimentVariables"), 2));
}
setnodename(last(model().find("Tools/Experimenter/ExperimentVariables")), gettablestr(tabla, i, 1));
setnodestr(first(last(model().find("Tools/Experimenter/ExperimentVariables"))), gettablestr(tabla, i, 1));
for (int i = 2; i <= rows; i++) {
    createcopy(last(model().find("Tools/Experimenter/ExperimentVariables")), model().find("Tools/Experimenter/ExperimentVariables"));
    setnodename(last(model().find("Tools/Experimenter/ExperimentVariables")), gettablestr(tabla, i, 1));
    setnodestr(first(last(model().find("Tools/Experimenter/ExperimentVariables"))), gettablestr(tabla, i, 1));
}
maintree().find("project/exec/optquest/definitionLogic/updateOptQuestVars");
5 |100000

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

Mischa Spelt avatar image
0 Likes"
Mischa Spelt answered Julio R commented

Hi Julio. The code in your comment looked a bit un-FlexSim 17.2-y so I took the liberty of refactoring it and then found some room for optimization (no pun intended). I cannot guarantee that it does exactly the same as your code, but two problems with what you posted seem to be:

* The variable name and value are set to the same string (gettablestr(tabla, i, 1)) - I assume you have different columns for the name and value?

* The updateOptQuestVars method at the end just gets looked up in the tree but it never gets called

Here's my version:

string nombreTabla = "GlobalTable1";

// Let's get the table by name once and re-use it
Table tabla = Table(nombreTabla);

// We'll use this a lot, so let's do the "expensive" find() operation once.
treenode experimentVariables = model().find("Tools/Experimenter/ExperimentVariables");

// Only destroy the variables we don't need...
int numExisting = experimentVariables.subnodes.length;
int numNew = tabla.numRows;
for(int i = numExisting + 1; i <= numNew; i++)
{
	destroyobject(experimentVariables.subnodes.last;
}

// !! Note: this whole thing breaks if you start with no variables because then
// there will be nothing to copy when you need a new one!!
for(int i = 1; i <= numNew; i++)
{
	// If we can reuse a variable, do that; otherwise copy the first one.
	treenode variable = (i <= numExisting) 
		? experimentVariables.subnodes[i] 
		: createcopy(experimentVariables.first, experimentVariables);

	variable.name = tabla[i][1];
	variable.first.value = table[i][2]; // Assume you meant column 2 here?
}

// Added a nodefunction call here to make sure the update gets done
treenode updateOptQuestVars = maintree().find("project/exec/optquest/definitionLogic/updateOptQuestVars");
nodefunction(updateOptQuestVars);
· 1
5 |100000

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

Julio R avatar image Julio R commented ·

Thank you, I didn't realize I was just calling the last function.

It was intentionally 16ish because my client has that version, but I will try to implement your optimizations.

0 Likes 0 ·