Main Content

simevents.SimulationObserver class

Package: simevents
Superclasses: handle

Interface to create your custom observer for models with SimEvents blocks

Description

This class is an interface for creating custom observers for models with SimEvents® blocks. Subclass this class to create your own observer, using the methods below. Some utility functions are also provided to interact with event calendars, blocks, and entities. Do not overwrite these utility functions.

Class Attributes

Abstract
false
HandleCompatible
true
StrictDefaults
false

For information on class attributes, see Class Attributes.

Creation

obj = SimulationObserver(modelName) returns an object of the SimulationObserver class, used to create a model observer for a SimEvents model.

Input Arguments

expand all

The name of the model to observe.

Methods

expand all

Examples

collapse all

This example shows how to construct an animator.

function this = seExampleRestaurantAnimator
            % Constructor
            modelname = 'seExampleCustomVisualization';
            this@simevents.SimulationObserver(modelname);
            this.mModel = modelname;
        end

This example shows how to create a simulation observer object and use it to observe entities in a model. For more information, see Observe Entities Using simevents.SimulationObserver Class.

Create the observer.

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.

Enable the observer object to monitor ObserverPreexitModel model.

obj  = myObserverPreexit('ObserverPreexitModel');

Version History

Introduced in R2016a