Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Profiles doesn't work correctly in XMI-serialized UML
Profiles doesn't work correctly in XMI-serialized UML [message #1699324] Tue, 23 June 2015 12:06 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 200
Registered: August 2013
Senior Member
Hi

Here is a very simple program which loads the UML model:
https://github.com/AresEkb/XMILoadTest
After loading it tries to find stereotypes applied to the root package.
If it doesn't find any stereotypes, it tries to apply a new one.

Here is a code:
package xmiLoadTest;

import java.io.File;

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.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.XMI2UMLResource;
import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;

public class Main {

    public static void main(String[] args) {
        final String input = "model/Test.xmi";

        ResourceSet rs = new ResourceSetImpl();
        rs.setURIConverter(new CustomURIConverter());
        UMLResourcesUtil.init(rs);
        
        rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
                XMI2UMLResource.FILE_EXTENSION, XMI2UMLResource.Factory.INSTANCE);

        Resource resource = rs.getResource(createFileURI(input), true);
        System.out.println("Root objects count: " + resource.getContents().size());
        System.out.println("Root objects:");
        for (EObject obj : resource.getContents()) {
            System.out.println(obj);
        }
        Model uml = (Model)EcoreUtil.getObjectByType(
                resource.getContents(), UMLPackage.eINSTANCE.getModel());
        
        if (uml.getAppliedStereotypes().isEmpty()) {
            System.out.println("No stereotypes were found. Trying to apply a stereotype.");
            Stereotype stereotype = findStereotype(uml);
            if (stereotype != null) {
                uml.applyStereotype(stereotype);
            }
            else {
                System.out.println("Stereotype to apply not found!");
            }
        }
        else {
            for (Stereotype st : uml.getAppliedStereotypes()) {
                System.out.println("Applied stereotype: " + st);
            }
        }
    }
    
    private static Stereotype findStereotype(Model uml)
    {
        for (Profile profile : uml.getAllAppliedProfiles()) {
            if (profile.getName().equals("TestProfile")) {
                for (Stereotype st : profile.getOwnedStereotypes()) {
                    if (st.getName().equals("ModelStereotype")) {
                        return st;
                    }
                }
            }
        }
        return null;
    }

    private static URI createFileURI(String relativePath)
    {
        return URI.createFileURI(new File(relativePath).getAbsolutePath());
    }
}


It works as expected for "model/Test.uml". But it doesn't work for "model/Test.xmi". It doesn't find applied stereotypes and it unable to apply a new one:
Exception in thread "main" java.lang.IllegalArgumentException: stereotype "TestProfile::ModelStereotype" has no Ecore definition
	at org.eclipse.uml2.uml.internal.operations.ElementOperations.getDefinition(ElementOperations.java:1255)
	at org.eclipse.uml2.uml.internal.operations.ElementOperations.applyStereotype(ElementOperations.java:1479)
	at org.eclipse.uml2.uml.internal.impl.ElementImpl.applyStereotype(ElementImpl.java:511)
	at xmiLoadTest.Main.main(Main.java:44)


Both UML and XMI files are exported from Rational Softwrare Architect 9.1.

How to open UML XMI-files properly?
Re: Profiles doesn't work correctly in XMI-serialized UML [message #1699348 is a reply to message #1699324] Tue, 23 June 2015 13:58 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5967
Registered: July 2009
Senior Member
Hi

*.xmi is for OMG compliant UML; no Ecore/Eclipse-isms.

*.uml is for Eclipse UML2 behavior with the added link through from UML
to Ecore.

Don't expect Ecore related functionality to work with *.xmi.

Regards

Ed Willink


On 23/06/2015 13:06, Denis Nikiforov wrote:
> Hi
>
> Here is a very simple program which loads the UML model:
> https://github.com/AresEkb/XMILoadTest
> After loading it tries to find stereotypes applied to the root package.
> If it doesn't find any stereotypes, it tries to apply a new one.
>
> Here is a code:
> package xmiLoadTest;
>
> import java.io.File;
>
> 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.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.util.EcoreUtil;
> import org.eclipse.uml2.uml.Model;
> import org.eclipse.uml2.uml.Profile;
> import org.eclipse.uml2.uml.Stereotype;
> import org.eclipse.uml2.uml.UMLPackage;
> import org.eclipse.uml2.uml.resource.XMI2UMLResource;
> import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
>
> public class Main {
>
> public static void main(String[] args) {
> final String input = "model/Test.xmi";
>
> ResourceSet rs = new ResourceSetImpl();
> rs.setURIConverter(new CustomURIConverter());
> UMLResourcesUtil.init(rs);
> rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
> XMI2UMLResource.FILE_EXTENSION,
> XMI2UMLResource.Factory.INSTANCE);
>
> Resource resource = rs.getResource(createFileURI(input), true);
> System.out.println("Root objects count: " +
> resource.getContents().size());
> System.out.println("Root objects:");
> for (EObject obj : resource.getContents()) {
> System.out.println(obj);
> }
> Model uml = (Model)EcoreUtil.getObjectByType(
> resource.getContents(), UMLPackage.eINSTANCE.getModel());
> if (uml.getAppliedStereotypes().isEmpty()) {
> System.out.println("No stereotypes were found. Trying to
> apply a stereotype.");
> Stereotype stereotype = findStereotype(uml);
> if (stereotype != null) {
> uml.applyStereotype(stereotype);
> }
> else {
> System.out.println("Stereotype to apply not found!");
> }
> }
> else {
> for (Stereotype st : uml.getAppliedStereotypes()) {
> System.out.println("Applied stereotype: " + st);
> }
> }
> }
> private static Stereotype findStereotype(Model uml)
> {
> for (Profile profile : uml.getAllAppliedProfiles()) {
> if (profile.getName().equals("TestProfile")) {
> for (Stereotype st : profile.getOwnedStereotypes()) {
> if (st.getName().equals("ModelStereotype")) {
> return st;
> }
> }
> }
> }
> return null;
> }
>
> private static URI createFileURI(String relativePath)
> {
> return URI.createFileURI(new
> File(relativePath).getAbsolutePath());
> }
> }
>
> It works as expected for "model/Test.uml". But it doesn't work for
> "model/Test.xmi". It doesn't find applied stereotypes and it unable to
> apply a new one:
> Exception in thread "main" java.lang.IllegalArgumentException:
> stereotype "TestProfile::ModelStereotype" has no Ecore definition
> at
> org.eclipse.uml2.uml.internal.operations.ElementOperations.getDefinition(ElementOperations.java:1255)
> at
> org.eclipse.uml2.uml.internal.operations.ElementOperations.applyStereotype(ElementOperations.java:1479)
> at
> org.eclipse.uml2.uml.internal.impl.ElementImpl.applyStereotype(ElementImpl.java:511)
> at xmiLoadTest.Main.main(Main.java:44)
>
> Both UML and XMI files are exported from Rational Softwrare Architect
> 9.1.
>
> How to open UML XMI-files properly?
Re: Profiles doesn't work correctly in XMI-serialized UML [message #1699418 is a reply to message #1699348] Wed, 24 June 2015 08:10 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 200
Registered: August 2013
Senior Member
Hi

I thought that org.eclipse.uml2.uml.resource.XMI2UMLResource adapts *.xmi to Ecore. Actually everithing works fine except profiles...

What is a preffered way to work with *.xmi? Should I convert *.xmi to *.uml at first? Is the any example of such a conversion?
Re: Profiles doesn't work correctly in XMI-serialized UML [message #1699425 is a reply to message #1699418] Wed, 24 June 2015 09:24 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 5967
Registered: July 2009
Senior Member
On 24/06/2015 09:10, Denis Nikiforov wrote:
> Hi
>
> I thought that org.eclipse.uml2.uml.resource.XMI2UMLResource adapts
> *.xmi to Ecore. Actually everithing works fine except profiles...
Might be a bug; see if anyone comments. A problem is that profiles cause
both the UML model and UML metamodel to co-exist. For Eclipse UML2 the
actual metamodel is the Ecore-based
http://www.eclipse.org/uml2/5.0.0/UML, although the profile is specified
against the OMG namespace. There are some pragmatic adjustments to make
the two different namespaces appear the same.
>
> What is a preffered way to work with *.xmi? Should I convert *.xmi to
> *.uml at first? Is the any example of such a conversion?
I have always hand edited the XMI header as text, but I have usually
been working in advance of the version X support. You could well find
that you can just load *.xmi and SaveAs *.uml.

Regards

Ed Willink
Previous Topic:Importing IFML metamodel and profile
Next Topic:UML 2.5 DI support
Goto Forum:
  


Current Time: Sun Jul 15 19:07:51 GMT 2018

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

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

Back to the top