Create a Custom Entity Storage Block with Iteration Event
A discrete-event System object™ can contain multiple event types for manipulating entities, acting on the storages, and resource management. When an event is due for execution, a discrete-event system can respond to that event by invoking event actions. The goal of this example is to show how to work with events and event actions when creating a custom block. To see the list of provided event and event actions, see Customize Discrete-Event System Behavior Using Events and Event Actions.
To open the model and to observe the behavior of the custom block, see CustomEntityStorageBlockWithIterationEventExample
.
Create the Discrete-Event System Object
In this example, a custom block allows entities to enter its storage element
through its input port. The storage element sorts the entities based on their
Diameter
attribute in ascending order. Every entity entry to
the block's storage invokes an iteration event to display the diameter and the
position of each entity in the storage.
The storage element allows you to define its capacity to store and sort entities
during which any entity can be accessed and manipulated. In this example, the
storage with capacity 5
is used to store and sort car wheels
based on their Diameter
attribute in an ascending order. When a
new wheel enters the storage, an iteration event eventIterate
is
invoked, which triggers an iteration event action iterate
to
display wheel positions in the storage and their diameter.
See the Code to Generate the Custom Storage Block with Iteration Event
Define Custom Block Behavior
Define a storage with capacity
obj.Capacity
, which sorts wheels based in their priority value. The priority values are acquired from theDiameter
attributes of the entities and are sorted in ascending order.function [storageSpecs, I, O] = getEntityStorageImpl(obj) storageSpecs = obj.queuePriority('Wheel',obj.Capacity, 'Diameter','ascending'); I = 1; O = []; end
A wheel's entry into the storage invokes an iterate event.
function [entity, event] = WheelEntry(obj,storage,entity, source) % Entity entry invokes an iterate event. event = obj.eventIterate(1, ''); end
Input argument
1
is the storage index for the iterate event, and''
is the tag name.The iterate event invokes an iterate event action.
% The itarate event action function [entity,event,next] = WheelIterate(obj,storage,entity,tag,cur) % Display wheel id, position in the storage, and diameter. coder.extrinsic('fprintf'); fprintf('Wheel id %d, Current position %d, Diameter %d\n', ... entity.sys.id, cur.position, entity.data.Diameter); if cur.size == cur.position fprintf('End of Iteration \n') end next = true; event=[]; end
In the code,
coder.extrinsic('fprintf')
declares the functionfprintf()
as extrinsic function for code generation. For each iteration, the code displays the new wheel ID, current position, and diameter, which is used as sorting attribute.
Implement Custom Block
Save the
.m
file asCustomEntityStorageBlockIteration
. 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, and an Entity Generator block.
In the Entity Generator block:
In the Entity type tab, set the Attribute Name as
Diameter
.The attribute
Diameter
is used to sort entities in the MATLAB Discrete-Event System block.In the Event actions tab, in the Generate action field, add this code to randomize the size of the incoming entities.
entity.Diameter = randi([1 10]);
In the Statistics tab, output the Number of entities departed, d statistic and connect to a scope.
Connect the blocks as shown and simulate the model.
Observe that the Entity Generator block generates
5
entities since the capacity of the storage block is5
.The Diagnostic Viewer displays the iteration event for each wheel entry to the storage. Each iteration displays ID, position, and diameter of the wheels. Observe how each wheel entry changes the order of the stored wheels. In the last iteration,
5
entities in the storage are sorted in ascending order.
See Also
matlab.DiscreteEventSystem
| entry
| matlab.System
| getEntityStorageImpl
| getEntityPortsImpl
| getEntityTypesImpl
| eventIterate
| iterate