www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cageview/+optimoutput/ConSummaryView.m

    classdef ConSummaryView < mbcgui.multiview.View
    %cageview.optimoutput.ConSummaryView class
    %   cageview.optimoutput.ConSummaryView extends mbcgui.multiview.View.
    %
    %    cageview.optimoutput.ConSummaryView properties:
    %       Parent - Property is of type 'MATLAB array'
    %       Position - Property is of type 'rect'
    %       Enable - Property is of type 'on/off'
    %       Visible - Property is of type 'on/off'
    %       UserData - Property is of type 'MATLAB array'
    %       Tag - Property is of type 'string'
    %       MessageService - Property is of type 'handle'
    %       Options - Property is of type 'handle vector'
    %       Actions - Property is of type 'handle vector'
    %       UIContextMenu - Property is of type 'MATLAB array'
    %
    %    cageview.optimoutput.ConSummaryView methods:
    %       gettitle - Return string to use as title for view
    %       pGetTableData - Get the data to fill the table
    %       redraw - Redraw the constraint summary table
    %       redrawFeasible - Redraw the feasible state.
    
    
    %  Copyright 2005-2015 The MathWorks, Inc.
    
    properties (Access=protected, AbortSet)
        %HTABLE Property is of type 'handle'
        hTable = [];
    end
    
    methods  % constructor block
        function obj = ConSummaryView(varargin)
        %ConSummaryView Constructor for ConSummaryView
        %  OBJ = ConSummaryView(PROP, VALUE) constructs a class that displays
        %  a summary of the constraint values at the current point.
        
        % Call the inherited constructor
        obj@mbcgui.multiview.View(varargin{ : }); % converted super class constructor call
        
        P = com.mathworks.toolbox.mbc.gui.peer.OptimConSummaryTablePeer;
        obj.hTable = mbcwidgets.Table1D(P, ...
            'Parent', obj.Parent, ...
            'Position', obj.Position, ...
            'Visible', obj.Visible, ...
            'SelectionMode', 'MultiRegion', ...
            'UIContextMenu', obj.UIContextMenu);
        obj.hTable.Peer.setBorder([]);
        obj.ContentHandle = obj.hTable;
        cnames = {'Name', 'Description', 'Constraint Value', 'Left Value', 'Right Value'};
        obj.hTable.Peer.setColumnData(cnames);
        obj.hTable.Peer.setColumnWidths([100 350 90 90 90]);
        
        obj.addListeners(handle.listener(obj.hTable.Peer, 'MousePressed', {@i_passonbtndown, obj}));
        
        if ~isempty(obj.MessageService)
            obj.pPostSetMessageService;
        end
        end  % ConSummaryView
        
    end  % constructor block
    
    methods  % public methods
        %----------------------------------------
        function str = gettitle(obj) %#ok<MANU>
        %GETTITLE Return string to use as title for view
        %  STR = GETTITLE(OBJ) returns a string that should be used as a title for
        %  the container the view sits in.
        
        str = 'Constraint Summary';
        
        end  % gettitle
        
        %----------------------------------------
        function [ConFeas, Data, ConIcons] = pGetTableData(obj)
        %PGETTABLEDATA Get the data to fill the table
        %   [CONFEAS, DATA, CONICONS] = PGETTABLEDATA(OBJ) retrieves the data to
        %   fill the table from the optim message service in OBJ.
        
        ms = obj.MessageService;
        RunIdx = ms.getFocusRun;
        SolIdx = ms.getFocusSolution;
        
        Out = ms.getOptimOutput;
        or = getOptimRunner(Out);
        
        [ConLen, nCon] = numConstraints(or);
        if nCon>0
            
            % Number of constraint labels in optimization
            nConLab = length(ConLen);
            
            % Evaluate the current solution for all constraints
            [ConData, ConName] = getSingleSolution(Out, RunIdx, SolIdx, ...
                'OutputFormat', 'cell', ...
                'OutputContents', {'ConstraintFeas', 'Constraints', ...
                'ConstraintLHS', 'ConstraintRHS', 'FreeVars'});
            
            % Get the constraint summary information
            ItemNames = ConName(1:nConLab);
            feasValue = ConData(1:nConLab);
            conValue = ConData(nConLab+1:2*nConLab);
            leftValue = ConData(2*nConLab+1:3*nConLab);
            rightValue = ConData(3*nConLab+1:4*nConLab);
            freeValue = ConData(4*nConLab+1:end);
            [feasSummary, sumConDesc, conSummary, leftSummary, rightSummary] = ...
                getConstraintSummary(or, ItemNames, feasValue, conValue, leftValue, ...
                rightValue, freeValue, RunIdx);
            
            % Feasibility
            ConFeas = [feasSummary{:}]';
            
            if nargout > 1
                % Number of rows in table
                sumLen = cellfun('length', sumConDesc);
                nRows = sum(sumLen);
                
                % Name column
                conNameSummary = cell(nRows, 1);
                nameIdx = [1, 1+cumsum(sumLen)];
                nameIdx(end) = [];
                conNameSummary(nameIdx) = ConName(1:nConLab)';
                AllConIcons = getItemIcons(or, 'constraint');
                ConIcons = cell(nRows, 1);
                ConIcons(nameIdx) = AllConIcons';
                
                % Description column
                sumConDesc = [sumConDesc{:}]';
                
                % Constraint, left and right values
                sumConData = zeros(nRows, 3);
                sumConData(:, 1) = [conSummary{:}]';
                sumConData(:, 2) = [leftSummary{:}]';
                sumConData(:, 3) = [rightSummary{:}]';
                Data = [conNameSummary(:), sumConDesc(:), num2cell(sumConData(:, 1:3))];
            end
            
        else
            ConFeas = false(0,1);
            if nargout > 1
                Data = cell(0,5);
                ConIcons = cell(0,1);
            end
        end
        
        end  % pGetTableData
        
        %----------------------------------------
        function redraw(obj)
        %REDRAW Redraw the constraint summary table
        %   REDRAW(OBJ) redraws the constraint summary table
        
        if obj.hasData ...
                && ~strcmp(obj.MessageService.CurrentSliceDirection, 'weightedsolution') ...
                && obj.MessageService.hasFocusIndex
            [ConFeas, Data, ConIcons] = pGetTableData(obj);
        else
            Data = cell(0,5);
            ConIcons = cell(0,1);
            ConFeas = false(0,1);
        end
        
        obj.hTable.Peer.setStoreSwingCalls(true);
        obj.hTable.Peer.setData(Data);
        obj.hTable.Peer.setIconData(ConIcons);
        obj.hTable.Peer.setConFeasible(ConFeas);
        obj.hTable.Peer.setStoreSwingCalls(false);
        
        end  % redraw
        
        %----------------------------------------
        function redrawFeasible(obj)
        %REDRAWFEASIBLE Redraw the feasible state.
        %   REDRAWFEASIBLE(OBJ) redraws the feasibility status of all the
        %   constraints.
        
        if ~obj.hasData ...
                || strcmp(obj.MessageService.CurrentSliceDirection, 'weightedsolution') ...
                || ~obj.MessageService.hasFocusIndex
            
            ConFeas = false(0,1);
        else
            ConFeas = pGetTableData(obj);
        end
        obj.hTable.Peer.setConFeasible(ConFeas);
        
        end  % redrawFeasible
        
    end  % public methods
    
    methods(Access=protected)
        %----------------------------------------
        function pPostSetMessageService(obj)
        %PPOSTSETMESSAGESERVICE Method that is called when the message service is set
        %  PPOSTSETMESSAGESERVICE(OBJ) is called in response to a new message
        %  service being set in the object.
        
        pPostSetMessageService@mbcgui.multiview.View(obj)
        
        if ~isempty(obj.MessageService)
            % Add redraw events
            obj.addMessageServiceListener( {'ObjectChanged', ...
                'FeasibleToleranceChanged', ...
                'SliceDirectionChanged', ...
                'CurrentFocusChanged'}, ...
                {{@i_refresh, obj}, ...
                {@i_refreshFeas, obj}, ...
                {@i_refresh, obj}, ...
                {@i_refresh, obj}});
        end
        
        obj.redraw;
        end  % pPostSetMessageService
        
        function setUIContextMenu(obj)
        obj.hTable.UIContextMenu = obj.UIContextMenu;
        end
    end
    
end  % classdef

function i_passonbtndown(~, ~, obj)
obj.notify('ButtonDown');
end  % i_passonbtndown

function i_refresh(~, ~, obj)
% Refresh all the table data
obj.redraw;
end  % i_refresh

function i_refreshFeas(~, ~, obj)
% Refresh all the table data
obj.redrawFeasible;
end  % i_refreshFeas