Delay Entities with a Custom Entity Storage Block
This example shows how to use discrete-event System object™ methods to create a custom entity storage block that has one input port,
            one output port, and one storage element. The discrete-event System object is the instantiation of the matlab.DiscreteEventSystem class, which
            allows you to use the implementation and service methods provided by this class. Then,
            you use the MATLAB Discrete-Event System block to
            integrate the System object into a SimEvents® model.
The custom MATLAB Discrete-Event System block accepts an entity from its input port and forwards it to its output port with a specified delay. The figure visualizes the block using the discrete-event system framework.
To open the model and to observe the behavior of the custom block, see CustomEntityStorageBlockExample.

Create the Discrete-Event System Object
- Create a new script and inherit the - matlab.DiscreteEventSystemclass.- classdef CustomEntityStorageBlock < matlab.DiscreteEventSystem
- Add a custom description to the block. - % A custom entity storage block with one input, one output, and one storage.
- Declare two nontunable parameters - Capacityand- Delayto represent the storage capacity and the entity departure delay from the storage.- % Nontunable properties properties (Nontunable) % Capacity Capacity = 1; % Delay Delay = 4; end - The parameters capture the properties of the block. - Tunable parameters can be tuned during run time. 
- Non-tunable parameters cannot be tuned during run time. 
 
- Specify these methods and set access to protected. - methods (Access = protected) % Specify the number of input ports. function num = getNumInputsImpl(~) num = 1; end % Specify the number of output ports. function num = getNumOutputsImpl(~) num = 1; end % Specify a new entity type Car. function entityTypes = getEntityTypesImpl(obj) entityTypes = obj.entityType('Car'); end % Specify Car as the entity type that is used in % input and output ports. function [inputTypes,outputTypes] = getEntityPortsImpl(obj) inputTypes = {'Car'}; outputTypes = {'Car'}; end % Specify the storage type, capacity, and connection to % the input and output ports. function [storageSpecs, I, O] = getEntityStorageImpl(obj) storageSpecs = obj.queueFIFO('Car', obj.Capacity); % First element of I indicates the entity storage index 1 that is % connected to input 1. I = 1; % First element of O indicates the entity storage index 1 that is % connected to output 1. O = 1; end end- Only one storage sorts cars in a first-in-first-out (FIFO) manner. The - Capacityparameter of the object defines the server capacity.- The method - getEntityStorageImpl()also specifies the connections between the ports and the storage,- Iand- O.- The return value - Iis a vector of elements i =- 1, ...n where its length n is equal to the number of input ports.- In this example, - nis- 1because only one input port is declared.
- The ith element indicates the entity storage index that the ith input port connects to. - In this example, input port - 1is connected to storage- 1.
- If an input port is a signal port, the corresponding element is - 0.
 - Similarly the return value - Ois used to define the connections between the storage and the output port.
- Specify an - eventForwardevent to forward an entity of type- Carto the output when it enters the storage.- function [entity,event] = CarEntry(obj,storage,entity,source) % Specify event actions when entity enters storage. event = obj.eventForward('output', 1, obj.Delay); end - A - Carentry to the storage invokes an event action and the event- obj.eventForwardforwards- Carto the output with index- 1with a delay specified by- obj.Delay.- You can use the input arguments of this method to create custom behavior. The argument - objis the discrete-event System object inherited by the method. The argument- storageis the index of the storage element that the entity enters. The argument- entityis the entity that enters the storage and it has two fields,- entity.sysand- entity.data. The argument- sourceis the source location of the entity that enters the storage.- Note - You cannot manipulate entity data within an exit action. 
- Name your discrete-event System object - CustomEntityStorageBlockand save it as- CustomEntityStorageBlock.m.- The custom block represents a simplified gas station that can serve one car at a time. A car arrives at the gas station and is serviced for 4 minutes before departing the station. 
Implementing the Custom Entity Storage Block
- Create a model using an Entity Generator block, MATLAB Discrete-Event System block, and an Entity Terminator block.  
- Open the MATLAB Discrete-Event System block, and set the Discrete-event System object name to - CustomEntityStorageBlock. 
- Double-click the MATLAB Discrete-Event System block to observe its capacity and delay.  
- Output the Number of entities arrived, a statistic from the Entity Terminator block and connect it to a scope 
- Increase the simulation time to - 20and run the simulation. Observe the entities arriving at the Entity Terminator block with a delay of- 4. 
See Also
matlab.DiscreteEventSystem | entry | matlab.System | getEntityStorageImpl | getEntityPortsImpl | getEntityTypesImpl
