www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/+tpsetup/DataDrivenIO.m

    classdef DataDrivenIO < handle
    %DataDrivenIO IO selector for data-driven modelling

    %  Copyright 2014-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (SetAccess=private)
        %Layout main layout
        Layout
    end
    
    properties (Access=private)
        hDataList
        hResponse
        DataInputCard
        IOList
        hBoundary
        %Parent parent of component
        Parent
        %Listeners store for listeners
        Listeners
        %Model data model objec
        Model
    end

    
    methods
        function obj = DataDrivenIO(Parent,Model)

        obj.Model = Model;
        obj.Parent = Parent;
        
        panel = mbcgui.container.layoutpanel('Parent',Parent,...
            'Title','Inputs and Responses',...
            'BorderType', 'etchedin', ...
            'LayoutBorder', [10 10 10 3]);
        
        
        % main data channel list on RHS
        dataPanel = mbcgui.container.titlebarpanel('Parent',panel,...
            'BarTitle','Data channels');
        obj.hDataList= uicontrol('Parent',dataPanel,...
            'BackgroundColor','w',...
            'Min',1,'Max',3,...
            'Style','listbox');
        set(dataPanel,'ContentHandle',obj.hDataList)
        
        % response list
        [selResponses,obj.hResponse] = createSelector(obj,panel,'Responses',false);
        
        % get example templates for different types from builtin templates
        tp = load('tpbuiltintemps.mat');
        
        obj.DataInputCard = xregcardlayout(panel,'NumCards',length(tp.templates));
        rowsizes = {-1,[70,-1],[-1 70]};
        for TPno=1:length(tp.templates)
            % one input setup for each test plan type
            T = tp.templates{TPno};
            [~,InputLabels]= type(T);
            BaseInputs = getInputs(T);
            obj.IOList{TPno} = gobjects(1,length(BaseInputs));
            InputLyt = cell(1,length(BaseInputs));
            for lvl=1:length(BaseInputs);
                % selector for each stage
                [InputLyt{lvl},obj.IOList{TPno}(lvl)] = createSelector(obj,panel,InputLabels{lvl},true);
            end
            typelyt = xreggridlayout(panel, ...
                'correctalg','on',...
                'dimension',[length(BaseInputs), 1],...
                'rowsizes',rowsizes{TPno},...
                'elements',InputLyt);
            
            attach(obj.DataInputCard,typelyt,TPno);
            
        end
        
        obj.hBoundary = uicontrol('Parent',panel,...
            'String','Fit boundary model',...
            'Value',1,...
            'Style','checkbox');
        lyt=xreggridbaglayout(panel,...
            'Dimension',[3,2],...
            'elements',{selResponses,obj.hBoundary,obj.DataInputCard,dataPanel,[],[]},...
            'rowsizes',[80 15 -1],...
            'colsizes',[-1 180],...
            'gapx',10,...
            'gapy',5,...
            'MergeBlock',{[1 3],[2 2]});  % data list spans whole height of component
        set(panel,'LayoutComponent',{lyt})
        obj.Layout = panel;
        
        obj.Listeners = event.proplistener(Model,Model.findprop('Selected'),'PostSet',@obj.onUpdate);
        end
        
        function onUpdate(obj,~,~)
        
        update(obj);
        end
        
        
        function update(obj)
        
        T = obj.Model.Selected;
        pSSF = obj.Model.pData;
        
        % setup test plan type
        TypeIndex = obj.Model.TypeIndex;
        set(obj.hBoundary,'Value',obj.Model.Boundary);
        set(obj.DataInputCard,'CurrentCard',TypeIndex);
        
        hInputList = obj.IOList{TypeIndex};
        
        if isnull(pSSF)
            AllChannels = {};
        else
            ssf = pSSF.info;
            AllChannels = sort(get(ssf,'Name'));
        end
        Inputs = getInputs(T);
        
        OK = true;
        % setup inputs
        for i=1:length(Inputs);
            InputNames = {Inputs{i}.Name};
            InputNames(cellfun('isempty',InputNames))= [];
            OK = OK && ~isempty(InputNames);
            pos = double(~isempty(InputNames));
            set(hInputList(i),'String',InputNames,'Value',pos);
            AllChannels = setdiff(AllChannels,InputNames,'stable');
        end
        obj.Model.InputsOK = OK;
        
        % setup response signals
        resp = responseNames(T);
        if ~isempty(resp)
            resp = intersect(resp,AllChannels,'stable');
            pos = double(~isempty(resp));
            set(obj.hResponse,'String',resp,'Value',pos);
            AllChannels = setdiff(AllChannels,resp,'stable');
        else
            % remove selected responses from list
            AllChannels = setdiff(AllChannels,get(obj.hResponse,'String'),'stable');
        end
        pos = double(~isempty(AllChannels));
        set(obj.hDataList,'String',AllChannels,'Value',pos);
        end
        
        function [selpnl,hList] = createSelector(obj,parent,title,doCheckInputs)
        
        selpnl = mbcgui.container.titlebarpanel('Parent',parent,...
            'BorderType','etchedin',...
            'BarTitle',title);
        
        hList= uicontrol('Parent',selpnl,...
            'Style','listbox',...
            'Min',1,'Max',3,...
            'BackgroundColor','w');
        
        hSel = xregGui.iconuicontrol('parent', selpnl,...
            'TooltipString', sprintf('Add to %s',lower(title)),...
            'TransparentColor', [0 255 0],...
            'ImageFile', xregrespath('addObject.bmp'),...
            'Tag',sprintf('Add%s',strrep(title,' ','')),...
            'callback', {@obj.selectChannel,hList,doCheckInputs});
        hDeSel = xregGui.iconuicontrol('parent', selpnl,...
            'TooltipString', sprintf('Remove from %s',lower(title)),...
            'TransparentColor', [0 255 0],...
            'ImageFile', xregrespath('removeObject.bmp'),...
            'Tag',sprintf('Remove%s',strrep(title,' ','')),...
            'callback', {@obj.deselectChannel,hList,doCheckInputs});
        
        lyt=xreggridbaglayout(selpnl,...
            'Dimension',[4,3],...
            'elements',{hList,[],[],[],...
            [],[],[],[],...
            hSel,[],hDeSel,[]},...
            'rowsizes',[20 2 20 -1],...
            'colsizes',[-1,20,20],...
            'border',[2 2 5 2],...
            'MergeBlock',{[1 4],[1 1]});
        set(selpnl,'ContentHandle',{lyt});
        end
        
        function selectChannel(obj,~,~,hList,doCheckInputs)
        % copy selected to LHS
        
        UnusedChannels = get(obj.hDataList,'String');
        NewDataValue = obj.hDataList.Value;
        if isempty(UnusedChannels) || isempty(NewDataValue)
            return
        end
        NewDataValue = unique(obj.hDataList.Value);
        
        % update IO list
        NewChannels = sort([get(hList,'String'); UnusedChannels(NewDataValue)]);
        pos = min(get(hList,'Value')+1,length(NewChannels));
        set(hList,'String',NewChannels,...
            'Value',pos)
        
        % update data channels list
        UnusedChannels = setdiff(UnusedChannels,UnusedChannels(get(obj.hDataList,'Value')));
        pos = min(get(obj.hDataList,'Value'),length(UnusedChannels));
        set(obj.hDataList,'String',UnusedChannels,...
            'Value',pos)
        
        if doCheckInputs
            checkInputs(obj,hList)
        end
        end
        
        function deselectChannel(obj,~,~,hList,doCheckInputs)
        % copy selected to RHS
        Channels = get(hList,'String');
        if isempty(Channels)
            return
        end
        UnusedChannels = sort([get(obj.hDataList,'String'); Channels(get(hList,'Value'))]);
        pos = min(get(obj.hDataList,'Value')+1,length(UnusedChannels));
        set(obj.hDataList,'String',UnusedChannels,...
            'Value',pos)
        
        Channels = setdiff(Channels,Channels(get(hList,'Value')),'stable');
        pos = min(get(hList,'Value'),length(Channels));
        set(hList,'String',Channels,...
            'Value',pos)
        if doCheckInputs
            checkInputs(obj,hList)
        end
        end
        
        function checkInputs(obj,hList)
        if obj.Model.InputsOK
            % checking current list is OK
            obj.Model.InputsOK = ~isempty(get(hList,'String'));
        else
            % have to check all input lists
            hInputList = obj.IOList{obj.Model.TypeIndex};
            OK = true;
            for i=1:length(hInputList)
                OK = OK && ~isempty(get(hInputList(i),'String'));
            end
            obj.Model.InputsOK = OK;
        end
        end
        
        
        function [T,OK] = finalize(obj)
        
        T = obj.Model.Selected;
        OK = true;
        obj.Model.Boundary = logical(get(obj.hBoundary,'Value'));
        TypeIndex = obj.Model.TypeIndex;
        
        ChangedInputs = true;
        BaseInputs = getInputs(T);
        
        for i=1:length(BaseInputs)
            NewChannels = get(obj.IOList{TypeIndex}(i),'String');
            if ~isequal(NewChannels,{BaseInputs{i}.Name}')
                ChangedInputs = true;
                % create inputs based with Name and Symbol set by channel name
                BaseInputs{i}= mbcinputfactor('Name',NewChannels,...
                    'Symbol',NewChannels);
            end
        end
        if ChangedInputs
            % update inputs if they have changed
            T = setInputs(T,BaseInputs);
        end
        
        NewResp = get(obj.hResponse,'String');
        T = responseNames(T,NewResp);

        end
        
    end
    
end