%% Create and Use a Coverage Filter Object
% This example shows how to create a filter object and add a rule to
% exclude a subsystem from coverage analysis.

% Copyright 2020 The MathWorks, Inc.

%%
% Open the |slvnvdemo_covfilt| model. Use a |SimulationInput| object to
% enable coverage recording and specify coverage settings.

modelName = 'slvnvdemo_covfilt';
load_system(modelName)
simIn = Simulink.SimulationInput(modelName);
simIn = setModelParameter(simIn,'CovEnable','on');
simIn = setModelParameter(simIn,'CovMetricStructuralLevel','MCDC');
simIn = setModelParameter(simIn,'CovSaveSingleToWorkspaceVar','on');
simIn = setModelParameter(simIn,'CovSaveName','covData');

%%
% Simulate the model. The coverage data is stored in the value supplied for
% the |CovSaveName| property.

simOut = sim(simIn);
covData = simOut.covData;

%%
% You can filter a block by using the |slcoverage.BlockSelector| class. To
% exclude the Switchable config subsystem, use
% |slcoverage.BlockSelectorType.SubsystemAllContent| as the first input.

subsysSel = slcoverage.BlockSelector(...
                      slcoverage.BlockSelectorType.SubsystemAllContent,...
                      'slvnvdemo_covfilt/Switchable config');

%%
% Create the filter rule by passing the selector, rationale, and the
% exclude filter mode as inputs.

rule = slcoverage.FilterRule(subsysSel,...
                    'Unused configuration',...
                    slcoverage.FilterMode.Exclude);

%%
% Create an |slcoverage.Filter| object and then add the rule to it.

filt = slcoverage.Filter;
addRule(filt,rule);

%%
% Save the filter to a file called |blockFilter.cvf|. To create a report
% that uses this coverage filter, add the filter file as the value to the
% |filter| property of |covData|, and create a report called
% |coverageData.html| using |covData|.

save(filt,'blockFilter')
covData.filter = 'blockFilter';
cvhtml('coverageData',covData)
