UML2 stereotype properties
I’ve been working with the eclipse uml2 libraries a lot lately. They’re confusing to start with, since the api makes no effort to encapsulate the underlying ecore model. Today I spent half an hour puzzling over a slightly complex use of stereotypes: we have a stereotype in our profile with properties which are defined as lists of objects, whose class is also defined in the profile. The uml library offers a property.getValue(stereotype, propertyname) method for getting values of stereotype properties (when applied to a uml property in this case), but when I ran this I got an ugly EcoreEList$Dynamic object, instead of a nice list of objects containing the values defined in my model.
Fortunately, uml2 is open source, so I could easily download the code and see if there was any way of navigating this monstrosity. It turned out that all that was needed was to use getValue is a different way: the property name takes qualified names. If only the documentation had said that, I wouldn’t have posted for help on the newsgroup only to retract it less than an hour later. Anyway, here’s how to access deep nested stereotype properties:
Collection< ?> coll = umlProperty.getValue(myStereotype, “property”);
for (int i = 0 ; i < coll.size(); i++)
{
String a = umlProperty.getValue(myStereotype, "property["+i+"]::subproperty");
EnumerationLiteral b = umlProperty.getValue(myStereotype, "property["+i+"]::subproperty2");
Integer c = umlProperty.getValue(myStereotype, "property["+i+"]::subproperty3");
doStuff(a,b,c);
}