通过编程方式检测和更换子系统克隆
克隆是建模模式,相同的块类型和连接。您可以重构模型通过创建库块从子系统克隆和取代克隆这些库块的链接,这使您能够重用组件。关于克隆的更多信息,请参阅使组件重用通过克隆检测。
编程模型提交过程中检查克隆可帮助您识别机会重用前子系统模型部署到主要产品部门。当你更新模型,您可以使用克隆探测器同时应用和克隆器API。当您使用克隆器API时,发现克隆出现在模型中金宝app®编辑器。
这个例子展示了如何使用克隆检测api来识别并取代克隆在一个模型中通过创建一个库文件与子系统块和替换克隆库文件块的链接。
在这个例子中,您将了解如何使用:
金宝appSimulink.CloneDetection.checkEquivalency检查更新后的模型与原模型的等效性。
金宝appSimulink.CloneDetection.Settings添加条件
findClones
操作。金宝appSimulink.CloneDetection.ReplacementConfig添加条件
replaceClones
操作。
在一个模型识别克隆
打开模型
ex_detect_clones_B
。openExample (“ex_detect_clones_B”);
在当前工作目录中保存模型。
克隆找到子系统,使用函数
金宝appSimulink.CloneDetection.findClones ()
。这个函数创建一个对象cloneResults
。cloneResults = 金宝appSimulink.CloneDetection.findClones (“ex_detect_clones_B”)
cloneResults =结果与属性:克隆:1×1 struct ExceptionLog:”
的
cloneResults
对象有克隆
,这是一个结构有两个字段,总结
和CloneGroups
。cloneResults.Clones
ans =结构体字段:摘要:[1×1 struct] CloneGroups:[1×2结构)
查看
总结
字段。cloneResults.Clones.Summary
ans =结构体字段:CloneGroups: 2 SimilarClones: 5 ExactClones: 0克隆:5 PotentialReusePercentage: [1×1 struct]
在这个例子中,有两个模型
CloneGroups
模式匹配的子系统,5SimilarClones
,0ExactClones
和五大子系统克隆
。查看
CloneGroups
字段。cloneResults.Clones.CloneGroups
ans = 1×2结构体数组字段:名称CloneList总结
该模型在这个例子中返回一个数组
CloneGroups
。每个数组包含了的名字
,总结
和CloneList
。视图的细节首先克隆组。
cloneResults.Clones.CloneGroups (1)
ans =结构体字段:名称:“类似克隆组1”简介:[1×1 struct] CloneList:{3×1细胞}
查看
总结
。cloneResults.Clones.CloneGroups (1) .Summary
ans =结构体字段:ParameterDifferences: [1×1 struct]克隆:3 BlocksPerClone: 8 CloneType:“类似”BlockDifference: 1
查看
CloneList
第一个CloneGroup。cloneResults.Clones.CloneGroups (1) .CloneList
ans = 3×1单元阵列{' ex_detect_clones_B / Subsystem1 '} {' ex_detect_clones_B / Subsystem2 '} {' ex_detect_clones_B / Subsystem3 '}
类似地,您可以找到其他的结果
CloneGroups
使用上述步骤。
在一个模型取代克隆
取代克隆在一个模型中,使用函数
金宝appSimulink.CloneDetection.replaceClones ()
。这个函数使用cloneResults
对象的findClones
函数。cloneReplacementResults = 金宝appSimulink.CloneDetection.replaceClones (cloneResults)
cloneReplacementResults = ReplacementResults属性:ReplacedClones: [1×5 struct] ExcludedClones: {}
的
cloneReplacementResults
对象包含两个属性,ReplacedClones
和ExcludedClones
。视图的内容
ReplacedClones
财产。cloneReplacementResults.ReplacedClones
ans = 1×5结构体数组字段:名称ReferenceSubsystem
1-by-5数组表示函数取代五子系统模型中克隆。
视图的列表取代子系统克隆。
struct2table (cloneReplacementResults.ReplacedClones)
ans = 5×2表名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(魔法石,第1章的}{' ex_detect_clones_B / SS4 '} {newLibraryFile(魔法石,第1章的}
识别克隆使用子系统参考块
保存模型和库文件在当前工作目录中。
ex_detect_clones_E clones_library
使用
金宝appSimulink.CloneDetection.Settings ()
类来创建一个对象,指定某些条件寻找克隆在模型中。cloneDetectionSettings = 金宝appSimulink.CloneDetection.Settings ()
cloneDetectionSettings =属性设置:IgnoreSignalName: 0 IgnoreBlockProperty: 0 ExcludeModelReferences: 0 ExcludeLibraryLinks: 0 ExcludeInactiveRegions: 0 SelectedSystemBoundary:“DetectClonesAcrossModel: 0 FindClonesRecursivelyInFolders: 1 ParamDifferenceThreshold: 50 ReplaceExactClonesWithSubsystemReference: 0库:{}文件夹:{}
设置
ParamDifferenceThreshold
参数。该参数指定的数量差异,子系统必须要考虑克隆。cloneDetectionSettings。ParamDifferenceThreshold = 0
cloneDetectionSettings =属性设置:IgnoreSignalName: 0 IgnoreBlockProperty: 0 ExcludeModelReferences: 0 ExcludeLibraryLinks: 0 ExcludeInactiveRegions: 0 SelectedSystemBoundary:“DetectClonesAcrossModel: 0 FindClonesRecursivelyInFolders: 1 ParamDifferenceThreshold: 0 ReplaceExactClonesWithSubsystemReference: 0库:{}文件夹:{}
值0表示子系统必须是相同的。
添加一个引用库文件使用克隆的模式相匹配
cloneDetectionSettings
对象。在这个例子中,SSL1
和SSL2
子系统模式在图书馆吗clones_library
。cloneDetectionSettings = cloneDetectionSettings.addLibraries (“clones_library”)
cloneDetectionSettings =属性设置:IgnoreSignalName: 1 IgnoreBlockProperty: 0 ExcludeModelReferences: 0 ExcludeLibraryLinks: 0 ExcludeInactiveRegions: 0 SelectedSystemBoundary:“DetectClonesAcrossModel: 0 FindClonesRecursivelyInFolders: 1 ParamDifferenceThreshold: 50 ReplaceExactClonesWithSubsystemReference: 0库:{“C: \用户\ \ clones_library例子。基金的}文件夹:{}
找到克隆,执行函数
金宝appSimulink.CloneDetection.findClones ()
名称和使用模型cloneDetectionSettings
对象。cloneResults = 金宝appSimulink.CloneDetection.findClones (“ex_detect_clones_E”cloneDetectionSettings)
cloneResults =结果与属性:克隆:[1×1 struct]
cloneResults.Clones.Summary
ans =结构体字段:CloneGroups: 2 SimilarClones: 5 ExactClones: 0克隆:5 PotentialReusePercentage: [1×1 struct]
在这个例子中,有两个模型
CloneGroups
,五SimilarClones
、零ExactClones
,5子系统克隆
。视图的细节
CloneGroup
。cloneResults.Clones.CloneGroups (1)
ans =结构体字段:名称:“clones_library / SSL1”简介:[1×1 struct] CloneList:{3×1细胞}
克隆替换为条件
1。指定的条件
replaceClones
函数创建一个处理使用金宝appSimulink.CloneDetection.ReplacementConfig ()
类:cloneReplacementConfig = 金宝appSimulink.CloneDetection.ReplacementConfig ()
cloneReplacementConfig = ReplacementConfig属性:LibraryNameToAddSubsystemsTo:“newLibraryFile”IgnoredClones: {}
添加子系统
IgnoredClones
列表。在这个例子中,忽视Subsystem1
避免取而代之的一个克隆。cloneReplacementConfig.addCloneToIgnoreList (“ex_detect_clones_E / Subsystem1”)
ans = ReplacementConfig属性:LibraryNameToAddSubsystemsTo:“newLibraryFile”IgnoredClones: {' ex_detect_clones_E / Subsystem1 '}
取代克隆,可以使用
replaceClones
函数与cloneResults
和cloneReplacementConfig
作为输入参数。cloneReplacementResults = 金宝appSimulink.CloneDetection。replaceClones(cloneResults, cloneReplacementConfig)
cloneReplacementResults = ReplacementResults属性:ReplacedClones: [1×4 struct] ExcludedClones: [1×1 struct]
查看
ReplacedClones
财产。struct2table (cloneReplacementResults.ReplacedClones)
ans = 4×2表名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
参考参考图书馆取代了子系统子系统街区的克隆在模型中。查看
ExcludedClones
财产。struct2table (cloneReplacementResults.ExcludedClones)
ans = 1×2表名ReferenceSubsystem ___________________________________ _____________ {' ex_detect_clones_E / Subsystem1 '}{'没有'}
检查模型的等效性
您可以检查是否更新后的模型与原始模型是等价的金宝appSimulink.CloneDetection.checkEquivalency ()
函数。这个函数使用仿真软件测试经理比较仿真结金宝app果的保存与更新的模型和原始模型保存结果checkEquiResults
句柄。
checkEquiResults = 金宝appSimulink.CloneDetection.checkEquivalency (cloneReplacementResults)
(21 - 12月- 2020 16:35:13)运行模拟……(21 - 12月- 2020 16:35:32)完成1的2模拟运行(21 - 12月- 2020 16:35:33)完成2 2模拟运行checkEquiResults = EquivalencyCheckResults属性:列表:[1×1 struct]
查看结果等价性检验。
checkEquiResults.List
ans =结构体字段:IsEquivalencyCheckPassed: 1 OriginalModel: m2m_ex_detect_clones_E / snapshot_2020_12_21_16_35_06_ex_detect_clones_E。基金”UpdatedModel:“ex_detect_clones_E.slx”
房地产IsEquivalencyCheckPassed
是1,这表明该模型是等价的。的OriginalModel
和UpdatedModel
属性显示模型的功能检查。