%% Using Custom Visualization for Entities

% Copyright 2014 The MathWorks, Inc.
 
%% Overview
% These examples show how you can create MATLAB based custom visualization 
% for entities. 
% 
% * The first example illustrates the visualization of a restaurant layout with customer entities entering, dining, and leaving.  
% * The second example illustrates a generic tool for visualizing entities stored on servers and queues.  This example uses the same restaurant model as in the first example. However, you can use this tool on any SimEvents model with servers and queues.  
%

%% Structure of Model
% The model contains the following major components:
%
% * The Time-Based Entity Generator block (Patron Enter) generates entities representing customer entities. These customer entities enter a waiting area, represented by a FIFO Queue (Waiting block).
% * The Tables subsystem contains a FIFO queue with 10 table entities. These table entities are reallocated as customer entities leave the restaurant.
% * When a table is available for a customer, a table entity gets combined with a customer entity using the Entity Combiner (Assign Table).
% * When a customer entity is done dining, the table entity and customer entity are split using the Entity Splitter block (Release Table).  Upon table release, the customer entity leaves the restaurant through the Entity Sink block (Patron Leave) while the table is reallocated in the Tables subsystem.
modelname = 'sedemo_custom_visualization';
open_system(modelname);

%% Part 1: Visualizing the Restaurant
% sedemo_RestaurantAnimator visualizes the restaurant as follows:
%
% * sedemo_RestaurantAnimator provides the visualization of the restaurant layout for the model.  
% * It generates the figure containing the layout of a restaurant with an entrance, a waiting area, 10 dining tables, and an exit.
% * As entities move during the simulation, it creates a marker (glyph) for each entity in the figure and programs motion for the marker so that it appears to move from one point to another.
% * The model assigns a table ID for each waiting customer and the observer shows the customer and table combined.  
% * To inspect the attributes of the customer entity, pause the model and click on a customer entity glyph.   The figure displays the TimeToDine attribute.
% * To make the motion appear continuous, it uses a MATLAB timer to periodically execute a function that incrementally moves each entity towards its destination.
% * It uses MATLAB graphics to display statistics on the figure about the number of entities entering, waiting, and leaving.
% * Clicking an entity in the visualization displays the attributes that it contains.  It uses a MATLAB graphics callback to program a ButtonDownFcn on each entity marker.
%
animator = sedemo_RestaurantAnimator;
simevents.connectObserver(modelname, animator);
sim(modelname);
waitfor(animator.getFigureHandle, 'Tag', 'End');
simevents.disconnectObserver(modelname);


%% Part 2: Visualizing Entities on Storage Blocks
% sedemo_StorageAnimator visualizes the entities on storage blocks as follows:
%
% * During initialization, sedemo_StorageAnimator uses sedb.blkinfo to identify blocks with the Capacity field (storage blocks).
% * It draws a blue bar for each capacity storage unit.  
% * It uses sedb.blkinfo to obtain all the entities in each storage block using the Entities field and overlays an entity (green bar) on its capacity storage unit.
% * Clicking an entity in the bar graph displays the attributes that it contains.  It uses a MATLAB graphics callback to program a ButtonDownFcn on each entity bar element. You can use this tool on any SimEvents model with servers and queues.
%
animator = sedemo_StorageAnimator(modelname);
simevents.connectObserver(modelname, animator);
sim(modelname);
simevents.disconnectObserver(modelname);

%%
%
close all;
bdclose all;
clear modelname

displayEndOfDemoMessage(mfilename)
