Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Is the XMI file generated by EMF standard and can be opened with other UML tools?(VP for UML cannot open the XMI file generated by EMF)
Is the XMI file generated by EMF standard and can be opened with other UML tools? [message #644223] Fri, 10 December 2010 02:22 Go to next message
Behrang Saeedzadeh is currently offline Behrang SaeedzadehFriend
Messages: 11
Registered: July 2009
Location: Melbourne, Australia
Junior Member
Hi,

I have created an XMI file using the following EMF program that contains a class named TimeEntry that has an EJB stereotype (or at least, that's my goal):

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

  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
  {
    // This line needs to be here, to force running of the UMLPackage
    //  static initializer, otherwise loading the meta-model will return null
    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" );


    // The model should be added to a resource before creating/applying stereotypes
    //  otherwise EMF won't persist the created stereotypes
    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 );

    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 );
    sampleClass.applyStereotype( ejbStereo );    

    resource.save( null );
  }

  private static void extendMetaclass( final Model umlMetamodel,
                                       final Profile profile,
                                       final String name,
                                       final Stereotype stereotype )
  {
    // The isRequired argument must be false, otherwise all classes will inherit the stereotype
    //  by default
    stereotype.createExtension( referenceMetaclass( umlMetamodel, profile, name ), false );
  }

  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;
  }
}


However I am unable to open the file using Visual Paradigm for UML 8 and it says it cannot parse the file.

The generated file is:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SampleProfile="http:///schemas/SampleProfile/_AHok4AQEEeClQKRD-V5YLA/0" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" xsi:schemaLocation="http:///schemas/SampleProfile/_AHok4AQEEeClQKRD-V5YLA/0 #_AHs2UQQEEeClQKRD-V5YLA">
  <uml:Model xmi:id="_AG9PcAQEEeClQKRD-V5YLA" name="Sample Model">
    <packagedElement xmi:type="uml:Package" xmi:id="_AHs2VgQEEeClQKRD-V5YLA" name="sample">
      <packagedElement xmi:type="uml:Class" xmi:id="_AHxHwAQEEeClQKRD-V5YLA" name="TimeEntry"/>
      <profileApplication xmi:id="_AHtdYAQEEeClQKRD-V5YLA" appliedProfile="_AG9PcQQEEeClQKRD-V5YLA">
        <eAnnotations xmi:id="_AHvSkAQEEeClQKRD-V5YLA" source="http://www.eclipse.org/uml2/2.0.0/UML" references="_AHs2UQQEEeClQKRD-V5YLA"/>
      </profileApplication>
    </packagedElement>
  </uml:Model>
  <uml:Profile xmi:id="_AG9PcQQEEeClQKRD-V5YLA" name="Sample Profile" metaclassReference="_AHO8QAQEEeClQKRD-V5YLA">
    <eAnnotations xmi:id="_AHs2UAQEEeClQKRD-V5YLA" source="http://www.eclipse.org/uml2/2.0.0/UML">
      <contents xmi:type="ecore:EPackage" xmi:id="_AHs2UQQEEeClQKRD-V5YLA" name="SampleProfile" nsURI="http:///schemas/SampleProfile/_AHok4AQEEeClQKRD-V5YLA/0" nsPrefix="SampleProfile">
        <eClassifiers xmi:type="ecore:EClass" xmi:id="_AHs2UgQEEeClQKRD-V5YLA" name="EJB">
          <eAnnotations xmi:id="_AHs2UwQEEeClQKRD-V5YLA" source="http://www.eclipse.org/uml2/2.0.0/UML" references="_AHASwAQEEeClQKRD-V5YLA"/>
          <eStructuralFeatures xmi:type="ecore:EReference" xmi:id="_AHs2VAQEEeClQKRD-V5YLA" name="base_Class" ordered="false" lowerBound="1">
            <eType xmi:type="ecore:EClass" href="http://www.eclipse.org/uml2/3.0.0/UML#//Class"/>
          </eStructuralFeatures>
        </eClassifiers>
      </contents>
    </eAnnotations>
    <elementImport xmi:id="_AHO8QAQEEeClQKRD-V5YLA">
      <importedElement xmi:type="uml:Class" href="pathmap://UML_METAMODELS/UML.metamodel.uml#Class"/>
    </elementImport>
    <packagedElement xmi:type="uml:Stereotype" xmi:id="_AHASwAQEEeClQKRD-V5YLA" name="EJB">
      <ownedAttribute xmi:id="_AHT0wAQEEeClQKRD-V5YLA" name="base_Class" association="_AHTNsAQEEeClQKRD-V5YLA">
        <type xmi:type="uml:Class" href="pathmap://UML_METAMODELS/UML.metamodel.uml#Class"/>
      </ownedAttribute>
    </packagedElement>
    <packagedElement xmi:type="uml:Extension" xmi:id="_AHTNsAQEEeClQKRD-V5YLA" name="Class_EJB" memberEnd="_AHTNsQQEEeClQKRD-V5YLA _AHT0wAQEEeClQKRD-V5YLA">
      <ownedEnd xmi:type="uml:ExtensionEnd" xmi:id="_AHTNsQQEEeClQKRD-V5YLA" name="extension_EJB" type="_AHASwAQEEeClQKRD-V5YLA" aggregation="composite" association="_AHTNsAQEEeClQKRD-V5YLA"/>
    </packagedElement>
  </uml:Profile>
  <SampleProfile:EJB xmi:id="_AHzkAAQEEeClQKRD-V5YLA" base_Class="_AHxHwAQEEeClQKRD-V5YLA"/>
</xmi:XMI>


Two questions:

1- Is my program correct?
2- Is the generated XMI conforming to the XMI standard or is it VP UML that has a bogus parser that cannot open the XMI file?

Thanks in advance,
Behrang

[Updated on: Fri, 10 December 2010 02:22]

Report message to a moderator

Re: Is the XMI file generated by EMF standard and can be opened with other UML tools? [message #644240 is a reply to message #644223] Fri, 10 December 2010 06:22 Go to previous messageGo to next message
Rafael Chaves is currently offline Rafael ChavesFriend
Messages: 161
Registered: July 2009
Senior Member
According to this old post, for better interoperabilty, it is recommended that you save your model using ".xmi" as extension, which will ensure any non standard elements are wrapped into <XMI:Extension> elements (and possibly be silently ignored by the other tool).

I also find it strange that you are adding the profile to the same resource as the model. Not sure that is causing the issues you are seeing, but profiles are usually stored in their own resource (and file) so they can be used by (applied to) multiple models.

Cheers,

Rafael
http://abstratt.com


Re: Is the XMI file generated by EMF standard and can be opened with other UML tools? [message #644269 is a reply to message #644240] Fri, 10 December 2010 10:25 Go to previous messageGo to next message
Vlad Varnica is currently offline Vlad VarnicaFriend
Messages: 546
Registered: July 2009
Location: Milton Keynes - UK
Senior Member
If you want to know if you xmi is correct you only need to try to open it with the UML2 editor coming from the EclipseUML2 plugin. If it is open then it is correct.

A good test is to create xmi from EMF and transform the same xmi to EMF, finally generate again xmi from the generated EMF. You will then notice that sometimes xmi transformation is not as perfect. The only real modeling technology which always works for UML is live synchronization from MOF to XMI !!
You can create advanced profiles in few minutes, create specifics icons and get a standard xmi. All your diagrams will be based on your xmi and not anymore on their internal model and therefore your diagrams are a view of your model and not the model itself. You model elements can be displayed in more than one diagram giving view of the full project and not only the package or the diagram level; Your xmi is live synchronized and if you add a parser then you can detect any change and create a workflow. Just amazing technology Shocked
Re: Is the XMI file generated by EMF standard and can be opened with other UML tools? [message #644331 is a reply to message #644269] Fri, 10 December 2010 15:04 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26237
Registered: July 2009
Senior Member
Vlad,

Most people stop flogging the horse after it dies but you're just like
the Energizer Bunny...


Vlad Varnica wrote:
> If you want to know if you xmi is correct you only need to try to open
> it with the UML2 editor coming from the EclipseUML2 plugin. If it is
> open then it is correct.
>
> A good test is to create xmi from EMF and transform the same xmi to
> EMF, finally generate again xmi from the generated EMF. You will then
> notice that sometimes xmi transformation is not as perfect. The only
> real modeling technology which always works for UML is live
> synchronization from MOF to XMI !!
> You can create advanced profiles in few minutes, create specifics
> icons and get a standard xmi. All your diagrams will be based on your
> xmi and not anymore on their internal model and therefore your
> diagrams are a view of your model and not the model itself. You model
> elements can be displayed in more than one diagram giving view of the
> full project and not only the package or the diagram level; Your xmi
> is live synchronized and if you add a parser then you can detect any
> change and create a workflow. Just amazing technology 8o
Re: Is the XMI file generated by EMF standard and can be opened with other UML tools? [message #644468 is a reply to message #644223] Sat, 11 December 2010 18:20 Go to previous message
Thomas Neustupny is currently offline Thomas NeustupnyFriend
Messages: 75
Registered: October 2009
Member
Hi Behrang,

your produced XMI can be perfectly read by ArgoUML, which is also based on EMF/UML2 (in "UML2 mode"which is under development but inluded in many last releases). (The only problem, purely ArgoUML related: it doesn't like that both a model and a profile package are in the same xmi, but that's another story.)

Hope that helps,
Thomas

[Updated on: Sat, 11 December 2010 18:24]

Report message to a moderator

Previous Topic:[solved] applied stereotype gone after save/load
Next Topic:Missing serialized package: uml.ecore
Goto Forum:
  


Current Time: Fri Nov 28 18:30:34 GMT 2014

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

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