Main Content

Generate Entities Carrying Nested Data Structures

This example shows how to investigate the throughput of a vehicle service facility using the Simulink® Type Editor to create nested data structures carried by entities.

The facility has three service stations represented by three Entity Server blocks. The vehicles arriving at the facility are queued and then directed to one of the three service stations based on their size and mileage. It is assumed that the older vehicles require more service time.

Snapshot of an example Simulink model that creates entities, in this case vehicles, from the Entity Generator block, and passes them to a FIFO Entity Queue block that is connected to an Entity Output Switch block. This block routes vehicles to Entity Server blocks Service Station 1, Service Station 2 or Service Station 3. Each of these three blocks is connected to an Entity Terminator block and a Scope block.

  1. Create entities that represent vehicles arriving at a service facility. The entities carry data representing the vehicle dimensions and properties as nested bus objects. Vehicle dimensions include vehicle height and width in meters and vehicle properties include its age and current mileage.

    1. On the Modeling tab, in the Design gallery, select Type Editor.

    2. In the docked Type Editor, click the Create types button to create a Simulink.Bus object.

    3. Set Name to Dimensions.

    4. With the bus object selected, click the Create elements button twice to create two Simulink.BusElement objects. Name them Height and Width.

    5. Create another Simulink.Bus object and set its Name property to Properties. Add three Simulink.BusElement objects named Station, Year, and Mileage.

    6. Create another Simulink.Bus object and set its Name property to Vehicle.

    7. Add two Simulink.BusElement objects and set their Name properties to VehicleDimensions and VehicleProperties. For their Data type properties, use the Bus: <object name> template, replacing <object name> with Dimensions and Properties, respectively.

    Type Editor displaying three bus objects named Dimensions, Properties, and Vehicle along with their respective elements and properties where applicable.

  2. Add an Entity Generator block. Double-click the Entity Generator block.

    1. Select the Entity type tab. Set the Entity type to Bus object and Entity type name as Vehicle.

      Vehicle is the bus object created by the Type Editor.

    2. Select the Event actions tab. In the Generate action field, enter:

      % Vehicle Dimensions
      entity.VehicleDimensions.Height = 1+rand();
      entity.VehicleDimensions.Width = 1+rand();
      % Vehicle Properties
      entity.VehicleProperties.Year = randi([1996 2018]);
      entity.VehicleProperties.Mileage = randi([50000 150000]);

      The vehicles arrive at the facility with random dimensions and properties.

  3. Add an Entity Queue block and rename it Vehicle Queue.

    1. In the Main tab, set the Capacity to Inf.

    2. Select the Event actions tab. In the Entry action field, enter this code to specify service station selection for vehicles.

      % If the height and width of the vehicle are greater than 1.5 m, select Station 1.
      if entity.VehicleDimensions.Width > 1.5 && entity.VehicleDimensions.Height > 1.5  
          entity.VehicleProperties.Station = 1;
      % Else, if the vehicle's mileage is greater than 90000 km, select Station 2.
      else if entity.VehicleProperties.Mileage > 90000    
           entity.VehicleProperties.Station = 2;
      % If the vehicle's mileage is less than 90000 km, select Station 3.
      else
           entity.VehicleProperties.Station = 3;   
      end
      end

      The vehicles are queued to be directed to the correct service station and vehicle dimensions and properties are used to select the appropriate service station.

  4. Add an Entity Output Switch block.

    1. Set the Number of output ports to 3.

    2. Set the Switching criterion to From attribute.

    3. Set the Switch attribute name to VehicleProperties.Station.

    The Entity Output Switch block directs the vehicles to the stations based on the specified Station attribute.

  5. Add an Entity Server block that represents the service station. Rename the block Service Station 1.

    1. In the Main tab, set the Service time source to MATLAB action.

    2. In the Service time action field, enter:

      if entity.VehicleProperties.Year > 2015
          dt = 1;
      else
          dt = 5;
      end

      It is assumed that the vehicle service time is longer for older vehicles.

    3. In the Statistics tab, select Number of entities departed, d statistic and connect it to a scope.

  6. Connect Service Station 1 to an Entity Terminator block.

  7. Follow the same steps to create Service Station 2 and Service Station 3 and connect them as shown.

  8. Increase the simulation time to 100 and run the simulation.

    Observe the number of vehicles served at Service Station 1.

    Scope block showing the number of vehicles departing from Station 1.

    Observe the number of vehicles served at Service Station 2.

    Scope block showing the number of vehicles departing from Station 2.

    Observe the number of vehicles served at Service Station 3.

    Scope block showing the number of vehicles departing from Station 3.

See Also

| | |

Related Topics