Main Content

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 the model or in a subsystem boundaries. You can use the Clone Detector app or the MATLAB commands programmatically to reuse components by creating library blocks of the clone patterns and replacing the clones with links to those library blocks. You can also use it to link the clones from an existing library.

Exact Clones and Similar Clones

Clones can be categorized into two types: exact clones and similar clones. Exact clones have identical block types, connections, and parameter values. Similar clones have the identical block types and connections, but they can have different block parameter values. For instance, in similar clones, the value of a Gain block can be different, and Sum blocks with different operations are also considered as similar clones, but they must be the same in exact clones.

Both 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.

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

Specify Where to Detect Clones

The Clone Detector app supports two options for detecting clones in a model. You can search for clones in a subsystem or anywhere across the model using clone detection Settings.

  • Subsystem clones: Identifies clones only in a subsystems.

  • Clones across model: Identifies clones across the model.

Identify Exact and Similar Clones

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

  1. Open the model ex_detect_clones.

    openExample("ex_detect_clones")

    Simulink canvas for ex_detect_clones model

  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_detect_clones")
  4. The app opens the Clone Detector tab. This example takes you through each section.

    Clone Detector toolstrip with sections for View, Prepare, Detect, Refactor, and Verify

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 Results and Actions 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.

  • Click Replace Exact Clones With Subsystem References to find and replace exact clones with subsystem reference blocks.

  • 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 Components 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 Identify and Replace 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.

  • Click Detect Clones Across Model to enable detect clones anywhere across the model. You can choose the values of Minimum Region Size and Minimum Clone Group Size to detect the clones with these matching blocks. Minimum Region Size is the number of blocks per clone and Minimum Clone Group Size is the number of clones present in that clone group. The default size for each is 2.

Identify Subsystem Clones in the Model

  1. To find clones within the model, click on the subsystem that you want to analyse. In the Detect section, the selected subsystem name appears under Find Clones in System tab. Use the pin to remember the selection.

    Find Clone in System tab in Detect section of toolstrip

  2. Click Find Clones to identify clones.

    Simulink canvas for ex_detect_clones model with blocks highlighted in red and blue

  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.

Clone Detector app creates a backup folder in the working directory. The backup folder name has the prefix m2m_<model name>. It saves the clones data in a MAT-file. You can also find the backup of the original model in this folder after refactoring the model to replace clones with links to library blocks.

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 Results and Actions panel, on the Clone Results tab, a list of clone groups are displayed.

  2. 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.

  3. In the Clone Detection Results and Actions pane, click the Logs tab. Click the hyperlink on the Logs pane.

    The Clone Detection Report is generated in HTML and saved in the MATLAB current folder. The report opens in a new web browser window, showing a summary of clones, individual detection results with detailed information on clone groups, clone types, detection parameters, and exclusion configurations applied to the model.

    Clone detection report

  4. Click the Model Hierarchy tab. Click the hyperlinks to highlight the particular subsystems in the model. To go back to highlighting all clones, on the Clone Results tab, click the Highlight all clones.

  5. On the Clone Results tab, expand Similar Clone Group 1 and click the View Parameter Difference hyperlink.

  6. On the Detected Clone Properties panel, click the ex_detect_clones/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 Results and Actions pane, in the Clone Results tab, select the clones you would like to refactor. Select all the clone groups for refactoring to reduce 22.5806% of the model reuse.

    Detected Clone properties with Refactor Benefits for Overall, Exact, and Similar types of clones

Replace Clones

  1. You may use the default library name or change the name of the library file and its location on the Clone Results 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 the Restore button in the Clone Results tab of the Clone Detection Results and Actions pane.

Identify and Replace Clones in Model Libraries

You can use the external library file to identify and replace clones in your model.

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

    openExample('ex_clone_library')
    

    Simulink canvas for 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 using external libraries must be done separately in the model. During model refactoring only exact clones within the libraries will be replaced with 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 use Check Equivalency drop-down button and click on it. A window opens and displays that the current model has been successfully refactored into an equivalent model.

Equivalency Check window with button to View Results

Limitations

Identifying clones within Configurable Subsystems is not supported.

Related Topics