Using system composer, it is possible to create a report with the sequence diagrams?

30 visualizaciones (últimos 30 días)
I´m using system composer, and i want to create a report with the views and sequence diagrams that i have. I have this code to generate the report and add the views, but i don´t know if there is a way to add too the sequence diagrams because there is nothing like "SequenceDiagramFinder".
Here is the code for the views:
model = systemcomposer.loadModel(model_name);
rpt = slreportgen.report.Report(output="ViewFinderReport",...
CompileModelBeforeReporting=false);
viewFinder = ViewFinder(model_name);
chapter = Chapter("Title","Views");
while hasNext(viewFinder)
view = next(viewFinder);
sect = Section("Title",view.Name);
add(sect,view);
add(chapter,sect);
end

Respuestas (2)

Umar
Umar el 21 de Ag. de 2024

Hi @Victoria Ramirez ,

You asked, “I´m using system composer, and i want to create a report with the views and sequence diagrams that i have. I have this code to generate the report and add the views, but i don´t know if there is a way to add too the sequence diagrams because there is nothing like "SequenceDiagramFinder".”

Please see my response to your comments below.

To incorporate sequence diagrams into your report alongside the views, you can leverage the `systemcomposer.rptgen.report.SequenceDiagram` class, which allows you to create a reporter for sequence diagrams within your System Composer model. For more information regarding to this class, please refer to,

https://www.mathworks.com/help/systemcomposer/ref/systemcomposer.rptgen.report.sequencediagram-class.html

Below is an adapted version of your existing code that demonstrates how to include both views and sequence diagrams in your report. So, first make sure that your model is loaded as you have done. Initialize the report as before. Then, iterate through the views using ViewFinder and use the SequenceDiagram class to add sequence. Please see modified version of your code below.

% Load the System Composer model
model = systemcomposer.loadModel(model_name);
% Create a new report
rpt = slreportgen.report.Report(output="ViewFinderReport",...
  CompileModelBeforeReporting=false);
% Create a ViewFinder for views
viewFinder = ViewFinder(model_name);
chapter = Chapter("Title","Views");
% Add views to the chapter
while hasNext(viewFinder)
  view = next(viewFinder);
  sect = Section("Title", view.Name);
  add(sect, view);
  add(chapter, sect);
end
add(rpt, chapter);  % Add the views chapter to the report
% Now add a section for Sequence Diagrams
seqChapter = Chapter("Title", "Sequence Diagrams");
% Get all sequence diagrams in the model
seqDiagrams = systemcomposer.sequenceDiagrams(model_name);  % Adjust 
if necessary
for i = 1:length(seqDiagrams)
  seqDiagram = seqDiagrams(i);
      % Create a SequenceDiagram reporter for each diagram
      seqReporter = systemcomposer.rptgen.report.SequenceDiagram("Name",     
      seqDiagram.Name, "ModelName", model_name);
      % Create a section for each sequence diagram and add it to the chapter
      seqSect = Section("Title", seqDiagram.Name);
      add(seqSect, seqReporter);
      add(seqChapter, seqSect);
  end
add(rpt, seqChapter);  % Add the sequence diagrams chapter to the report
% Finally, close and generate the report
close(rpt);

I wanted to let you know that the SequenceDiagram reporter allows customization through its properties like TemplateSrc and TemplateName. If you have specific formatting or content requirements, consider creating custom templates. Also, I will suggest implementing error handling when loading models or generating reports to catch any potential issues that may arise during execution.After implementing these changes, test your script with different models and sequences to ensure compatibility and that all expected diagrams are included in the output. By following these steps, you should be able to effectively generate a comprehensive report that includes both architectural views and sequence diagrams, enhancing your documentation process within System Composer. Please let me know if you have any further questions.

  3 comentarios
Umar
Umar el 22 de Ag. de 2024

Hi @Victoria Ramirez ,

To address the problem you are facing, I had to first understand the context of the error and then provide a solution that not only resolves the issue but also enhances your reporting process.

Understanding the Error

The error message:

Error using zeros Size inputs must be integers.

suggests that somewhere in your code, an operation is trying to use non-integer values for defining array sizes. This typically occurs in MATLAB when a function expects integer dimensions but receives floating-point numbers or undefined variables instead. The stack trace indicates that this error arises during the execution of methods related to exporting or capturing images of the sequence diagrams. Specifically, it seems to be triggered when the createSnapshot method is called within the SequenceDiagram reporter.

So, I will suggest following steps to troubleshoot and resolve this issue.

First, make sure that the seqDiagrams variable correctly retrieves existing sequence diagrams from your model. You can do this by checking its length before entering the loop:

   seqDiagrams = systemcomposer.sequenceDiagrams(model_name);
   if isempty(seqDiagrams)
       warning('No sequence diagrams found in the model.');
       return; % Exit if there are no diagrams
   end

Secondly, when creating instances of systemcomposer.rptgen.report.SequenceDiagram, make sure that both seqDiagram.Name and model_name are valid strings. An invalid name could lead to issues when generating images:

   if ~ischar(seqDiagram.Name) || isempty(seqDiagram.Name)
       error('Invalid sequence diagram name detected.');
   end

Afterwards, catch potential issues while loading models or generating reports, consider adding try-catch blocks around critical sections:

   try
       % Your existing code for generating reports
   catch ME
       fprintf('Error encountered: %s\n', ME.message);
       % Additional logging or handling can go here
   end

After implementing these checks and error handling, run your script with various models and sequences to identify if specific diagrams cause issues.

If you are customizing your sequence diagram reports with templates, make sure that your templates are correctly set up and accessible. Missing or improperly formatted templates could also trigger errors during report generation.

One more thing I forgot to mention if you are using a version of MATLAB that supports all functions used in your script, as discrepancies between versions can lead to unexpected errors.

Hope this helps.

Victoria Ramirez
Victoria Ramirez el 22 de Ag. de 2024
i still have the same error. Everything is fine until adding the seqReporter to the general report.
This is the code i´m working:
clear
import mlreportgen.report.*
import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.dom.*
import mlreportgen.utils.*
import systemcomposer.query.*
import systemcomposer.rptgen.finder.*
rpt = slreportgen.report.Report('OutputPath','FullArchitecture',...
'CompileModelBeforeReporting',false);
model = systemcomposer.loadModel(model_name);
seqChapter = Chapter("Title", "Sequence Diagrams");
seqDiagrams = model.getInteractions;
if isempty(seqDiagrams)
warning('No sequence diagrams found in the model.');
return; % Exit if there are no diagrams
end
try
for i = 1:length(seqDiagrams)
seqDiagram = seqDiagrams(i);
% Create a SequenceDiagram reporter for each diagram
seqReporter = systemcomposer.rptgen.report.SequenceDiagram("Name", ...
seqDiagram.Name, "ModelName", model.Name);
seqSect = Section("Title", seqDiagram.Name);
add(seqSect, seqReporter);
add(seqChapter, seqSect);
end
add(rpt, seqChapter);
catch ME
fprintf('Error encountered: %s\n', ME.message);
% Additional logging or handling can go here
end
and this is what i got: Error encountered: Size inputs must be integers.

Iniciar sesión para comentar.


Sahas
Sahas el 21 de Ag. de 2024
Editada: Sahas el 21 de Ag. de 2024
As per my understanding, you are using System Composer and want to incorporate “sequence diagrams” while generating report.
Since MATLAB R2022b, one can show the “sequence diagram” in a report as an image. Using the “systemcomposer.rptgen.report.SequenceDiagram” class, we can create a reporter that reports on a sequence diagram in a System Composer architecture model.
For more information on this class, refer to the following MathWorks documentation:
Assuming the sequence diagram name to be “AtoB”, refer to the following code snippet:
%General imports
import slreportgen.report.*
import mlreportgen.report.*
%Previous code
model = systemcomposer.loadModel(model_name);
rpt = slreportgen.report.Report(output="ViewFinderReport",...
CompileModelBeforeReporting=false);
viewFinder = ViewFinder(model_name);
chapterViews = Chapter("Title", "Views");
while hasNext(viewFinder)
view = next(viewFinder);
sect = Section("Title", view.Name);
add(sect, view);
add(chapterViews, sect);
end
add(rpt, chapterViews);
%%%%Code for adding sequence diagrams%%%%
%Chapter for sequence diagrams
chapterSeqDiagrams = Chapter();
chapter.Title = "Sequence Diagram Example";
%Adding a sequence diagram named 'AtoB' from the model manually
%You can repeat this step to add more sequece diagrams
dRptr = systemcomposer.rptgen.report.SequenceDiagram("Name", 'AtoB', "ModelName", model_name);
add(chapterSeqDiagrams, dRptr);
%Adding the sequence diagrams chapter to the report
add(rpt, chapterSeqDiagrams);
%Close and view the report
close(rpt);
rptview(rpt);
Additionally in MATLAB R2024a, using thesystemcomposer.interaction” classes, one can interact, iterate and navigate over all the “sequence diagram” objects. Refer to the following MathWorks documentation links for more information:
I hope this is beneficial!
  4 comentarios
Victoria Ramirez
Victoria Ramirez el 22 de Ag. de 2024
i´m using this code:
clear
import mlreportgen.report.*
import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.dom.*
import mlreportgen.utils.*
import systemcomposer.query.*
import systemcomposer.rptgen.finder.*
rpt = slreportgen.report.Report('OutputPath','FullArchitecture',...
'CompileModelBeforeReporting',false);
model = systemcomposer.loadModel(model_name);
seqChapter = Chapter("Title", "Sequence Diagrams");
seqDiagrams = model.getInteractions;
if isempty(seqDiagrams)
warning('No sequence diagrams found in the model.');
return; % Exit if there are no diagrams
end
try
for i = 1:length(seqDiagrams)
seqDiagram = seqDiagrams(i);
% Create a SequenceDiagram reporter for each diagram
seqReporter = systemcomposer.rptgen.report.SequenceDiagram("Name", ...
seqDiagram.Name, "ModelName", model.Name);
seqSect = Section("Title", seqDiagram.Name);
add(seqSect, seqReporter);
add(seqChapter, seqSect);
end
add(rpt, seqChapter);
catch ME
fprintf('Error encountered: %s\n', ME.message);
% Additional logging or handling can go here
end
Umar
Umar el 23 de Ag. de 2024

Hi @Victoria Ramirez,

I took some time out of work to resolve your problem. So, first I had to define a generic model in Matlab in order to resolve your issue and used to your code and it did execute successfully with no problems and since the model I defined did not contain any sequence data, I was expecting the warning message as shown below by executing your code. However, the error message you encountered, “Size inputs must be integers." the likely cause of this error is related to your length(seqDiagrams) function call. If seqDiagrams is not a valid array or if it contains elements that do not conform to expected types, the length function will return unexpected results like the one you observed. Also,if seqDiagrams is empty or not properly initialized, it could lead to issues when trying to access its elements in the loop.

import mlreportgen.report.*
import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.dom.*
import mlreportgen.utils.*
import systemcomposer.query.*
import systemcomposer.rptgen.finder.*
rpt = slreportgen.report.Report('OutputPath','FullArchitecture',...
  'CompileModelBeforeReporting',false);
model = systemcomposer.loadModel(model_name);
seqChapter = Chapter("Title", "Sequence Diagrams");
seqDiagrams = model.getInteractions; 
if isempty(seqDiagrams)
     warning('No sequence diagrams found in the model.');
     return; % Exit if there are no diagrams
end
try
  for i = 1:length(seqDiagrams)
      seqDiagram = seqDiagrams(i);
      % Create a SequenceDiagram reporter for each diagram
      seqReporter =   
systemcomposer.rptgen.report.SequenceDiagram("Name", ...
      seqDiagram.Name, "ModelName", model.Name);
      seqSect = Section("Title", seqDiagram.Name);
      add(seqSect, seqReporter);
      add(seqChapter, seqSect);
  end
add(rpt, seqChapter);  
catch ME
     fprintf('Error encountered: %s\n', ME.message);
     % Additional logging or handling can go here
end

Please see attached results

Afterwards, I had to modify my code that was shared in posted comments. Here is brief summary of the modified version of my code, it does include some part of your code as well. However, it utilizes various libraries to create a report that documents sequence diagrams from a System Composer model. Initially, it imports necessary packages for report generation and model handling. The model is specified by its file path, and a report object is created with a designated output path. The code then loads the specified model and checks for successful loading. If the model is not loaded, it raises an error. It retrieves sequence diagrams from the model and checks if any exist; if none are found, it issues a warning and exits. For each sequence diagram, the code creates a corresponding section in the report, adding the diagram to the report chapter. A try-catch block is employed to handle any potential errors during this process. Finally, the report is closed, which triggers the viewing or saving of the report, depending on the platform.

import mlreportgen.report.*
import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.dom.*
import mlreportgen.utils.*
import systemcomposer.query.*
import systemcomposer.rptgen.finder.*
% Define a more descriptive model name as a string
modelFileName = '/MATLAB Drive/Examples/R2024a/model_name.slx'; %     
Replace with your actual model name
% Create a report object
rpt = slreportgen.report.Report('OutputPath', 'FullArchitecture', ...
  'CompileModelBeforeReporting', false);
% Load the model
model = systemcomposer.loadModel(modelFileName);
% Check if the model is loaded successfully
if isempty(model)
  error('Model could not be loaded: %s', modelFileName);
end
% Get sequence diagrams from the model
seqDiagrams = model.getInteractions; 
% Check if there are any sequence diagrams
if isempty(seqDiagrams)
  warning('No sequence diagrams found in the model: %s', modelFileName);
  return; % Exit if there are no diagrams
end
% Create a chapter for sequence diagrams
seqChapter = Chapter("Title", "Sequence Diagrams");
try
  % Loop through each sequence diagram and add it to the report
  for i = 1:length(seqDiagrams)
      seqDiagram = seqDiagrams(i);
      % Create a SequenceDiagram reporter for each diagram
      seqReporter =     
systemcomposer.rptgen.report.SequenceDiagram("Name", ...
          seqDiagram.Name, "ModelName", model.Name);
      seqSect = Section("Title", seqDiagram.Name);
      add(seqSect, seqReporter);
      add(seqChapter, seqSect);
  end
  % Add the chapter to the report
  add(rpt, seqChapter);  
catch ME
  fprintf('Error encountered: %s\n', ME.message);
end
% Finally, close and view or save the report (depending on platform)
close(rpt);

Please see attached results of pdf file and modified version of code.

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by