Main Content

Identify and Replace Clones using Model Libraries

Clones are modeling patterns that have identical block types and connections. By using the Clone Detector app, you can find clones and replace them with links to library blocks. This facilitates the reuse of components from model libraries, making it easier to update models. You can baseline modeling patterns in the library and use Clone Detector to replace all instances of these modeling patterns in your model with links to the library blocks.

You can also use the Clone Detector app to create new library blocks for recurring clone patterns in your model if they are not already present in the library, and then replace the clones with links to these new library blocks. For more information, see Enable Component Reuse by Using Clone Detection.

Identify Clones Using External Library

This example shows how to use the Clone Detector app to identify clones and then replace them with links to external library blocks.

Detect Clones

  1. Open the model ex_detect_clones.

    openExample("ex_detect_clones")

    ex_detect_clones model

  2. Save the model to your working folder. The model must be open to access the app.

  3. On the Apps tab, select Clone Detector from the Apps gallery. Alternatively, on the MATLAB® command line enter:

    clonedetection("ex_detect_clones")
  4. Open the library ex_clone_library. At the MATLAB command line, enter:

    openExample("ex_clone_library")
    

    Simulink library ex_clone_library

  5. In the Clone Detector tab, select Settings > Match Patterns from Libraries.

    Clone Detector Setting menu. Match Patterns from Libraries is highlighted

  6. Add the library file, ex_clone_library.slx.

    You can also provide path of the folder containing the libraries, and Clone Detector will select all the library files in that folder.

  7. Click Find Clones to identify clones.

    In the Clone Detection Results and Actions panel, on the Clone Results tab, a list of clone groups is displayed.

    Click the > next to ex_clone_library/libsubsystem and select Clone Region 2. Subsystem SS2 is highlighted in the model, indicating the clone.

    ex_detect_clones model with subsystem SS2 highlighted

    In the Refactor Benefits pane, you can evaluate number of exact clones and similar clones as a percentage of the total number of blocks in the model.

    Refactor Benefits panel

Replace Clones

Click Replace Clones. During the refactoring process, only exact clones are replaced with library links. Similar clones are not linked to the library unless they are refined to match exactly.

You can restore the model to its original state by clicking the Restore button in the Clone Results tab of the Clone Detection Results and Actions pane. The Clone Detector app creates a backup folder in the working directory. The backup folder name has the prefix m2m_model name. The clone data is saved in a MAT file. You can also find a backup of the original model in the backup folder after refactoring the model.

Identify Clones Programmatically

You can also use MATLAB commands to identify clones in your model from the library. For more details see, Detect and Replace Subsystem Clones Programmatically.

  1. Create a Simulink.CloneDetection.Settings object.

     cloneDetectionSettings = Simulink.CloneDetection.Settings
     cloneDetectionSettings = 
                                IgnoreSignalName: 0
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0                      
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 50
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {}
                                         Folders: {}
                         DetectClonesAcrossModel: 0
                          ExcludeInactiveRegions: 0
  2. To search for clones using the library file, provide the library path in the Libraries property.

    cloneDetectionSettings.Libraries = "C:\Project\Libraries\ex_clone_library.slx"
    cloneDetectionSettings = 
    
                                IgnoreSignalName: 0
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0
                          SelectedSystemBoundary: []
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 50
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {'C:\Project\Libraries\ex_clone_library.slx'}
                                         Folders: {}
                         DetectClonesAcrossModel: 0
                          ExcludeInactiveRegions: 0
    
  3. To find clones, use the Simulink.CloneDetection.findClones function with the cloneDetectionSettings object.

    cloneResults = Simulink.CloneDetection.findClones("ex_detect_clones",cloneDetectionSettings)
    cloneResults = 
    
              Clones: [1×1 struct]
        ExceptionLog: ''

    For more details on analyzing the clone detection results, see Programmatically Identify Clones in a Model.

See Also

Topics