Main Content

Run Clone Detection Programmatically in a Loop on Multiple Models

This example shows how to programmatically detect and replace clones on a multiple models in a loop by operating on models individually. For more information about Clone Detection APIs, see Detect and Replace Clones Programmatically.

This example shows how to detect and replace clones programmatically for five Simulink® models using the library file TestLib_1 as a subsystem reference to replace clones.

1. At the MATLAB® command line, enter:

     addpath(fullfile(docroot,'toolbox','simulink','examples'))
     ex_clone_detection_A
     ex_clone_detection_B
     ex_clone_detection_C
     ex_clone_detection_E
     ex_clone_detection_F
     TestLib_1

Save the models and library file in the current working directory.

2. Create an array to add the models to:

     modelList = {};

3. Add the models to the modelList array:

      modelList{end+1,1} = 'ex_clone_detection_A';
      modelList{end+1,1} = 'ex_clone_detection_B';
      modelList{end+1,1} = 'ex_clone_detection_C';
      modelList{end+1,1} = 'ex_clone_detection_E';
      modelList{end+1,1} = 'ex_clone_detection_F';

4. Define containers to store Results, ReplacementResults and equivalencyCheck object for the models.

     cloneResultsStorage = containers.Map();
     cloneReplacementStorage = containers.Map();
     equivalencyCheckStorage = containers.Map();

5. Add the library file to the cloneDetectionSettings object created from Settings class.

     libName = 'TestLib_1';
     cloneDetectionSettings = Simulink.CloneDetection.Settings();
     cloneDetectionSettings = cloneDetectionSettings.addLibraries(libName);

6. Use a loop to cycle through the models using the Simulink.CloneDetection.findClones, Simulink.CloneDetection.replaceClones, and Simulink.CloneDetection.checkEquivalency functions.

     for modelIndex = 1:length(modelList)
          modelName = modelList{modelIndex};
         try
             cloneResults = Simulink.CloneDetection.findClones(modelName, cloneDetectionSettings);
             cloneResultsStorage(modelName) = cloneResults;
             cloneReplacementResults = Simulink.CloneDetection.replaceClones(cloneResults);
             cloneReplacementStorage(modelName) = cloneReplacementResults;
             equivalencyCheckResults = Simulink.CloneDetection.checkEquivalency(cloneReplacementResults);
             equivalencyCheckStorage(modelName) = equivalencyCheckResults;
         catch exception
         end
     end

You can access the results of cloneResultsStorage, cloneReplacementStorage, and equivalencyCheckStorage objects for individual models. For more details, see Detect and Replace Clones Programmatically.

Related Topics