%% pvs_check_for_imports_g
%   recursively check a grid for an imported functions, see comment
%   for pvs_check_for_imports
% inputs:
%   object:PVS_checker - PVS_checker object
%   grid:Grid - grid to check
%   found:array - list of functions already imported so we dont
%       import the same thing twice.
% outputs:
%   string:string - pvs code
%   found:array - updated list of found functions to import
%
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function [string,found] = pvs_check_for_imports_g(object,grid,found)
string = [];

hashtable = {{'sqrt', 'reals@sqrt'} {'sin', 'trig@trig_basic'} {'cos', 'trig@trig_basic'} {'tan', 'trig@trig_basic'}};

for i=1:size(grid.cells,2)
    % recurse through
    if (~isempty(grid.cells(i).subgrid))
        string = [string object.pvs_check_for_imports_g(grid.cells(i).subgrid)];
    end
    text = char(grid.cells(i).cond_text);

    if size(text) > 0
        s = [text(1,:)];
        for j = 2:size(text,1)
           s = [s ' ' text(j,:)];
        end
        text = s;
    end
    
    vars = regexp(text,'([a-zA-Z][a-zA-Z0-9_]*)','match');
    
    
    
    
    for j=1:size(vars,2)
        if(exist(vars{j},'file')==2 && ~strcmp(vars{j},'otherwise'))
            %check if already imported file
            if (~any(ismember(found,vars{j})))
                string = [string 'IMPORTING ' vars{j} sprintf('\n')];
                found = [found vars{j}];
            end
        end
        
        for k=1:size(hashtable,2)
            if strcmp(vars{j},hashtable{k}(1))
                if (~any(ismember(found,vars{j})))
                string = [string 'IMPORTING ' char(hashtable{k}(2)) sprintf('\n')];
                found = [found vars{j}];
                end
            end    
        end
    end
end
end



