On this page

An example of elements of a tag variable in the Containment tree and all tags of a Class.

Using normal UML specifications to retrieve tag values

The sample code of usage of normal UML specifications to retrieve tag values is as follows:

#foreach($class in $Class)
	#foreach($tag in $class.taggedValue)
		#if($tag.tagDefinition.name == "Name")
			#if($tag.value.size() > 0)
        		#set($t = $tag.value.get(0))
        	#else
            	#set($t = "")
        	#end
        	Tag value is $t
        #end
	#end
#end

Using tag properties to retrieve tag values

You can use tag properties as a shortcut to get the following information from tags.

Retrieving tag information from elements

The following code retrieves tag information from an element.

$element.tags

Given that:

  • $element is an element.
  • tags is a property for getting the tag variable.

For example, if you want to obtain a collection of tag properties of an element, type the following line:

#foreach($tagProperty in $class.tags)
    $tagProperty.name
#end

Retrieving tag information from elements and stereotype names

The following code retrieves tag information from an element and a stereotype name.

$element.tags.StereotypeName

or

$element.tags.get("StereotypeName")

Given that:

  • $element is an element.
  • tags is a property for getting the tag variable.
  • StereotypeName is the name of a stereotype.

The code returns a list of tag variables whose tag stereotype names are StereotypeName.

Note

  • If there are multiple tag stereotype names that match StereotypeName, the code returns the tag property of the first tag stereotype name.
  • If there is no tag stereotype name that matches StereotypeName, the code returns null.

For example, if you want to retrieve all tag values from the Communicate tag stereotype, type the following:

$class.tags.Communicate

or

$class.tags.get("Communicate")

Retrieving tag values from elements, tag stereotype names, and tag names

Type one of the following lines:

$element.tags.StereotypeName.TagName
$element.tags.StereotypeName.get("TagName")
$element.tags.get("StereotypeName").TagName
$element.tags.get("StereotypeName").get("TagName")

Given that:

  • $element is an element.
  • tags is a property for getting the tag variable.
  • StereotypeName is the name of a stereotype.
  • TagName is the name of a tag.

The code returns the following:

  • A tag value named TagName and a tag stereotype named StereotypeName.
  • If the tag multiplicity is < =1 (less than or equal to 1), it will return a value.
  • If the tag multiplicity is > 1, it will return an array of values.

For example, if you want to retrieve the values of a tag named Address and those of a tag stereotype named Communicate, type the following lines:

$class.tags.Communicate.Address
$class.tags.Communicate.get("Address")
$class.tags.get("Communicate").Address
$class.tags.get("Communicate").get("Address")

Note

StereotypeName and TagName are case-sensitive properties.

Code examples to retrieve tag values

This section explains how to get the value of a tag from a class. The figure below shows the elements of a tag variable in the Containment tree in MagicDraw. You can see the values of the Member element in the tree, or you can right-click it and select Specification to open the Specification dialog to see the values.

An example of elements of a tag variable in the Containment tree.

The following figure shows the Specification dialog of the Member element of the tag variable. The Member element of the tag variable contains tag names and values.

All tags of a Class.

The following code examples are for retrieving the value of a tag.


Example 1:

If you want to print the Address tag value of the Member element from the Communicate stereotype, you can use the following template code as a shortcut.

Note that the tag multiplicity = 1.

#foreach($class in $Class)
    Tag value is $class.tags.Communicate.Address
#end

The output of the code shown above is:

Tag value is US


Example 2:

If you want to print the Telephone tag value of the Member element from the Communicate stereotype, you can use the following template code as a shortcut.

Note that the tag multiplicity is > 1, [1..*].

#foreach($class in $Class)
    Tag value is $class.tags.Communicate.Telephone
#end

The output of the code shown above is:

Tag value is [0899999999, 0811111111, 02222222]


Example 3:

If you want to print the Name tag value of the Member element from the Personal Data stereotype, you can use the following template code as a shortcut.

Note that the tag multiplicity = 1.

#foreach($class in $Class)
    Tag value is $class.tags.get("Personal Data").get("Name")
#end

The output of the code shown above is:

Tag value is Peter


Example 4:

If you want to print the Nickname tag value of the Member element from the Personal Data stereotype, you can use the following template code as a shortcut.

Note that the tag multiplicity is > 1, [0..*].

#foreach($class in $Class)
    Tag value is $class.tags.get("Personal Data").get("Nickname")
#end

The output of the code shown above is:

Tag value is [Peet, P]


Example 5:

If you want to print the names and values of all stereotypes and the tag properties of all class elements, you can use the following template code as a shortcut.

#foreach($class in $Class)
	#foreach($tagProperty in $class.tags)
    	Stereotype name: $tagProperty.name
    	Tags:
      		#foreach($tag in $tagProperty)
       			$tag.name : $tag.value
      		#end
  	#end
#end

The output of the code shown above is:

Stereotype name: Communicate
Tags:
	Address : US
	Telephone : [0899999999, 0811111111, 02222222]
	E-mail : [a@example.com, b@example.com]
Stereotype name: Personal Data
Tags:
 	Gender : Male
	ID : 1234
	Age : 20
	Name : Peter
	Nickname : [Peet, P]