How to use Profile tailor UML to meet your specific needs
When you create your UML model, it is helpful if you label your model elements with the terminology specific to the environment or platform you've chosen. In other words, wouldn't it be great to be able to specify that a component is in fact going to be an EJB, instead of just calling it a component?
Trying to make UML target every possible platform or domain would be a losing battle and not really in the spirit of a general purpose modeling language. The group behind UML, the Object Management Group (OMG), realized this and built a mechanism by which UML can be adapted and tailored to meet your own specific needs. That mechanism was the profile.
Profiles are a lightweight way to adapt UML to a particular platform. Profiles are made up of stereotypes, tagged values, and a set of constraints. They capture a vocabulary commonly used across a domain or platform and allow that vocabulary to be applied to your model's elements to make them more meaningful.
Stereotypes signify that an element has a special use or intent. Stereotypes are most often shown by specifying the name of the stereotype between two guillemots, as in <<stereotype_name>>;
In this example, there are two stereotypes: <<Message>> and <<Data>>.
Stereotypes may have one or more associated tagged values. Tagged values provide extra information that is associated with the stereotype. A tagged value is shown in a note that is attached to the stereotyped element.
Unlike stereotypes and tagged values, constraints don't correspond to symbols that you use in your UML models. Constraints are also specified in the profile definition, but they impose rules or restrictions on model elements.
Create Profile in UML2
In the UML2 model, you can create a profile using UML Model Wizard, define the stereotypes, and then apply the stereotype to your UML diagram model, adding extensional information to your diagrams.
//create a profile
Profile profile = UMLFactory.eINSTANCE.createProfile();
//import primitive type
protected static PrimitiveType importPrimitiveType(
org.eclipse.uml2.uml.Package package_, String name) {
ModelumlLibrary= (Model) load(URI
.createURI(UMLResource.UML_PRIMITIVE_TYPES_LIBRARY_URI));
PrimitiveType primitiveType = (PrimitiveType) umlLibrary
.getOwnedType(name);
Package_.createElementImport(primitiveType);
return primitiveType;
}
//create a stereotype
Stereotype stereotype = profile.createOwnedStereotype(name, isAbstract);
A stereotype usually has an extension metaclass which is a UML Class defined in UML criterion; the stereotype defines the extensional properties based on the metaclass. The properties defined in stereotypes can be primitive type, metaclass type and also stereotype. When you use a property typed by a stereotype, you need another way to get the value of this property.
In the example above, we defined two stereotypes <<Message>> and <<Data>>, they are both has an extension metaclass Class, the relationship between them shows in the picture.
//reference metaclass
protected static org.eclipse.uml2.uml.Class referenceMetaclass(Profile profile,
String name) {
Model umlMetamodel = (Model) load(URI
.createURI(UML2Resource.UML2_METAMODEL_URI));
org.eclipse.uml2.Class metaclass = (org.eclipse.uml2.uml.Class) umlMetamodel
.getOwnedType(name);
profile.createMetaclassReference(metaclass);
return metaclass;
}
//create an extension
protected static Extension createExtension(
org.eclipse.uml2.uml.Class metaclass, Stereotype stereotype,
boolean required) {
Extension extension = stereotype.createExtension(metaclass, required);
return extension;
}
Work with Profile
You also can use the getAllAppliedStereotype() method to get all the applied stereotype of a UML element, and use getValue() method to get the values of properties defined in stereotypes.
// get the value of properties defined in stereotype
public static Object getStereotypePropertyValue(NamedElement
namedElement, Stereotype stereotype, Property property) {
Object value = namedElement.getValue(stereotype, property.getName());
return value;
}
when the property is defined using a stereotype, if you use the method above, you will get a DynamicEObjectImpl not the target object. The reason is: when you use stereotype to type a property, it will associate several DynamicEObjectImpls in the package, the real targets are put into these DynamicEObjectImpls. You can use the UMLUtil.getBaseElement(DynamicEObjectImpl) method to get the base element namely the real target of this DynamicEObjectImpl.
public static Element getStereotypeTypedPropertyValue(NamedElement
namedElement, Stereotype stereotype,String propertyName){
//get the DynamicEObjectImpl using the getValue method
DynamicEObjectImpl ob=(DynamicEObjectImpl)namedElement.getValue
(stereotype, propertyName);
// get the base object of the DynamicEObjectImpl using the getBaseElement() method defined in UMLUtil
Element element=UMLUtil.getBaseElement(ob);
return element;
}
