Jump to: navigation, search

EMF/Deriving EMF Models

In accordance with the EMF FAQ, EMF models can be created in memory, leveraging the reflective side of EMF.

Firstly, we need to select which EMF package is going to provide containment for our custom classes. There are multiple options, and we are going to examine two of them. The first option is to try and extend one of the existing EMF packages, such as the one contained within org.eclipse.etf.ecore.context.persistence. This is not a very good idea, because that package uses a namespace which potentially differs from the namespace we would like to assign to our custom classes. Therefore, the second viable option, which is recommended in this essay, is to create a custom EMF package. This is done with the following code:

EPackage pkg = EcoreFactory.eINSTANCE.createEPackage();

After creating this package, we must assign its namespace URI. Assigning the name and the namespace prefix is optional, but in favor of keeping the code clean and comprehensible, we would like to set those optional attributes just as well.

pkg.setName( "BuddyListPackage" );
pkg.setNsPrefix( "org.socialphysics.buddylist" );

pkg.setNsURI( BuddyList.namespace );

The last, but certainly not the least part of the package initialization process is to create an EMF factory and assign that instance to our EMF package. Why do we need this custom factory? Why can't we get by using the default implementation? A discussion on this subject is later in the document.

pkg.setEFactoryInstance( new EFactoryImpl()
public EObject create( EClass cls )
if( cls.getName().equals( "BuddyList" ) )
return new BuddyList();
return super.create( cls );

} );