首页 > 解决方案 > EMF:在 Ecore 元模型中定义通用包含引用

问题描述

自从我使用 EMF 已经很长时间了,我一直坚持这一点。我想创建一个等效于的泛型类型:

class Result<T:ASTNode>{
  T root;
}

我在 Kotlin 中定义这个:

    val result = ePackage.createEClass("Result").apply {
        // I think this part is correct
        val typeParameter = EcoreFactory.eINSTANCE.createETypeParameter().apply {
            this.name = "T"
            this.eBounds.add(EcoreFactory.eINSTANCE.createEGenericType().apply {
                // astNode is my EClass
                this.eClassifier = astNode
            })
        }
        this.eTypeParameters.add(typeParameter)
        val rootContainment = EcoreFactory.eINSTANCE.createEReference()
        rootContainment.name = "root"
         
        // STUCK!
        // here should I set rootContainment.eType? rootContainment.eGenericType?
        // how? 

        rootContainment.isContainment = true
        rootContainment.lowerBound = 0
        rootContainment.upperBound = 1
        this.eStructuralFeatures.add(rootContainment)

        addContainment("issues", issue, 0, -1)
    }

标签: eclipse-emfecore

解决方案


等效的 .ecore 是:

<eClassifiers xsi:type="ecore:EClass" name="Result">
<eTypeParameters name="T">
  <eBounds eClassifier="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eTypeParameters>
<eStructuralFeatures xsi:type="ecore:EReference" name="t">
  <eGenericType eTypeParameter="#//Result/T"/>
</eStructuralFeatures>

所以你想使用rootContainment.eGenericType一个引用你的 ETypeParameter 的新 EGenericType


推荐阅读