question

Chandler avatar image
1 Like"
Chandler asked Chandler commented

How to get a label using a variable?

I am trying to use a string variable in a reference to a patient label, to check the value of multiple labels inside a loop inside of an Assign Labels object.

When tokens enter this Assign Labels, they have several labels set to 0 or 1 depending on whether they need certain imaging tests.

The new label value should be a 0 if all of several labels are set to 0, or 1 if there are any labels set to 1.

I am currently assigning these labels to the patient (they are "xray", "ct", etc. 0/1 for each) and trying to evaluate them by referencing the list in a table, as pictured below:

/**Custom Code*/
Object current = param(1); Object patient = current; treenode activity = param(2); Token token = param(3); Variant assignTo = param(4); string labelName = param(5); treenode processFlow = ownerobject(activity); print(patient); int imgCount = 0; // set count of imaging processes to 0 // check the table of imaging assignment values for imaging names Table headers = Model.find("Tools/GlobalTables/ptFlowPcts"); print(headers); print(headers.numCols); for (int i = 1; i = headers.numCols; i++){ // loop through the number of columns     string img = headers.getColHeader(i); // get the column name (imaging type) for each loop     imgCount += patient.img; // try to get the 0 or 1 value of this label } if (imgCount = 0){     return 0; // return 0 (no) only if there are no imaging called for } else{     return 1; // return 1 if one or more imaging is called for }

The question at hand is: how can i reference a label on a patient using a variable, so that I can check an arbitrary number of labels in a code block? I have tried a few different things:

  • label(patient, img)
  • patient.img
  • patient."img"
  • patient.labels[img].value
  • patient.labels[img].evaluate()
  • ... and every other combination I could come up with.

Some of them led to a crash, some just threw an exception saying the label didn't exist. I am at the point where I just have to say I don't know how this should work.

---

Ideally I would just have a label that contains an array with a list of imaging randomly assigned based on our client data. And now that I think about it, I should be able to do that with code. But I'd still like to know better how to do what I described above.

Thanks in advance, you are all always so helpful!

FlexSim 23.1.0
labelsflexscriptcustom codeassigning labels
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 Chandler commented

The correct way to get a label node from a string variable is

patient.labels[img]

Then to get the value off that node you can use .value or .evaluate() if you want it to execute a FlexScript node:

patient.labels[img].value
patient.labels[img].evaluate()

Here's an example model that demonstrates it working

LabelFromString.fsm


labelfromstring.fsm (25.9 KiB)
· 3
5 |100000

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

Chandler avatar image Chandler commented ·

It turns out that I was getting the label correctly. It only threw an exception when I tried one of the invalid ways to get the label.


The code is crashing in the for loop. For some reason the following bit returns a seemingly infinite number of empty lines:, even when headers.numCols is replaced with a low integer like 5

  
                    
  • for (int i = 1; i = headers.numCols; i++){ // loop through the number of columns
  •     string img = headers.getColHeader(i); // get the column name (imaging type) for each loop


0 Likes 0 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Chandler commented ·

You forgot the less than character in your for loop end condition:

for (int i = 1; i = headers.numCols; i++)

but should be

for (int i = 1; i <= headers.numCols; i++)
0 Likes 0 ·
Chandler avatar image Chandler Matthew Gillespie ♦♦ commented ·
Thanks. I had figured this out and meant to update but looks like I never came back to it.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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