Instead of using Stereotype and Tag, some projects may use Instance Specification and Slot to define new attributes of an element.
Instance Specification and Slot defining element attributes
You can retrieve a slot value by either using a normal UML Specification structure, or by using a slot property.
Using normal UML Specification to Retrieve Slot Values
For example, to retrieve a Name slot value type:
#foreach($instance in $InstanceSpecification) #foreach($slot in $instance.slot) #if($slot.definingFeature.name == 'Name') #if($slot.value.size() > 0) #set($v = $slot.value.get(0).text) #else #set($v = "") #end Slot value is $v #end #end #end
Using Slot Property to Retrieve Slot Values
You can use a slot property as a shortcut to get the following information from a slot.
Retrieving slot information from an element
The following code retrieves slot information from an element.
$element.slots
In which:
- $element is an element.
- slots is a property for getting slot variables.
For example, if you want to obtain a collection of slot variables of an element, type:
#foreach($slot in $instanceSpecification.slots) $slot.name #end
Retrieving slot information from an element and a classifier’s name
The following code retrieves slot information from an element and a classifier’s name.
$element.slots.ClassifierName or $element.slots.get("ClassifierName")
Where:
- $element is an element.
- slots is a property for getting the slots variable.
- ClassifierName is the classifier’s name.
The code returns:
- A list of slot variables whose classifier names are ClassifierName.
- If there are multiple classifiers that match the ClassifierName, the code returns the slot property of the first classifier.
- If there is no classifier that matches the ClassifierName, the code returns null.
For example, if you want to retrieve all slot values from the “Communicate” classifier, type:
$instanceSpecification.slots.Communicate $instanceSpecification.slots.get("Communicate")
Retrieving slot value from an element, a classifier’s Name, and a defining feature name
$element.slots.ClassifierName.DefiningFeatureName or $element.slots.ClassifierName.get("DefiningFeatureName") or $element.slots.get("ClassifierName").DefiningFeatureName or $element.slots.get("ClassifierName").get("DefiningFeatureName")
Where:
- $element
is an element. - slots
is a property for getting slots variable. - ClassifierName
is the classifier’s name. - DefiningFeatureName
is the name of a slot.
The code returns:
- A slot value whose name is DefiningFeatureName and a classifier whose name is ClassifierName.
- If the slot multiplicity is < =1 (less than or equal to 1), it will return the value in ValueSpecification.
- If the slot multiplicity is > 1, it will return the value in array of ValueSpecification.
For example, if you want to retrieve the values of a slot whose name is “Address” and those of a classifier whose name is “Communicate”, type:
$instanceSpecification.slots.Communicate.Address $instanceSpecification.slots.Communicate.get("Address") $instanceSpecification.slots.get("Communicate").Address $instanceSpecification.slots.get("Communicate").get("Address")
- ClassifierName and DefiningFeatureName are case-sensitive properties.
You can use:
$report.getSlotValue($element, ClassifierName, DefiningFeatureName)
to retrieve the slot value. See $report below for further details.
Code examples to retrieve slot values
This section explains how to get the value of an instance slot from a classifier. The figure below shows the elements of a slot variable in the Containment tree in MagicDraw. You can see the values of the element Member in the tree, or you can right-click on it and open the Specification dialog to see the values in the dialog.
Example of elements in the Containment tree
The following figure shows the Instance Specification dialog of the element Member of the slot variable. As you can see in the dialog, the element Member of the slot variable contains slot names and values.
All slots of Instance Specification
The following are examples of using the code to retrieve a value of a slot.
Example 1.
If you want to print the value of a slot Address of the element Member from the classifier Communicate, you can use the following template code as a shortcut. Note that the slot multiplicity = 1.
#foreach($instance in $InstanceSpecification) Slot value is $instance.slots.Communicate.Address #end
The output of the code shown above is :
Slot value is US
Example 2.
If you want to print the value of a slot Telephone of the element Member from the classifier Communicate, you can use the following template code as a shortcut. Note that the slot multiplicity is > 1, [1..*].
#foreach($instance in $InstanceSpecification) Slot value is $instance.slots.Communicate.Telephone #end
The output of the code shown above is:
Slot value is [0899999999, 0811111111, 02222222]
Example 3.
If you want to print the value of a slot Name of the element Member from the classifier Personal Data, you can use the following template code as a shortcut. Note that the slot multiplicity = 1.
#foreach($instance in $InstanceSpecification) Slot value is $instance.slots.get("Personal Data").get("Name") #end
The output of the code shown above is:
Slot value is Peter
Example 4.
If you want to print the value of a slot Nickname of the element Member from the classifier Personal Data, you can use the following template code as a shortcut. Note that the slot multiplicity is > 1, [0..*].
#foreach($instance in $InstanceSpecification) Slot value is $instance.slots.get("Personal Data").get("Nickname") #end
The output of the code shown above is:
Slot value is [Peet, P]
Example 5.
If you want to use $Report method to print the value of a slot Name of the element Member from the classifier Personal Data, you can use the following template code.
#foreach($instance in $InstanceSpecification) Slot value is $report.getSlotValue($instance, "Personal Data", "Name") #end
The output of the code shown above is:
Slot value is [Peter]
Example 6.
If you want to use $Report method to print the value of a slot Nickname of the element Member from the classifier Personal Data, you can use the following template code.
#foreach($instance in $InstanceSpecification) Slot value is $report.getSlotValue($instance, "Personal Data", "Nickname") #end
The output of the code shown above is:
Slot value is [Peet, P]
Example 7.
If you want to print the names and values of all classifiers and the slot properties of all instance elements, you can use the following template code as a shortcut.
#foreach($instance in $InstanceSpecification) #foreach($classifier in $instance.slots) Classifier name: $classifier.name Slots: #foreach($slot in $classifier) $slot.name : $slot.value #end #end #end
The output of the code shown above is:
Classifier name: Personal Data Slots: Name : Peter Age : 20 Nickname : [Peet, P] Gender : Male ID : 1234 Classifier name: Communicate Slots: Telephone : [0899999999, 0811111111, 02222222] E-mail : [a@example.com, b@example.com] Address : US