www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/+cgfillsetup/TableFiller.m

    classdef TableFiller < handle
    %CGFILLSETUP.TABLEFILLER table fill data for table fill wizard
     
    %  Copyright 2009-2012  The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties 
        %FILLER cgoptimtablefiller object
        Filler
    end
    
    properties(SetAccess=private)
        %PROJECT pointer to CAGE project
        Project
        %OPTIMOUTPUT pointer to cgoptimoutput
        OptimOutput
        %OPTIM pointer to cgoptim
        Optim
        %POSSIBLETABLES list of tables that could be filled with the
        %    optimization results
        PossibleTables
        %FILLITEMS list of variables and models that can be used to perform
        %    filling
        FillItems
        %PRIMARYEXPRESSION primary expression for optimization
        %    This is the model used in the first objective
        PrimaryExpression
        %ISNEW flag to determine whether the cgoptimtablefiller is being
        %   used for the first time. This property is used to determine
        %   whether default rules should be created.
        IsNew
    end
    
    properties(Dependent,SetAccess=private)
        %FILLNAMES list of names for fill items
        FillNames
        %VALIDEXPRESSIONS valid expressions that can be evaluated with optimization results
        ValidExpressions
        %Normalizers set of normalizers for tables being filled
        Normalizers
    end
    
    properties(Dependent)
        %Tables list of tables to be filled
        Tables
        %NormalizerLinks two column links for normalizers [pNormalizers pInputs]
        NormalizerLinks
        %FillFactors list of fill factors
        FillFactors
        %FillFunction filling function handle
        FillFunction
    end
    
    
    methods
        function F = TableFiller(otf,Project,pOpt,OptimOutput)
            %TABLEFILLER constructor
            %    F = TableFiller(otf,Project,pOpt,OptimOutput)
            
            F.Filler = otf;
            % treat as new if there are no tables in the table filler or the
            % fill factors haven't been defined
            F.IsNew = isempty(F.Tables);
            F.Project = Project;
            F.OptimOutput= OptimOutput;
            F.Optim = pOpt;
            
            F.PossibleTables = findCompatibleTables(F);
            
            F.FillItems = getFillFactorPtrs(F);
            F.PrimaryExpression = pOpt.getPrimaryExpression;
        end
        
        function pExpr = get.ValidExpressions(F)
        pExpr = getValidExpression(F.Filler, F.OptimOutput, F.Project);
        end
        
        function pNorm = get.Normalizers(F)
        
        pNorm= pveceval(F.Tables,@getinputs);
        pNorm = unique([pNorm{:}]);
        
        end
        
        function N = get.FillNames(F)
            if ~isempty(F.FillItems)
                N = pveceval(F.FillItems,@getname);
            else
                N = {};
            end
        end
        
        function pTab = get.Tables(F)
        pTab = getTables(F.Filler);
        end

        function set.Tables(F,pTab)
        F.Filler = setTables(F.Filler,pTab);
        end
        
        function pLink = get.NormalizerLinks(F)
        pLink = getNormalizerLinks(F.Filler);
        end

        function set.NormalizerLinks(F,pLink)
        F.Filler = setNormalizerLinks(  F.Filler, pLink );
        end

        function pFactors = get.FillFactors(F)
        pFactors = getFillFactors(F.Filler);
        end

        function set.FillFactors(F,pFactors)
        F.Filler = setFillFactors(  F.Filler, pFactors );
        end
        
        function fcn = get.FillFunction(F)
        fcn = getFillFn(F.Filler);
        end

        function set.FillFunction(F,fcn)
        F.Filler = setFillFn(  F.Filler, fcn );
        end
        
        

        function OK = wizard(F)
            %WIZARD setup wizard for table filling
            if isempty(F.PossibleTables) 
                OK = false;
                errordlg('There are no tables in the CAGE Project.','Fill Error','modal')
            else
                OK = xregwizard([], 'Table Filling from Optimization Results Wizard', ...
                    @cgfillsetup.TablePage.createPage, F);
            end
            
        end
        
        function [fillstat, fillmsg] = doFill(F,DOWAITBAR)
            %DOFILL perform table filling
            %    [fillstat, fillmsg] = doFill(F,DOWAITBAR)
            
            if nargin<2
                DOWAITBAR = true;
            end
            otf = F.Filler;
            [fillstat, fillmsg] = fill(otf, F.OptimOutput, F.Project, DOWAITBAR);
            % Save the table filler
            hOut = info(F.OptimOutput);
            hOut = setTableFiller(hOut, otf);
            F.OptimOutput.info = hOut;
            pOptNode =  F.Project.findItem('data',F.Optim,'node');
            % update optimization connections for new tables
            F.Project.UpdateConnections( pOptNode );
        end
        
        
        function setDefaultRules(F)
            %SETDEFAULTRULES set default rules for composite models
            
            otf = F.Filler;
            
            mdl = F.PrimaryExpression.info;
            
            if isMultiModal(mdl)
                SwitchModel = getSwitchModel(mdl);
                m = get(SwitchModel,'model');
                MS = ModeDataset(m);
                pInp = getinputs(SwitchModel);
                
                pFill = getFillFactors(otf);
                [OK,loc]=ismember(pInp,pFill);
                
                ModeName = pInp(end).getname;
                for i=1:length(MS.names)-1
                    tableno = loc(i);
                    if OK(i) && sum(pFill(tableno) == pFill)==1
                        % use mode rule if quantity is filling a single
                        % table
                        d = MS.data(:,i+1);
                        modes = find(~d);
                        switch length(modes)
                            case {0,size(d,1)}
                                Rule = '';
                            case size(d,1)-1
                                % Single mode selection (Mode==n)
                                Rule = sprintf('%s==%d',ModeName,find(d));
                            case 1
                                % Single mode exclusion (Mode~=n)
                                Rule = sprintf('%s~=%d',ModeName,find(~d));
                            otherwise
                                % Multiple row selection (ismember(Mode,[1,2]))
                                List = sprintf('%d,',find(d));
                                Rule = sprintf('ismember(%s,[%s])',ModeName,List(1:end-1));
                        end
                        % set rules
                        [otf,~] = setfilterfcn(otf,tableno,Rule,F.FillItems);
                        
                    end
                end
            end
            F.Filler = otf;
            F.IsNew = false;
        end
        
        function updateNormalizerLinks(F)
        %updateNormalizerLinks update normalizer links
        %    updateNormalizerLinks(F)
        % 
        %    Update the normalizer links and set any new links to their default. The default is
        %    detemined from the normalizer input. If the normalizer input has the name
        %    <modelname>_input and <modelname> is in ValidExpressions then the <modelname> is used.
        %    This default choice links in with the dummy variable created in the 'Create Tables from
        %    Model' wizard.
        
        pLinks= F.NormalizerLinks;
        pNorms = F.Normalizers(:);
        
        [ExistingLink,indexLink] = ismember(pNorms,pLinks(:,1));
        % find new normalizers that have not been considered before.
        pNew = [pNorms(~ExistingLink) mbcpointer(nnz(~ExistingLink),1)];
        pExpr = F.ValidExpressions;

        for i=1:size(pNew,1)
            % default input from 'Create Tables from Model' wizard is <modelname>_input
            
            % input to normalizer
            pInp = pNew(i,1).getinputs;
            ModelName = regexp(pInp.getname,'^(.*?)_input$','tokens','once');
            if ~isempty(ModelName)
                % set normalizer input to model
                index = find(strcmp(ModelName,pveceval(pExpr,@getname)));
                if ~isempty(index)
                    pNew(i,2) = pExpr(index);
                end
            else
                % use expression in FillItems. This will be a variable as tables cannot be
                % explicitly connected to a model but we don't need to make this distinction.
                index = find(strcmp(pInp.getname,pveceval(F.FillItems,@getname)));
                if ~isempty(index)
                    pNew(i,2) = F.FillItems(index);
                end
            end
        end
        % merge existing and new normalizers
        F.NormalizerLinks = [pLinks(indexLink(ExistingLink),:); pNew];
        end
        
    end
    
    
    methods (Access=private)
        
        function pTabs = findCompatibleTables(F)
            % FINDCOMPATIBLETABLES Produce a filtered list of CAGE tables
            %   PTABS = CGNOEMPTYNONORMTABS(PROOT) returns a list of pointers to CAGE
            %   tables in the CAGE project pointed to by PROOT. The returned list will
            %   not contain any normalizers or uninitialized tables.
            
            pTabs = gettables(F.Project.info);
            EmptyTabs = pveceval(pTabs, @isempty);
            EmptyTabs = [EmptyTabs{:}];
            SimpleTabs = pveceval(pTabs, @issimpletable);
            SimpleTabs = [SimpleTabs{:}];
            
            
            pTabs = pTabs(~EmptyTabs & SimpleTabs);
            

        end
        
        function pFill = getFillFactorPtrs(F)
            %GETFILLFACTORS list of possible fill factors for optimization output
            
            pResInps = getInputExpression(F.Filler, F.OptimOutput, F.Project, true);
            pFill = [pResInps, F.ValidExpressions];
        end
    end
end