%% Absolute Time Temporal Logic in the Presence of Enabled Subsystems
% This model shows the behavior of absolute time temporal logic in
% the presence of enabled subsystems.

% Copyright 2009-2014 The MathWorks, Inc.

bdclose all
clear
close all

model = 'sf_abstemporal_enable';
open_system(model);

r = sfroot;
m = r.find('-isa', 'Stateflow.Machine', 'Name', model);
c = m.find('-isa', 'Stateflow.Chart', 'Name', 'Chart');

close_system([model '/EnabledSubsys']);
close_system([model '/Scope']);
c.Visible = 0;

%% 
% The model contains an enabled subsystem |EnabledSubsys| which contains a
% single chart |Chart|. Initially the option "States when Enabling" for the
% enable port |EnabledSubsys/Enable| is set to "held". 

set_param([model '/EnabledSubsys/Enable'], 'StatesWhenEnabling', 'held');
open_system([model '/EnabledSubsys']);

%% 
% The chart itself is very simple. It consists of two states |A| and |B|
% with the transition from |A| to |B| guarded by |after(5,sec)|. 

c.Visible = 1;

%% 
% The signal driving |EnabledSubsys| looks as below.

open_system([model '/Signal Builder']);

%% 
% When the model is simulated, we get the following output.

close_system([model '/Signal Builder']);
evalc('sim(model)');
open_system([model '/Scope']);

%% 
% Notice that the transition from |A| to |B| happens at |t = 9|, rather
% than at |t = 7| even though we entered |A| at |t = 2|. This illustrates
% an important semantic rule for absolute time temporal logic in the
% presence of enabled subsystems. In particular, when a chart is disabled,
% we do _not_ continue to accumulate the time which the chart spent in the
% currently active state(s).
% 
% If we change the option "States when Enabling" for the enable port
% |EnabledSubsys/Enable| to "reset" and re-run the simulation, we see the
% following behavior:

set_param([model '/EnabledSubsys/Enable'], 'StatesWhenEnabling', 'reset');
evalc('sim(model)');
open_system([model '/Scope']);

%% 
% Notice that now, the transition from |A| to |B| happens at |t = 10|. This
% is because at |t = 5|, when the enabled subsystem got re-enabled,
% Stateflow(R) will "start fresh". In other words, it executes the default
% transitions, resets all the temporal counters and performs all the other
% initializations it did at |t = 2|. We therefore stay in |A| for 5 more
% seconds after |t = 5|.

%% 
bdclose all
clear
close all
