www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/+mbcmodelview/+testplan/CrossSectionView.m

    classdef CrossSectionView < mbcgui.multiview.View
    %mbcgui.multiview.CrossSectionView Creates a CrossSection plot and
    % MessageService and connects them together
    
    %  Copyright 2015 The MathWorks, Inc.
    
    properties
        %GraphView Handle to GraphView object
        GraphView;
        %EventListeners Holds listeners for the MessageService
        EventListeners = [];
    end
    
    methods
        function obj=CrossSectionView(varargin)
            %CrossSectionView Constructor for CrossSectionView class
            obj@mbcgui.multiview.View(varargin{:});
            modelInterface = mbccrosssectiongui.MBCModelInterface();
            
            obj.GraphView = mbccrosssectiongui.GraphView(modelInterface, obj.Parent);
            obj.ContentHandle = obj.GraphView;
            obj.addEventListeners();
        end
        
        function variableTable = createVariableTable(obj,parent)
            %createVariableTable Creates an variable slice table for the
            % cross-section view
            variableTable = mbccrosssectiongui.VariableSliceTable(parent, obj.GraphView.DataModel);
        end
        
        function s = serializeView(obj)
        %serializeView serialize cross-section view
        s = serializeView(obj.GraphView);
        s.CurrentTest = obj.MessageService.CurrentTest; 
        
        end
        
        function deserializeView(obj,s)
        %deserializeView deserialize cross-section view
        
        deserialize(obj.MessageService,s);
        deserializeView(obj.GraphView,s);
        
        end
        
    end
    
    methods (Access=private)
        function addEventListeners(obj)
            %addEventListeners Adds listeners to the MessageService for
            % when it updates
            if ~isempty(obj.MessageService)
                obj.EventListeners = [...
                    event.listener(obj.MessageService,'OptionChanged',@obj.onOptionChanged), ...
                    event.listener(obj.MessageService,'NodeChanged',@obj.onNodeChanged), ...
                    event.listener(obj.MessageService,'NodeUpdated',@obj.onNodeUpdated), ...
                    event.listener(obj.MessageService,'TestChanged',@obj.onTestChanged)
                    ];
            end
        end
        
        function onOptionChanged(obj,~,evt)
            %onOptionChanged Update data model with new message service
            % option
            changedOption = evt.Data.Option;
            obj.GraphView.DataModel.(changedOption) = obj.MessageService.(changedOption);
        end
        
        function onTestChanged(obj,ms,~)
            %onTestChanged
            setupDataModel(obj,ms)
        end
        
        function onNodeChanged(obj,ms,~)
            %onNodeChanged Update data model with new message service node
            setupDataModel(obj,ms)
        end
        
        function onNodeUpdated(obj,~,~)
        %onNodeUpdated update graphview
        
        update(obj.GraphView);
        end
        
        function setupDataModel(obj,ms)
            pointer = ms.Pointer;
            dm = obj.GraphView.DataModel;
            currNode = info(pointer);
            
            if ms.IsPointByPoint
                [dm.Models, dm.ModelIsFitted] = getPBPModels(currNode,ms.CurrentTest);
            else
                [dm.Models, dm.ModelIsFitted] = getNonPBPModels(currNode);
            end
            
            if isempty(dm.Models)
                dm.ModelNames = {};
            else
                dm.ModelNames = ResponseLabels(currNode); 
            end
            dm.NumModels = length(dm.ModelNames);
            
            if ~(dm.NumModels==0)
                allInputs = factors(currNode);
                if ms.IsPointByPoint
                    X = getdata(currNode,'fit');
                    
                    dm.NumInputs = size(X{1},2);
                    dm.InputNames = allInputs(1:dm.NumInputs);
                    
                    dm.Boundary = BoundaryModel(currNode,dm.Models{1},ms.CurrentTest);

                    [dm.XFitData, dm.YFitData] = getPointByPointFitData(pointer, X, ms.CurrentTest);                   
                else
                    dm.InputNames = allInputs;
                    dm.NumInputs = length(dm.InputNames);
                    
                    dm.Boundary = BoundaryModel(currNode,dm.Models{1});
                    if numstages(currNode) == 1
                        [dm.XFitData, dm.YFitData] = getOneStageFitData(pointer);
                    else
                        [dm.XFitData, dm.YFitData] = getTwoStageFitData(pointer);
                    end
                end
            end
            
            initialize(dm);
        end
        
    end
    
end

function [models,modelIsFitted] = getPBPModels(currNode, testId)
[models,modelIsFitted] = children(currNode,@(mdev) LocalModel(mdev,testId));
modelIsFitted = [modelIsFitted{:}];
end

function [models,modelIsFitted] = getNonPBPModels(currNode)
modelIsFitted = areModelsFitted(currNode);
models = children(currNode,@model);
end

function isFitted = areModelsFitted(currNode)
%findFittedModels Returns a list of the fitted models
allModelsPtr = children(currNode);
isFitted = parrayeval(allModelsPtr,@(ptr)hasBest(info(ptr)),{},@false);
end


function [xDataAsMatrix, yDataAsCell] = getPointByPointFitData(pointer, X, CurrentTest)
currNode = info(pointer);
[~,YData] = children(currNode,@(mdev) FitData(mdev,CurrentTest));
yDataAsCell = cellfun(@(x)double(x), YData, 'UniformOutput',0);
xDataAsMatrix = X{1}{CurrentTest}; %double(X{1}(:,:,dm.CurrentTest));
end

function [xDataAsMatrix, yData] = getOneStageFitData(pointer)
%getOneStageData Returns data points for a one-stage model

xData = getdata(info(pointer.children(1)));
xDataAsMatrix = double(xData);
yData = children(info(pointer),@(mdev) getdata(mdev,'Y'));
end

function [xDataAsMatrix, yData] = getTwoStageFitData(pointer)
%getOneStageData Returns data points for a one-stage model

xData = getdata(info(pointer.children(1)),'FIT');
xDataAsMatrix = double(concat(xData{:}));
yData = children(info(pointer),@(mdev) getdata(mdev,'Y'));
end