Difference between revisions of "EMF/Deriving EMF Models"

From Eclipsepedia

< EMF
Jump to: navigation, search
 
Line 6: Line 6:
  
 
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.
 
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.
 +
 +
<blockquote>
 +
<code>pkg.setName( "BuddyListPackage" );</code><br/>
 +
<code>pkg.setNsPrefix( "org.socialphysics.buddylist" );</code><br/>
 +
<code>pkg.setNsURI( BuddyList.namespace );</code></blockquote>
 +
 +
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.
 +
 +
<blockquote>
 +
<code>pkg.setEFactoryInstance( new EFactoryImpl()</code><br/>
 +
<code> {</code><br/>
 +
<code> public EObject create( EClass cls )</code><br/>
 +
<code> {</code><br/>
 +
<code> if( cls.getName().equals( "BuddyList" ) )</code><br/>
 +
<code> return new BuddyList();</code><br/>
 +
<code> return super.create( cls );</code><br/>
 +
<code> }</code><br/>
 +
<code> } );</code></blockquote>

Revision as of 12:20, 4 September 2006

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 );
}
} );