%% Copyright (C) 2012, SRI International.  All Rights Reserved.

%% This program is free software; you can redistribute it and/or
%% modify it under the terms of the GNU General Public License
%% as published by the Free Software Foundation; either version 2
%% of the License, or (at your option) any later version.

%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.


function [] = getGESignalTransitions(sysPath, searchDepth)
    
global variableTypeMap;
variableTypeMap = java.util.HashMap;

global transition;
transition = java.util.HashMap;

global blockSuccessor;
blockSuccessor = java.util.HashMap;

global signalVars;
signalVars = java.util.HashSet;


if strcmp(searchDepth, '1') == true
    lines = find_system(sysPath,'FindAll','on', 'SearchDepth', '1', 'Type','line');
else
    lines = find_system(sysPath,'FindAll','on', 'Type','line');
end

handle = 0;
signalname = '';
transition.clear();
blockSuccessor.clear();


for i = length(lines):-1:1,

    name  = get_param(lines(i),'Name'); 
    name = regexprep(name, '\s', '');
    % To deal with bus selector signals
    name = regexprep(name, '<', '');            
    name = regexprep(name, '>', '');
    
    srcParent = get_param(lines(i),'Parent');
    srcblk = get_param(lines(i),'SrcBlock');
    if not(isempty(srcblk)),
        blkValue = strcat(srcParent,'/',srcblk);
        handle = get_param(blkValue,'Handle');
    end
    dstblk = get_param(lines(i),'DstBlock');
    if not(isempty(dstblk)),
        blkValue = strcat(srcParent,'/',dstblk);
        handle2 = get_param(blkValue,'Handle');
    else
        handle2 = 0;
    end
    srcParent = regexprep(srcParent,'/','__');
    srcParent  = regexprep(srcParent,'\s','');
    srcblk  = regexprep(srcblk,'\s','');
    dstblk  = regexprep(dstblk,'\s','');
    
    srcport = get_param(lines(i),'SrcPort');
    
    dstport = get_param(lines(i),'DstPort');
    
    parsestring = srcParent;
    
    if not(isempty(srcblk) && isempty(srcport)),
        blockHandle = handle;
        link_status = get_param(blockHandle,'LinkStatus');
        srcType = get_param(blockHandle,'BlockType');
        masktype = '';
        try masktype = char(get_param(blockHandle, 'MaskType')); catch end
        if strcmp(srcType,'SubSystem') && (strcmp(link_status,'none') || strcmp(link_status,'inactive')) && strcmp(masktype, 'Stateflow') == 0
            blks = find_system(blockHandle,'SearchDepth','1', 'BlockType', 'Outport');
            if not(isempty(blks))
                nameVal = get_param(blks(str2num(srcport)),'Name');
                nameVal = regexprep(nameVal, '\s', '');
                %srcblkport = strcat(srcblk,'__',nameVal);
                % Dfferent from getSignalTransitions
                srcblkport = strcat(srcblk,'__',nameVal, '__In1');
            end
        else
            srcblkport = strcat(srcblk,'__Out',srcport);
        end
        
        srcblkport = strcat(parsestring,'__',srcblkport);
        
        if not(isempty(name)),
            if not(variableTypeMap.containsKey(strcat(parsestring,'__',name))),
                signalType = variableTypeMap.get(srcblkport);
                x = transition.get(srcblkport);
                if isempty(x),
                    x = java.util.ArrayList;
                end
                x.add(strcat(parsestring,'__',name));
                transition.put(srcblkport,x);
                
                if not(isempty(signalType))
                    variableTypeMap.put(strcat(parsestring,'__',name), signalType);
                else
                    variableTypeMap.put(strcat(parsestring,'__',name), 'real');
                end
            end
        end
    end;
    
    if strcmp(signalname,name),
        % the name already defined so inside a branch
    else
        % the name is new
        signalname = name;
        class(signalname);
        if not(isempty(name)) && not(signalVars.contains(strcat(parsestring,'__',name))),
            signalVars.add(strcat(parsestring,'`',name));
        end
        if (isempty(srcblk) && isempty(srcport)),
            srcblkport = '';
            handle = 0;
        end
    end
    
    if not(isempty(dstblk)) && not(isempty(dstport))
        % srcblkport empty but destblkport is not
        dstblk = strcat(parsestring,'__',dstblk);
        blockHandle = handle2;
        link_status = get_param(blockHandle,'LinkStatus');
        destType = get_param(blockHandle,'BlockType');
        masktype = '';
        try masktype = char(get_param(blockHandle, 'MaskType')); catch end
        if strcmp(destType,'SubSystem') && (strcmp(link_status,'none') || strcmp(link_status,'inactive')) && strcmp(masktype, 'Stateflow') == 0
            blks = find_system(blockHandle,'SearchDepth','1', 'BlockType', 'Inport');
            if not(isempty(blks))
                nameV  = get_param(blks(str2num(dstport)),'Name');
                nameV = regexprep(nameV, '\s', '');
            else
                nameV = dstport;
            end
            %dstblkport = strcat(dstblk,'__',nameV);
            % Dfferent from getSignalTransitions
            dstblkport = strcat(dstblk,'__',nameV, '__Out1');
        else
            dstblkport = strcat(dstblk,'__In',dstport);
        end

        if not(isempty(srcblkport))
            if not(variableTypeMap.containsKey(dstblkport)) && isempty(strfind(dstblkport, 'ifaction')) && isempty(strfind(dstblkport, 'trigger'))
                destType = variableTypeMap.get(srcblkport);
                x = transition.get(srcblkport);
                if isempty(x),
                    x = java.util.ArrayList;
                end;
                x.add(dstblkport);
                transition.put(srcblkport,x);
                
                if (handle > 0),
                    x = blockSuccessor.get(handle);
                    if(handle2 > 0),
                        if isempty(x),
                            x = java.util.ArrayList;
                        end
                        x.add(handle2);
                        blockSuccessor.put(handle,x);
                    end
                end;
                
                if not(isempty(destType)),
                    variableTypeMap.put(dstblkport,destType);
                end
            end
        end
              
        if not(isempty(name)),
            if not(variableTypeMap.containsKey(dstblkport)) && isempty(strfind(dstblkport, 'ifaction')) && isempty(strfind(dstblkport, 'trigger'))
                destType = variableTypeMap.get(strcat(parsestring,'__',name));
                if not(isempty(destType))
                    variableTypeMap.put(dstblkport, destType);
                end
                x = transition.get(strcat(parsestring,'__',name));
                if isempty(x),
                    x = java.util.ArrayList;
                end
                x.add(dstblkport);
                transition.put(strcat(parsestring,'__',name),x);
            end
        end
    end
    
end

end