question

Marc R5 avatar image
0 Likes"
Marc R5 asked Joerg Vogel edited

Query number of items in a queue where item label meets a condition

Hello,

I have a queue with multiple items. Each item has its labels, and I would want to know the number of items that the label "oDepartureTruck" == X. How can I do that?

1664354318325.png

Thanks!

FlexSim 22.2.2
queuequerymap
1664354318325.png (42.9 KiB)
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

Steven Hamoen avatar image
1 Like"
Steven Hamoen answered Felix Möhlmann edited

@Marc R5 There are several solutions possible. Maybe the easiest is using the onentry and onexit triggers and everytime an item enters or exits increase a label on the queue. (or use process flow to do this)

The 2nd option is create a for loop that loop through all the items in the queue and adds up the ones with the correct label. You could for instance put this in a user command

The 3d option is more or less the same as the second but you can use the build in query language to create a query.

Option 1 is the fastest, 3 the slowest

· 2
5 |100000

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

Marc R5 avatar image Marc R5 commented ·

Can you give a hint on how to do it using the query?

Table.query("SELECT count(*) FROM $1 WHERE $2", Model.find("StockRack"), item.labels["oDateDeparture"].value == 1.00)[1][1]

The "where" clause, I do not know how to do it...

Thanks

0 Likes 0 ·
Felix Möhlmann avatar image Felix Möhlmann commented ·

Edit: Originally meant as an answer, but Steven was faster.

There are multiple ways. The best approach is going to depend on personal preference and how many items are in the queue, how many different values are there of the label and how often you want to retrieve that value.

1) Query

You can use the Table.query and have it interpret the queue's subnodes and their labels as a table. All the necessary information can be found in the respective manual page (Querying the contens of a queue is described in "Advaned Query Techniques" about halfway down the page).

A query to retrieve the number of items with "Type" 1 from Queue1 would, for example, look like this:

int itemNum = Table.query("SELECT COUNT($2) AS ItemCount FROM $1 WHERE $3 == $4", Model.find("Queue1"), $iter(1), $iter(1).Type, 1)[1][1];


2) For-Loop

You can also write a simple for-loop that counts the occurence of the given value. (Again, the example counts the occurence of "Type" 1)

Object queue = Model.find("Queue1");
int itemNum = 0;
for(int i = 1; i <= queue.subnodes.length; i++) {
    if(queue.subnodes[i].Type == 1) {
        itemNum++;
    }
}


3) Map

If there are a lot of items in the queue and you need to read the quantity often, it might be worthwhile to keep a Map of the contents. That way the total of each type is always directly available without needing to count every time.

You can keep the map as a label of the queue, as demonstrated in the attached model. First create any label, then assign a map value to it.

Map map;
Model.find("Queue1").labels["ContentMap"].value = map;

Then set this as the reset value, so the map gets cleared on every model reset.

1664356263679.png

In the "OnEntry" and "OnExit" triggers, the map is updated to always hold the current number of items of each different label value.

1664356195459.png

CountLabelValueOccurence_fm.fsm

0 Likes 0 ·
1664356195459.png (23.8 KiB)