function  checkGlobalAccess(sliceMdlH,slcrMapObj, origSortedH)
%

%   Copyright 2015-2017 The MathWorks, Inc.

    sliceSortedH = Analysis.ExpandedSortedlist(sliceMdlH);
 
    EmptyInd = [];
    origMapBlockH = [];
    for i = 1:length(sliceSortedH)    
        try
            origH = slcrMapObj.findInOrig(sliceSortedH(i));
            if isempty(origH)
                EmptyInd(end+1) = i;
            else
                origMapBlockH(end+1) = origH;
            end
        catch ex %#ok<*NASGU>
            EmptyInd(end+1) = i;
        end
    end
    
    sliceSortedH(EmptyInd) = [];
    
    %Sorted order index of orginal model handles
    sortMapOrig = containers.Map(origSortedH,1:length(origSortedH));
    
    %MappedSortedOrder: order of sliced model blocks relative to orginal
    %model blocks.
    MappedSortedOrder = arrayfun(@(h) getSortedMapIdx(sortMapOrig,h),origMapBlockH,'UniformOutput',false);
    EmptyId = cellfun(@isempty,MappedSortedOrder);
    MappedSortedOrder(EmptyId) = [];
    MappedSortedOrder = [MappedSortedOrder{:}];
    sliceSortedH(EmptyId) = [];
    origMapBlockH(EmptyId) = [];
    
    %sorted order index of sliced model handles
    sortMapSliced = containers.Map(sliceSortedH,1:length(sliceSortedH));
      
    [dsmBlocks,varUsage] = Analysis.getGlobalMemVars(sliceMdlH);
    
    %%DSM sorted order change   
     
    for i = 1:length(dsmBlocks)
        dsmObj = get_param(dsmBlocks(i),'object');
        blockHandles = [dsmObj.DSReadWriteBlocks(:).handle];
        compareSortedOrder(dsmObj,blockHandles,sortMapSliced,MappedSortedOrder, origMdlH);
    end
    
    %%Simulink Signals order change
    
    for i=1:length(varUsage)
        blockHandles = cellfun(@(u) get_param(u,'handle'),varUsage(i).Users);
        compareSortedOrder(varUsage(i).Name,blockHandles,sortMapSliced,MappedSortedOrder, origMdlH);
    end

end

function compareSortedOrder(globalVal,blockHandles,sortMapSliced,MappedSortedOrder, origMdlH)

if isempty(blockHandles)
    return;
end

sortOrder = arrayfun(@(h) getSortedMapIdx(sortMapSliced,h),blockHandles,'UniformOutput',false);
EmptyId = cellfun(@isempty,sortOrder);
sortOrder(EmptyId) = [];
blockHandles(EmptyId) = [];
sortOrder = [sortOrder{:}];

orderMapped = MappedSortedOrder(sortOrder);

[~,index] = sort(sortOrder);
orderMapped = orderMapped(index);
blockHandles = blockHandles(index);

blocksChanged = [];
orderChanged = zeros(1,length(orderMapped));
detectRW = arrayfun(@(h)strcmp(get_param(h,'blocktype'),'DataStoreRead'),blockHandles);

    for i = 1:length(orderMapped)-1
        if(orderMapped(i) > orderMapped(i+1)) && ...
            ~(detectRW(i) && detectRW(i+1))
            orderChanged(i) = 1;
            orderChanged(i+1) = 1;
        
        end
    end
    
    for i =  1:length(orderMapped)
        if orderChanged(i)
            blockName = getfullname(blockHandles(i));
            blockName=sldvprivate('util_hilite_model_element',blockName);
            blocksChanged = [blocksChanged '\n' blockName]; %#ok<*AGROW>
        end
    end
    
    if ~isempty(blocksChanged)
      blocksChanged = [blocksChanged '\n'];
      
      if iscell(blocksChanged)
          blocksChanged = [blocksChanged{:}];
      end
      
      if isa(globalVal,'Simulink.DataStoreMemory')
          type = 'Data Store Memory';
          source = sldvprivate('util_hilite_model_element',getfullname(globalVal.handle));
      else
          type = 'Global Variable';
          source = globalVal;
      end
         
      mex = MException('Sldv:ModelSlicer:ModelSlicer:SortedOrderChangeGlobal', ...
            getString(message('Sldv:ModelSlicer:ModelSlicer:SortedOrderChangeGlobal',...
            type,source,blocksChanged)));
        
      modelslicerprivate('MessageHandler','warning',mex, getfullname(origMdlH));      
    end

end


function idx = getSortedMapIdx(sortMap,h)
        if sortMap.isKey(h)
            idx = sortMap(h);
        else
            idx = [];
        end
end

