Skip to main content



      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 08:06 Go to next message
Eclipse UserFriend
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 09:58 Go to previous messageGo to next message
Eclipse UserFriend
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 04:10 Go to previous messageGo to next message
Eclipse UserFriend
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 05:24 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Profiles doesn't work correctly in XMI-serialized UML [message #1805987 is a reply to message #1699425] Tue, 30 April 2019 02:34 Go to previous messageGo to next message
Eclipse UserFriend
Maybe it will be useful for someone. You can just export your profile in UML format, change its extension to xmi and replace the original "xmi" profile. The model itself doesn't require any modifications. It works for me.
Re: Profiles doesn't work correctly in XMI-serialized UML [message #1805990 is a reply to message #1805987] Tue, 30 April 2019 03:30 Go to previous messageGo to next message
Eclipse UserFriend
If you can't re-export your profile, then you can just remove first surrounding xmi:Extension tags from profile.
For example in this profile https://github.com/AresEkb/XMILoadTest/blob/master/model/TestProfile.profile.xmi you can remove line 3:
<?xml version="1.0" encoding="UTF-8"?>
<uml:Profile xmi:version="2.4.1" xmlns:xmi="http://www.omg.org/spec/XMI/2.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:uml="http://schema.omg.org/spec/UML/2.2" xsi:schemaLocation="http://schema.omg.org/spec/UML/2.2 http://www.eclipse.org/uml2/3.0.0/UML" xmi:id="_vFWtYLWLEeO_TMNNr8yIMQ" name="TestProfile" metamodelReference="_vFYikLWLEeO_TMNNr8yIMQ">

  <xmi:Extension extender="http://www.eclipse.org/emf/2002/Ecore">

    <eAnnotations xmi:type="ecore:EAnnotation" xmi:id="_vFepMLWLEeO_TMNNr8yIMQ" source="http://www.eclipse.org/uml2/2.0.0/UML">

and line 64:
    </eAnnotations>

  </xmi:Extension>

  <packageImport xmi:type="uml:PackageImport" xmi:id="_vFX7gLWLEeO_TMNNr8yIMQ">


So, that eAnnotations tag will be a first child tag of uml:Profile

I guess that xmi-resource reader doesn't understand xmi:Extension tags
Re: Profiles doesn't work correctly in XMI-serialized UML [message #1805994 is a reply to message #1805990] Tue, 30 April 2019 04:15 Go to previous message
Eclipse UserFriend
Here is the transformation:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*:Extension">
        <xsl:apply-templates select="*"/>
    </xsl:template>

</xsl:stylesheet>
Previous Topic:How to load and use MARTE profile
Next Topic:What Industry Wants from Academia in Software Modeling
Goto Forum:
  


Current Time: Tue Jul 08 19:59:28 EDT 2025

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

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

Back to the top