Main Content

generate

Class: matlab.DiscreteEventSystem
Package: matlab

Event action upon entity creation

Syntax

[entity,events]=generate(obj,storage,entity,tag)
[entity,events,out1,...]=generate(obj,storage,entity,tag,in1,...)

Description

[entity,events]=generate(obj,storage,entity,tag) specifies event actions of the object when an entity is created inside a storage component.

[entity,events,out1,...]=generate(obj,storage,entity,tag,in1,...) specifies such event actions of the object when the block has one or more input signal ports and/or signal output ports.

Input Arguments

expand all

Discrete-event System object.

Index of the storage element.

Entity to create inside storage element. Entity has these fields:

  • sys (MATLAB structure) — It has these fields:

    • id (double) — Entity ID

    • priority (double) — Entity priority

  • data — Entity data

Tag of the current entity generation event.

Any data inputs of the object. These input arguments exist only when the object has data inputs.

Output Arguments

expand all

Entities created with possibly changed values.

Events to be scheduled for just after entities are created. Use matlab.DiscreteEventSystem class methods to create events. Each event has these fields:

  • type (character vector) — Type of the event

  • delay (double) — Delay before the event

  • priority (double) — Priority of the event

  • Storage (double) — Index of the storage element

  • tag (character vector) — Event tag

  • location (MATLAB structure) — Source or destination location of entity

Data outputs of the object. You must specify these output arguments when the object has data outputs.

Examples

Set Initial Values When Entity is Generated

Initialize attribute values when entity is generated in a storage element.

function [entity,events] = generate(obj,storage,entity,tag)
    % Specify event actions when entity generated in storage.
    % - For entitiy generation event of tag 'Adam', initialize the
    %   entity so that its attribute 'gender' has value '0', and its
    %   priority is '200'.
    % - For entity generation event of tag 'Eve', initialize the
    %   entity so that its attribute 'gender' has value '1', and its
    %   priority is '100'.
    % - An event is returned to forward the entity to storage
    %   element 2 with a time delay of 0.6.
    switch tag
        case 'Adam'
            entity.data.gender = 0;
            entity.sys.priority = 200;
        case 'Eve'
            entity.data.gender = 1;
            entity.sys.priority = 100;
    end
    events = obj.eventForward('storage',2,0.6);
end

Create a Custom Block to Generate Entities

This example shows how to create a custom source block that generates entities and to manage discrete states when implementing the discrete-event System object methods.

For more information, see Custom Entity Generator Block with Signal Input and Signal Output.

classdef CustomEntityStorageBlockGeneration < matlab.DiscreteEventSystem...
                                            & matlab.system.mixin.Propagates
    % A custom entity generator block.
    
    % Nontunable properties 
    properties (Nontunable)
        % Generation period
        period = 1;
    end

    properties(DiscreteState)
        % Entity priority
        priority;
        % Entity value
        value;
    end

    % Discrete-event algorithms
    methods
        function [events, out1] = setupEvents(obj)
            % Set up entity generation events at simulation start.
            events = obj.eventGenerate(1,'mygen',obj.period,obj.priority);
            % Set up the initial value of the output signal.
            out1 = 10;
        end

        function [entity,events,out1] = generate(obj,storage,entity,tag,in1)            
            % Specify event actions when entity is generated in storage.            
            entity.data = obj.value;
            % The priority value is assigned from the input signal.
            obj.priority = in1;
            % Output signal is the assigned priority value.
            out1 = obj.priority;
            events = [obj.eventForward('output',1,0) ...
                      obj.eventGenerate(1,'mygen',obj.period,obj.priority)];
        end
    end

    methods(Access = protected)
        
        function entityTypes = getEntityTypesImpl(obj)
            entityTypes = obj.entityType('Material');
        end
        
        function [inputTypes,outputTypes] = getEntityPortsImpl(obj)
            % Specify entity input and output ports. Return entity types at
            % a port as strings in a cell array. Use empty string to
            % indicate a data port.
            inputTypes = {''};
            outputTypes = {'Material',''};
        end

        function resetImpl(obj)
            % Initialize / reset discrete-state properties.
            obj.priority = 10;
            obj.value = 1:12;            
        end
        
        function [storageSpecs, I, O] = getEntityStorageImpl(obj)
            storageSpecs = obj.queueFIFO('Material', 1);
            I = 0;
            O = [1 0];
        end   

        function num = getNumInputsImpl(obj)
            % Define total number of inputs for system with optional
            % inputs.
            num = 1;
        end
        
        function num = getNumOutputsImpl(~)
            % Define total number of outputs.
            num = 2;
        end  
        function [out1 out2] = getOutputSizeImpl(obj)
            % Return size for each output port.
            out1 = [1 12];
            out2 = 1;
        end

        function [out1 out2] = getOutputDataTypeImpl(obj)
            % Return data type for each output port.
            out1 = "double";
            out2 = "double";
        end

        function [out1 out2] = isOutputComplexImpl(obj)
            % Return true for each output port with complex data.
            out1 = false;
            out2 = false;
        end

        function [sz,dt,cp] = getDiscreteStateSpecificationImpl(obj,name)
            % Return size, data type, and complexity of discrete-state
            % specified in name.
            switch name
                case 'priority'
                    sz = [1 1];
                case 'value'
                    sz = [1 12];
            end
            dt = "double";
            cp = false;           
        end
    end
end
Introduced in R2016a