Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » How to get a metaclass for Stereotype.createExtension()
How to get a metaclass for Stereotype.createExtension() [message #1006304] Wed, 30 January 2013 18:30 Go to next message
Dominique Marcadet is currently offline Dominique MarcadetFriend
Messages: 18
Registered: January 2013
Junior Member
Hello everybody,

I am trying to create a profile with a Java program.
I can create a Stereotype, but I am unable to create the extension to a UML metaclass (I got a IllegalArgumentException).

The debug shows that StereotypeOperations.createExtension() throws this exception because metaclass.isMetaclass() is false.

Here is the code, it shows that indeed my metaclass isn't one (it comes from my searches on Internet) :

public class ShowBug {

	private static final ResourceSet RESOURCE_SET = new ResourceSetImpl();

	public static void main(String[] args) throws Exception {
	    String JAR_FILE_ECLIPSE_UML2_UML_RESOURCES = "jar:file:/Users/marcadet/.eclipse/org.eclipse.platform_4.2.0_1211646801/plugins/org.eclipse.uml2.uml.resources_4.0.1.v20120913-1441.jar!/";

	    RESOURCE_SET.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
	    RESOURCE_SET.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
                       UMLResource.FILE_EXTENSION,UMLResource.Factory.INSTANCE);
	    Map<URI, URI> _resourceSetURIMap = RESOURCE_SET.getURIConverter().getURIMap();
	    URI uri = URI.createURI(JAR_FILE_ECLIPSE_UML2_UML_RESOURCES);
	    _resourceSetURIMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));

		String name = "Class";
		Model umlMetamodel2 =  (Model) load(URI.createURI(UMLResource.UML_METAMODEL_URI));
		final Class metaclass = (Class) umlMetamodel2.getOwnedType(name);
		System.out.println(name + " : " + metaclass.isMetaclass());
	}

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

}


What is wrong ?

Regards,
Dominique
Re: How to get a metaclass for Stereotype.createExtension() [message #1006346 is a reply to message #1006304] Wed, 30 January 2013 22:48 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Dominique,

The problem will be that the isMetaclass() API doesn't find the
<<metaclass>> stereotype applied to the class from the UML metamodel.
It seems that the application of the standard profile providing the
<<metaclass>> stereotype didn't resolve the reference to that profile.

Make sure that your resource set can properly resolve the location of
the standard profile. This will need a URI mapping for the
pathmap://UML2_PROFILES/ prefix, similar to what you did for the
pathmap://UML2_METAMODELS/ prefix.

HTH,

Christian


On 2013-01-30 20:37:53 +0000, Dominique Marcadet said:

> Hello everybody,
>
> I am trying to create a profile with a Java program.
> I can create a Stereotype, but I am unable to create the extension to a
> UML metaclass (I got a IllegalArgumentException).
>
> The debug shows that StereotypeOperations.createExtension() throws this
> exception because metaclass.isMetaclass() is false.
>
> Here is the code, it shows that indeed my metaclass isn't one (it comes
> from my searches on Internet) :
>
>
> public class ShowBug {
>
> private static final ResourceSet RESOURCE_SET = new ResourceSetImpl();
>
> public static void main(String[] args) throws Exception {
> String JAR_FILE_ECLIPSE_UML2_UML_RESOURCES =
> "jar:file:/Users/marcadet/.eclipse/org.eclipse.platform_4.2.0_1211646801/plugins/org.eclipse.uml2.uml.resources_4.0.1.v20120913-1441.jar!/";
>
>
> RESOURCE_SET.getPackageRegistry().put(UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE);
> RESOURCE_SET.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
>
> UMLResource.FILE_EXTENSION,UMLResource.Factory.INSTANCE);
> Map<URI, URI> _resourceSetURIMap =
> RESOURCE_SET.getURIConverter().getURIMap();
> URI uri = URI.createURI(JAR_FILE_ECLIPSE_UML2_UML_RESOURCES);
>
> _resourceSetURIMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP),
> uri.appendSegment("metamodels").appendSegment(""));
>
> String name = "Class";
> Model umlMetamodel2 = (Model)
> load(URI.createURI(UMLResource.UML_METAMODEL_URI));
> final Class metaclass = (Class) umlMetamodel2.getOwnedType(name);
> System.out.println(name + " : " + metaclass.isMetaclass());
> }
>
> protected static Package load(URI uri) throws Exception {
> Resource resource = RESOURCE_SET.getResource(uri, true);
> return (Package) EcoreUtil.getObjectByType(resource.getContents(),
> Literals.PACKAGE);
> }
>
> }
>
>
> What is wrong ?
>
> Regards,
> Dominique
Re: How to get a metaclass for Stereotype.createExtension() [message #1006384 is a reply to message #1006346] Thu, 31 January 2013 08:23 Go to previous messageGo to next message
Dominique Marcadet is currently offline Dominique MarcadetFriend
Messages: 18
Registered: January 2013
Junior Member
Hi, Christian,

Thanks for your advice.

However, I've tried before, without success, with these 3 URI mappings instead of one :
    _resourceSetURIMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
    _resourceSetURIMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
    _resourceSetURIMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));


It's in the process of getting the code smaller while still showing the bug that I removed 2 of them.

I stll get a false result on isMetaclass() with these 3 URI mappings.

Regards,
Dominique

Quote:
Hi, Dominique,

The problem will be that the isMetaclass() API doesn't find the
<<metaclass>> stereotype applied to the class from the UML metamodel.
It seems that the application of the standard profile providing the
<<metaclass>> stereotype didn't resolve the reference to that profile.

Make sure that your resource set can properly resolve the location of
the standard profile. This will need a URI mapping for the
pathmap://UML2_PROFILES/ prefix, similar to what you did for the
pathmap://UML2_METAMODELS/ prefix.

HTH,

Christian

Re: How to get a metaclass for Stereotype.createExtension() [message #1006488 is a reply to message #1006384] Thu, 31 January 2013 13:32 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Dominique,

The <<metaclass>> stereotype is defined in the L2 profile. So, can you
check whether the pathmap://UML_PROFILES/StandardL2.profile.uml
resource is loaded in your ResourceSet? I think it mustn't be, still.

If that's the case, then you can debug the ResourceSetImpl's attempt to
load this resource to see why it isn't working. It's strange that you
can load the UML metamodel but not this profile; they are in the same
JAR and the mechanics are so similar.

HTH,

Christian


On 2013-01-31 08:23:49 +0000, Dominique Marcadet said:

> Hi, Christian,
>
> Thanks for your advice.
>
> However, I've tried before, without success, with these 3 URI mappings
> instead of one :
>
>
> _resourceSetURIMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP),
> uri.appendSegment("libraries").appendSegment(""));
>
> _resourceSetURIMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP),
> uri.appendSegment("metamodels").appendSegment(""));
> _resourceSetURIMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP),
> uri.appendSegment("profiles").appendSegment(""));
>
>
> It's in the process of getting the code smaller while still showing the
> bug that I removed 2 of them.
>
> I stll get a false result on isMetaclass() with these 3 URI mappings.
>
> Regards,
> Dominique
>
> Quote:
>> Hi, Dominique,
>>
>> The problem will be that the isMetaclass() API doesn't find the
>> <<metaclass>> stereotype applied to the class from the UML metamodel.
>> It seems that the application of the standard profile providing the
>> <<metaclass>> stereotype didn't resolve the reference to that profile.
>>
>> Make sure that your resource set can properly resolve the location of
>> the standard profile. This will need a URI mapping for the
>> pathmap://UML2_PROFILES/ prefix, similar to what you did for the
>> pathmap://UML2_METAMODELS/ prefix.
>>
>> HTH,
>>
>> Christian
Re: How to get a metaclass for Stereotype.createExtension() [message #1006552 is a reply to message #1006488] Thu, 31 January 2013 16:43 Go to previous messageGo to next message
Dominique Marcadet is currently offline Dominique MarcadetFriend
Messages: 18
Registered: January 2013
Junior Member
Hi, Christian,

I've looked around in the code in the uml plugins, and found that the method UMLResourcesUtil.init() seems to be concerned with URI mappings of uml stuff. So I decided to call it explicitly, and that's it !
I don't know if it is the correct way, but "Class" is now a metaclass !

Here is the complete code for anyone having this problem :
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
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.Class;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.UMLPackage.Literals;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;

public class GetUMLMetaclass {
	public static void main( String[] args ) throws Exception {
		ResourceSet resource_set = UMLResourcesUtil.init( new ResourceSetImpl() );
		
	    String JAR_FILE_ECLIPSE_UML2_UML_RESOURCES = "jar:file:/Users/marcadet/.eclipse/org.eclipse.platform_4.2.0_1211646801/plugins/org.eclipse.uml2.uml.resources_4.0.1.v20120913-1441.jar!/";

	    resource_set.getPackageRegistry().put( UMLPackage.eNS_URI, UMLPackage.eINSTANCE );
	    resource_set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
                       UMLResource.FILE_EXTENSION,UMLResource.Factory.INSTANCE );
	    Map<URI, URI> _resourceSetURIMap = resource_set.getURIConverter().getURIMap();
	    URI uri = URI.createURI( JAR_FILE_ECLIPSE_UML2_UML_RESOURCES );
	    _resourceSetURIMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
	    _resourceSetURIMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
	    _resourceSetURIMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));

		Resource resource = resource_set.getResource( URI.createURI( UMLResource.UML_METAMODEL_URI ), true );
		Model umlMetamodel =  (Model) EcoreUtil.getObjectByType(resource.getContents(), Literals.PACKAGE);;

		final Class metaclass = (Class) umlMetamodel.getOwnedType( "Class" );
		System.out.println( metaclass.isMetaclass() );
	}
}



I've still have to find how to be independent of the location (and if possible of the version) of the org.eclipse.uml2.uml.resources plugin.

Dominique

Christian W. Damus wrote on Thu, 31 January 2013 08:32
Hi, Dominique,

The <<metaclass>> stereotype is defined in the L2 profile. So, can you
check whether the pathmap://UML_PROFILES/StandardL2.profile.uml
resource is loaded in your ResourceSet? I think it mustn't be, still.

If that's the case, then you can debug the ResourceSetImpl's attempt to
load this resource to see why it isn't working. It's strange that you
can load the UML metamodel but not this profile; they are in the same
JAR and the mechanics are so similar.

HTH,

Christian
Re: How to get a metaclass for Stereotype.createExtension() [message #1006564 is a reply to message #1006552] Thu, 31 January 2013 18:32 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Dominique,

I'm glad you got it working! Thanks for the update.

Yes, UMLResourceUtil is intended for this purpose, to help configure
the UML APIs for stand-alone (Eclipse-free) execution.

cW


On 2013-01-31 16:43:53 +0000, Dominique Marcadet said:

> Hi, Christian,
>
> I've looked around in the code in the uml plugins, and found that the
> method UMLResourcesUtil.init() seems to be concerned with URI mappings
> of uml stuff. So I decided to call it explicitly, and that's it !
> I don't know if it is the correct way, but "Class" is now a metaclass !
>
> Here is the complete code for anyone having this problem :
>
> import java.util.Map;
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> 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.Class;
> import org.eclipse.uml2.uml.Model;
> import org.eclipse.uml2.uml.UMLPackage;
> import org.eclipse.uml2.uml.UMLPackage.Literals;
> import org.eclipse.uml2.uml.resource.UMLResource;
> import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
>
> public class GetUMLMetaclass {
> public static void main( String[] args ) throws Exception {
> ResourceSet resource_set = UMLResourcesUtil.init( new ResourceSetImpl() );
>
> String JAR_FILE_ECLIPSE_UML2_UML_RESOURCES =
> "jar:file:/Users/marcadet/.eclipse/org.eclipse.platform_4.2.0_1211646801/plugins/org.eclipse.uml2.uml.resources_4.0.1.v20120913-1441.jar!/";
>
>
> resource_set.getPackageRegistry().put( UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE );
> resource_set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
>
> UMLResource.FILE_EXTENSION,UMLResource.Factory.INSTANCE );
> Map<URI, URI> _resourceSetURIMap =
> resource_set.getURIConverter().getURIMap();
> URI uri = URI.createURI( JAR_FILE_ECLIPSE_UML2_UML_RESOURCES );
>
> _resourceSetURIMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP),
> uri.appendSegment("libraries").appendSegment(""));
>
> _resourceSetURIMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP),
> uri.appendSegment("metamodels").appendSegment(""));
>
> _resourceSetURIMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP),
> uri.appendSegment("profiles").appendSegment(""));
>
> Resource resource = resource_set.getResource( URI.createURI(
> UMLResource.UML_METAMODEL_URI ), true );
> Model umlMetamodel = (Model)
> EcoreUtil.getObjectByType(resource.getContents(), Literals.PACKAGE);;
>
> final Class metaclass = (Class) umlMetamodel.getOwnedType( "Class" );
> System.out.println( metaclass.isMetaclass() );
> }
> }
>
>
>
> I've still have to find how to be independent of the location (and if
> possible of the version) of the org.eclipse.uml2.uml.resources plugin.
>
> Dominique
>
> Christian W. Damus wrote on Thu, 31 January 2013 08:32
>> Hi, Dominique,
>>
>> The <<metaclass>> stereotype is defined in the L2 profile. So, can you
>> check whether the pathmap://UML_PROFILES/StandardL2.profile.uml
>> resource is loaded in your ResourceSet? I think it mustn't be, still.
>>
>> If that's the case, then you can debug the ResourceSetImpl's attempt to
>> load this resource to see why it isn't working. It's strange that you
>> can load the UML metamodel but not this profile; they are in the same
>> JAR and the mechanics are so similar.
>>
>> HTH,
>>
>> Christian
Re: How to get a metaclass for Stereotype.createExtension() [message #1006569 is a reply to message #1006564] Thu, 31 January 2013 19:47 Go to previous messageGo to next message
Dominique Marcadet is currently offline Dominique MarcadetFriend
Messages: 18
Registered: January 2013
Junior Member
Hi, Christian,

Christian W. Damus wrote on Thu, 31 January 2013 13:32
Hi, Dominique,
Yes, UMLResourceUtil is intended for this purpose, to help configure
the UML APIs for stand-alone (Eclipse-free) execution.


OK, except that I'm not in a stand-alone execution environment.
My test code is a simple java project running inside Eclipse (and the final code will be in an eclipse plugin).

So, the UML APIs should be correctly configured when running inside Eclipse without calling UMLResourceUtil.init() ?

Dominique

Re: How to get a metaclass for Stereotype.createExtension() [message #1006576 is a reply to message #1006569] Thu, 31 January 2013 20:37 Go to previous message
Kenn Hussey is currently offline Kenn HusseyFriend
Messages: 1620
Registered: July 2009
Senior Member
Even within Eclipse, you need to use UMLUtil#init(ResourceSet) if you
want to work with legacy resources. The method in UMLResourcesUtil will
work too; it just attempts to do a number of registrations that are
already done if you're running in Eclipse.

Kenn

On 13-01-31 2:47 PM, Dominique Marcadet wrote:
> Hi, Christian,
>
> Christian W. Damus wrote on Thu, 31 January 2013 13:32
>> Hi, Dominique,
>> Yes, UMLResourceUtil is intended for this purpose, to help configure
>> the UML APIs for stand-alone (Eclipse-free) execution.
>
>
> OK, except that I'm not in a stand-alone execution environment.
> My test code is a simple java project running inside Eclipse (and the
> final code will be in an eclipse plugin).
>
> So, the UML APIs should be correctly configured when running inside
> Eclipse without calling UMLResourceUtil.init() ?
>
> Dominique
>
>
Previous Topic:Set Enumeration Type in Property of Stereotype
Next Topic:Building an GMF-based editor for an definde UML-profile
Goto Forum:
  


Current Time: Thu Apr 25 04:33:08 GMT 2024

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

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

Back to the top