Build a Custom Block with Multiple Storages
This example shows how to create a custom block with multiple storages and manage storage behavior using discrete-event System object™ methods.
Suppose that you manage a facility that produces items for customer orders. To prepare for repetitive orders, the facility produces a supply of items before the orders arrive. When a new order arrives, the stocks are checked for availability.
If the item is found in the storage, it departs the facility to fulfill the order.
If the item is not found in the storage, a new item is produced and the generated item departs the facility to fulfill the order.
To generate this custom behavior, you manipulate multiple storages through a
discrete-event System object, created using the matlab.DiscreteEventSystem methods. To
observe the behavior of the custom block, see CustomEntityStorageBlockWithTwoStoragesExample.
Create the Discrete-Event System Object
Generate a custom entity storage block with two inputs, one output, and two storage elements.

The desired behavior of the custom block is to select and output entities based on a reference entity.
Input port
1accepts entities with typeItemto storage1.Input port
2accepts reference entities with typeOrderto storage2.When a reference
Orderarrives at storage2, its attribute data is recorded as the reference value, and the entity is destroyed.The
Orderarrival invokes an iteration event at storage1to search for anItemcarrying data that is equal to the reference value.If a match is found, the matching item is forwarded to output port
1and the iteration ends.If the match is not found, a new
Itemis generated at storage1with a matching attribute value and forwarded to output port1.
See the Code to Generate the Custom Storage Block with Multiple
Storages
Custom Block Behavior
Discrete state variable
InputKeyrepresents the recorded reference value fromOrder, which is used to select correspondingItem.properties (DiscreteState) InputKey; endThe block has two storages with FIFO behavior. Storage
1supports entities with typeItem, and storage2supports entities with typeOrder. The block has two input ports and one output port. Input port1and output port1are connected to storage1. Input port2is connected to storage2. For more information about declaring ports and storages, see Implement a Discrete-Event System Object with MATLAB Discrete-Event System Block.function num = getNumInputsImpl(~) num = 2; end function num = getNumOutputsImpl(~) num = 1; end function [entityTypes] = getEntityTypesImpl(obj) entityTypes = [obj.entityType('Item'), ... obj.entityType('Order')]; end function [inputTypes, outputTypes] = getEntityPortsImpl(~) inputTypes = {'Item' 'Order'}; outputTypes = {'Order'}; end function [storageSpecs, I, O] = getEntityStorageImpl(obj) storageSpecs = [obj.queueFIFO('Item', obj.capacity)... obj.queueFIFO('Order', obj.capacity)]; I = [1 2]; O = 1; end
Specify the discrete state and reset the state
InputKey. For more information about states in discrete-event systems, see Custom Entity Generator Block with Signal Input and Signal Output.function [sz, dt, cp] = getDiscreteStateSpecificationImpl(obj, name) sz = 1; dt = 'double'; cp = false; end function resetImpl(obj) obj.InputKey = 0; end
When
Orderarrives at storage 2, its dataKeyis recorded in the discrete state variableObj.InputKey. This entry also invokes an iteration event at storage1and another event to destroyOrder.function [Order, events] = OrderEntry(obj, storage, Order, source) % A key entity has arrived; record the Inputkey value. obj.InputKey = Order.data.Key; % Schedule an iteration of the entities in storage 1. % Destroy input key entity. events = [obj.eventIterate(1, '') ... obj.eventDestroy()]; coder.extrinsic('fprintf'); fprintf('Order Key Value: %f\n', Order.data.Key); end
The purpose of the iteration is to find items with data that matches
InputKey.function [Item,events,continueIter] = ItemIterate(obj,... storage, Item, tag, cur) % Find entities with matching key. events = obj.initEventArray; continueIter = true; if (Item.data.Attribute1 == obj.InputKey) events = obj.eventForward('output', 1, 0.0); % If a match is found, the iteration ends and the state is reset. continueIter = false; elseif cur.size == cur.position % If a match is not found, this invokes an entity generation event. events = obj.eventGenerate(1,'mygen',0.0,100); end end
Generate an entity with type
entity1and a matchingKeyvalue. Then, forward the generated entity to output port1.function [Item,events] = ItemGenerate(obj,storage,Item,tag) % Specify event actions when entity generated in the storage. Item.data.Attribute1 = obj.InputKey; events = obj.eventForward('output',1,0.0); end
Implement the Custom Block
Save the
.mfile asCustomBlockTwoEntityStorages. Link the System object to a SimEvents® model 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, two Entity Generator blocks, and an Entity Terminator block. Connect the blocks as shown in the model.

In the Entity Generator block:
In the Entity generation tab, set the Generate entity at simulation start to
off.In the Entity type tab, set the Entity type name as
Item.In the Event Actions tab, in the Generate action field enter:
entity.Attribute1 = randi([1 3]);
By default, the entities are generated with intergeneration time
1and theirAttribute1value is a random integer between1and3.In the Statistics tab, output the Number of entities departed, d statistic and connect it to a scope.
In the Entity Generator1 block:
In the Entity generation tab, set Generate entity at simulation start to
off, and set Period to5.In the Entity type tab, set the Entity type name as
Orderand Attribute Name asKey.In the Event Actions tab, in the Generate action field enter:
entity.Key = randi([1 4]);
Entities with type
Orderare generated with intergeneration time5, and theKeyattribute takes integer values between1and4.There is no possible match between
KeyandAttribute1when theKeyvalue is4becauseAttribute1can take the value1,2, or3.In the Entity Terminator block, output the Number of entities arrived, a statistic and connect it to a scope.
Right-click the entity path from the MATLAB Discrete-Event System block to the Entity Terminator block and select Log Selected Signals.
Increase simulation time to
50and simulate the model. Observe that:50entities with typeEntity1enter storage 1 in the block.
In the Diagnostic Viewer, observe the incoming
Keyreference values carried by10entities that enter storage 2 and are destroyed afterward.
The Simulation Data Inspector shows the departing items and their
Attribute1values. The values match theKeyvalues displayed in the Diagnostic Viewer.
Also observe
5entities departing withAttribute1value4. These entities are generated in storage 2 becauseAttribute1cannot have the value4for the entities generated by the Entity Generator block.
See Also
matlab.DiscreteEventSystem | entry | matlab.System | getEntityStorageImpl | getEntityPortsImpl | generate | iterate