Overview of Sequence Diagram Model Operations
The extension allows you to access each element of a sequence diagram via API.
Sequence Diagram Elements
|No.|Element Name|Description|
|--:|:--|:--|
|1|Lifeline|A lifeline that represents the lifespan of an object. |
|2|Message|Represents a message between objects. |
|3|Execution Specifications|Represents the interval in which an object is activated. |
|4|Combined Fragment|Represents the interval in which the message sequence is controlled by conditions. |
|5|Interaction Usage|Represents the interval in which the details of the message sequence are split into another sequence diagram. |
|6|Note|Represents a comment. |
Sequence diagrams in Next Design are composed of shapes that represent the expression method and models that represent the expression content, just like other editor elements. Therefore, like other editor elements, you can access model information with the Model
property of each sequence diagram element. However, please note that following model changes in the view is not supported. If you change the field value related to the configuration of the sequence diagram in the model information of the sequence diagram, you will not be able to open the sequence diagram.
Get lifelines
To get lifelines, use the Lifelines
property of the ISequenceDiagram
object.
public void GetLifelines(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the lifelines
ILifelineShapeCollection lifelines = sequenceDiagram.Lifelines;
var no = 1;
foreach (ILifelineShape lifeline in lifelines)
{
c.App.Output.WriteLine("sample", $"{no}th lifeline : {lifeline.Text}");
no++;
}
}
Get execution specifications
To get the execution specifications of a lifeline, use the ExecutionSpecifications
property of the ILifelineShape
object.
public void GetExecutionSpecifications(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the execution specifications of the selected lifeline
ILifelineShape lifeline = sequenceDiagram.GetSelectedShapes().FirstOrDefault() as ILifelineShape;
IExecutionSpecificationShapeCollection executionSpecifications = lifeline.ExecutionSpecifications;
c.App.Output.WriteLine("sample", $"Lifeline: {lifeline.Text} has {executionSpecifications.Count()} execution specifications");
}
You can also get all the execution specifications on a sequence diagram by using the ExecutionSpecifications
property of the ISequenceDiagram
object.
public void GetAllExecutionSpecifications(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the execution specifications
IExecutionSpecificationShapeCollection executionSpecifications = sequenceDiagram.ExecutionSpecifications;
foreach (IExecutionSpecificationShape executionSpecification in executionSpecifications)
{
//The Lifeline property gives access to the lifeline that holds the execution specification.
ILifelineShape lifeline = executionSpecification.Lifeline;
//The Activator property gives access to the message that activates the execution specification.
IMessageShape activator = executionSpecification.Activator;
c.App.Output.WriteLine("sample", $"{lifeline.Text} is activated by {activator.Text}.");
}
}
Get messages
To get messages sent and received from a lifeline, access the ExecutionSpecifications
property of the ILifelineShape
object and get the execution specification. In addition, get messages using the SendMessages
and ReceiveMessages
properties of the IExecutionSpecificationShape
object.
public void GetMessages(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the sent and received messages from the execution specifications of the selected lifeline
ILifelineShape lifeline = sequenceDiagram.GetSelectedShapes().FirstOrDefault() as ILifelineShape;
var sendMessages = lifeline.ExecutionSpecifications.SelectMany(ec => ec.SendMessages);
var receiveMessages = lifeline.ExecutionSpecifications.SelectMany(ec => ec.ReceiveMessages);
c.App.Output.WriteLine("sample", $"Lifeline: {lifeline.Text} has {sendMessages.Count()} sent messages");
c.App.Output.WriteLine("sample", $"Lifeline: {lifeline.Text} has {receiveMessages.Count()} received messages");
}
You can also get all messages on a sequence diagram by using the Messages
property of the ISequenceDiagram
object.
public void GetAllMessages(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get messages
IMessageShapeCollection messages = sequenceDiagram.Messages;
foreach (IMessageShape message in messages)
{
//The sender and receiver lifelines can be accessed using the Sender and Receiver properties, respectively.
//These properties return null if the sender and receiver are not lifelines.
ILifelineShape sender = message.Sender;
ILifelineShape receiver = message.Receiver;
var senderName = sender != null ? sender.Text : "Unknown";
var receiverName = receiver != null ? receiver.Text : "Unknown";
c.App.Output.WriteLine("sample", $"{message.Text} is a message from {senderName} to {receiverName}.");
}
}
You can find out the type and context of a message by accessing the message model from the IMessageShape
object. To find the message model, cast the Model
property of the IMessageShape
object to the IMessage
type.
Finding the Message Type
You can identify the type of a message with the Kind
property of the IMessage
object.
|Kind property value|Type|
|:--|:--|
|sync|Synchronous message|
|async|Asynchronous message|
|create|Create message|
|destroy|Destroy message|
|reply|Reply message|
Check message context
The Before
property of the IMessage
object can be used to obtain the preceding message (the message sent just before the same message and sender). The After
property of the IMessage
object can be used to obtain the following message (the message sent just after the same message and sender).
The preceding and following messages do not include the response message. If the message being evaluated is a response message, both the preceding and following messages will be null.
Even if messages are sent from the same lifeline, messages with different execution specifications will not be treated as preceding and succeeding messages.
public void GetMessageInfo(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the selected message
IMessageShape messageShape = sequenceDiagram.GetSelectedShapes().FirstOrDefault() as IMessageShape;
IMessage message = (IMessage)messageShape.Model;
c.App.Output.WriteLine("sample", $"The type of {messageShape.Text} is {message.Kind}.");
//Get the shape of the preceding and following messages
IMessageShape beforeMessageShape = GetMessageShape(sequenceDiagram, message.Before);
IMessageShape afterMessageShape = GetMessageShape(sequenceDiagram, message.After);
if (beforeMessageShape != null)
{
c.App.Output.WriteLine("sample", $"The message before {messageShape.Text} is {beforeMessageShape.Text}.");
}
if (afterMessageShape != null)
{
c.App.Output.WriteLine("sample", $"The message after {messageShape.Text} is {afterMessageShape.Text}." ");
}
}
private IMessageShape GetMessageShape(ISequenceDiagram sequenceDiagram, IMessage message)
{
if (message == null) return null;
return sequenceDiagram.Messages.FirstOrDefault(m => m.Model.Equals(message));
}
Get combined fragments
To get combined fragments, use the Fragments
property of the ISequenceDiagram
object.
public void GetFragments(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get combined fragments
IFragmentShapeCollection fragments = sequenceDiagram.Fragments;
var no = 1;
foreach (IFragmentShape fragment in fragments)
{
c.App.Output.WriteLine("sample", $"{no}th composite fragment : {fragment.Text}");
no++;
}
}
You can also access the operands using the Operands
property of the IFragmentShape
object. In the operands, you can see the guard conditions and messages contained within the operand compartment.
public void GetFragmentInfo(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the selected composite fragment
IFragmentShape fragment = sequenceDiagram.GetSelectedShapes().FirstOrDefault() as IFragmentShape;
var no = 1;
foreach (IOperandShape operand in fragment.Operands)
{
c.App.Output.WriteLine("sample", $"Operand[{no}]:{operand.Guard}");
//Get the message contained in the operand compartment
foreach (IMessageShape message in operand.Messages)
{
c.App.Output.WriteLine("sample", $"-- {message.Text}");
}
no++;
}
}
Get interaction uses
To get interaction uses, use the InteractionUses
property of the ISequenceDiagram
object.
public void GetInteractionUses(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get interaction uses
IInteractionUseShapeCollection interactionUses = sequenceDiagram.InteractionUses;
var no = 1;
foreach (IInteractionUseShape interactionUse in interactionUses)
{
c.App.Output.WriteLine("sample", $"{no}th interaction use : {interactionUse.Text}");
no++;
}
}
Get notes
To get notes, use the Notes
property of the ISequenceDiagram
object.
public void GetNotes(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get notes
INoteShapeCollection notes = sequenceDiagram.Notes;
var no = 1;
foreach (INoteShape note in notes)
{
c.App.Output.WriteLine("sample", $"{no}th note : {note.Text}");
no++;
}
}
You can also get notes connected to sequence diagram elements by using INoteAnchorShape
, which can be obtained from the NoteAnchors
property of the ISequenceDiagram
object.
public void GetComments(ICommandContext c, ICommandParams p)
{
ISequenceDiagram sequenceDiagram = c.App.Workspace.CurrentEditor as ISequenceDiagram;
if (sequenceDiagram == null) return;
//Get the selected sequence diagram element
var shape = sequenceDiagram.GetSelectedShapes().OfType<ISequenceShape>().FirstOrDefault();
if (shape == null)
{
return;
}
//Get the note anchors attached to the element from the diagram
var anchors = sequenceDiagram.NoteAnchors.Where(a => shape.Equals(a.Target));
foreach (INoteAnchorShape anchor in anchors)
{
INoteShape note = anchor.Note;
c.App.Output.WriteLine("sample", $"Comment: {note.Text}");
}
}