Main Content

プログラムによるサブシステム クローンの検出と置換

クローンは、同一のブロック タイプと接続をもつモデル化パターンです。サブシステム クローンからライブラリ ブロックを作成し、クローンをそれらのライブラリ ブロックへのリンクに置き換えることによりモデルをリファクタリングでき、コンポーネントの再利用が可能になります。クローンの詳細については、クローン検出を使用したコンポーネント再利用の有効化を参照してください。

モデルの送信プロセス中にプログラムでクローンをチェックすると、モデルが製品のメイン ブランチに展開される前に、サブシステムを再利用する方法を特定できます。モデルの更新時には、クローン検出器アプリとクローン検出 API を同時に使用できます。クローン検出 API を使用する場合、検出されたクローンは Simulink® エディターに表示されます。

この例では、クローン検出 API を使用し、サブシステム ブロックを含むライブラリ ファイルを作成して、クローンをそのライブラリ ファイル内のブロックへのリンクに置換することにより、単一モデル内のクローンを特定して置換する方法を説明します。

この例では、以下を使用する方法を学習します。

モデル内のクローンの特定

  1. モデル ex_detect_clones_B を開きます。

    openExample('ex_detect_clones_B');

    モデルを現在の作業ディレクトリに保存します。

  2. サブシステム クローンを検出するには、関数 Simulink.CloneDetection.findClones() を使用します。この関数は cloneResults というオブジェクトを作成します。

     cloneResults = Simulink.CloneDetection.findClones('ex_detect_clones_B')
    
    cloneResults = 
      Results with properties:
            Clones: [1×1 struct]
      ExceptionLog: ''

    cloneResults オブジェクトには Clones があります。これは、SummaryCloneGroups の 2 つのフィールドをもつ構造体です。

     cloneResults.Clones
    
    ans = 
      struct with fields:
            Summary: [1×1 struct]
        CloneGroups: [1×2 struct]
    
  3. Summary フィールドを表示します。

     cloneResults.Clones.Summary
    
    ans = 
      struct with fields:
                     CloneGroups: 2
                   SimilarClones: 5
                     ExactClones: 0
                          Clones: 5
        PotentialReusePercentage: [1×1 struct]
    

    この例では、モデルにはサブシステムのパターンが一致する CloneGroups が 2 個、および SimilarClones が 5 個、ExactClones が 0 個、サブシステム Clones が 5 個あります。

  4. CloneGroups フィールドを表示します。

     cloneResults.Clones.CloneGroups
    
    ans = 
      1×2 struct array with fields:
        Name
        Summary
        CloneList
    

    この例のモデルは 2 つの CloneGroups の配列を返します。各配列には、NameSummary および CloneList が含まれます。

  5. 最初のクローン グループの詳細を表示します。

     cloneResults.Clones.CloneGroups(1)
    
    ans = 
      struct with fields:
             Name: 'Similar Clone Group 1'
          Summary: [1×1 struct]
        CloneList: {3×1 cell}
    
  6. Summary を表示します。

     cloneResults.Clones.CloneGroups(1).Summary
    
    ans = 
      struct with fields:
        ParameterDifferences: [1×1 struct]
                      Clones: 3
              BlocksPerClone: 8
                   CloneType: 'Similar'
             BlockDifference: 1
    
  7. 最初の CloneGroup の CloneList を表示します。

     cloneResults.Clones.CloneGroups(1).CloneList
    
    ans =
      3×1 cell array
        {'ex_detect_clones_B/Subsystem1'}
        {'ex_detect_clones_B/Subsystem2'}
        {'ex_detect_clones_B/Subsystem3'}
    

    同様に、上記の手順を使用して他の CloneGroups の結果を確認できます。

モデル内のクローンの置換

  1. モデル内のクローンを置換するには、関数 Simulink.CloneDetection.replaceClones() を使用します。この関数は、関数 findClones からの cloneResults オブジェクトを使用します。

     cloneReplacementResults = Simulink.CloneDetection.replaceClones(cloneResults)
    
    cloneReplacementResults = 
      ReplacementResults with properties:
        ReplacedClones: [1×5 struct]
        ExcludedClones: {}
    

    cloneReplacementResults オブジェクトには 2 つのプロパティ ReplacedClonesExcludedClones が含まれます。

  2. ReplacedClones プロパティの内容を表示します。

    cloneReplacementResults.ReplacedClones
    
    ans = 
      1×5 struct array with fields:
        Name
        ReferenceSubsystem
    

    1 行 5 列の配列は、関数がモデル内の 5 つのサブシステム クローンを置換したことを示しています。

  3. 置換されたサブシステム クローンのリストを表示します。

     struct2table(cloneReplacementResults.ReplacedClones)
    
    ans =
      5×2 table
                       Name                         ReferenceSubsystem      
        ___________________________________    _____________________________
        {'ex_detect_clones_B/Subsystem1'}    {'newLibraryFile/Subsystem1'}
        {'ex_detect_clones_B/Subsystem2'}    {'newLibraryFile/Subsystem1'}
        {'ex_detect_clones_B/Subsystem3'}    {'newLibraryFile/Subsystem1'}
        {'ex_detect_clones_B/SS3'       }    {'newLibraryFile/SS1'       }
        {'ex_detect_clones_B/SS4'       }    {'newLibraryFile/SS1'       }
    

サブシステム参照ブロックを使用したクローンの特定

  1. モデルとライブラリ ファイルを現在の作業ディレクトリに保存します。

    ex_detect_clones_E
    clones_library
  2. Simulink.CloneDetection.Settings() クラスを使用して、モデル内のクローン検出用の特定の条件を指定するオブジェクトを作成します。

     cloneDetectionSettings = Simulink.CloneDetection.Settings()
    
    cloneDetectionSettings = 
    
      Settings with properties:                            
                                IgnoreSignalName: 0
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0
                          ExcludeInactiveRegions: 0
                          SelectedSystemBoundary: ''
                         DetectClonesAcrossModel: 0
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 50
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {}
                                         Folders: {}
  3. ParamDifferenceThreshold パラメーターを設定します。このパラメーターは、クローンと見なされるのに必要なサブシステムの相違点の数を指定します。

     cloneDetectionSettings.ParamDifferenceThreshold = 0
    
    cloneDetectionSettings = 
    
      Settings with properties:                            
                                IgnoreSignalName: 0
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0
                          ExcludeInactiveRegions: 0
                          SelectedSystemBoundary: ''
                         DetectClonesAcrossModel: 0
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 0
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {}
                                         Folders: {}

    0 の値は、サブシステムが同一でなければならないことを示します。

  4. cloneDetectionSettings オブジェクトのクローン パターンとの照合に使用する参照ライブラリ ファイルを追加します。この例では、SSL1SSL2 がライブラリ clones_library のサブシステム パターンです。

    cloneDetectionSettings = cloneDetectionSettings.addLibraries('clones_library')
    
    cloneDetectionSettings = 
    
      Settings with properties:                            
                                IgnoreSignalName: 1
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0
                          ExcludeInactiveRegions: 0
                          SelectedSystemBoundary: ''
                         DetectClonesAcrossModel: 0
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 50
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {'C:\Users\Examples\clones_library.slx'}
                                         Folders: {}
  5. クローンを検出するには、モデル名と cloneDetectionSettings オブジェクトを使用して関数 Simulink.CloneDetection.findClones() を実行します。

     cloneResults = Simulink.CloneDetection.findClones('ex_detect_clones_E', cloneDetectionSettings)
    
    cloneResults = 
      Results with properties:
        Clones: [1×1 struct]
    
     cloneResults.Clones.Summary
    
    ans = 
      struct with fields:
                     CloneGroups: 2
                   SimilarClones: 5
                     ExactClones: 0
                          Clones: 5
        PotentialReusePercentage: [1×1 struct]
    

    この例では、モデルには CloneGroups が 2 個、SimilarClones が 5 個、ExactClones が 0 個、サブシステム Clones が 5 個あります。

  6. 最初の CloneGroup の詳細を表示します。

     cloneResults.Clones.CloneGroups(1)
    
    ans = 
      struct with fields:
             Name: 'clones_library/SSL1'
          Summary: [1×1 struct]
        CloneList: {3×1 cell}
    

条件を使用したクローンの置換

  1. 1.関数 replaceClones の条件を指定するには、Simulink.CloneDetection.ReplacementConfig() クラスを使用してハンドルを作成します。

     cloneReplacementConfig = Simulink.CloneDetection.ReplacementConfig()
    
    cloneReplacementConfig = 
      ReplacementConfig with properties:
        LibraryNameToAddSubsystemsTo: 'newLibraryFile'
                       IgnoredClones: {}
    
  2. サブシステムを IgnoredClones リストに追加します。この例では、Subsystem1 を無視してクローンに置換されないようにします。

    cloneReplacementConfig.addCloneToIgnoreList('ex_detect_clones_E/Subsystem1')
    
    ans = 
      ReplacementConfig with properties:
        LibraryNameToAddSubsystemsTo: 'newLibraryFile'
                       IgnoredClones: {'ex_detect_clones_E/Subsystem1'}
    
  3. クローンを置換するには、cloneResults および cloneReplacementConfig を入力引数として関数 replaceClones を使用します。

    cloneReplacementResults = Simulink.CloneDetection.replaceClones(cloneResults, cloneReplacementConfig)
    
    cloneReplacementResults = 
      ReplacementResults with properties:
        ReplacedClones: [1×4 struct]
        ExcludedClones: [1×1 struct]
    
  4. ReplacedClones プロパティを表示します。

    struct2table(cloneReplacementResults.ReplacedClones)
    
    ans =
      4×2 table
                       Name                    ReferenceSubsystem
        ___________________________________    __________________
        {'ex_detect_clones_E/SS3'       }     {'clones_library/SSL1'} 
        {'ex_detect_clones_E/SS4'       }     {'clones_library/SSL1'} 
        {'ex_detect_clones_E/Subsystem1'}     {'clones_library/SSL2'} 
        {'ex_detect_clones_E/Subsystem2'}     {'clones_library/SSL2'} 
    

    モデル内のサブシステム クローンが参照ライブラリの SSL1 および SSL2 Reference Subsystem ブロックに置換されました。

  5. ExcludedClones プロパティを表示します。

    struct2table(cloneReplacementResults.ExcludedClones)
    
    ans =
      1×2 table
                       Name                    ReferenceSubsystem
        ___________________________________    __________________
        {'ex_detect_clones_E/Subsystem1'}     {'unselected'} 
    

モデルの同一性のチェック

関数 Simulink.CloneDetection.checkEquivalency() を使用すると、更新されたモデルが元のモデルと等価であるかどうかを確認できます。この関数は、Simulink テスト マネージャーを使用し、保存されている元のモデルと更新されたモデルのシミュレーション結果を比較して、結果を checkEquiResults ハンドルに保存します。

 checkEquiResults = Simulink.CloneDetection.checkEquivalency(cloneReplacementResults)
[21-Dec-2020 16:35:13] Running simulations...
[21-Dec-2020 16:35:32] Completed 1 of 2 simulation runs
[21-Dec-2020 16:35:33] Completed 2 of 2 simulation runs

checkEquiResults = 
  EquivalencyCheckResults with properties:
    List: [1×1 struct]

同一性チェックの結果を表示します。

 checkEquiResults.List
ans = 
  struct with fields:
    IsEquivalencyCheckPassed: 1
               OriginalModel: 'm2m_ex_detect_clones_E/snapshot_2020_12_21_16_35_06_ex_detect_clones_E.slx'
                UpdatedModel: 'ex_detect_clones_E.slx'

プロパティ IsEquivalencyCheckPassed は 1 であり、モデルが等価であることを示しています。OriginalModel プロパティと UpdatedModel プロパティは、関数がどのモデルをチェックしたかを示しています。

関連するトピック