Adding code to sequence diagrams

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  UML Diagrams > Behavioral Diagrams > Sequence Diagram > Generate code from sequence diagram >

Adding code to sequence diagrams

Program code can be generated from new, and reverse-engineered sequence diagrams, but only for a sequence diagram linked to the "main operation".

 

When reverse-engineering code, standard sequence diagram elements, e.g. CombinedFragments, are "mapped/assigned" to coding elements (e.g. "if" statements, loops, etc.).

 

For those programming statements that have no corresponding sequence diagram elements, e.g. "i = i+1", UModel makes use of "code" notes to add code to diagrams. These notes must then be linked to the lifeline.

 

Note that UModel does not check, or parse, these code fragments. It is up to you to make sure that the code fragments are correct and will compile.

 

 

To add code to a sequence diagram:

1.Click the Note icon ic-note then click the model element where you want to insert it, e.g. CombinedFragment.
2.Enter the code fragment, e.g. return.
3.Click the Node Link handle of the inserted note and drop the cursor on the lifeline.
4.Activate the "Is Code" check box in the Properties tab to include this code fragment when generating code.

sd-codeNote2

When selecting a note on a sequence diagram, which can be used for code generation, the property "is code" is available in the Properties window. Clicking the check box, allows you to switch between "ordinary" notes and code generation notes.

 

Ordinary notes:

sd-codeNoteNorm

 

Code generation notes

sd-codeNoteCode - shown with a darker dog-ear

 

Code updates occur automatically on every forward engineering process if the "Use for forward engineering" check box is active. If changes were made to the sequence diagram, the code of the operation is always overwritten.

 

The sequence diagram shown below was generated by right clicking the OnCommand operation and selecting Generate sequence diagram from code. The C# code of this example is available in the C:\Users\<user>\Documents\Altova\UModel2019\UModelExamples\IDEPlugIn\Styles\ folder. Use the option Project | Import Source Project, to import the project.

sd-codeNote

The code shown below is generated from the sequence diagram.

 

Public void OnCommand(int nID, object pUModel)
{
  //Generated by UModel. This code will be overwritten when you re-run code generation.
 
  if (!m_bPlugINVersionOK)
  {
  return;
  }
 
  if (nID == 3 || nID == 6)
  {
  OnSetStyles((IApplication)pUModel, "red");
  }
 
  if (nID == 4 || nID == 7)
  {
  OnSetStyles((IApplication)pUModel, "green");
  }
  GC.Collect();
 
}


© 2019 Altova GmbH