Refactor Models with the Clone Detector App and Model Transformer Tool

With Simulink® Check™, you can use the Model Transformer tool, and the Clone Detector app to refactor a model, improve model componentization and readability, and reuse components within a model.

Identify and Replace Clones with Links to Library Blocks

You can use the Clone Detector app to find clones of components and identify opportunities to reuse them. With the Clone Detector, you can:

  • Identify subsystem clones.

  • Create library blocks from clones.

  • Create a model that replaces clones with links to library blocks.

To use the Clone Detector:

  1. Open the example model ex_clone_detection.

    addpath(fullfile(docroot,'toolbox','simulink','examples'))
    ex_clone_detection
    
  2. Save the model to the current folder on the MATLAB path.

  3. On the Apps tab, click Clone Detector. To open the Clone Detector app programmatically, at the MATLAB command prompt type: clonedetection('ex_clone_detection').

  4. On the Clone Detector tab, click View and select Help, Results and Properties.

  5. Click Find Clones.

    The model contains three groups of clones. One group contains exact clones. Exact clones are highlighted in pink. The other two groups contain similar clones. These groups are highlighted in blue. Darker shades of blue indicate that the clones in those groups are more similar than clones in groups with lighter shades of blue.

  6. In the Clone Detection Actions and Results pane, in the Map Clone Groups to Library tab, for the Library to place clones parameter, specify a library. If you specify a new library name, the app creates the library.

  7. In the Clone Detector tab, click Replace Clones.

  8. The refactored model contains links to the library blocks in the newLibraryFile.slx file.

After you refactor, you can remove the latest changes from the model. In the Clone Detection Actions and Results pane, in the Logs tab, open the latest log and click Restore.

Each time you refactor a model, the app creates a backup model in the folder named with the prefix m2m_ plus the model name. If you have a Simulink Test license, you can verify the equivalency of the refactored model and the original model. Click Check Equivalency.

Replace Qualifying Modeling Patterns with Variant Blocks

To improve model componentization by replacing qualifying modeling patterns with Variant Source and Variant Subsystem blocks, use the Model Transformer tool.

The ex_variants_transformer model contains several modeling patterns that qualify for transformation into variant blocks.

  1. Open the example model ex_variants_transformer by entering these commands at the MATLAB command line:

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

  2. Save the model to your working folder.

  3. On the Apps tab, open the Model Transformer tool by selecting Model Transformer. Or, in the Command Window, type:

    mdltransformer(‘ex_variants_transformer’)

  4. Click Transform model to variant system.

  5. Click Run This Check. In the Analysis section, the Result table contains a list of system constants that qualify to be part of condition expressions in Variant Source or Variant Subsystem blocks.

  6. Click Refactor Model.

  7. Your working folder contains a folder called m2m_ex_variants_transformer. This folder contains the transformed model gen0_ex_variants_transformer.

  8. In the Action section, the Results table contains hyperlinks to the original and transformed models.

The Eliminate data store blocks allows you to replace data stores with blocks that improve model readability by making data dependency explicit. For an example, see Improve Model Readability by Eliminating Local Data Store Blocks.

The Transform table lookup into prelookup and interpolation check allows you to replace Lookup Table blocks into a single shared Prelookup block and multiple Interpolation blocks. For an example, see Improve Efficiency of Simulation by Optimizing Prelookup Operation of Lookup Table Blocks.

Related Topics