Create a Custom Resource Acquirer Block
This example shows how to use resource management methods to create a custom entity storage block in which entities acquire resources from specified Resource Pool blocks.
Suppose that you manage a facility that produces parts from two different materials,
            material 1 and material 2, to fulfill orders.
            After a part is produced, it is evaluated for quality assurance.
Two testing methods for quality control are:
- Test 1 is used for parts that are produced from material - 1.
- Test 2 is used for parts that are produced from material - 2
After the production phase, parts are tagged based on their material to apply the correct test.
To generate the custom behavior, you create a discrete-event System object™ using the matlab.DiscreteEventSystem class
            methods for resource management.
Create the Discrete-Event System Object
Generate a custom entity storage block with one input, one output, and one storage element.

The block accepts an entity of type Part to its storage with
                capacity 1. The entity has an attribute Test
                to indicate the material from which the part is produced. Based on the value of the
                attribute, the entity acquires a resource from the specified Resource
                    Pool block and departs the block to be tested.
 See the Code to Generate the Custom Block to Acquire Resources
 See the Code to Generate the Custom Block to Acquire Resources
Custom Block Behavior
- Define - Test1and- Test2type resources to be acquired by the entity type- Part.- function resNames = getResourceNamesImpl(obj) % Define the names of the resources to be acquired. resNames = obj.resourceType('Part', {'Test1', 'Test2'}) ; end 
- The entity enters the storage. If its - entity.data.Testvalue is- 1, the entity is produced from- Material1. The entity acquires- 1resource from the Resource Pool block with resources of type- Test1. Similarly, If its- entity.data.Testvalue is- 2, the entity acquires one resource from the Resource Pool block with resources of type- Test2.- methods function [entity,events] = entry(obj, storage, entity, source) % On entity entry, acquire a resource from the specified pool. if entity.data.Test == 1 % If the entity is produced from Material1, it acquires resource of type Test1. resReq = obj.resourceSpecification('Test1', 1); else % If the entity is produced from Material2, it acquires resource of type Test2. resReq = obj.resourceSpecification('Test2', 1); end % Acquire the resource from the corresponding pool. events = obj.eventAcquireResource(resReq, 'TestTag'); end function [entity,events] = resourceAcquired(obj, storage,... entity, resources, tag) % After the resource acquisition, forward the entity to the output. events = obj.eventForward('output', storage, 0.0); end end- After the resource is successfully acquired, the - resourceAcquiredinvokes the forwarding of the entity.
Implement the Custom Block
- Save the - .mfile as- CustomBlockAcquireResources. 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 using a MATLAB Discrete-Event System block, an Entity Generator block and an Entity Terminator block, and two Resource Pool blocks. Connect the blocks as shown in the diagram. - Label Entity Generator block as Part Generator and Entity Terminator block as Departure for Testing.  
- In the Part Generator: - 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 - Partand Attribute Name to- Test.
- In the Event Actions tab, in the Generate action field enter: - entity.Test= randi([1 2]); - Parts are generated with intergeneration time - 1and their- Testattribute value is- 1or- 2to indicate the material type.
 
- In the Resource Pool block: - Set the Resource name to - Test1and the Reusable upon release parameter to- off.
- In the Statistics tab, output the Amount available, avail statistic and connect it to a scope. 
 
- In the Resource Pool1 block: - Set the Resource name to - Test2and the Reusable upon release parameter to- off.
- In the Statistics tab, output the Amount available, avail statistic and connect it to a scope. 
 
- Right-click the entity path from Part Generator to the MATLAB Discrete-Event System block and select Log Selected Signals. 
- Simulate the model. - Observe the - Testattribute values of the incoming entities to the custom block. Three entities require test 1 and seven entities requires test 2. 
- Observe that three resources of type - Test1are acquired by entities. 
- Observe that seven resources of type - Test2are acquired by entities. 
 
See Also
matlab.DiscreteEventSystem | entry | matlab.System | cancelAcquireResource | getResourceNamesImpl | resourceAcquired | eventAcquireResource | resourceSpecification