Use utility class com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper to work with profiles, stereotypes, tagged values. Keep in mind that Tagged Values in this helper class are called Slots or stereotype properties.

 

StereotypeHelper provides API to check for stereotypes by a stereotype name. Try to avoid this API and pass Stereotype as object if possible. In many models several profiles with stereotypes with same names can be used and this "name based" API can cause unpredictable results.

So general rules are:

  • Put URI for your profiles and find Profile in model by URI, not by profile name
  • Find Stereotype element inside a specific Profile, not in a whole model
  • Pass Stereotype element to StereotypesHelper methods instead of passing a stereotype name


Find a stereotype by name

Find profile by URI (or by name) first using 

StereotypesHelper.getProfileByURI(Project, java.lang.String)

StereotypesHelper.getProfile(Project, java.lang.String)

 

Find stereotype by name inside a profile using 

StereotypesHelper.getStereotype(Project, java.lang.String, Profile)


Check if stereotype is applied

Check if any stereotype is applied

StereotypesHelper.hasStereotype(Element)

Check if concrete stereotype is assigned


Check if concreate stereotype or derived stereotype is applied

StereotypesHelper.hasStereotypeOrDerived(Element, Stereotype)


Get value of tagged value

Values of tagged values are returned as java.util.List even if upper multiplicity of tag 1. In this case List will have zero or one object.

StereotypesHelper.getStereotypePropertyValue(Element, Stereotype, java.lang.String)

StereotypesHelper.getStereotypePropertyFirst(Element, Stereotype, java.lang.String)

 

Set value of tagged value

StereotypesHelper.setStereotypePropertyValue(Element, Stereotype, java.lang.String, java.lang.Object)

 

Collect all elements with applied stereotype

For a concrete stereotype use

 

For a concrete stereotype and derived stereotypes from it use

StereotypesHelper.getExtendedElementsIncludingDerived(Stereotype)


Check if stereotype can be applied to Element

Remove stereotype from element

StereotypesHelper.removeStereotype(Element, Stereotype)

Apply stereotype to element

StereotypesHelper.addStereotype(Element, Stereotype)

Sample
Element element = ....; //Element for which we add stereotype, set tag value and then remove the stereotype.
Project project = ...; //Project
String tagValue = "test value";
Profile profile = StereotypesHelper.getProfile(project, "ProfileNameExample");
Stereotype stereotype = StereotypesHelper.getStereotype(project, "StereotypeNameExample", profile);
StereotypesHelper.addStereotype(element, stereotype); //We add stereptype to element
StereotypesHelper.setStereotypePropertyValue(columnIds, stereotype, "TagName", tagValue); //we set stereotype tag named "TagName"
StereotypesHelper.removeStereotype(element, stereotype); //we remove stereotype from element