Serve High-Priority Customers by Sorting Entities Based on Priority
This example shows how to minimize the time required to serve high-priority customers by using a priority queue and Entity Input Switch and Entity Output Switch blocks. Customers are served based on their service priorities. In this example, two types of customers enter a queuing system. One type represents high-priority customers with high urgency. The second type of customers are lower priority and are served with less urgency. The priority queue places high-priority customers ahead of low-priority customers.

Build the Model
In the model, arriving customers are represented by Entity Generator and Entity Generator1.
- In the Entity Generator block, customer inter arrival times are generated from an exponential distribution with a mean of - 3.
- The Entity Generator block generates entities that have attributes, - priorityand- start time. The- priorityattribute is set to- 1, which is the service urgency of the customer. The- start timeattribute is also set to- 1, which initializes the start time value used in the model.
- Similarly, Entity Generator1 generates entities whose inter arrival times are generated from an exponential distribution with a mean of - 1. The entities have the same attributes,- priorityand- start time. The- priorityattribute is set to- 2which is the service urgency of the customer. The- start timeattribute is set to- 1.
The Entity Output Switch block accepts entities generated by the Entity Generator and the Entity Generator1 blocks and forwards them to the priority queue.
The Entity Queue block represents the queueing of the customers and prioritizes them based in their service urgency.
- The Capacity of the Entity Queue block is - 25.
- Queue type is set to - Priorityto sort the entities based on their priority values.
- Priority source is set to - priority, which is the attribute used to sort the entities.
- Sorting direction is set to - Ascending. Entities with lower values of- priorityare placed at the front of the queue. In this setup, The customers with- priorityvalue of- 1are prioritized over the customers with a value of- 2.

The Simulink Function block is used to timestamp the entities that enter the Entity Queue block.
- In the Entity Queue block, in the Event actions tab, in the Entry action, the following code is used so that every time an entity enters the block, the - getCurrentTime()Simulink function is called.
entity.starttime = getCurrentTime();
In the Simulink Function block, a Digital Clock block is used to timestamp the entity entering the Entity Queue block.

The Entity Server block represents the service the customer receives.
The Entity Output Switch block output the entities for departure.
- Switching criterion is set to - From attribute, which selects the departure path based on an entity attribute.
- Switch attribute name is set to - priority. If the- priorityvalue is- 1, the block switches to output port- 1and if the- priorityvalue is- 2, the block switches to output port- 2for entity departure.

When an entity enters the Entity Terminator block, the recordHighPriorityWaitTimes(starttime) function is called to calculate the time spent between an entity's arrival at the Entity Queue block and its departure from the Entity Terminator block.
- In the Entity Terminator block, in the Event actions tab, in the Entry, the - recordHighPriorityWaitTimes(starttime)function is called.
- The input argument of the function is - startime, which is the timestamp that was recorded when the entity entered the Entity Queue block.
- The Simulink Function block takes this argument and calculates the difference between the start time and departure time. 

- Similarly, the - recordLowPriorityWaitTimes(starttime)function calculates the time for the low-priority entities.
- The calculated total service time is displayed by a Dashboard Scope block. 
Simulate Model and Review Results
The simulation time of the model is set to 100.
Simulate the model and observe the results displayed in the Dashboard Scope block. The block shows that the waiting time for high-priority customers is significantly less than the low-priority customers.

See Also
Entity Queue | Entity Server | Entity Output Switch | Entity Input Switch | Simulink Function