Manage Entities Using Event Actions
This example shows how to control entity generation rate and write event actions to change entity attributes in a simple queuing system. In a discrete-event simulation, an event is an observation of an instantaneous incident that may change a state variable, an output, or the occurrence of other events. SimEvents® allows you to create custom actions when an event occurs. These actions are called event actions. Events can have corresponding actions. You can write event actions to change entity attributes by using MATLAB® code or Simulink® functions.
Start with a Simple Queuing System
This is a simple queueing system with Entity Generator, Entity Queue, Entity Server, and Entity Terminator blocks. In this example, an entity represents a tank of a truck that arrives at a gas station. The attribute of an entity represents the current gas level in a tank. Event actions represent the changes of the gas level in a tank. Tanks are randomly generated, queued, and they are serviced with a pump which transfers a constant amount of gas for one second. Tanks depart from the station with their new total gas amount.
Modify the Model
Select the whole model or the entity paths originating from the Entity Generator, Entity Queue, and Entity Server blocks and right-click to select Log Selected Signals. Simulation Data Inspector is used to visualize the flow of tanks and their gas level in the model. For more information, see Inspect Simulation Data.
Rename the Entity Generator block as Tank Generator, the Entity Queue block as Waiting Queue, the Entity Server block as Pump, and the Entity Terminator block as Exit.
Rename the path originating from the Tank Generator block as Tank to Queue, the Waiting Queue block as Tank to Pump, and the Pump block as Tank to Exit.
Configure and Simulate Model
Double-click the Tank Generator, and select the Entity type tab. Change the Entity type name to
Tank
, and the Attribute Name toCurrentGasLevel
.The entity attribute
CurrentGasLevel
represents the existing amount of gas in each tank.Simulate the model. Open the Simulation Data Inspector. Observe that the tanks approach the Waiting Queue, the Pump, and the Exit with the same rate.
Tanks leave the station with their initial gas amount
1
which is the Attribute Initial Value.Open the Tank Generator block parameters dialog box. In the Entity generation tab, set Time source to
Matlab action
. Observe the default MATLAB code.dt = rand(1,1);
The code randomizes the entity intergeneration time parameter
dt
to represent random tank arrivals.Simulate the updated model. In the Simulation Data Inspector, observe that tanks arrive randomly with the same initial gas amount
1
.Observe that the tanks are generated randomly but they approach the pump with a regulated fixed rate because service time for the Pump is
1
.Open the Tank Generator block dialog box. In the Event actions tab, in the Generate action field, enter the code.
entity.CurrentGasLevel = randi([1,4]);
Tanks arrive at the station with a random gas amount that ranges from
1
to4
.Simulate the updated model. In the Simulation Data Inspector, observe that the tanks arrive with random amounts of gas.
For the Pump block, set these parameters:
In the Event actions tab, select Service complete.
For the Service complete action field, enter the code.
entity.CurrentGasLevel = entity.CurrentGasLevel + 3;
Each tank is filled with 3 units of gas for
1
s duration, and then it departs the pump.
Observe that the Tank Generator and the Pump blocks update with the event action icon {...} indicating that the blocks define an event action.
Simulate the updated model. In the Simulation Data Inspector, observe that each tank leaves the station with 3 additional units of gas.
Modified Model to Manage Entities in a Queueing System
This is the modified model after configuring the simple queueing system.
See Also
Entity Generator | Entity Queue | Entity Server | Entity Terminator