Main Content

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.

Graphical representation of a Discrete-Event system as a rectangle containing a storage element holding one entity. The rectangle has input and output ports.

Create the Discrete-Event System Object

  1. Create a new script and inherit the matlab.DiscreteEventSystem class.

    classdef CustomEntityStorageBlock < matlab.DiscreteEventSystem
  2. Add a custom description to the block.

    % A custom entity storage block with one input, one output, and one storage.
  3. Declare two nontunable parameters Capacity and Delay to 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.

  4. 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 Capacity parameter of the object defines the server capacity.

    The method getEntityStorageImpl() also specifies the connections between the ports and the storage, I and O.

    • The return value I is a vector of elements i = 1, ...n where its length n is equal to the number of input ports.

      In this example, n is 1 because 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 1 is connected to storage 1.

    • If an input port is a signal port, the corresponding element is 0.

    Similarly the return value O is used to define the connections between the storage and the output port.

  5. Specify an eventForward event to forward an entity of type Car to 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 Car entry to the storage invokes an event action and the event obj.eventForward forwards Car to the output with index 1 with a delay specified by obj.Delay.

    You can use the input arguments of this method to create custom behavior. The argument obj is the discrete-event System object inherited by the method. The argument storage is the index of the storage element that the entity enters. The argument entity is the entity that enters the storage and it has two fields, entity.sys and entity.data. The argument source is the source location of the entity that enters the storage.

    Note

    You cannot manipulate entity data within an exit action.

  6. Name your discrete-event System object CustomEntityStorageBlock and 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.

     See the Code to Generate Custom Entity Storage Block

Implementing the Custom Entity Storage Block

  1. Create a model using an Entity Generator block, MATLAB Discrete-Event System block, and an Entity Terminator block.

    Block diagram showing an Entity Generator block connected to an unspecified MATLAB Discrete-Event System that, in turn, connects to an Entity Terminator block. The Entity Terminator block is connected to a Scope.

  2. Open the MATLAB Discrete-Event System block, and set the Discrete-event System object name to CustomEntityStorageBlock.

    The same block diagram showing the Entity Generator block now connected to a MATLAB Discrete-Event System with Discrete-event System object name specified as CustomEntityStorageBlock.

  3. Double-click the MATLAB Discrete-Event System block to observe its capacity and delay.

    Block Parameters dialog box of the MATLAB Discrete-Event System showing Capacity as 1 and Delay as 4.

  4. Output the Number of entities arrived, a statistic from the Entity Terminator block and connect it to a scope

  5. Increase the simulation time to 20 and run the simulation. Observe the entities arriving at the Entity Terminator block with a delay of 4.

    Scope block showing entities arriving at the Entity Terminator block, graphically.

See Also

| | | | |

Related Topics