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
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.
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 in your model using clone detection Settings.
Subsystem clones: Identifies clones only in a subsystems.
Clones anywhere: Identifies clones anywhere in 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.
Open the model
ex_clone_detection. At the MATLAB® command line, enter:
Save the model to your working folder. A model must be open to access the app.
On the Apps tab, click Clone Detector. Alternatively, on the MATLAB command line enter:
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 Actions 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.
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 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 Identify and Replace Clones in Model Libraries.
The Maximum number of unmatched block parameters is
50by 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. The default size is set to
Identify Subsystem Clones in the Model
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.
Click Find Clones to identify clones.
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:
In the Clone Detection Results and Actions panel, on the Clone Results tab, a list of clone groups are displayed.
>symbol next to
Exact Clone Group 1to see all of the subsystems that are exact clones, the number of blocks, and the block differences. Repeat the same for
Similar Clone Group 1and
Similar Clone Group 2.
In the Clone Detection Actions and Results pane, click the Logs tab. Click the hyperlink on the Logs pane.
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.
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.
On the Clone Results tab, expand
Similar Clone Group 1and click the View Parameter Difference hyperlink.
On the Detected Clone Properties panel, click the
ex_clone_detection/SS5/G9hyperlink, which opens the gain block
G9in the subsystem
SS5, where you can access the parameter that are different from the baseline subsystem.
Change value of the gain parameter from
Band click Find Clones. This will reclassify
Similar Clone Group 1to
Exact Clone Group 2because you resolved the difference in the subsystems and converted it into an exact clone.
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 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.
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.
Click Replace Clones.
The model is refactored and the clones are replaced with links to the
newLibraryFilelibrary file in your working directory.
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 Results and Actions pane.
Identify and Replace Clones in Model Libraries
Open the library
ex_clone_library. At the MATLAB command line, enter:
Click Settings > Match Patterns with Libraries and select
ex_clone_library.slx. Then click Find Clones.
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.
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.