Main Content

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, seeCustomize Discrete-Event System Behavior Using Events and Event Actions.

To open the model and to observe the behavior of the custom block, seeCustomEntityStorageBlockWithIterationEventExample.

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 theirDiameterattribute 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 capacity5is used to store and sort car wheels based on theirDiameterattribute in an ascending order. When a new wheel enters the storage, an iteration eventeventIterateis invoked, which triggers an iteration event actioniterateto 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

  1. Define a storage with capacityobj.Capacity, which sorts wheels based in their priority value. The priority values are acquired from theDiameterattributes 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
  2. 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 argument1is the storage index for the iterate event, and''is the tag name.

  3. The iterate event invokes an iterate event action.

    % itarate事件动作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);ifcur.size == cur.position fprintf('End of Iteration \n')endnext = 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

  1. Save the.mfile asCustomEntityStorageBlockIteration. Link the System object to a SimEvents®model by using aMATLAB Discrete-Event Systemblock. For more information about linking, seeCreate Custom Blocks Using MATLAB Discrete-Event System Block.

  2. Create a SimEvents model including theMATLAB Discrete-Event Systemblock, and anEntity Generatorblock.

  3. In theEntity Generatorblock:

    1. In the实体类型tab, set theAttribute NameasDiameter.

      The attributeDiameteris used to sort entities in theMATLAB Discrete-Event Systemblock.

    2. In theEvent actionstab, in theGenerate actionfield, add this code to randomize the size of the incoming entities.

      entity.Diameter = randi([1 10]);
    3. In theStatisticstab, output theNumber of entities departed, dstatistic and connect to a scope.

  4. Connect the blocks as shown and simulate the model.

    1. Observe that theEntity Generatorblock generates5entities since the capacity of the storage block is5.

    2. 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,5entities in the storage are sorted in ascending order.

See Also

|||||||

Related Topics