Enable Component Reuse by Using Clone Detection

Clones are modeling patterns that have identical block types and connections. The Clone Detector app identifies clones across referenced model boundaries. You can use the Clone Detector app to reuse components by creating library blocks from subsystem clones and replacing the clones with links to those library blocks. You can also use the tool to link to clones in an existing library.

Exact Clones and Similar Clones

There are two types of clones: exact clones and similar clones. Exact clones have identical block types, connections, and parameter values. Similar clones have identical block types and connections, but they can have different block parameter values. For example, the value of a Gain block can be different in similar clones but must be the same in exact clones.

Exact clones and similar clones can have these differences:

  • Two clones can have a different sorted order.

  • The length of signal lines and the location and size of blocks can be different if the block connections are the same.

  • Blocks and signals can have different names.

To detect only exact clones, for each check in the Identify Modeling Clones tool, set the Maximum Number of Unmatched Block Parameters to 0. Increasing this parameter value increases the number of similar clones that the tool can potentially detect.

After you identify clones, you can replace them with links to library blocks. Similar clones link to masked library subsystems.

Identify Exact and Similar Clones

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

  1. Open the model ex_clone_detection. At the MATLAB® command line, enter:

    addpath(fullfile(docroot,'toolbox','simulink','examples'))
    ex_clone_detection

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

  3. On the Apps tab, click Clone Detector. Alternatively, on the MATLAB command line enter:

    clonedetection("ex_clone_detection")
  4. The app opens the Clone Detector tab. This example takes you through each section.

Set Up panes for Clone Detection

The app displays information on multiple panes. You can select three of the panes under the View menu. The panes are:

  • Help. Select to access a help pane that contains an overview of the clone detection workflow.

  • Results. Select to view the Clone Detection and Results pane.

  • Properties. Select to view the Detected Clone Properties pane.

Set the Parameters for Clone Detection

You can set up the parameters for clone detection by using the Settings drop-down menu.

  • Select Ignore differences in > Signal Names to identify and classify clones when the signal names are different.

  • Select Ignore differences in > Block Properties to identify and classify clones when the block properties are different. For more information about block properties, see Specify Block Properties (Simulink).

  • Click Exclude Components to access the Exclude model references, Exclude library Links, and Exclude inactive and commented out regions options. Enabling the Exclude inactive and commented out regions option identifies variable number clones because of Variant Source block in the model. For more information, see Exclude subsystems and referenced models from clone detection. Enabling the Exclude model references and Exclude library Links options will lead to identification of fewer clones, depending on the model.

  • Click Match Patterns with Libraries and select an external library to look for clones. For more information, see Identifying and Replacing Clones in Model Libraries.

  • The Maximum number of unmatched block parameters is 50 by default. This represents the number of parameters that can vary among subsystems and still be classified as similar clones. You may reduce this number to identify and classify fewer similar clones. Setting the value to zero, will identify only exact clones.

Identify Clones in the Model

  1. In the Detect section, in the Find Clones in System tab, enter ex_clone_detection as the model to identify clones in. Toggle the pin to access other subsystems to identify clones in.

  2. Click Find Clones to identify clones.

  3. The color of the subsystems changes to reflect the similar and exact clones identified. The red highlighting represents exact clones and the different shades of blue highlighting represent similar clones.

Analyze the Clone Detection Results

After identifying clones, you can analyze the results of the clone detection and make changes to the model as necessary. To analyze the results:

  1. In the Clone Detection Action and Results panel, on the Logs pane, click the hyperlink.

    A new window opens the clone detection results with an integrated report on the identified clones, the types of clones, the parameters of detection, and the exclusions in the clone detection.

  2. In the Clone Detection Action and Results pane, click the Map Clone Groups to Library tab.

    A list of clone groups are displayed.

  3. Click the > symbol next to Exact Clone Group 1 to see all of the subsystems that are exact clones, the number of blocks, and the block differences. Repeat the same for Similar Clone Group 1 and Similar Clone Group 2.

  4. Click the Model Hierarchy tab. Click the hyperlinks to highlight the subsystems that are present in the model.

  5. On the Map Clone Groups to Library tab, expand Similar Clone Group 1 and click the View Parameter Difference hyperlink.

  6. On the Detected Clone Properties panel, click the ex_clone_detection/SS5/G9 hyperlink, which opens the gain block G9 in the subsystem SS5, where you can access the parameter that are different from the baseline subsystem.

  7. Change value of the gain parameter from A to B and click Find Clones. This will reclassify Similar Clone Group 1 to Exact Clone Group 2 because you resolved the difference in the subsystems and converted it into an exact clone.

  8. Under the Refactor Benefits panel, you can consider the percentage of different types of clones present.

    In the Clone Detection Actions and Results pane, in the Map Clone Groups to Library tab, select the clones you would like to refactor. Select all the clone groups for refactoring to reduce 22.5806% of the model reuse.

Replace Clones

  1. You may use the default library name or change the name of the library file and its location on the Map Clone Groups to Library tab before replacing the clones.

  2. Click Replace Clones.

    The model is refactored and the clones are replaced with links to the newLibraryFile library file in your working directory.

  3. You can restore the model to its original configuration with clones by clicking Restore button found in the clone detector log that was generated on the Logs tab of the Clone Detector Actions and Results pane.

Identifying and Replacing Clones in Model Libraries

  1. Open the library ex_clone_library. At the MATLAB command line, enter:

    addpath(fullfile(docroot,'toolbox','simulink','examples'))
    ex_clone_library
    

  2. Click Settings > Match Patterns with Libraries and select ex_clone_library.slx. Then click Find Clones.

    Note

    Identifying and refactoring clones in external libraries must be done separately from the model. During model refactoring only exact clones within the libraries will be replaced library links.

  3. Click Replace Clones.

    The model is refactored with the exact clones replaced.

Check the Equivalency of the Model

If you have a Simulink Test™ license, you can click Check Equivalency. A window opens and displays that the current model has been successfully refactored into an equivalent model.

Related Topics