Main Content

Report Systems Hierarchically

This example shows how to create a report with sections that are numbered according to system hierarchy. Each section contains a system snapshot and subsections that contain subsystem snapshots. To create such a section, create a section object, add a diagram snapshot, and then add subsystem sections. To create the subsystem sections, again create a section, add a subsystem diagram snapshot and then add its subsystem sections. The algorithm to create the sections is recursive. This example creates and uses a local function called createSystemSection, which implements the recursive algorithm.

Create Hierarchical Report Using createSystemSection Function

Open a model.

model = "sf_car";
open_system(model);

Create and open a report object.

% Change the output type from "pdf" to "docx" or "html" to create a 
% Word or HTML report, respectively.
rpt = slreportgen.report.Report("myreport", "pdf");
open(rpt);

Add a title page.

titlepage = mlreportgen.report.TitlePage();
titlepage.Title = "Hierarchical Report";
add(rpt, titlepage);

Add a table of contents with the number of levels set to 6, which is the maximum.

toc = mlreportgen.report.TableOfContents();
toc.TOCObj.NumberOfLevels = 6;
add(rpt, toc);

Create system sections for the model by calling the createSystemSection local function (see below). This function recursively calls itself to create sections for the subsystems.

section = createSystemSection(model);
add(rpt, section);

Generate and display the report.

close(rpt);
rptview(rpt);

Define createSystemSection Local Function

A system section is composed of a system snapshot and its subsystems in subsections. To create a system section, find all systems one level deep by using an slreportgen.finder.DiagramFinder with a SearchDepth of 1.

function section = createSystemSection(sys)
    df = slreportgen.finder.DiagramFinder(sys);
    df.SearchDepth = 1;

    % Use the finder in iterator mode. The next function returns search results
    % one-by-one and the hasNext function determines when there are no more 
    % search results. To obtain the current system, call the next function 
    % once.
    sysResult = next(df);

    % Now, create a section using mlreportgen.report.Section with the system 
    % name as the title.
    section = mlreportgen.report.Section( ...
        "Title", mlreportgen.utils.normalizeString(sysResult.Name));

    % Add a system snapshot and a caption that shows the full diagram path. 
    % To include additional information about the system, add it to the 
    % section object.
    diag = slreportgen.report.Diagram(sysResult.Object);
    diag.Snapshot.appendCaption(sysResult.Path);
    add(section, diag);
    
    % To create subsections, loop through all subsystems and recursively call 
    % createSystemSection. Before calling createSystemSection, add a page break
    % so each system starts on a new page. Note that adding a page break right 
    % after the system snapshot would add a blank page at the end of the report.
    while hasNext(df)
        childSysResult = next(df);
        add(section, mlreportgen.dom.PageBreak());
        subSection = createSystemSection(childSysResult.Object);
        add(section, subSection);
    end
end