Example: Defining Templates to Extract Message Descriptions

We are going to generate a textual description of the messages in the diagram, giving for each message, the names of the sender, message, and receiver. To do so, we will need to define PowerDesigner Generation Template Language (GTL) templates to extract the information and a generated file to contain and display the extracted information.

To generate this textual description, we will need to extract information from the Message metaclass (to extract the message sequence number, name, sender, and receiver) and the CommunicationDiagram (to gather all the messages from each diagram and sort them)

  1. Right-click the Profile category, select Add Metaclasses to open the Metaclass Selection dialog, select CommunicationDiagram and Message on the PdOOM tab and click OK to add them to the extension file.
  2. Right-click the Message category and select New > Template to create a template under the metaclass.
  3. Enter description in the Name field, and then enter the following GTL code in the text area:
    .set_value(_tabs, "", new)
    .foreach_part(%SequenceNumber%, '.')
       .set_value(_tabs, "   %_tabs%")
    .next
    %_tabs%%SequenceNumber%) %Sender.ShortDescription% sends message "%Name%" to %Receiver.ShortDescription%

    The first line of the template initializes the _tabs variable, and the foreach_part macro calculates an appropriate amount of indentation by looping through each sequence number, and adding 3 spaces whenever a dot is found. The last line uses this variable to indent, format, and display information extracted for each message.

  4. Right-click the CommunicationDiagram category and select New > Template to create a template under the metaclass.
  5. Enter compareCbMsgSymbols in the Name field, and then enter the following GTL code in the text area:
    .bool (%Item1.Object.SequenceNumber% >= %Item2.Object.SequenceNumber%)

    This template resolves to a boolean value to determine if one message number is greater than another, and the result will be used in a second template.

  6. Right-click the CommunicationDiagram category and select New > Template to create a second template, enter description in the Name field, and then enter the following GTL code in the text area:
    Collaboration Scenario %Name%:
    \n
    .foreach_item(Symbols,,, %ObjectType% == CollaborationMessageSymbol, %compareCbMsgSymbols%)
      %Object.description%
    .next(\n)

    The first line of this template generate the title of the scenario from the name of the communication diagram. Then the .foreach_item macro loops on each message symbol, and calls on the other templates to format and output the message information.



  7. Click Apply to save your changes before continuing.
    For detailed information about templates and GTL, see Templates (Profile) and Customizing Generation with GTL.