Altova UModel 2024 Professional Edition

Creating new Sequence Diagrams programmatically from scratch using the UModel API is basically nothing more than placing interaction fragments, such as Lifelines on a diagram and connecting them with messages.

 

Messages can easily be created using the AddUMLLineElement() method of IUMLGuiLineLink, which removes the necessity of creating multiple underlying UML Elements such as MessageEnds, ExecutionOccurrences and similar manually.

 

To make it simple to create Messages between two interaction fragments such as Lifelines, create a small helper function which calls AddUMLLineElement() and positions the created line:

 

// Creates a message between two interaction fragments (i.e. lifelines, interaction uses,
// combined fragments or gates) and attaches all necessary elements like events and activation bars.
// Possible values for 'kind': "Message", "Reply", "Create", "Destruct"
protected static IUMLMessage addMessage(int ypos, string kind,
                                IUMLGuiNodeLink from, IUMLGuiNodeLink to,
                                DiagramWindow wnd)
{
  // add message
  IUMLGuiLineLink line = wnd.Diagram.AddUMLLineElement(kind, from, to);
 
  if (line == null)
      return null;
 
  // set position of the line where we want it to show up
  wnd.UpdateWindow();
 
  if (from == to && line.Waypoints.Count > 3)
  {
      // self-message
      ((IUMLGuiWaypoint)line.Waypoints[1]).SetPos(0, ypos);
      ((IUMLGuiWaypoint)line.Waypoints[4]).SetPos(0, ypos + 25);
  }  
  else
  if (line.Waypoints.Count > 1)
  {
      // normal message
      ((IUMLGuiWaypoint)line.Waypoints[1]).SetPos(0, ypos);
      ((IUMLGuiWaypoint)line.Waypoints[2]).SetPos(0, ypos);
  }
 
  return (IUMLMessage)line.Element;
}

 

As you can see, IUMLDiagram.AddUMLLineElement() accepts as a parameter not only the string "Message", to create a Message Line; but also "Reply", "Create" and "Destruct", for Reply Messages, Creation Messages and Destruction Messages.

 

In order to create a simple diagram it is only necessary to create a Sequence Diagram in the GuiRoot object, open the diagram, add a handful of lifelines and connect them with messages using this helper function:

 

IDocument document = theapplication.ActiveDocument;
 
// create diagram and open it
IUMLGuiSequenceDiagram sequenceDiagram =
  (IUMLGuiSequenceDiagram)document.GuiRoot.InsertOwnedDiagramAt(0, document.RootPackage, "SequenceDiagram");
 
DiagramWindow wnd = document.OpenDiagram(sequenceDiagram);
 
// create two lifelines
IUMLGuiNodeLink lifeline1 = sequenceDiagram.AddUMLElement("Lifeline", 0, 0);
IUMLGuiNodeLink lifeline2 = sequenceDiagram.AddUMLElement("Lifeline", 100, 70);
 
// connect these lifelines using some messages
addMessage(100, "Create", lifeline1, lifeline2, wnd);
addMessage(150, "Message", lifeline1, lifeline2, wnd);
addMessage(200, "Reply",   lifeline2, lifeline1, wnd);

 

The resulting created Diagram will look like this:

SequenceGeneration_SimpleDiagram

 

Setting the Type of a Lifeline

To display the Type represented by a Lifeline, be it be a Class, Interface, DataType or similar, use the IUMLLifeline.Represents property which references a IUMLProperty. If the Type of this property is set, the Type will show up on the diagram as well.

 

The following code creates a Lifeline which references a class:

 

// create a class to be referenced by the lifeline
IUMLClass someclass = (IUMLClass)document.RootPackage.InsertPackagedElementAt(0, "Class");
 
// create a lifeline and a property with the class as type in the interaction
// of the sequence diagram to reference this class
IUMLInteraction interaction = (IUMLInteraction)sequenceDiagram.LinkedOwner;
 
IUMLProperty prop = interaction.InsertOwnedAttributeAt(0);
prop.Type = someclass;
 
UModelLib.IUMLLifeline lifeline = interaction.InsertLifelineAt(0);
lifeline.Represents = (IUMLConnectableElement)prop;
 
// show the lifeline on the diagram
sequenceDiagram.AddUMLGuiNodeLink(lifeline, 200, 0);

 

The resulting lifeline would then look like this:

SequenceGeneration_LifelineReferencingType

 

Setting the Operation of a Message

Messages usually represent the invocation of an operation of an object. Note: based on the type of the Message (normal Message, Creation, Deletion or Reply) and the existence, or absence of underlying UML elements, such as MessageOccurenceSpecifications or CallEvents, it is not always possible for a Message to represent an Operation, and getting the cprrect UML element to point to the Operation is not that trivial.

 

This is why the IUMLMessage interface in the UModel API, offers the method SetOperation() with makes it possible to let a Message refer an Operation if it is able to do so:

 

// create a message, an operation in a class and let the message refer this operation
IUMLMessage msg = addMessage(250, "Message", lifeline1, lifeline2, wnd);
 
UModelLib.IUMLOperation someoperation = someclass.InsertOwnedOperationAt(0);
someoperation.Name = "SomeOperation";
 
msg.SetOperation(someoperation);

© 2017-2023 Altova GmbH