В этом примере показано, как использовать simevents.SimulationObserver объект для подсчета отправлений сущностей и получения временных меток отправления.
Используйте simevents.SimulationObserver объект для наблюдения или визуализации сущностей и реализации аниматоров для отладки симуляций модели. Для получения дополнительной информации смотрите Использовать класс SimulationObserver для мониторинга модели SimEvents.
В этой модели simevents.SimulationObserver объект используется для получения количества сущностей, отходящих из блока или набора блоков в модели, и временной метки их отбытия. Модель имеет два блоков Генератора сущностей и Терминатора сущностей и блок Сервера сущностей. Блоки Scope отображают Количество удаленных сущностей, d статистику для блоков Entity Generator и Entity Server.

Откройте новый скрипт и инициируйте simevents.SimulationObserver объект этим кодом.
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Сохраните файл следующим myObserverPreexit.m файл.
Включите объект ObserverPreexitModel модель.
obj = myObserverPreexit('ObserverPreexitModel');
Наблюдатель контролирует блок Entity Server, который определяется BlockName параметр в файле наблюдателя myObserverPreexit.m.
Симулируйте модель. Нажмите View Diagnostics в окне модели и заметьте, что количество сущностей, отходящих от блока Entity Server и временных меток отправления.

Для валидации обратите внимание на блок Scope, который отображает Количество удаленных сущностей, статистику d для блока Entity Server.

Используйте тот же наблюдатель, чтобы контролировать отходы сущностей от всех блоков Генератора сущностей в вашей модели.
Измените BlockName параметр в preExit метод для 'Entity Generator'. Блоки Генератора сущностей в модели помечены как Generator1 сущностей и 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Включите объект ObserverPreexitModel модель.
obj = myObserverPreexit('ObserverPreexitModel');
Симулируйте модель. Наблюдайте Diagnostic Viewer, который отображает информацию для 15 сущности, отходящие от обоих блоков Генератора сущностей.

Для валидации, наблюдайте, Scope1 и Scope2 блоки отображают Количество удаленных объектов, статистику d для Generator1 сущности и Generator2 сущности.
Наблюдайте за этим 4 сущности отходят от Generator1 сущности.

Кроме того, 11 сущности отходят от Generator2 сущности. Всего, 15 сущности отошли от блоков Генератора сущностей в модели.

getBlocksToNotify | getEventCalendars | preExit | simevents.SimulationObserver | simStarted