Is the XMI file generated by EMF standard and can be opened with other UML tools? [message #644223] |
Fri, 10 December 2010 02:22 |
Behrang Saeedzadeh 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
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01982 seconds