www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoppoint/CheckDataset.m

    function op = CheckDataset(op,pr)
% CGOPPOINT/CHECKDATASET
% op = CheckDataset(op)
%  Checks: units, groups, ddvariables

%  Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc.


if ~isempty(op.ptrlist) && any(isvalid(op.ptrlist))
    op = CheckGroup(op,false);
    op = i_CheckConstants(op);
    % Units not used so don't check
    % op = i_CheckUnits(op);
    if nargin>2
        op = CheckNames(op,pr);
    end
    op = i_CheckSpecials(op);

    % now do eval fill
    op= eval_fill(op);
end


%---------------------------------------


%---------------------------------------
function op = i_CheckConstants(op)
%---------------------------------------
%  Check assigned ptrs for if ddvariable; disallow constants (unassign)
valid_idx = find(isvalid(op.ptrlist));
hObj = infoarray(op.ptrlist(valid_idx));

for n = 1:length(valid_idx)
    ptr = op.ptrlist(valid_idx(n));
    if isddvariable(hObj{n}) && isconstant(hObj{n})
        % Anything linked to this factor?
        % Create internal ptr to maintain link.
        f2 = (op.linkptrlist==ptr);
        if any(f2)
            op = AddCage(op,valid_idx(n));
            op.linkptrlist(f2) = op.ptrlist(valid_idx(n));
        end
        % unassign
        op.ptrlist(valid_idx(n)) = 0;
        if isempty(op.orig_name{valid_idx(n)})
            op.orig_name{valid_idx(n)} = getname(hObj{n});
        end
    end
end



%---------------------------------------
function op = i_CheckSpecials(op)
%---------------------------------------
% Check anything created from feature.
% Ensure current model and equation both present.

FeatIdx = find(op.created_flag==-2);
if isempty(FeatIdx)
    return
end

AllFeatPtr = op.ptrlist(FeatIdx);
AllFeatLinkPtr = op.linkptrlist(FeatIdx);
FeatLinkObj = infoarray(AllFeatLinkPtr);

FeatPtr = unique(AllFeatPtr);
FeatObj = infoarray(FeatPtr);

% Mapping pointers used to do quick updates when model/equation has changed
FeatRemap = null(xregpointer, [0 2]);

% Columns that are no longer needed and Features that need to be re-added:
% this is done in one shot at the end
ColsToRemove = [];
FeatsToAdd = null(xregpointer, 0);


% For each feature, check that it's model and strategy pointers are up to
% date.  If not, change them and record in FeatRemap so that any errors can
% also be updated afterwards.
for n = 1:length(FeatObj)
    % Get info on the features current setup
    NewMdlPtr = get(FeatObj{n}, 'model');
    HasNewMdl = ~isempty(NewMdlPtr);
    NewEqPtr = FeatPtr(n);
    HasNewEq = ~isempty(get(FeatObj{n}, 'equation'));

    % Get info on how the feature looked last time the oppoint was updated
    itemsidx = find(AllFeatPtr==FeatPtr(n));
    if length(itemsidx)>2
        % This is an unexpected state.  Schedule this feature for complete
        % removal and re-adding
        ColsToRemove = [ColsToRemove, FeatIdx(itemsidx)];
        FeatsToAdd = [FeatsToAdd, FeatPtr(n)];

    elseif length(itemsidx)==2
        % model and equation currently in oppoint, work out which is which
        switch FeatureLinkType(FeatLinkObj{itemsidx(1)})
            case 'Strategy'
                OldMdlIdx = itemsidx(2);
                OldEqIdx = itemsidx(1);
            case 'Model'
                OldMdlIdx = itemsidx(1);
                OldEqIdx = itemsidx(2);
            case 'Data'
                if strcmp(FeatureLinkType(FeatLinkObj{itemsidx(2)}),'Strategy')
                    % check that the second item is a strategy
                    OldMdlIdx = itemsidx(1);
                    OldEqIdx = itemsidx(2);
                else
                    % if the second item is not a strategy then use it as
                    % the filling item (Model or Data)
                    OldMdlIdx = itemsidx(2);
                    OldEqIdx = itemsidx(1);
                end
        end
        % make sure that the feature model name is correct
        %   The name of the feature could have changed in project
        op.orig_name{FeatIdx(OldEqIdx)} = sprintf('%s: Strategy',getname(FeatObj{n}));
        %   In addition, the type of filling item could have changed (e.g. Model
        %   to Data)
        op.orig_name{FeatIdx(OldMdlIdx)} = sprintf('%s: %s',getname(FeatObj{n}),FeatureLinkType(NewMdlPtr.info));

        if HasNewMdl && HasNewEq
            % Place new model and equation into the list of required maps
            if NewEqPtr~=AllFeatLinkPtr(OldEqIdx)
                FeatRemap = [FeatRemap; AllFeatLinkPtr(OldEqIdx), NewEqPtr];
                AllFeatLinkPtr(OldEqIdx) = NewEqPtr;
            end

            if NewMdlPtr~=AllFeatLinkPtr(OldMdlIdx)
                % Also need to check that whether the old model was in the
                % dataset in its own right - don't need to remap if this is
                % the case
                OldMdlPtr = AllFeatLinkPtr(OldMdlIdx);
                AllFeatLinkPtr(OldMdlIdx) = NewMdlPtr;
                if isFilledByData(FeatObj{n})
                    ColsToRemove = [ColsToRemove, OldMdlIdx];
                elseif ~any(op.ptrlist==OldMdlPtr) 
                    FeatRemap = [FeatRemap; OldMdlPtr, NewMdlPtr];
                end
            end

        else
            if ~HasNewMdl || isFilledByData(FeatObj{n})
                % Schedule column removal
                ColsToRemove = [ColsToRemove, OldMdlIdx];
            end
            if ~HasNewEq
                % Schedule column removal
                ColsToRemove = [ColsToRemove, OldEqIdx];
            end
        end

    else
        % Only one item currently in oppoint
        HasOldMdl = false;
        HasOldEq = false;
        FType = FeatureLinkType(FeatLinkObj{itemsidx});
        switch FType
            case {'Model','Data'}
                % Filling item 
                HasOldMdl = true;
                % make sure that the feature model name is correct
                op.orig_name{FeatIdx(itemsidx)} = sprintf('%s: %s',getname(FeatObj{n}),FType);
            otherwise
                % Strategy
                HasOldEq = true;
                op.orig_name{FeatIdx(itemsidx)} = sprintf('%s: Strategy',getname(FeatObj{n}));
        end

        if HasNewMdl && HasNewEq
            % The new feature item needs to be added properly
            ColsToRemove = [ColsToRemove, itemsidx];
            FeatsToAdd = [FeatsToAdd, FeatPtr(n)];
        elseif HasNewMdl && HasOldMdl
            if isFilledByData(FeatObj{n})
                ColsToRemove = [ColsToRemove, itemsidx];
            else
                if ~any(op.ptrlist==AllFeatLinkPtr(itemsidx))
                    FeatRemap = [FeatRemap; AllFeatLinkPtr(itemsidx), NewMdlPtr];
                end
                AllFeatLinkPtr(itemsidx) = NewMdlPtr;
            end
        elseif HasNewEq && HasOldEq
            FeatRemap = [FeatRemap; AllFeatLinkPtr(itemsidx), NewEqPtr];
            AllFeatLinkPtr(itemsidx) = NewEqPtr;
        else
            % The new and old column types don't match
            ColsToRemove = [ColsToRemove,itemsidx];
            if HasNewEq
                FeatsToAdd = [FeatsToAdd, FeatPtr(n)];
            end
        end
    end
end

% Put changes back into main ptrlist
op.linkptrlist(FeatIdx) = AllFeatLinkPtr;

% Find all outputs that are dataset-held and check each one for being an
% error that needs it's inputs checking against the map we have created
if ~isempty(FeatRemap)
    possible_sums = (op.factor_type==2) & (op.created_flag==1);
    if any(possible_sums)
        for n = find(possible_sums)
            hSum = op.ptrlist(n).info;
            if isa(hSum, 'cgsubexpr')
                ANY_REMAPPED = false;

                pLeft = get(hSum, 'left');
                remapidx = find(FeatRemap(:,1)==pLeft);
                if ~isempty(remapidx)
                    hSum = set(hSum, 'left', FeatRemap(remapidx,2));
                    ANY_REMAPPED = true;
                end

                pRight = get(hSum, 'right');
                remapidx = find(FeatRemap(:,1)==pRight);
                if ~isempty(remapidx)
                    hSum = set(hSum, 'right', FeatRemap(remapidx,2));
                    ANY_REMAPPED = true;
                end

                if ANY_REMAPPED
                    % Update heap copy
                    op.ptrlist(n).info = hSum;
                end
            end
        end
    end
end

% Remove columns and add features
if ~isempty(ColsToRemove)
    op = removefactor(op,FeatIdx(ColsToRemove),false);
end
if ~isempty(FeatsToAdd)
    op = AddExpr(op, FeatsToAdd,false);
end



function t=FeatureLinkType(Item,Type)


if isa(Item,'cgmodexpr')
    t = 'Model';
elseif isa(Item,'cgfeature')
    t = 'Strategy';
else
    % equation pointer from old features
    t = 'Strategy';
end
if nargin>1
    t = strcmp(t,Type);
end