build a custom block with multiple storages -凯发k8网页登录

main content

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 methods. to observe the behavior of the custom block, see .

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.

  1. input port 1 accepts entities with type item to storage 1.

  2. input port 2 accepts reference entities with type order to storage 2.

  3. when a reference order arrives at storage 2, its attribute data is recorded as the reference value, and the entity is destroyed.

  4. the order arrival invokes an iteration event at storage 1 to search for an item carrying data that is equal to the reference value.

  5. if a match is found, the matching item is forwarded to output port 1 and the iteration ends.

  6. if the match is not found, a new item is generated at storage 1 with a matching attribute value and forwarded to output port 1.

 

custom block behavior

  1. discrete state variable inputkey represents the recorded reference value from order, which is used to select corresponding item.

        properties (discretestate)
            inputkey;
        end
  2. the block has two storages with fifo behavior. storage 1 supports entities with type item, and storage 2 supports entities with type order. the block has two input ports and one output port. input port 1 and output port 1 are connected to storage 1. input port 2 is connected to storage 2. for more information about declaring ports and storages, see .

        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
  3. specify the discrete state and reset the state inputkey. for more information about states in discrete-event systems, see .

        function [sz, dt, cp] = getdiscretestatespecificationimpl(obj, name)
            sz = 1;
            dt = 'double';
            cp = false;
        end  
        
        function resetimpl(obj)
            obj.inputkey = 0;
        end
  4. when order arrives at storage 2, its data key is recorded in the discrete state variable obj.inputkey. this entry also invokes an iteration event at storage 1 and another event to destroy order.

        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
  5. 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
  6. generate an entity with type entity1 and a matching key value. then, forward the generated entity to output port 1.

        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

  1. save the .m file as customblocktwoentitystorages. link the system object to a simevents® model using a block. for more information about linking, see .

  2. 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.

  3. in the entity generator block:

    1. in the entity generation tab, set the generate entity at simulation start to off.

    2. in the entity type tab, set the entity type name as item.

    3. 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 1 and their attribute1 value is a random integer between 1 and 3.

    4. in the statistics tab, output the number of entities departed, d statistic and connect it to a scope.

  4. in the entity generator1 block:

    1. in the entity generation tab, set generate entity at simulation start to off, and set period to 5.

    2. in the entity type tab, set the entity type name as order and attribute name as key.

    3. in the event actions tab, in the generate action field enter:

      entity.key = randi([1 4]);

    entities with type order are generated with intergeneration time 5, and the key attribute takes integer values between 1 and 4.

    there is no possible match between key and attribute1 when the key value is 4 because attribute1 can take the value 1, 2, or 3.

  5. in the entity terminator block, output the number of entities arrived, a statistic and connect it to a scope.

  6. right-click the entity path from the matlab discrete-event system block to the entity terminator block and select log selected signals.

  7. increase simulation time to 50 and simulate the model. observe that:

    1. 50 entities with type entity1 enter storage 1 in the block.

    2. in the diagnostic viewer, observe the incoming key reference values carried by 10 entities that enter storage 2 and are destroyed afterward.

    3. the simulation data inspector shows the departing items and their attribute1 values. the values match the key values displayed in the diagnostic viewer.

      also observe 5 entities departing with attribute1 value 4. these entities are generated in storage 2 because attribute1 cannot have the value 4 for the entities generated by the entity generator block.

see also

| | | | | |

related topics

    网站地图