Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to create an apply a UML2 stereo-type using EMF in a standalone app?
How to create an apply a UML2 stereo-type using EMF in a standalone app? [message #643194] Sun, 05 December 2010 23:21 Go to next message
Behrang Saeedzadeh is currently offline Behrang SaeedzadehFriend
Messages: 11
Registered: July 2009
Location: Melbourne, Australia
Junior Member
I have written the sample code 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.
Re: How to create an apply a UML2 stereo-type using EMF in a standalone app? [message #643196 is a reply to message #643194] Sun, 05 December 2010 23:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Behrang,

This is best asked on the UML2 newsgroup. I believe they have
documentation for all the pathmap URI mappings you'll need to set...

Cheers,
Ed


Behrang Saeedzadeh wrote:
> I have written the sample code 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$1Diagnos ticWrappedException:
> java.net.MalformedURLException: unknown protocol: pathmap
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:315)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:274)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(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.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to create an apply a UML2 stereo-type using EMF in a standalone app? [message #643197 is a reply to message #643196] Mon, 06 December 2010 00:07 Go to previous messageGo to next message
Behrang Saeedzadeh is currently offline Behrang SaeedzadehFriend
Messages: 11
Registered: July 2009
Location: Melbourne, Australia
Junior Member
Where's the UML2 newsgroup? Couldn't find it in the list of newsgroups.

Also, I have simplified the code to:

import java.io.File;
import java.net.URL;
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.URIConverter;
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 String umlProfile = "metamodels/UML.metamodel.uml";
    final URL url = SampleProfile.class.getClassLoader().getResource( umlProfile );
    String baseUrl = url.toString();    
    baseUrl = baseUrl.substring( 0, baseUrl.length() - umlProfile.length() );
    registerPathmaps( URI.createURI( baseUrl ));

    Model umlMetamodel = (Model) load( URI.createURI( UMLResource.UML_METAMODEL_URI ) );
    System.out.println( "umlMetamodel = " + umlMetamodel );
  }

  protected static void registerPathmaps( URI baseUri )
  {
    System.out.println( "baseUri = " + baseUri );
    URIConverter.URI_MAP.put(URI.createURI( UMLResource.LIBRARIES_PATHMAP ), baseUri.appendSegment( "libraries" ).appendSegment( "" ));
    URIConverter.URI_MAP.put(URI.createURI( UMLResource.METAMODELS_PATHMAP ), baseUri.appendSegment( "metamodels" ).appendSegment( "" ));
    URIConverter.URI_MAP.put(URI.createURI( UMLResource.PROFILES_PATHMAP ), baseUri.appendSegment( "profiles" ).appendSegment( "" ));
  }
}


Now the problem is that it returns null for the UML meta model.

Cheers,
Behrang
Re: How to create an apply a UML2 stereo-type using EMF in a standalone app? [message #643199 is a reply to message #643197] Mon, 06 December 2010 00:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
I guess you found it; I see your post there.


Behrang Saeedzadeh wrote:
> Where's the UML2 newsgroup? Couldn't find it in the list of newsgroups.
>
> Also, I have simplified the code to:
>
> import java.io.File;
> import java.net.URL;
> 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.URIConverter;
> 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 String umlProfile = "metamodels/UML.metamodel.uml";
> final URL url = SampleProfile.class.getClassLoader().getResource(
> umlProfile );
> String baseUrl = url.toString(); baseUrl = baseUrl.substring(
> 0, baseUrl.length() - umlProfile.length() );
> registerPathmaps( URI.createURI( baseUrl ));
>
> Model umlMetamodel = (Model) load( URI.createURI(
> UMLResource.UML_METAMODEL_URI ) );
> System.out.println( "umlMetamodel = " + umlMetamodel );
> }
>
> protected static void registerPathmaps( URI baseUri )
> {
> System.out.println( "baseUri = " + baseUri );
> URIConverter.URI_MAP.put(URI.createURI(
> UMLResource.LIBRARIES_PATHMAP ), baseUri.appendSegment( "libraries"
> ).appendSegment( "" ));
> URIConverter.URI_MAP.put(URI.createURI(
> UMLResource.METAMODELS_PATHMAP ), baseUri.appendSegment( "metamodels"
> ).appendSegment( "" ));
> URIConverter.URI_MAP.put(URI.createURI(
> UMLResource.PROFILES_PATHMAP ), baseUri.appendSegment( "profiles"
> ).appendSegment( "" ));
> }
> }
>
> Now the problem is that it returns null for the UML meta model.
>
> Cheers,
> Behrang


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to create an apply a UML2 stereo-type using EMF in a standalone app? [message #643200 is a reply to message #643199] Mon, 06 December 2010 00:24 Go to previous message
Behrang Saeedzadeh is currently offline Behrang SaeedzadehFriend
Messages: 11
Registered: July 2009
Location: Melbourne, Australia
Junior Member
Yes, I just found it. It was in the parent listing...

Thanks anyway.
Previous Topic:How to create a dynamic instances of two dependant Metamodels
Next Topic:Default Values of simpleContent
Goto Forum:
  


Current Time: Thu Apr 25 08:46:10 GMT 2024

Powered by FUDForum. Page generated in 0.03087 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top