如何在独立应用程序中使用EMF创建和应用UML2立体声类型?

时间:2010-12-03 06:25:58

标签: eclipse uml eclipse-emf stereotype

我在下面编写了示例应用程序以创建EJB构造型并将其应用于TimeEntry类:

import java.io.File;
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.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();

  public static void main( String[] args ) throws Exception
  {

    Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
      UMLResource.FILE_EXTENSION,
      UMLResource.Factory.INSTANCE
    );

    final Model umlMetamodel = (Model) loadPackage( UMLResource.UML_METAMODEL_URI );

    final Model sampleModel = UMLFactory.eINSTANCE.createModel();
    sampleModel.setName( "Sample Model" );

    final Profile sampleProfile = UMLFactory.eINSTANCE.createProfile();
    sampleProfile.setName( "Sample Profile" );

    final Stereotype ejbStereo = sampleProfile.createOwnedStereotype( "EJB" );
    extendMetaclass( umlMetamodel, sampleProfile, "Class", ejbStereo );

    sampleProfile.define();

    final Package samplePackage = sampleModel.createNestedPackage( "sample" );
    samplePackage.applyProfile( sampleProfile );

    final Class sampleClass = samplePackage.createOwnedClass( "TimeEntry", false );
    sampleClass.applyStereotype( ejbStereo );

    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 );
    resource.save( null );
  }

  private static Package loadPackage( final String uri )
  {
    System.out.println( "uri = " + uri );
    final Resource resource = RESOURCE_SET.getResource( URI.createURI( uri ), true );
    EcoreUtil.resolveAll( resource );
    return (org.eclipse.uml2.uml.Package) EcoreUtil.getObjectByType( resource.getContents(), Literals.PACKAGE );
  }

  private static void extendMetaclass( final Model umlMetamodel,
                                       final Profile profile,
                                       final String name,
                                       final Stereotype stereotype )
  {
    stereotype.createExtension( referenceMetaclass( umlMetamodel, profile, name ), true );
  }

  private static org.eclipse.uml2.uml.Class referenceMetaclass( final Model umlMetamodel,
                                                                final Profile profile,
                                                                final String name )
  {
    final Class metaclass = (Class) umlMetamodel.getOwnedType( name );
    profile.createMetaclassReference( metaclass );
    return metaclass;
  }
}

然而,运行应用程序时,我在控制台中收到此错误消息:

uri = pathmap://UML_METAMODELS/UML.metamodel.uml
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: java.net.MalformedURLException: unknown protocol: pathmap
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:315)
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)

任何想法我的代码可能有什么问题以及如何解决这个问题?该应用程序是一个在Eclipse外部运行的命令行应用程序。

3 个答案:

答案 0 :(得分:1)

刻板印象与EMF不是实时同步的。我的意思是你需要在项目中加载你的个人资料,以便能够应用一个刻板印象。这不能通过代码完成,而是使用其他插件!!

我还注意到第一次转换后EMF会丢失构造型,因此不要花太多时间,因为这段EMF代码不稳定: - )

答案 1 :(得分:1)

路径图通常通过org.eclipse.emf.ecore.uri_mapping扩展点在插件中提供。由于您没有运行插件,因此您需要手动执行org.eclipse.emf.ecore所采取的操作。

我一直在插件中挖掘但是却找不到这个扩展点的消费者。祝你好运!

答案 2 :(得分:1)

我在EMF / UML2论坛上找到了这个问题的答案:Getting null for the meta model