Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Getting null for the meta model
Getting null for the meta model [message #643198] Mon, 06 December 2010 00:10 Go to next message
Behrang Saeedzadeh is currently offline Behrang SaeedzadehFriend
Messages: 11
Registered: July 2009
Location: Melbourne, Australia
Junior Member
I want to create and apply sterotypes to classes. To do so, I need to have a reference to the UML 2 meta model, but the following code returns null for the meta model:

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

  protected static Package load( URI uri )
    throws Exception
  {
    System.out.println( "uri = " + uri );
    Resource resource = RESOURCE_SET.getResource( uri, true );
    return (Package) EcoreUtil.getObjectByType( resource.getContents(), Literals.PACKAGE );
  }
}


Any ideas what's wrong with the above code?

Thanks in advance,
Behrang

[Updated on: Mon, 06 December 2010 00:11]

Report message to a moderator

Re: Getting null for the meta model [message #643232 is a reply to message #643198] Mon, 06 December 2010 08:19 Go to previous messageGo to next message
Rafael Chaves is currently offline Rafael ChavesFriend
Messages: 161
Registered: July 2009
Senior Member
Your code is failing because the UMLPackage is not registered in the global package registry, so you end up with a resource that uses the generic ECore model objects instead of the UML2 model objects.

In the UML FAQ, it is suggested you should explicitly register the UML package on the resource set.

That should not be necessary though. Just by referencing UMLPackage, it gets registered automatically in the global package registry. IOW, it should work fine if you inserted this line in your code anywhere before you starting loading resources:

UMLPackage.eINSTANCE.getName();

which is apparently useless but gets the static initialization going.

Cheers,

Rafael
http://abstratt.com/blog
Re: Getting null for the meta model [message #644027 is a reply to message #643232] Thu, 09 December 2010 07:18 Go to previous messageGo to next message
Behrang Saeedzadeh is currently offline Behrang SaeedzadehFriend
Messages: 11
Registered: July 2009
Location: Melbourne, Australia
Junior Member
Hi Rafael,

Thanks for the reply. This solved the problem with the null meta model. However, now whenever I want to apply the stereo type to a class I get an exception, because EMF thinks that it's already applied to the class I guess (it is contained in the class's stereotype applications). Here's a sample code that can reproduce the problem:

import java.io.File;
import java.net.URL;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
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();



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

  protected static Package load( URI uri )
    throws Exception
  {
    System.out.println( "uri = " + uri );
    Resource resource = RESOURCE_SET.getResource( uri, true );
    return (Package) EcoreUtil.getObjectByType( resource.getContents(), Literals.PACKAGE );
  }

  public static void main( String[] args )
    throws Exception
  {
    UMLPackage.eINSTANCE.getName();
    
    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 );

    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, UMLPackage.Literals.CLASS.getName(), ejbStereo );

    sampleProfile.define();

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

    final Class sampleClass = samplePackage.createOwnedClass( "TimeEntry", false );
    for ( final EObject eo : sampleClass.getStereotypeApplications())
    {
      System.out.println( "eo = " + eo );
    }

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

  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 );
    if ( !profile.getReferencedMetaclasses().contains( metaclass ) )
    {
      profile.createMetaclassReference( metaclass );
    }
    return metaclass;
  }
}


Any ideas what am I probably doing wrong?

Thanks in advance,
Behrang

[Updated on: Thu, 09 December 2010 07:19]

Report message to a moderator

Re: Getting null for the meta model [message #644113 is a reply to message #644027] Thu, 09 December 2010 15:16 Go to previous message
Rafael Chaves is currently offline Rafael ChavesFriend
Messages: 161
Registered: July 2009
Senior Member
Your stereotypes are being created as required (last boolean argument):

stereotype.createExtension( referenceMetaclass( umlMetamodel, profile, name ), true );


That means they are automatically applied to any instances of the target metaclass (in your case: uml:Class). That is why it fails when manually applying them.

If that is not what you want, specify isRequired as false.

Cheers,

Rafael
http://alphasimple.com

P.S.: BTW, it is better to start a new thread for a new question as it helps with searching
Previous Topic:Drawing Associations with Eclipse UML2
Next Topic:[solved] applied stereotype gone after save/load
Goto Forum:
  


Current Time: Fri Apr 26 05:09:58 GMT 2024

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

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

Back to the top