%% pvs_check_for_imports
%   checks both horizontal and vertical grids for functions for
%   which there is a file on the path of the same name, in which
%   case create pvs code to import the pvs file for that function
% inputs:
%   object:PVS_checker - PVS_checker object
% outputs:
%   string:string - pvs code of imports
%
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function string = pvs_check_for_imports(object)



[string,found] = object.pvs_check_for_imports_g(object.data.Grid2,{});
[string2,found2] = object.pvs_check_for_imports_g(object.data.Grid1,found);





% check for imports in the inputs field



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

% check for imports in the results grid.
for i=1:size(object.data.Grid0.Cells,2)

    text = char(object.data.Grid0.Cells(i).result_text);
    
    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



for k=1:size(hashtable,2)

    string_parts = char(object.data.function_inputs);
    
    s = [string_parts(1,:)];
    for j = 2:size(string_parts,1)
       s = [s ' ' string_parts(j,:)];
    end
    string_parts = s;
    
    functions = regexp(string_parts,hashtable{k}(1),'once');
            if ~isempty(functions{1})
                if (~any(ismember(found,hashtable{k}(1))))
                string = [string 'IMPORTING ' char(hashtable{k}(2)) sprintf('\n')];
                found = [found hashtable{k}(1)];
                end
            end    
        end
        
string = [string string2];
end

