【问题标题】:How to set enum reference with default value = null in ecore and gen model?如何在 ecore 和 gen 模型中使用默认值 = null 设置枚举引用?
【发布时间】:2012-09-12 14:17:27
【问题描述】:

我有一个 Eclass Vehicle,它有一个枚举属性 BreakType breakType

BreakType 在相同的 Ecore 模型中定义为:

BreakType{
    DRUM(0), DISC(1), BLADE(2)
}

我想将属性breakType 默认设置为空。为此,我为breakType 属性设置了以下属性->

DefaultLiteralValue: // it's blank
Unsettable: True

Properties of BreakType enum
Default Value : DRUM=0 // this is shown in editor UI even If i remove it from xml.

生成 gen-model 和代码后我得到的是

BreakType breakType = DRUM // attribute set with default value

如何将其设置为默认值?

【问题讨论】:

    标签: eclipse-emf eclipse-emf-ecore emf


    【解决方案1】:

    我不认为你可以。如果您未能通过defaultValueLiteral 属性提供默认值,EMF 会自动选择适合属性类型的值。对于枚举类型,它是它定义的第一个字面值。

    您始终可以自己修改生成的代码。或者,也许您应该利用生成的方法来提供不可设置的功能:

    void unsetAttribute();
    boolean isSetAttribute();
    

    并检查未设置状态而不是 null 值。

    【讨论】:

    • 嗯,看看我在 EMF 食谱中遇到了什么。我还没有测试过,但也许这就是你要找的东西:Generating enumeration-based attributes that support null
    • 作为链接的 EMF 配方的替代方案,您可以定义一个额外的枚举实例,例如 UNDEFINED,并确保将其用作默认值,即在 ecorediag 中将其移动到顶部文字列表(该枚举类型的默认值将自动调整)。有了这个,至少有一个未定义的默认值,但它不是空的。
    • @JoséM.Benítez:太好了!这是值得自己回答的,我会赞成。
    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 2014-11-30
    • 2021-08-23
    • 2011-07-23
    • 1970-01-01
    • 2019-01-03
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多