How to create and apply a UML2 stereo-type using EMF in a standalone app?

I have written the sample application below to create an EJB stereotype an apply it to the TimeEntry class:

import java.io.File;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Factory;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.*;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage.Literals;
import org.eclipse.uml2.uml.resource.UMLResource;

public class SampleProfile
{

  private static final ResourceSet RESOURCE_SET = new ResourceSetImpl();

  public static void main( String[] args ) throws Exception
  {

    Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
      UMLResource.FILE_EXTENSION,
      UMLResource.Factory.INSTANCE
    );

    final Model umlMetamodel = (Model) loadPackage( UMLResource.UML_METAMODEL_URI );

    final Model sampleModel = UMLFactory.eINSTANCE.createModel();
    sampleModel.setName( "Sample Model" );

    final Profile sampleProfile = UMLFactory.eINSTANCE.createProfile();
    sampleProfile.setName( "Sample Profile" );

    final Stereotype ejbStereo = sampleProfile.createOwnedStereotype( "EJB" );
    extendMetaclass( umlMetamodel, sampleProfile, "Class", ejbStereo );

    sampleProfile.define();

    final Package samplePackage = sampleModel.createNestedPackage( "sample" );
    samplePackage.applyProfile( sampleProfile );

    final Class sampleClass = samplePackage.createOwnedClass( "TimeEntry", false );
    sampleClass.applyStereotype( ejbStereo );

    final File outputFile = new File( "sample_model.uml" );
    final URI outputUri = URI.createFileURI( outputFile.getAbsolutePath() );
    final Resource resource = RESOURCE_SET.createResource( outputUri );
    resource.getContents().add( sampleModel );
    resource.getContents().add( sampleProfile );
    resource.save( null );
  }

  private static Package loadPackage( final String uri )
  {
    System.out.println( "uri = " + uri );
    final Resource resource = RESOURCE_SET.getResource( URI.createURI( uri ), true );
    EcoreUtil.resolveAll( resource );
    return (org.eclipse.uml2.uml.Package) EcoreUtil.getObjectByType( resource.getContents(), Literals.PACKAGE );
  }

  private static void extendMetaclass( final Model umlMetamodel,
                                       final Profile profile,
                                       final String name,
                                       final Stereotype stereotype )
  {
    stereotype.createExtension( referenceMetaclass( umlMetamodel, profile, name ), true );
  }

  private static org.eclipse.uml2.uml.Class referenceMetaclass( final Model umlMetamodel,
                                                                final Profile profile,
                                                                final String name )
  {
    final Class metaclass = (Class) umlMetamodel.getOwnedType( name );
    profile.createMetaclassReference( metaclass );
    return metaclass;
  }
}

However running the application, I get this error message in the console:

uri = pathmap://UML_METAMODELS/UML.metamodel.uml
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: java.net.MalformedURLException: unknown protocol: pathmap
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:315)
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)

Any ideas what's probably wrong with my code and how this problem can be solved? The application is a command-line app that runs outside Eclipse.

Answers


Stereotypes are not live synchronized with EMF. I mean that you need to load your profile inside your project in order to be able to apply a stereotype. This could not be done by just code but by using other plugins !!

I also notice that stereotypes are lost by EMF after first transformation, therefore don't spend too much time because this piece of EMF code is not stable :-)


The pathmaps are usually given in a plugin through the org.eclipse.emf.ecore.uri_mapping extension point. Since you're not running plugins, you need to manually take the actions that org.eclipse.emf.ecore takes.

I've been digging around in the plugin but couldn't find the consumer of this extension point in a hurry. Good luck!


I found the answer to this question on the EMF/UML2 forum: Getting null for the meta model.


Need Your Help

Handling a List <Integer> to a thread

java arrays integer

Basically I have an integer array which i want to hand over to a thread, but i can't get the syntax right.

Twiml - Auto press (digit) and hangup

php key twilio twiml

I want a simple script that will allow me to:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.