Commit 9773cc7e authored by Colin Eles's avatar Colin Eles
Browse files

latest

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/trunk/TableTool@5881 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent fdecf616
Loading
Loading
Loading
Loading
+9 −353
Original line number Diff line number Diff line
@@ -90,354 +90,15 @@ classdef EMLGenerator < handle
        function code = generate_eml_code(obj)
            code = [];
            code = [code obj.generate_preamble];
            code = [code obj.generate_eml2(obj.data.Grid1,obj.data.Grid2,0)];
            code = [code obj.generate_eml_cond(obj.data.Grid1,obj.data.Grid2,0)];
            %code = [code obj.generate_conditional(obj.data.Grid1,obj.data.Grid2,0)];
            
        end
        
          
        
        
         %% generate_code
        %   this function will generate a string that represents the
        %   embedded matlab code. code will have proper syntax
        %   indentations.
        % inputs:
        %   obj:GUI - current GUI object
        %   g1:Grid - the grid on the top
        %   g2:Grid - the grid on the left
        %   depth:integer - the depth of the current call, incremented
        %      every recursive call, initial value should be 0. used for
        %      indenting the code.
        % outputs;
        %   error:string - string containing the error message, empty if
        %   there is no error
        function code = generate_conditional(obj,g1,g2,depth)
            space = '';
            % space is equal to 2 * depth number of ' '
            for i=1:depth
                space = [space sprintf('  ')];
            end
            code = [];
            g1cond1 = g1.cells(1).cond_text;
            g2cond1 = g2.cells(1).cond_text;
           
            % 1D horizontal table
            if (isempty(g2cond1) && ~isempty(g1cond1))
                found = 0;
                elsecell = [];
                for j=1:size(g1.cells,2)
                    g1cond = g1.cells(j).cond_text;
                    % condition string of otherwise corresponds to an else
                    % statement, we allow this statement to be in any order
                    % in the cells of the grid, so we need to find where it
                    % is, if it exists.
                    if (strcmp(g1cond,'otherwise'))
                        elsecell = g1.cells(j);
                        found = 1;
                        continue
                    end
                    if (j == 1 || (j==2 && found == 1))
                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(g1.cells(j).cond_text)))];
                    else
                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(g1.cells(j).cond_text)))];
                    end
                    
                    cell = obj.data.Grid0.search_return(g1.cells(j),g2.cells(1));
                    if(~isempty(cell))
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.datatype,strtrim(char(cell.result_text)))];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%smissing!!!\n',space)];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    end
                    
                end
                if (~isempty(elsecell))
                    code = [code sprintf('%selse\n',space)];

                    cell = obj.data.Grid0.search_return(elsecell,g2.cells(1));
                    if(~isempty(cell))
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.datatype,strtrim(char(cell.result_text)))];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%smissing!!!\n',space)];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    end
                end
                code = [code sprintf('%send\n',space)];

            %either 2D table, 1D vertical table or empty table    
            else
                found = 0;
                elsecell = [];
                for i=1:size(g2.cells,2)
                    g2cond = g2.cells(i).cond_text;
                    if (strcmp(g2cond,'otherwise'))
                        elsecell = g2.cells(i);
                        elseindex = i;
                        found = 1;
                        continue
                    end

                    if (i == 1 || (i == 2 && found == 1))
                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(g2.cells(i).cond_text)))];
                    else
                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(g2.cells(i).cond_text)))];
                    end
                    
                    if (~isempty(g2.cells(i).subgrid))
                        code = [code obj.generate_code(g1,g2.cells(i).subgrid,depth+1)];
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        found1 = 0;
                        elsecell1 = [];
                        for j=1:size(g1.cells,2)
                             g1cond = g1.cells(j).cond_text;
                            if (strcmp(g1cond,'otherwise'))
                                elsecell1 = g1.cells(j);
                                found1 = 1;
                                continue
                            end
                            
                            if(~isempty(g1.cells(j).subgrid))
                                %%TODO if add more dimensions to grid1
                            else
                                if (~isempty(g1cond1))
                                    if (j == 1 || (j==2 && found1 == 1))
                                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(g1.cells(j).cond_text)))];
                                    else
                                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(g1.cells(j).cond_text)))];
                                    end
                                end
                                
                                cell = obj.data.Grid0.search_return(g1.cells(j),g2.cells(i))
                                if(~isempty(cell))
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%soutput = %s(%s);\n',space,obj.datatype,strtrim(char(cell.result_text)))];
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                else
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%smissing!!!\n',space)];
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                end
                                
                            end
                        end
                        if(~isempty(g1cond1))
                            if(size(g1.cells,2)>0)
                                if (~isempty(elsecell1))
                                    code = [code sprintf('%selse\n',space)];

                                    cell = obj.data.Grid0.search_return(elsecell1,g2.cells(i))
                                    if(~isempty(cell))
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.datatype,strtrim(char(cell.result_text)))];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    else
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%smissing!!!\n',space)];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    end
                                end
                                code = [code sprintf('%send\n',space)];
                            end
                        end
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    end
                    
                end
                
                if(~isempty(elsecell))
                    code = [code sprintf('%selse\n',space)];
                    if (~isempty(g2.cells(elseindex).subgrid))
                        code = [code obj.generate_code(g1,g2.cells(elseindex).subgrid,depth+1)];
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        found1 = 0;
                        for j=1:size(g1.cells,2)
                             g1cond = g1.cells(j).cond_text;
                            if (strcmp(g1cond,'otherwise'))
                                elsecell1 = g1.cells(j);
                                found1 = 1;
                                continue
                            end
                            
                            if(~isempty(g1.cells(j).subgrid))
                                %%TODO if add more dimensions to grid1
                            else
                                if (~isempty(g1cond1))
                                    if (j == 1 || (j==2 && found1 == 1))
                                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(g1.cells(j).cond_text)))];
                                    else
                                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(g1.cells(j).cond_text)))];
                                    end
                                end
                                
                                cell = obj.data.Grid0.search_return(g1.cells(j),g2.cells(elseindex));
                                if(~isempty(cell))
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%soutput = %s(%s);\n',space,obj.datatype,strtrim(char(cell.result_text)))]
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                else
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%smissing!!!\n',space)];
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                end
                                
                            end
                        end
                        if(~isempty(g1cond1))
                            if(size(g1.cells,2)>0)
                                if (~isempty(elsecell1))
                                    code = [code sprintf('%selse\n',space)];

                                    cell = obj.data.Grid0.search_return(elsecell1,g2.cells(elseindex));
                                    if(~isempty(cell))
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.datatype,strtrim(char(cell.result_text)))];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    else
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%smissing!!!\n',space)];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    end
                                end
                                code = [code sprintf('%send\n',space)];
                            end
                        end
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        
                    end
                end
                code = [code sprintf('%send\n',space)];
                
            end
            
            
            
        end
        
        
        
        %% generate_eml_horizontal
        function code = generate_eml_horizontal2(obj,g1,g2_cell,depth)
        function code = generate_eml_horizontal(obj,g1,g2_cell,depth)
            space = '';
            for i=1:depth
                space = [space sprintf('  ')];
@@ -465,7 +126,6 @@ classdef EMLGenerator < handle
                if(~isempty(resultcell))
                     code = [code sprintf('%soutput = %s(%s);\n',[space '  '],obj.datatype,strtrim(char(resultcell.result_text)))];
                                      
                  
                else
                end
            end
@@ -476,13 +136,10 @@ classdef EMLGenerator < handle
            end
            code = [code sprintf('%send\n',space)];

       
            
     
        end
        
        %% genereate_pvs
        function code = generate_eml2(obj,g1,g2,depth)
        function code = generate_eml_cond(obj,g1,g2,depth)
             space = '';
            for i=1:depth
                space = [space sprintf('  ')];
@@ -492,7 +149,7 @@ classdef EMLGenerator < handle
          code = [];
          %1D horizontal
          if(~isempty(g1cond1) && isempty(g2cond1))
            code = [code obj.generate_eml_horizontal2(g1,g2.cells(1),depth)];
            code = [code obj.generate_eml_horizontal(g1,g2.cells(1),depth)];
          % something in vertical column
          else

@@ -515,17 +172,16 @@ classdef EMLGenerator < handle
                end

                if(~isempty(g2.cells(i).subgrid))
                    code = [code obj.generate_eml2(g1,g2.cells(i).subgrid,depth+1)];
                    code = [code obj.generate_eml_cond(g1,g2.cells(i).subgrid,depth+1)];
                else
                    if (~isempty(g1cond1))
                         code = [code obj.generate_eml_horizontal2(g1,g2.cells(i),depth+1)];
                         code = [code obj.generate_eml_horizontal(g1,g2.cells(i),depth+1)];
                    else    
                
                        resultcell = obj.data.Grid0.search_return(g1.cells(1),g2.cells(i));
                        if(~isempty(resultcell))
                                code = [code sprintf('%soutput = %s(%s);\n',[space '  '],obj.datatype,strtrim(char(resultcell.result_text)))];

                            
                        else
                        end
                    end
@@ -537,10 +193,10 @@ classdef EMLGenerator < handle
                code = [code sprintf('%selse\n',space)];

                if(~isempty(elsecell.subgrid))
                    code = [code obj.generate_pvs(g1,elsecell.subgrid,depth) sprintf('\n')];
                    code = [code obj.generate_eml_cond(g1,elsecell.subgrid,depth) sprintf('\n')];
                else
                    if (~isempty(g1cond1))
                         code = [code obj.generate_eml_horizontal2(g1,elsecell,depth) sprintf('\n')];
                         code = [code obj.generate_eml_horizontal(g1,elsecell,depth) sprintf('\n')];
                    else    
                
                        resultcell = obj.data.Grid0.search_return(g1.cells(1),elsecell);
+2 −2
Original line number Diff line number Diff line
@@ -429,7 +429,7 @@ classdef GUI < handle
            if (object.mode == 1)
                S = sfroot;
                code_block = sprintf('%s/code',getfullname(object.block_handle));
                code_blocks = find_system(getfullname(object.block_handle),'BlockType','SubSystem','Name','code');
                code_blocks = find_system(getfullname(object.block_handle),'LookUnderMasks','all','BlockType','SubSystem','Name','code');
                if ~isempty(code_blocks)
                    myState = S.find('-isa','Stateflow.EMChart', '-and', 'Path', code_block); % find which one we want to edit
                    if strncmp(myState.Outputs.DataType,'Inherit',7);
@@ -491,7 +491,7 @@ classdef GUI < handle
                %code = [code object.generate_code(object.Grid1,object.Grid2,0)];
                %fprintf('%s',code);

               TableBlock.set_code(object.block_handle,code,object.Data.function_name);
               eml_handle = TableBlock.set_code(object.block_handle,code,object.Data.function_name);

                
                
+1 −2
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ classdef TableBlock < handle
        % end copyright
            
        %% 
        function set_code(block_handle,code,function_name)
        function eml_handle = set_code(block_handle,code,function_name)
         % unlink the block from the library, this is necessary if
                % we
                % want to change the subsystem of the block.
@@ -79,7 +79,6 @@ classdef TableBlock < handle

                % get the path of the code block
                code_block = sprintf('%s/code',getfullname(block_handle));

                % determine if code block already exists
                code_blocks = find_system(getfullname(block_handle),'LookUnderMasks','all','BlockType','SubSystem','Name','code');