This approach provides a simple way to approximate flowchart level activity, especially if it has clear entry and exit points. The Source and Sink blocks have built-in count functions that return the total number of agents they have processed. For example, this code will give you an idea of how many agents are currently in the flowchart:
source.count() - sink.count();
If your flowchart has multiple Source or Sink blocks, adjust the code with their count() functions to determine the total number of agents in the whole flowchart. The resulting value has type long. You can use it in your code, assign it to a variable that can be used elsewhere, or display this value on a chart.
The same approach can be used to count agents between any two blocks of the flowchart. However, most other blocks require the call to the specific port’s count() function. For example, if there is a flowchart segment that starts with Seize and ends with Release:
seize.out.count() - release.in.count();
Here, we calculate how many agents at a time are currently passing through the flowchart between the Seize and Release blocks (namely, the out port of Seize and the in port of Release), but not including the blocks themselves. If you want to include them, you can simply replace the ports with their opposites (seize.in.count() and release.out.count()).
In such cases, it is more reliable to track agents manually: by summing the counters of individual blocks, or by introducing custom variables that reflect the number of agents under specific conditions, such as totals, momentary counts, or the number of agents in particular blocks.
A counter variable is a simple and effective way to track how many agents are currently within a particular segment of a flowchart.
For example, suppose you want to track the number of agents inside a segment that begins with a Seize block and ends with a Release block (inclusive) in your Process Modeling library flowchart.
- In the context in which the flowchart is located (typically the Main agent), create a counter variable by dragging the Variable element from the Agent palette onto the agent diagram. Name it agentsInFlowchart, set its Type to int, and its default value to 0.
-
In the On enter action of the Seize block, specify this code:
agentsInFlowchart++;
This will increment the value of the counter by one each time an agent enters this block and the flowchart segment. -
In the On exit action of the Release block, specify this code:
agentsInFlowchart--;
This will decrease the number when an agent leaves the segment we are tracking.
After this, you will always have a simple statistic of how many agents are currently inside the flowchart segment. If dynamic changes occur (agents are created, destroyed, or duplicated within the flowchart segment), adjust your logic accordingly or create additional counter variables to keep the statistics accurate.
An out-of-box approach to counting agents within a specific segment of a flowchart is to use RestrictedAreaStart and RestrictedAreaEnd blocks. By design, these blocks define a “restricted area” that limits the number of agents allowed within a segment of the flowchart. But in this context, we use them not to restrict agents, but to keep track of how many are currently inside.
The restricted area blocks do not contain agents: they only mark entry and exit points. However, the RestrictedAreaStart block provides a function that returns the number of agents currently inside the area:
restrictedAreaStart.entitiesInside();
This technique is useful because it allows you to avoid managing individual block counters, especially if the flowchart segment is too branching and complex to track agents manually. Make sure that your agents do not bypass the restricted area if you want to use it.
As an alternative, you can use the pair of TimeMeasureStart and TimeMeasureEnd blocks. Although these blocks are primarily intended to measure the time agents spend in a given segment, the TimeMeasureEnd block also collects statistics on agent flow, both as histogram data and as a data set, and exposes the API for both of these elements.
For example, to get the number of agents that have completed the segment, you can use:
timeMeasureEnd.dataset.size();
This returns the number of agents that have passed through the TimeMeasureEnd block, and by extension, through the entire measured segment.
These approaches may seem similar, but they return fundamentally different data:
- For the restricted area, the function returns how many agents are inside the segment.
- For the segment where you measure the time, the functions return how many agents have completed the segment.
Depending on whether you need a real-time count or cumulative statistics, opt for the approach that best suits your goal.
If you want to track agents that belong to an agent population in a particular place in the model (for example, between particular flowchart blocks), you can solve this task by adding a custom variable or parameter to track their state.
Consider this simple example. Suppose you have a model with a population people and you need to determine how many agents are between, say, the PedGoTo and PedService blocks in your Pedestrian library flowchart inclusively. You can use the following approach:
- In the Person agent type, add a Variable element (from the Agent palette). Name it isInFlowchartSegment, set its Type to boolean, and set its Default value to false.
-
Set the value of this variable to true, when the agent enters a flowchart segment, in the On enter action of the PedGoTo block:
ped.isInFlowchartSegment = true;
-
And similarly, set the value of this variable to false when the agent leaves the flowchart segment, in the On exit section of the PedService block:
ped.isInFlowchartSegment = false;
-
Now, to count how many agents are currently in the segment:
- Use the built-in statistics of the population. Open its properties, and in the Statistics section there, specify a Name, such as peopleFlowchartStatistics, Type: Count, and Condition: agent.isInFlowchartSegment. After this, to count people inside the segment, simply use the following expression where you need it: people.peopleFlowchartStatistics().
- Alternatively, use the following code where you need to count how many people are inside the segment: count(people, p -> p.isInFlowchartSegment). This is one of the standard functions of the Collection API; see the article for alternatives.
-
How can we improve this article?
-