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
.
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
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; end function num = getNumOutputsImpl(~) num = 2; end function entityTypes = getEntityTypesImpl(obj) entityTypes = obj.entityType('Part'); end function [inputTypes,outputTypes] = getEntityPortsImpl(obj) inputTypes = {'Part'}; outputTypes = {'Part' 'Part'}; end function [storageSpecs, I, O] = getEntityStorageImpl(obj) storageSpecs = obj.queueFIFO('entity1', obj.Capacity); I = 1; O = [1 1]; end
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', entity.data.TimeOut); % The expiration time of the timer ProcessComplete is a random integer between % 1 and 15. event2 = obj.eventTimer('ProcessComplete', ProcessingTime); event = [event1 event2]; end
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); end end
Implement Custom Block
Save the
.m
file asCustomEntityStorageBlockTimer
. 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.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.
In the Entity Generator block:
In the Entity type tab, set the Attribute Name as
TimeOut
.In the Event actions tab, in the Generate action field:
entity.TimeOut = 10;
In the Entity Terminator and Entity Terminator1 blocks, output the Number of entities arrived, a statistic and connect them to scopes.
Increase simulation time to
100
and simulate the model. Observe that entities are forwarded to the corresponding output based on the corresponding timer expiration.
See Also
eventTimer
| matlab.DiscreteEventSystem
| entry
| matlab.System
| getEntityStorageImpl
| getEntityPortsImpl
| getEntityTypesImpl
| timer