%% Temporal Logic Using the AFTER Function
% 
% This  example shows the difference between two Stateflow(R)
% charts: one chart uses temporal logic and the AFTER function to achieve the
% goal, and the other chart achieves the same goal without using temporal
% logic.

% Copyright 2006-2012 The MathWorks, Inc.

%% 

mdl = 'sf_tlafter';
open_system(mdl);
close_system([mdl, '/Scope']);

root = sfroot;
chart1 = find(root, '-isa', 'Stateflow.Chart', 'Name', 'Temporal Logic');
chart2 = find(root, '-isa', 'Stateflow.Chart', 'Name', 'Without Temporal Logic');

chart1.visible = 0;
chart2.visible = 0;

%% With Temporal Logic Operators
%
% The AFTER function allows you to easily obtain the number of events that
% have occurred within that chart and then used as a condition for an event
% transition. For instance, in this example, a transition from the 'Off'
% to the 'On' state will occur after 10 events (labeled as 'secs') have
% passed as long as the input, 'step', is less than 1. In this case the input
% 'step' is created using a Step function block from Simulink(R) and the event
% 'secs' is defined by using a Pulse Generator. The value of this step will
% be zero until 60 seconds of simulation have gone by. After 60 seconds pass,
% the step is created and the value of the step input is 1. Since one of the
% transition conditions in the chart is not met ('step' < 1) then the
% transition does not take place anymore and you can notice a halt in the
% chart activity.

chart1.visible = 1;

%% Without Temporal Logic Operators
%
% The same effect is achieved without the use of temporal logic. The
% difference is that an additional variable and transition need to be
% included in order to count the number of times an event takes place. In
% this case, the local variable 'count_after' is used to obtain the number of
% times the event 'secs' occurs. The transition will then take place if
% 'count_after' is greater than 10 and the value of step (which is the input)
% is less than 1.

chart2.visible = 1;
