Documentation

Enable Component Reuse with Clone Detection

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. Clones are modeling patterns that have identical block types and connections. The Identify Modeling Clones tool identifies clones across referenced model boundaries.

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

Exact 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 as long as the block connections are the same.

  • Blocks and signals can have different names.

These two examples show you how to identify exact clones and similar clones. You can replace exact clones with links to library blocks.

Identify Exact Clones

This example shows how to use the Identify Modeling Clones tool to identify exact graphical clones and exact functional clones and replace them with links to library blocks. Exact graphical clones must have identical block parameter settings and values. Exact functional clones must have identical block parameter values, but they can have different variables for parameter settings, as long as those variables evaluate to the same value.

  1. Open the example model ex_clone_detection and the corresponding library 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 the following command clonedetection('ex_clone_detection').

  4. Select the folder Identify Modeling Clones. If you want to perform all or some of the checks in the Identify Modeling Clones tool, you can click Run Selected Checks. This example takes you through each check one at a time.

  5. Select the folder Identify Exact Clones. If you want to perform some or all checks pertaining to exact clones, you can click Run Selected Checks. This example takes you through each check one at a time.

Identify Library Clones

Identify all modeling patterns that are exact graphical clones of a library subsystem. 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 Identify library clones and replace them with links to library blocks.

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

  3. In the Prefix of model name field, specify a prefix for the refactored model name or use the default value.

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

  5. Click Refactor Model.

  6. In the bottom Results table, there is a hyperlink to the refactored model. In the refactored model, the subsystems libsubsystem and SS2 link to the library subsystem. The refactored model is in the folder that has the prefix m2m_ plus the model name.

Identify Graphical Clones

Now identify all exact graphical subsystem clones and replace them with links to a library block.

  1. Select Identify graphical clones and replace them with library blocks.

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

  3. In the Prefix of model name field, specify a prefix for the refactored model name or use the default value. In the New library file name field, specify a name or use the default value.

  4. Click Run This Check. In the Results table, the left column contains hyperlinks to the subsystem clones. The right column lists whether the subsystem is virtual or atomic and provides the subsystem function packaging. SS1 and SS4 are exact graphical clones.

  5. Click Refactor Model.

  6. In the bottom Results table, there is a hyperlink to the refactored model. In the refactored model, the subsystems SS1 and SS4 link to the new library subsystem. The refactored model is in the folder that has the prefix m2m_ plus the model name. The new library is on the MATLAB path. You can also click hyperlinks to navigate to blocks before and after clone replacement.

Identify Functional Clones

Identify all exact functional subsystem clones and replace them with links to a library block. The tool evaluates the values of block parameter settings by performing an update diagram.

  1. Select Identify functional clones and replace them with links to library blocks.

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

  3. In the Prefix of model name field, specify a prefix for the refactored model name or use the default value.

  4. Click Run This Check. In the bottom Results table, the left column contains hyperlinks to the subsystem clones. The right column lists whether the subsystem is virtual or atomic and provides the subsystem function packaging. SS3 and SS5 are exact functional clones.

  5. Click Refactor Model.

  6. In the bottom Results table, there is a hyperlink to the refactored model. In the refactored model, the subsystems SS3 and SS5 link to the new library subsystem. The refactored model is in the folder that has the prefix m2m_ plus the model name. The new library is on the MATLAB path. You can also click hyperlinks to navigate to blocks before and after clone replacement.

Identify Similar Clones

This example shows how to use the Identify Modeling Clones tool to identify similar clones. Similar graphical clones and similar functional clones have identical block types and connections, but they can have different block parameter settings and values. The difference between similar graphical clones and similar functional clones is that the tool identifies similar graphical clones in all model regions including inactive variants and commented-out regions.

  1. Select Identify similar library clones.

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

  3. Click Run This Check. In the Results table, the left column contains hyperlinks to the modeling clones. The right column contains hyperlinks to the corresponding library subsystems. The model ex_clone_detection does not contain any library clones with differences in block parameter settings, so this check does not identify any new clones.

  4. Select Identify similar graphical clones.

  5. Click Run This Check. In the Results table, the left column contains hyperlinks for each clone group. The tool identifies two new clone groups that the checks for exact clones did not identify. In Clone Group 1, the values and settings of the Gain blocks are different. In Clone Group 3, the values of the Gain blocks are the same but the settings are different.

  6. Select Identify similar functional clones.

  7. Click Run This Check. In the Results table, the left column contains hyperlinks for each clone group. The tool identifies one of the three clone groups that the check for similar graphical clones identifies. The tool does not identify the other two clones because at least one of the two clones are in inactive variants.

Additional Information

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

  • For two similar clones, you can enable component reuse by manually replacing each clone instance with a link to a masked library block. For more information, see Block Masks (Simulink).

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

Related Topics

Was this topic helpful?