Surya \’s Blog

… ever streaming tools and technologies….!!!

EMF Howto’s

Posted by kathayat on July 26, 2007

Hints, tips and tricks concerning the use of the EMF API’s.

Initializing a ResourceSet

When using a ResourceSet object with the EMF-UML2 profile (as in various of the tutorials), you must build a map of URI mappings, as these are used internally by the UML2 metamodel. Each of these mappings maps a logical URI to a physical URI.

If you don’t define these mappings, your code will, for the most part, still appear to run happily. You could for example load metamodels and libraries by using physical URI’s directly. This is allowed – but internally these metamodels/libraries will use logical URI’s to make references to yet other metamodels/libraries. These references won’t work if you haven’t defined the mappings. Unfortunately, no obvious errors will occur; rather, the libs/metamodels will be loaded apparently without any problems; but they’ll incomplete, in the sense that whatever couldn’t be referenced wasn’t loaded. At a later stage in your program, when you actually use elements from the loaded libs/models, the exceptions will start flying.

This is an example of code that takes care of the mappings:

org.eclipse.emf.ecore.resource.ResourceSet rs = new ResourceSetImpl(); 
Map uriMap = rs.getURIConverter().getURIMap(); 

Of course, make sure you change the physical paths according to your own environment.

Saving a model with applied stereotypes and tag values

For reasons that will likely forever elude us, the obvious way of saving an EMF model skips any stereotype applications and tag-value assignments that are part of that model. If you want the stereotype applications (and hence the associated tag-value assignments) saved, you’ll have to explicitly add them to the resource, by iterating over all instances of org.eclipse.uml2.uml.Element in your model, fetching any applied stereotypes, and adding them to the resource’s contents collection.

The following code shows how:

ResourceSet rs = new ResourceSetImpl(); 
// ... some code to initialize the resourceset 
Resource resource = rs.createResource(URI.createFileURI("d:/temp/myrequirements.xmi")); 
EList contents = resource.getContents(); 
for (Iterator allContents = UMLUtil.getAllContents(model,true,false); allContents.hasNext(); ) { 
	EObject eObject = (EObject); 
    if (eObject instanceof Element) { 
        contents.addAll(((Element) eObject).getStereotypeApplications()); 

Getting notified of changes to tagged values

When you subscribe a listener (called an adapter in the EMF world) to listen to changes to an eCore model object, that listener’s notifyChanged method will not get called when a tagged value is set on a stereotype that has been applied to an object that is part of the model. If you want your listener to be called on such changes, you will have to register the listener specifically with the instance of the stereotype.

The following code shows how, assuming that stereotype is a reference to a stereotype with a tag called sometag.

org.eclipse.uml2.uml.Class fooclass = model.createOwnedClass("Foo", false); 

// Apply the stereotype and obtain a reference to it 
EObject eObject = fooclass.applyStereotype(stereotype); 

// Register a listener with the instance of the stereotype 
eObject.eAdapters().add(new MyAdapter()); 

// Now set a value for the tag. 
// This will make the framework call notifyChanged on the listener. 
fooclass.setValue(stereotype, "sometag", "newvalue");

Creating SysML requirements using EMF-UML2 and stereotypes

I constructed a small profile implementing the requirements part of the SysML 1.0 specification, which you’ll find here: Partial SysML 1.0 implementation based on EMF-UML2

If you load this profile programatically, you can construct a metamodel-based datamodel (confused yet?) in which to store requirements and their attributes. This code snippet is a basic example:

// load the resource (i.e. file) containing the profile 
ResourceSet rs = ... ; 
Resource r = rs.createResource(URI.createFileURI("c:/path/to/the/profile/SysML_Requirements.uml")); 

// retrieve the SysML profile, the nested Requirements profile, and the Requirement stereotype 
Profile sysMlProfile = (Profile)EcoreUtil.getObjectByType(r.getContents(), UMLPackage.Literals.PROFILE); 
Profile requirementsProfile = (Profile)sysMlProfile.getOwnedMember("Requirements"); 
Stereotype requirementStereotype = (Stereotype)requirementsProfile.getOwnedMember("Requirement"); 

// create a model and apply the profile to it 
Model model = UMLFactory.eINSTANCE.createModel(); 

// create a requirement, name it, and set it's Id and Text attributes 
org.eclipse.uml2.uml.Class req1 = model.createOwnedClass("RequirementOne", true); 
req1.setValue(requirementStereotype, "Id", "1"); 
req1.setValue(requirementStereotype, "Text", "My very first SysML requirement");

2 Responses to “EMF Howto’s”

  1. anand said

    Thanks man, that helped.

  2. Lorie said

    I need to to thank you for this good read!! I certainly enjoyed every bit of it.
    I have you book marked to look at new things you post…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: