Use utility class com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper to work with profiles, stereotypes, tagged values.

StereotypesHelper extends com.nomagic.uml2.ext.jmi.helpers.TagsHelper. Keep in mind that Tagged Values in these helpers are also called stereotype properties.

 

StereotypeHelper still provides deprecated 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)

or 

check if Element.getAppliedStereotype() collection is empty


Check if concrete stereotype is assigned
or
check if Element.getAppliedStereotype() collection contains stereotype


Check if concrete 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.


TagsHelper.getStereotypePropertyValue(Element, Property)

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


TagsHelper.getStereotypePropertyFirst(Element, Property)

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


TagsHelper.getTaggedValue(Element, Property)

TagsHelper.getTaggedValue(Element, Stereotype, java.lang.String)


Set value of tagged value

TagsHelper.setStereotypePropertyValue(Element, Stereotype, Property, java.lang.Object)

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


TagsHelper.clearStereotypeProperty(Element, Property)

TagsHelper.clearStereotypeProperty(Element, Property)


Collect all elements with applied stereotype

For a concrete stereotype use

StereotypesHelper.getStereotypedElements(Stereotype)

For a concrete stereotype and derived stereotypes from it use

StereotypesHelper.getStereotypedElementsIncludingDerived(Stereotype)


Check if stereotype can be applied to Element

Remove stereotype from element

StereotypesHelper.removeStereotype(Element, Stereotype)

or

remove from collection Element.getAppliedStereotype(). TaggedValues of removed stereotype will not be removed immediately using this approach, but will be removed on transaction commit.

Apply stereotype to element

StereotypesHelper.addStereotype(Element, Stereotype)

or

add into collection Element.getAppliedStereotype() 

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(element, stereotype, "TagName", tagValue); //we set stereotype tag named "TagName"
StereotypesHelper.removeStereotype(element, stereotype); //we remove stereotype from element