model advisor是simulink自帶的靜態(tài)模型檢查器,能夠根據設定的規則對模型進(jìn)行檢查,并且能夠進(jìn)行部分錯誤的自動(dòng)修改。在汽車(chē)電子以及相關(guān)基于MBD(Model Based Design)開(kāi)發(fā)的嵌入式項目中有重要的作用,能夠幫助開(kāi)發(fā)人員和團隊搭建高質(zhì)量的模型和使用高度統一的風(fēng)格。自帶的檢查規則中,包含MAAB這種行業(yè)中應用廣泛的規則,隨著(zhù)團隊或者項目進(jìn)程的積累,后續會(huì )慢慢發(fā)展出屬于自己的一套建模規則和體系。此時(shí)則需要進(jìn)行對model advisor的檢查規則進(jìn)行自定義。
自定義規則分兩種情況,一種是對現有規則進(jìn)行勾選,組合成團隊需要的檢查體系,另外一種是編寫(xiě)腳本,創(chuàng )建新的檢查規則。在確認現有檢查規則中無(wú)法滿(mǎn)足所需要的檢查,則可以進(jìn)行新規則的編寫(xiě)。例如:
Simplest。這條模型檢查規則在官方自帶的檢查中就沒(méi)有,如果想要統一團隊風(fēng)格只能通過(guò)編寫(xiě)腳本或者組織人工審查,顯然作為程序員的我們對于這種選擇問(wèn)題的思考是不存在的,答案只有一個(gè),代碼擼起來(lái)。
本文主要針對這條規則需求進(jìn)行編寫(xiě)腳本的詳細說(shuō)明和示例。
既然是在model advisor中添加新的檢查規則,就需要將編寫(xiě)的檢查規則都放到統一文件夾目錄下,不能與其它檢查規則混用。
%--- test_int_round_mode_check function description----------------------------function test_int_round_mode_checkint_round_mode = ModelAdvisor.Check('int_round_mode');int_round_mode.Title = 'int_round_mode:Check the integer rounding mode 檢查整數舍入模式設定';int_round_mode.TitleTips = ['Fail if the rounding mode config is not <Simplest>'... '該模塊設置如果不為<Simplest>則判斷為違反本條規則'];int_round_mode.setCallbackFcn(@test_int_round_mode_checkCallback,'None','StyleOne');% publish the checkmdladvRoot = ModelAdvisor.Root;mdladvRoot.publish(int_round_mode,'TEST'); 這些代碼定義了檢查規則的名字、簡(jiǎn)介、調用具體函數的句柄test_int_round_mode_checkCallback,并將這條規則發(fā)布在TEST文件夾下。
model advisor中,允許進(jìn)行部分規則的自動(dòng)修改,就是點(diǎn)一個(gè)按鈕直接把模型改的符合本條檢查規則,這個(gè)功能十分實(shí)用,尤其對于規模龐大的模型簡(jiǎn)直就是福音,所以回到本示例中,因為需要檢查的內容單一,規則明確,所以可以增加自動(dòng)修改的功能。當檢查出整數舍入模式?jīng)]有設置成Simplest的模塊,點(diǎn)擊一個(gè)按鈕,自動(dòng)將這些模塊舍入模式設成Simplest。
% fix action descriptionint_round_modeAct = ModelAdvisor.Action;int_round_modeAct.Name = 'Fix settings';int_round_modeAct.Description = ['Click the button to set all blocks to Simplest'... '按下按鈕,將所有模塊設置成 Simpleset'];int_round_modeAct.setCallbackFcn(@test_int_round_mode_checkActCallback);int_round_mode.setAction(int_round_modeAct);這段代碼定義了修復按鈕,按鈕名字為Fix settings,點(diǎn)擊按鈕的句柄為test_int_round_mode_checkActCallback。
find_system(system,'RegExp','On','RndMeth','.'); 可以找出模型中所有具有 integer rouding mode 配置選項的模塊。 get_param(all_rnd_meth_block{i},'RndMeth');得到模塊的整形舍入模式設定選項int_round_mode_fc = ModelAdvisor.FormatTemplate('TableTemplate'); int_round_mode_fc.setColTitles({'Block','Old Setting','New Setting'});int_round_mode_fc.addRow({all_rnd_meth_block{i},current_rnd,correct_cfg_rnd});set_param(all_rnd_meth_block{i},'RndMeth',correct_cfg_rnd);%------------------------------------------------------------------------------% Simulink scrip for customer check which used in model advisor.% MATLAB version: R2017a% Shibo Jiang 2017/8/20% Version: 0.1% Instructions: Type the 'sl_refresh_customizations' in matlab command % window with all the model closed. %------------------------------------------------------------------------------function sl_customization(cm)% --- register custom checkscm.addModelAdvisorCheckFcn(@test_int_round_mode_check);%-----Start of test_int_round_mode_check--------------------------------------- %--- test_int_round_mode_check function description------------------------ function test_int_round_mode_check int_round_mode = ModelAdvisor.Check('int_round_mode'); int_round_mode.Title = 'int_round_mode:Check the integer rounding mode'; int_round_mode.TitleTips = ['Fail if the rounding mode config is not ' ... '<Simplest>']; int_round_mode.setCallbackFcn(@test_int_round_mode_checkCallback, ... 'None','StyleOne'); % fix action description int_round_modeAct = ModelAdvisor.Action; int_round_modeAct.Name = 'Fix settings'; int_round_modeAct.Description = ['Click the button to set all blocks' ... ' to Simplest']; int_round_modeAct.setCallbackFcn(@test_int_round_mode_checkActCallback); int_round_mode.setAction(int_round_modeAct); % publish the check mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(int_round_mode,'TEST'); %--------------creates test_int_round_mode_checkCallback function---------- function int_round_mode_result = test_int_round_mode_checkCallback(system) int_round_mode_obj = Simulink.ModelAdvisor.getModelAdvisor(system); int_round_mode_result={}; int_round_mode_fc = ModelAdvisor.FormatTemplate('ListTemplate'); int_round_mode_fc.setInformation(['This check looks for the blocks' ... ' which integer rounding mode is not configed as [Simplest]']); % find all blocks which have 'integer ronuding mode' setting all_rnd_meth_block = find_system(system,'RegExp','On','RndMeth','.'); wrong_rnd_flag = 0; if ~isempty(all_rnd_meth_block) % find wrong config block length_rnd = length(all_rnd_meth_block); j = 1; correct_cfg_rnd = 'Simplest'; for i = 1:length_rnd current_rnd = get_param(all_rnd_meth_block{i},'RndMeth'); if 0 == strcmp(correct_cfg_rnd,current_rnd) wrong_rnd_flag = 1; wrong_rnd_block{j} = all_rnd_meth_block{i}; j = j + 1; else % Do nothing end end else wrong_rnd_flag = 0; end % report finding results if 0 == wrong_rnd_flag int_round_mode_fc.setSubResultStatusText(['Check has passed.']); int_round_mode_fc.setSubResultStatus('pass'); int_round_mode_obj.setCheckResultStatus(true); else int_round_mode_fc.setSubResultStatusText(['Check has failed.' ... ' The integer rounding mode config is wrong']); int_round_mode_fc.setListObj(wrong_rnd_block); int_round_mode_fc.setSubResultStatus('warn'); int_round_mode_fc.setRecAction(['Config the integer rounding' ... ' mode as [Simplest] .']); int_round_mode_obj.setCheckResultStatus(false); int_round_mode_obj.setActionEnable(true); end int_round_mode_fc.setSubBar(0); int_round_mode_result{end+1} = int_round_mode_fc; %--------------creates test_int_round_mode_checkActCallback function------- function int_round_mode_ActResult = ... test_int_round_mode_checkActCallback(taskobj) int_round_mode_obj = taskobj.MAObj; int_round_mode_ActResult = {}; system = getfullname(int_round_mode_obj.System); int_round_mode_fc = ModelAdvisor.FormatTemplate('TableTemplate'); int_round_mode_fc.setColTitles({'Block','Old Setting','New Setting'}) % find all blocks which have 'integer ronuding mode' setting all_rnd_meth_block = find_system(system,'RegExp','On','RndMeth','.'); % fix wrong config block length_rnd = length(all_rnd_meth_block); correct_cfg_rnd = 'Simplest'; for i = 1:length_rnd current_rnd = get_param(all_rnd_meth_block{i},'RndMeth'); if 0 == strcmp(correct_cfg_rnd,current_rnd) int_round_mode_fc.addRow({all_rnd_meth_block{i}, ... current_rnd,correct_cfg_rnd}); set_param(all_rnd_meth_block{i},'RndMeth',correct_cfg_rnd); else % Do nothing end end int_round_mode_fc.setSubBar(0); int_round_mode_ActResult = int_round_mode_fc; int_round_mode_obj.setActionEnable(false);%----End of test_int_round_mode_check -----------------------------------------將腳本放到matlab工作目錄下,然后在命令窗口輸入sl_refresh_customizations。
打開(kāi)模型,并打開(kāi)model advisor
點(diǎn)擊檢查本條規則
點(diǎn)擊修復按鈕

編寫(xiě)的規則存在一些使用上的bug,和Matlab版本有關(guān),例如在Matlab2017a中,僅在第一次打開(kāi)matlab時(shí)才能使用sl_refresh_customizations命令進(jìn)行model advisor的規則更新,如果修改了腳本代碼,再次進(jìn)行更新則會(huì )失敗,此時(shí)需要重啟Matlab才能更新,而這個(gè)問(wèn)題在Matlab2016a中不存在。
聯(lián)系客服