Main Content

Observe Entities Using simevents.SimulationObserver Class

This example shows how to use simevents.SimulationObserver object to count entity departures and acquire departure timestamps.

Use the simevents.SimulationObserver object to observe or visualize entities, and implement animators to debug model simulations. For more information, see Use SimulationObserver Class to Monitor a SimEvents Model.

In this model, the simevents.SimulationObserver object is used to acquire the number of entities departing a block or a set of blocks in the model and timestamp their departures. The model has two Entity Generator and Entity Terminator blocks and an Entity Server Block. The Scope blocks display the Number of entities departed, d statistics for the Entity Generator and Entity Server blocks.

Create the Observer

Open a new script and initiate the simevents.SimulationObserver object by this code.

classdef myObserverPreexit < simevents.SimulationObserver
    % Add the observer properties.
    properties
    Model
    % Initialize the property count.
    count
    end
properties (Constant, Access=private)
    increment = 1;
end
methods
     % Observe any model by incorporating its name to MyObserverPreexit.
     function this = myObserverPreexit(Model)
         % Input model name to the simulation observer.
         this@simevents.SimulationObserver(Model);
         this.Model = Model;
     end
     % Initialize the count in the simulation start.
     function simStarted(this)
         this.count = 0;
     end
     % Specify list of blocks to be notified of entity entry and exit
     % events.
     function Block = getBlocksToNotify(this)
         Block = this.getAllBlockWithStorages();
     end
     function preExit(this,evSrc,Data)
         % Get the names of all storage blocks that the entities depart.
         % This returns the block with its path.
         Block = Data.Block.BlockPath;
         % Remove the path to display only the
         % block name.
         Block = regexprep(Block,'ObserverPreexitModel/' ,'');
         % Initialize the blocks to observe.
         BlockName = 'Entity Server';
         % If the block that entity exits contains the block name
         % acquire data for exit time and block name.
         if contains(Block, BlockName)
             % Get time for entity preexit from event calendar.
             evCal = this.getEventCalendars;
             Time = evCal(1).TimeNow;
             % Increase the count for departing entities.
             this.count = this.count + this.increment;
             myInfo = [' At time ',num2str(Time), ...
             ' an entity departs ', Block, ', Total entity count is ', ...
             num2str(this.count)];
             disp(myInfo);
             end
         end
     end
 end

Save the file as myObserverPreexit.m file.

Monitor the Model

Enable the observer object to monitor ObserverPreexitModel model.

obj  = myObserverPreexit('ObserverPreexitModel');

The observer monitors the Entity Server block, which is determined by the BlockName parameter in the observer file myObserverPreexit.m.

  • Simulate the model. Click View Diagnostics on the model window and observe that the number of entities departing the Entity Server block and the departure timestamps.

  • For validation, observe the Scope block that displays the Number of entities departed, d statistic for the Entity Server block.

Monitor Multiple Blocks in the Model

Use the same observer to monitor the entity departures from all of the Entity Generator blocks in your model.

  • Change the BlockName parameter in the preExit method to 'Entity Generator'. Entity Generator blocks in the model are labeled Entity Generator1 and Entity Generator2.

function preExit(this,evSrc,Data)
    % Get the names of all storage blocks that the entities depart.
    % returns the block with its path.
    Block = Data.Block.BlockPath;
    % Remove the path to display only the block name
    Block = regexprep(Block,'ObserverPreexitModel/' ,'');
    % Initialize the common Entity Generator phrase
    BlockName = 'Entity Generator';
    % If the block that the entity exits contains the block name
    % acquire the exit time and the block name.
    if contains(Block, BlockName)
        % Get the time of entity preexit from the event calendar.
        evCal = this.getEventCalendars;
        Time = evCal(1).TimeNow;
        % Increase the count of departing entities.
        this.count = this.count + this.increment;
         myInfo = [' At time ',num2str(Time), ...
             ' an entity departs ', Block, ', Total entity count is ', ...
         num2str(this.count)];
         disp(myInfo);
    end
end
  • Enable the observer object to monitor ObserverPreexitModel model.

obj  = myObserverPreexit('ObserverPreexitModel');
  • Simulate the model. Observe the Diagnostic Viewer that displays the information for 15 entities departing from both Entity Generator blocks.

  • For validation, observe Scope1 and Scope2 blocks display the Number of entities departed, d statistic for the Entity Generator1 and the Entity Generator2.

Observe that 4 entities depart Entity Generator1.

Also, 11 entities depart Entity Generator2. In total, 15 entities departed from the Entity Generator blocks in the model.

See Also

| | | |

Related Topics