Retrieving slot value from an element, a classifier’s Name, and a defining feature name
Code Block
language
text
$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:
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.
Code Block
language
text
#foreach($instance in $InstanceSpecification)
Slot value is $instance.slots.Communicate.Address
#end
The output of the code shown above is :
Code Block
language
text
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..*].
Code Block
language
text
#foreach($instance in $InstanceSpecification)
Slot value is $instance.slots.Communicate.Telephone
#end
The output of the code shown above is:
Code Block
language
text
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.
Code Block
language
text
#foreach($instance in $InstanceSpecification)
Slot value is $instance.slots.get("Personal Data").get("Name")
#end
The output of the code shown above is:
Code Block
language
text
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..*].
Code Block
language
text
#foreach($instance in $InstanceSpecification)
Slot value is $instance.slots.get("Personal Data").get("Nickname")
#end
The output of the code shown above is:
Code Block
language
text
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.
Code Block
language
text
#foreach($instance in $InstanceSpecification)
Slot value is $report.getSlotValue($instance, "Personal Data", "Name")
#end
The output of the code shown above is:
Code Block
language
text
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.
Code Block
language
text
#foreach($instance in $InstanceSpecification)
Slot value is $report.getSlotValue($instance, "Personal Data", "Name")
#end
The output of the code shown above is:
Code Block
language
text
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.
Code Block
language
text
#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:
Code Block
language
text
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