%% Divide a Chart into Separate Units
% This example shows how to use linked atomic subcharts to break a
% Stateflow(R) chart into subcomponents so that multiple people can work on
% different parts of the chart. Atomic subcharts are supported only in
% Stateflow charts in Simulink(R) models. For more information, see
% <docid:stateflow_ug#bsbm40z-2>.

% Copyright 2019-2020 The MathWorks, Inc.

%% Original Model Without Atomic Subcharts
% This model contains two <docid:simulink_ref#f8-893976 Sine Wave> blocks:
% one with a frequency of 1 radian per second, and the other with a
% frequency of 2 radians per second.

model = 'ex_reuse_states';
load_system(model)

% Delete all annotations at the top level of the model
ah = find_system(model, 'FindAll', 'on', 'type', 'annotation');
for i = 1:length(ah)
   ao = get_param(ah(i), 'Object');
   ao.delete;
end

open_system(model)

%%
% In the chart, each state uses saturator logic to convert the input sine
% wave to an output square wave of the same frequency.
%
% <<../sf_identical_states_chart.png>>

% g2074387
% open_system([model '/Chart'])

%%
% Because this example does not use atomic subcharts, only one person at a
% time can edit the model. If you edit state |A| while someone else edits
% state |B|, you must merge those changes at submission time.
%
% In contrast, you can store different parts of this example as linked
% atomic subcharts. Because atomic subcharts behave as independent objects,
% different people can work on different parts of a chart without affecting
% the other parts of the chart. At submission time, no merge is necessary
% because the changes exist in separate models.

bdclose

%% Edit Model to Use Atomic Subcharts
% *Step 1: Convert a State to an Atomic Subchart*
%
% Right-click state |A| and select *Group & Subchart > Atomic Subchart*.
% State |A| changes to an atomic subchart and displays the label *Atomic*
% in the upper-left corner.
%
% <<../sf_identical_states_chart_with_one_atomic_subchart.png>>

% g2074387
% bdclose
% model = 'ex_reuse_states_with_one_atomic_subchart';
% load_system(model)
% open_system([model '/Chart'])
% 
% %%
% close_system(model)
 
%%
% *Step 2: Create a Library for the Atomic Subchart*
% 
% # Create a new library model.
% # Copy the atomic subchart and paste it in your library model.
% # Save your library model.
%
% In the library model, the atomic subchart appears as an independent chart
% with an input port and an output port.

model = 'ex_reuse_states_library';
open_system(model)

%%
% *Step 3: Replace State with Linked Atomic Subchart*
%
% # Delete state |A| in the chart.
% # Copy the atomic subchart in your library and paste it in your chart.
%
% The linked atomic subchart appears opaque and contains the label *Link*
% in the upper-left corner.

bdclose
model = 'ex_reuse_states_with_one_linked_atomic_subchart';
load_system(model)

%%
% <<../sf_identical_states_chart_with_one_linked_atomic_subchart.png>>

% g2074387
% open_system([model '/Chart'])

%% Propagate a Change in the Library Chart
% Suppose that, in the library chart, you edit the transition from |Pos| to
% |Neg|.
%
% <<../sf_identical_states_mylib_modified.png>>

% g2074387
% close_system(model)
% model = 'ex_reuse_states_library';
% load_system(model)
% set_param(model,'Lock','off');
% 
% rt = sfroot;
% ch = rt.find('-isa','Stateflow.Chart');
% s = ch.find('-isa','Stateflow.State','Name','Pos');
% t = ch.find('-isa','Stateflow.Transition','Source',s);
% t.LabelString = '[u1 < 0.5]';
% s = t.Destination;
% x = s.Position;
% s.Position = x + [10 0 0 0];
% 
% open_system([model '/A'])

%%
% This change propagates to the linked atomic subchart in the main chart.
% If someone else edits the main chart, the changes are merged
% automatically.