Main Content

Custom Entity Storage Block with Multiple Timer Events

A discrete-event system allows the implementation of distinct event types for manipulating entities and storages. Sometimes, the desired behavior involves more than one event acting on the same storage or entity. This example shows how to handle multiple events acting on the same target in a discrete-event system framework. In this example, a custom entity storage block is generated to implement the tag, which is one of the identifiers, when multiple timer events are acting on the same entity. To see the list of event identifiers, see Customize Discrete-Event System Behavior Using Events and Event Actions.

To open the model and observe the behavior of the custom block, see CustomEntityStorageBlockWithTwoTimerEventsExample.

Create the Discrete-Event System Object with Multiple Timer Events

Suppose that the discrete-event System object™ is used to represent a facility that processes metal parts using an oven. The processing time varies based on the detected metal. For safety, the parts have a maximum allowed processing time.

  • If the oven processing time is less than the allowed maximum time, the parts are processed and depart the oven and the facility.

  • If there is an error in detected metal, the service time exceeds the maximum allowed processing time, the process stops and the parts are taken out of the oven to be rerouted for further processing.

To represent this behavior, this example uses a custom entity storage block with one input, two outputs, and a storage element. An entity of type Part with TimeOut attribute enters the storage of the custom block to be processed. TimeOut determines the maximum allowed processing time of the parts. When a part enters the storage, two timer events are activated. One timer tracks the processing time of the part in the oven. When this timer expires, the entity is forwarded to output 1. Another timer acts as a fail-safe and tracks if the maximum allowed processing time is exceeded or not. When this timer expires, the process is terminated and the entity is forwarded to the output 2.

Graphical representation of a Discrete-Event system showing a rectangle with an input port and two output ports containing a storage element.

This example that generates the custom block and uniquely identifies these two timer events targeting on the same entity using custom tags.

 See the Code to Generate the Custom Storage Block with Timer Events

Custom Block Behavior

  1. Generate a custom block with one input, two outputs, and a storage element. For more information about creating a basic storage element, see Implement a Discrete-Event System Object with MATLAB Discrete-Event System Block.

        function num = getNumInputsImpl(~)
            num = 1;    
        function num = getNumOutputsImpl(~)
            num = 2;
        function entityTypes = getEntityTypesImpl(obj)
            entityTypes = obj.entityType('Part');
        function [inputTypes,outputTypes] = getEntityPortsImpl(obj)
            inputTypes = {'Part'};
            outputTypes = {'Part' 'Part'};    
        function [storageSpecs, I, O] = getEntityStorageImpl(obj)
            storageSpecs = obj.queueFIFO('entity1', obj.Capacity);
            I = 1;
            O = [1 1];
  2. Invoke two timers with tags 'TimeOut' and 'ProcessComplete' when an entity enters the storage.

        function [entity,event] = PartEntry(obj,storage,entity,source)
            % Specify event actions when entity enters storage.
            ProcessingTime = randi([1 15]);
            % The TimeOut attribute specifies the expiration time of the timer with tag TimeOut
            event1 = obj.eventTimer('TimeOut',;
            % The expiration time of the timer ProcessComplete is a random integer between 
            % 1 and 15.
            event2 = obj.eventTimer('ProcessComplete', ProcessingTime);   
            event = [event1 event2];
  3. The timer that expires the first determines the entity forward behavior.

        function [entity, event] = timer(obj,storage,entity,tag)
            % Specify event actions for when scheduled timer completes.
            event = obj.initEventArray;
                switch tag
                    case 'ProcessComplete'
                        % If ProcessComplete expires first, entities are forwarded to output 1.
                        event = obj.eventForward('output', 1, 0);
                    case 'TimeOut'
                        % If TimeOut expires first, entities are forwarded to output 2.
                        event = obj.eventForward('output', 2, 0);

Implement Custom Block

  1. Save the .m file as CustomEntityStorageBlockTimer. Link the System object to a SimEvents® model by using a MATLAB Discrete-Event System block. For more information about linking, see Create Custom Blocks Using MATLAB Discrete-Event System Block.

  2. Create a SimEvents model including the MATLAB Discrete-Event System block, an Entity Generator block, two Entity Terminator blocks. Connect the blocks as shown in the model.

    Block diagram showing an Entity Generator block connected to a MATLAB Discrete-Event System block with System Object name CustomEntityStorageBlockTimer. The two output signals from the MATLAB Discrete-Event System block connect to separate Entity Terminator blocks named Entity Terminator and Entity Terminator1. Entity Terminator connects to a Scope block called Process Complete. Entity Terminator1 connects to a Scope block named Incomplete.

  3. In the Entity Generator block:

    1. In the Entity type tab, set the Attribute Name as TimeOut.

    2. In the Event actions tab, in the Generate action field:

      entity.TimeOut = 10;
  4. In the Entity Terminator and Entity Terminator1 blocks, output the Number of entities arrived, a statistic and connect them to scopes.

  5. Increase simulation time to 100 and simulate the model. Observe that entities are forwarded to the corresponding output based on the corresponding timer expiration.

    Process Complete Scope block representing number of entities arriving at Entity Terminator, graphically.

    Incomplete Scope block representing number of entities arriving at Entity Terminator1, graphically

See Also

| | | | | | |

Related Topics