This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Enable Component Reuse by Using Clone Detection

Clones are modeling patterns that have identical block types and connections. The Identify Modeling Clones tool identifies clones across referenced model boundaries. You can use the Identify Modeling Clones tool to enable component reuse 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.

To open the tool, in the Simulink Editor, select Analysis > Refactor Model > Identify Modeling Clones.

Exact Clones Versus 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 different parameters to 0 (default value). 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 Identify Modeling Clones tool to identify exact clones and similar clones, and then replace them with links to library blocks.

  1. Open the model ex_clone_detection and the corresponding library ex_clone_library. At the MATLAB® command line, enter:

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

  2. Save the model to your working folder.

  3. In the Simulink Editor, from the Analysis menu, select Refactor Model > Identify Modeling Clones. To open the Identify Modeling Clones tool programmatically, at the MATLAB command prompt, type: clonedetection('ex_clone_detection').

  4. Select the folder Replace clones with library block links. If you want to perform all or some of the checks in the Identify Modeling Clones tool, you can click Run Selected Checks. Selecting this option does not refactor the model. It only identifies the clones. This example takes you through each check, one at a time.

Replace Clones of Library Blocks with Library Links

Identify modeling patterns that are graphical clones of a library subsystem. Graphical clones can be in modeling regions that include inactive variants and commented-out regions. If one clone has a link to a library block, the tool reports a missing link for the other subsystem or subsystem clones. The tool also reports clones that do not have links to library blocks. You choose whether to create a library block and replace a clone with a link to that library block.

  1. Select Replace clones of library blocks with library links.

  2. In the Library file name field, specify the library ex_clone_library.

  3. Leave the Maximum number of different parameters value as 0.

  4. Click Run This Check. In the top Result table, the left column contains hyperlinks to modeling clones. The right column contains hyperlinks to the corresponding library subsystems. The Gain blocks G12, G13, and G14 and SS2 are clones of libsubsystem.

  5. Click Refactor Model.

  6. In the bottom Result table, there is a message informing you that the model was successfully refactored. The Refactor Model button is now unavailable, and the Undo button is enabled.

  7. The model now contains links to libsubsystem. To remove the linked library blocks, click the Undo button. After you refactor, you can remove the latest changes to the model by clicking the Undo button. Each time you refactor a model, the tool creates a back-up model in the folder that has the prefix m2m_plus the model name.

Note

The Identify Modeling Clones tool identifies clones that are similar to library blocks. It does not refactor a model to replace similar clones with links to library blocks.

Replace Graphical Clones with Library Links

Now identify graphical subsystem clones and replace them with links to library blocks. If you do not want to refactor a model to replace clones in inactive variants or commented-out regions, you can skip this check and instead run the Replace functional clones with library links check.

  1. Select Replace graphical clones with library links.

  2. In the New library file name field, specify a library name or use the default name.

  3. Change the Maximum number of different parameters value to 2.

  4. Click Run This Check. The top Result table contains separate groupings for exact and similar clones. Exact Clone Group 1 contains hyperlinks to the subsystem clones. SS1 and SS4. Similar Clone Group 1 contains hyperlinks to SS3 and SS5. Similar Clone Group 2 contains hyperlinks to SS6 and SS7.

  5. Click the + symbol to reveal the contents in the second row and second column of the Result table. SS5 has one block and one parameter that is different from SS3. SS3 is the baseline clone for comparison.

  6. Click Refactor Model.

  7. In the bottom Result table, there is a message informing you that the model was refactored. The Refactor Model button is now unavailable, and the Undo button is enabled.

  8. For each clone group, the refactored model contains links to library subsystems. Similar Clone Group 1 and Similar Clone Group 2 link to masked library subsystems.

Replace Functional Clones with Library Links

Identify functional subsystem clones and replace them with links to library blocks. If you want to refactor a model to replace clones in active modeling regions and inactive variants and commented-out regions, you can skip this check and instead run the Replace graphical clones with library links check.

  1. Select Replace functional clones with library links.

  2. In the New library file name field, specify a name for the library or use the default name.

  3. Click Run This Check. The top Result table does not list new clones because the Replace Graphical Clones with Library Links step identifies functional clones.

Save and View Clone Detection Reports

When the Identify Modeling Clones tool runs checks, it generates an HTML report of check results. By default, the HTML report is in the slprj/modeladvisor/ folder. The Identify Modeling Clones tool uses the slprj folder in the code generation folder to store reports and other information. If the slprj folder does not exist in the code generation folder, the Identify Modeling Clones tool creates it.

View the report in the Identify Modeling clones tool by clicking the link on the Replace clones with library block links folder. Save the report to a new location by clicking the Save As button and specifying a location.

Additional Information

  • You can run the Identify Modeling Clones tool on a library.

  • You can exclude Subsystem and Model Reference blocks from clone detection by right-clicking the subsystem or Model Reference block and selecting Identify Modeling Clones > Subsystem and its contents > Add to exclusions. For more information, see Exclude subsystems and referenced models from clone detection.

  • For additional practice using the Identify Modeling Clones tool, try the model aero_dap3dof and the corresponding libraries aero_librcs and aero_libdap.

Related Topics