www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/+mbcboundary/Tree.m

    classdef Tree < mbcutils.pointerinterface 
    %MBCBOUNDARY.TREE boundary tree class
    %    You access the boundary tree from the Boundary property of
    %    mbcmodel.testplan. The root of the boundary tree for a one-stage test
    %    plan is a mbcboundary.Tree. The root of the boundary tree for a
    %    two-stage test plan is a mbcboundary.TwoStageTree, and this object
    %    has mbcboundary.Tree objects in its Local, Global and Response
    %    properties. 
    %
    %    mbcboundary.Tree properties:
    %        Data      - test plan data used for fitting boundary models (read only)
    %        Models    - cell array of boundary models (read only)
    %        BestModel - combined boundary model containing all best boundary models in the tree (read only)    
    %        InBest    - logical array indicating which boundary models are selected as best
    %        TestPlan  - test plan object that contains this boundary tree (read only)
    %
    %    mbcboundary.Tree methods:
    %        Add            - add a boundary model to tree and fit with test plan data
    %        Update         - update a boundary model in tree and fit with test plan data
    %        Remove         - remove a boundary model from tree
    %        CreateBoundary - create a new boundary model
    %
    %See also TwoStageTree

%  Copyright 2009-2011 The MathWorks, Inc.

    properties (Dependent,SetAccess=private)
        %DATA test plan data used for fitting boundary models (read only)
        %    Data is a double matrix for one-stage, response and global
        %    boundary models. For local boundary models, Data is a cell
        %    array of double matrices with one cell per test.
        Data
        %MODELS cell array of boundary models in tree (read only)
        %
        %See also BestModel, Best
        Models
        %BESTMODEL combined boundary model containing all best boundary models in the tree (read only)
        %    BestModel is the boundary model combining the models selected as
        %    best. You can select which boundary models to include in the
        %    best model with InBest. If more than one boundary model is
        %    included in the best boundary model, the boundary model is a
        %    mbcboundary.Boolean. 
        %
        %See also InBest, Boolean
        BestModel
    end
    properties (Dependent)
        %INBEST logical array indicating which boundary models are selected as best
        %    You can combine models into a single boundary model for the
        %    boundary tree. The logical array specifies which models to include
        %    in the best boundary model. The best boundary model for the
        %    boundary tree is given in BestModel.
        %
        %See also BestModel, Models
        InBest
    end
    
    properties(SetAccess=protected)
        %TESTPLAN test plan object that contains this boundary tree (read only)
        TestPlan
    end
    
    methods (Hidden)
        function T = Tree(pRoot,TestPlan)
            %Tree - constructor not intended for public calls
            if nargin==0
                pRoot = xregpointer;
            end
            T@mbcutils.pointerinterface(pRoot);
            if nargin>1
               T.TestPlan =  TestPlan;
            end
        end
    end
    methods
        
        function B = get.Models(T)
            root = T.Object;
            % make array of boundarymodel objects
            B = children(root,@boundarymodel);
        end
        
        function B = get.BestModel(T)
            root = T.Object;
            % make boundarymodel objects
            Mdls = T.Models;
            if ~isempty(Mdls)
                nf = Mdls{1}.NumberOfInputs;
                con = getconstraint(root,nf);
                if ~isempty(con)
                    B = boundarymodel(con,[],true);
                else
                    B = [];
                end
            else
                B = [];
            end
        end
        
        function D = get.Data(T)
            root = T.Object;
            X = getdata(root, 'testId', 'all');
            lvl = getstages(root);
            if lvl == 1
                D= cell(X);
            else
                D= double(X);
            end
        end
        
        function S = get.InBest(T)
            root = T.Object;
            S = children(root,@isbest);
            S = logical([S{:}]);
        end
        
        function set.InBest(T,Best)
            root = T.Object;
            pChildren = children(root);
            if islogical(Best) && all(size(Best)==size(pChildren))
                setbest(root,pChildren(Best) );
            else
                error(message('mbc:boundarymodel:InvalidValue', length( pChildren )))
            end
        end
        
        function B = Add(T,B,InBest)
            %ADD add boundary model to boundary tree and fit with test plan data
            %    B = Add(T,B)
            %    B = Add(T,B,InBest)
            %
            %    Inputs:
            %      T      mbcboundary.Tree object
            %      B      new boundary model object
            %      InBest specifies whether to include the boundary
            %             model in the best boundary model for the
            %             boundary tree. By default, the new boundary
            %             model is included in the best model.
            %    The boundary model must have the same inputs as the
            %    boundary tree. The boundary model is always fitted when it
            %    is added to the boundary tree, to ensure that the fitting
            %    data is compatible with the test plan data. 
            %    The fitted boundary model is returned.
            %
            %See also Update, Remove, CreateBoundaryModel
      
            narginchk(2,3)
            if nargin<3
                % default InBest
                InBest = true;
            end

            pChild = makechildren(T.Object,false);
            AddChild(T.Object,pChild);
            try
                % call Update to assign boundary model to last child
                B = Update(T,numChildren(T.Object),B,InBest);
            catch ME
                % delete new node
                delete(info(pChild));
                % throw error so it looks like to came from Add
                throw(ME)
            end

        end

        function B = Update(T,Index,B,InBest)
            %UPDATE update boundary model in boundary tree and fit with test plan data
            %    B = Update(T,Index,B)
            %    B = Update(T,Index,B,InBest)
            %
            %    Inputs:
            %      T      mbcboundary.Tree object
            %      Index  index to boundary model in tree
            %      B      new boundary model
            %      InBest specifies whether the boundary model is
            %             included in the best boundary model for the
            %             boundary tree. By default, the boundary model
            %             will retain its previous InBest status after
            %             calling Update. 
            %    The boundary model must have the same inputs as the
            %    boundary tree. The boundary model is always fitted when it
            %    is added to the boundary tree, to ensure that the fitting
            %    data is compatible with the test plan data.
            %    The fitted boundary model is returned.
            %
            % See also Add, Remove, CreateBoundaryModel

            narginchk(3,4)
            
            ch = children(T.Object);
            n = length(ch);
            if ~(isnumeric(Index) && isscalar(Index) && all(Index>0 && Index<=n && fix(Index)==Index)) 
                error('mbc:mbcboundary:tree:InvalidValue',...
                    'The index to the boundary model must be a scalar between 1 and %d',n)
            end
            if isscalar(B) && isa(B,'mbcboundary.AbstractBoundary') 
                pChild = children(T.Object,Index);
                try
                    UpdateTree(B,info(pChild));
                catch ME
                    createME = MException('mbc:boundary:tree:InvalidType',...
                        'Error updating boundary model in tree.');
                    createME = createME.addCause(ME);
                    throw(createME)
                end
            else
                error(message('mbc:mbcboundary:tree:ScalarObjectRequired'))
            end
            if nargin>3
                if ~(isscalar(InBest) && islogical(InBest))
                    error(message('mbc:mbcboundary:tree:InvalidValue1'))
                end
                T.InBest(Index) =  InBest;
            end
            % return the fitted boundary model
            B = boundarymodel(info(pChild));
        end

        function Remove(T,Index)
            %REMOVE remove boundary model from boundary tree
            %
            % Remove(T,Index)
            %
            % See also Add, Update
            
            narginchk(2,2)
            
            ch = children(T.Object);
            n = length(ch);
            if (isnumeric(Index) && all(Index>0 && Index<=n && fix(Index)==Index)) 
                Index = unique(Index);
            elseif ~(islogical(Index) && length(Index)==n)
                error('mbc:mbcboundary:tree:InvalidValue2',...
                    'Index must be a 1x%d logical array or an array of indices 1..%d',n)
            end
            children(T.Object,Index,@delete);
        end
        
        function B = CreateBoundary(T,Type,varargin)
            %CREATEBOUNDARY create a new boundary model
            %    B = CreateBoundary(T,Type);
            %    B = CreateBoundary(T);
            %    B = CreateBoundary(Inputs,Type,Property,Value,...);     
            %    The new boundary model is not added to the tree. The test
            %    plan inputs are used to define the boundary model inputs.
            %
            % See also Add, Update

            %create a temporary child on the boundary tree so we can access
            %the correct boundary model
            Child = makechildren(T.Object);
            AddChild(T.Object,Child);
            con = newconstraint(info(Child));
            % delete temporary node on boundary tree
            delete(info(Child));
            if nargin>1
                B = CreateBoundary(con,Type);
                B = mbcSetPropValuePairs(B,varargin{:});
            else
                B = boundarymodel(con);
            end
            
        end
        
    end
    
end