2016-10-23 5 views
1

Javaコードを使用してEMFモデルを生成したい。たとえば、私は新しいEcoreモデリングプロジェクトを作成し、多くのチュートリアル(例えばvogella tutorialのように)のような単純なモデルを作成したいと考えています。しかし、私はGUIを使って手でそれをやりたいとは思わない。 EMF-Ecore-APIを使って、Javaコードを使ってモデルを作成する方法を学びたいと思います。JavaコードでEMFモデルを生成する方法

このトピックに関するチュートリアルを検索しようとしました。しかし、私の失望のために、私はこの話題について多くを見つけることができませんでした。私が見つけることができる唯一のものはcode snippets to load and modify existing models by codeでした。しかし、新しいモデルの作成については何もしていません。 APIを見てもわかりませんでした。

このトピックに関する情報はありますか?それでは、どうすればJavaコードでEMFモデルを作成できますか?

答えて

2

IBMには動的EMFに関するトピックがあります。

emfでの作成の仕方に精通していれば、それはかなり簡単です。すべてのEPackageには、独自のEFactoryEPackage Instancesがあり、メタモデル自体(epackage)に関する作成(工場)またはストア情報を処理します。

たEcoreは、それがこのような場で新しいメタモデルを作成するために、絶対に可能です独自のEPackageEFactoryを持っています

/*Use the Ecore factory*/ 
    EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE; 
    /*Create your EPackage*/ 
    EPackage myPackage = ecoreFactory.createEPackage(); 
    /*set EPackage properties*/ 
    myPackage.setName("myTest"); 
    myPackage.setNsPrefix("myTest"); 
    myPackage.setNsURI("http://com.myTest"); 

    /*Create your first EClass*/ 
    EClass myFirstEClass = ecoreFactory.createEClass(); 
    myFirstEClass.setName("myClass"); 
    /*Add to your EPackage's EClassifiers*/ 
    /*EClasses and EDatatypes implement both EClassifiers*/ 
    myPackage.getEClassifiers().add(myFirstEClass); 

    /*Create your first EAtttribute*/ 
    EAttribute myFirstEAtt = ecoreFactory.createEAttribute(); 
    myFirstEAtt.setName("name"); 
    /*Use the EcorePackage Datatypes -> here EString*/ 
    myFirstEAtt.setEType(EcorePackage.eINSTANCE.getEString()); 
    /*use EStructuralFeatures to add your EAtt*/ 
    /*EReferences and EAttributes are both EStructuralfeatures*/ 
    myFirstEClass.getEStructuralFeatures().add(myFirstEAtt); 

更新:

/*Create your second EClass*/ 
    EClass mySecondEClass = ecoreFactory.createEClass(); 
    mySecondEClass.setName("mySecondClass"); 
    myPackage.getEClassifiers().add(mySecondEClass); 

    /*now, the firstClass should hold instances of secondClass*/ 
    /*1. create EReference (Ereferences unlike EAttributes define relationships between EClasses)*/ 
    EReference secondClassesRef = ecoreFactory.createEReference(); 
    secondClassesRef.setName("secondClasses"); 
    /*set containment true -> every EObject must have a Container*/ 
    secondClassesRef.setContainment(true); 
    /*set Type to your EClass*/ 
    secondClassesRef.setEType(mySecondEClass); 
    /*set upperbound -> now the reference is an EList*/ 
    secondClassesRef.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY); 

    /*finally add ERef to EClass*/ 
    myFirstEClass.getEStructuralFeatures().add(secondClassesRef); 

    /*and for example supertypes*/ 
    myFirstEClass.getESuperTypes().add(mySecondEClass); 

今、あなたとEPackageあなた自身を持っていますタイプEString

EAttributeの名前を持つ新しいEClass

/* 
    * load existing EPackage 
    */ 
    EcorePackage.eINSTANCE.eClass(); 
    /*Initialize your EPackage*/ 
    final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 
    final Map<String, Object> m = reg.getExtensionToFactoryMap(); 
    m.put(EcorePackage.eNAME, new XMIResourceFactoryImpl()); 

    final ResourceSet resSet = new ResourceSetImpl(); 
    Resource resource = null; 
    try { 
     resource = resSet.getResource(URI.createFileURI("/Your/Path/To/Directory/myTest.ecore"), true); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    /*load root and cast to EPackage*/ 
    final EPackage root = (EPackage) resource.getContents().get(0); 

更新: たEcoreがでていますが、既存たEcore済みEPackageをロードしたい場合は、

/* 
    * Save your EPackage to file ecore file: 
    */ 

    /*Initialize your EPackage*/ 
    myPackage.eClass(); 
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; 
    Map<String, Object> m = reg.getExtensionToFactoryMap(); 
    /*add default .ecore extension for ecore file*/ 
    m.put(EcorePackage.eNAME, new XMIResourceFactoryImpl()); 

    // Obtain a new resource set 
    ResourceSet resSet = new ResourceSetImpl(); 
    // create a resource 
    Resource resource = null; 
    try { 
     resource = resSet.createResource(URI.createFileURI("/Your/Path/To/Directory/myTest.ecore")); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    /*add your EPackage as root, everything is hierarchical included in this first node*/ 
    resource.getContents().add(myPackage); 

    // now save the content. 
    try { 
     resource.save(Collections.EMPTY_MAP); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

およびその逆:今では、このような.ecoreファイルに新しい済みEPackageを保存することも可能ですもちろん、独自のメタモデル。 Docsには、ecoreアーキテクチャの概要があります。

したがって、動的EMFを使用する場合は、これを理解する必要があります。私が示したように、EPackageを動的に作成するのは非常に簡単ですが、ecoreモデル(EClass、EAttributes、EReferences、EType、包含、スーパータイプなど)の基本属性を設定する方法を知る必要があります。一度アーキテクチャを理解すれば、かなり簡単です。ページの下部にあるUML-Diagramを見てください。

私もEClasses

+0

あなたの助けてくれてありがとうとの関係を初期化する方法をお見せするために上記のコードを更新したが、私はもう一つ質問があります:どのように2つのクラス間の関係を作成しますか?通常のReferenceやSuperTypeのようなものですか? – ConveniencePatterns

+1

私の答えを更新しました –

関連する問題