Prev Next

Query Methods

When you are using IfElse statements in a Shape Script, the condition is usually that the object has a certain tag or property, and possibly if that tag or property has a particular value. You can set up the conditional statement to check for the property and value using one of the two query methods described here.

Queries

Method

Description

See also

boolean HasTag(string tagname,
(string tagvalue))

HasTag(tagname) evaluates to 'True' if 'tagname' exists and its value is non-empty; otherwise it evaluates to 'false'.

HasTag(tagname,tagvalue) evaluates to 'True' if 'tagname' exists and its value is 'tagvalue'.

HasTag(tagname,tagvalue) will also evaluate to 'True' if 'tagname' doesn't exist and 'tagvalue' is empty, treating 'empty' and 'missing' as having the same meaning in this context.

Tagged Values

boolean HasProperty(string propertyname,
(string propertyvalue))

Evaluates to True if the associated element has a property with the name propertyname.

If the second parameter propertyvalue is provided, the property must be present, and the value of the property has to be equal to propertyvalue for the method to evaluate to True.

The propertyvalue parameter can have multiple values, separated by commas; for example:

     if(HasProperty("Type","Class,Action,Activity,Interface"))

     {

          SetFillColor(255,0,0);

          DrawNativeShape();

     }

This Shape Script will use the standard element fill color for elements of any type other than one of the four specified in the if(HasProperty()) statement; elements of any of those four types will display with a red fill.

Display Element/Connector Properties

HasProperty and user-selected settings

A particular application of the HasProperty() method is to check for property settings where you have provided the facility for the user to set that property for a specific instance of use of the stereotyped element. So, the user can drag the element onto the diagram and, through the element context menu, set one or more properties that the Shape Script responds to in rendering the diagram object. The element might, therefore, have one appearance on one diagram but a different appearance on another, because it has different property settings on the two diagrams.

To specify user-selectable properties in your Profile, create the appropriate Stereotype element and - for each property being defined - add an attribute with the stereotype «diagram property» to this element. For the attribute name, type the text of the option that will display on the context menu for the stereotyped element; for example, 'Is Red'. Also give the attribute an alias, which would be the name of the property as it is stored and which the HasProperty() method will evaluate. If you set the attribute's initial value to 1, the context menu option will initially be set; if there is no initial value, the property option will default to not set.

Also define an _image attribute with a Shape Script that applies the HasProperty() method. In this example, the Shape Script defines two Class properties (Is Red and Is Triangle) for the HasProperty() method to check whether the option is set or not.

shape main

{

     if (HasProperty("IsRed","1"))

          {

          SetFillColor(255,128,128);

          }

     if (HasProperty("IsTriangle","0"))

          {

          Polygon(50,50,3,50,0);

          }

     else

          {

          DrawNativeShape();

          }

     }

When the Stereotype for the extended element type is defined, it will resemble this:

After the MDG Technology is created and released to your users, when they drag the stereotyped element from the Toolbox it will be rendered according to current settings for the defined properties, which the users can access and re-set through the context menu, as shown:

 

Learn more