Home » Modeling » UML2 » Getting null for the meta model
Getting null for the meta model [message #643198] |
Sun, 05 December 2010 19:10  |
Eclipse User |
|
|
|
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: Sun, 05 December 2010 19:11] by Moderator
|
|
| |
Re: Getting null for the meta model [message #644027 is a reply to message #643232] |
Thu, 09 December 2010 02:18   |
Eclipse User |
|
|
|
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 02:19] by Moderator
|
|
|
Re: Getting null for the meta model [message #644113 is a reply to message #644027] |
Thu, 09 December 2010 10:16  |
Eclipse User |
|
|
|
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
|
|
|
Goto Forum:
Current Time: Thu May 15 02:58:38 EDT 2025
Powered by FUDForum. Page generated in 0.04477 seconds
|