A Stereotype 'S' from a Profile 'P' is applicable on an Element 'E':
Here are sample codes that show how to read Stereotypes.
for (Stereotype myStereotype : myElement.getApplicableStereotypes()) { ... }
for (Stereotype myStereotype : myElement.getAppliedStereotypes()) { ... }
Here is a sample code that shows how to get the Stereotype "Requirement" on a Class:
Stereotype myStereotype = myClass.getAppliedStereotype("SysML::Requirements::Requirement"); if(myStereotype!=null){ ... }
SysML::Requirements::Requirement
represents the qualified name of the Stereotype "Requirement", in the SysML Profile.
Stereotype myStereotype = myClass.getAppliedStereotype("SysML::Requirements::Requirement"); if(myStereotype!=null){ String textValue = (String) upiaElement.getValue(myStereotype, "text"); ... }
Here are sample codes that show how to apply Stereotypes.
Profile profile = stereotype.getProfile(); if (profile != null) { if (profile.isDefined()) { com.sodius.mdw.metamodel.uml21.Package owner = element.getNearestPackage(); if (!owner.isProfileApplied(profile)) { owner.applyProfile(profile); } } }
if (!element.isStereotypeApplied(stereotype)) { if (element.isStereotypeApplicable(stereotype)) { element.applyStereotype(stereotype); } }
public static Property getProperty(Classifier stereotype, String name) { // lookup on stereotype { Property property = stereotype.getAllAttributes().detect(UMLPackage.Literals.NAMED_ELEMENT__NAME, name); if (property != null) { return property; } } // lookup on generals for (Classifier parent : stereotype.getGenerals()) { Property property = getProperty(parent, name); if (property != null) { return property; } } return null; }To get its Type :
property.getType();Here are samples code that shows how to set a Tagged Value for a Stereotype:
if(type instanceof PrimitiveType){ element.setValue(stereotype, property.getName(), value); }
if(type instanceof Stereotype){ EObject theApplication = null; // exact stereotype? EObject application = element.getStereotypeApplication(stereotype); if (application != null) { theApplication=application; } // sub stereotype? EClass eStereotype = stereotype.getDefinition(); if (eStereotype != null) { for (EObject application : element.getStereotypeApplications()) { if (eStereotype.isInstance(application)) { theApplication = application; } } } } if (theApplication != null) { element.setValue(stereotype, property.getName(), application); }
if(type instanceof Enumeration){ if(value instanceof String) EnumerationLiteral literal = ((Enumeration) type).getOwnedLiteral((String) value); if (literal != null) { element.setValue(stereotype, property.getName(), stringValue); } } }
if(type instanceof Enumeration){ if(value instanceof EnumerationLiteral) if (((Enumeration) type).getOwnedLiterals().contains((EnumerationLiteral) value)) { element.setValue(stereotype, property.getName(), enumerationLiteralValue); } } }
element.unapplyStereotype(stereotype);