简体   繁体   English

覆盖派生的Ecore-class(EMF)中的属性值

[英]Override the value of an attribute in a derived Ecore-class (EMF)

I'm aware of the fact that I cannot override or redefine attributes as a whole in an ecore-based model when it comes to inheritance. 我知道以下事实:在涉及继承时,我无法在基于ecore的模型中整体上重写或重新定义属性。 But can I somehow assign a new value to an existing, inherited attribute? 但是我可以以某种方式为现有的继承属性分配新值吗?

Eg Class A defines the attribute name which is a string, the default value is set to 'defaultA'. 例如,类A定义属性名称 ,该名称是字符串,默认值设置为'defaultA'。 Class B inherits from Class A and name should have the value 'defaultB'. B类继承自A类,并且名称应具有值“ defaultB”。

I tried to just reimplement the attribute with the same name and datatype in Class B, but I cannot create a genmodel from that ("There may not be two features named 'name'"). 我试图在类B中重新实现具有相同名称和数据类型的属性,但无法从中创建一个genmodel(“可能没有两个名为'name'的功能”)。

Isn't it even possible to change the value of an inherited attribute? 甚至不可能更改继承属性的值吗?

No, that's not supported. 不,不支持。 The field for the feature is declared in the base class and that assigns the default defined for the feature... 要素的字段在基类中声明,并为该要素分配默认定义...

Attributes cannot be overridden in plain EMF but there is a workaround through OCL. 不能在普通EMF中覆盖属性,但是可以通过OCL解决。 (Of course, you can generate Java code and implement the attributes as needed but it won't work in the Eclipse instance where you created the meta-model.) (当然,您可以生成Java代码并根据需要实现属性,但在创建元模型的Eclipse实例中将无法使用。)

The trick is that operations can be overridden in EMF and through OCL you can implement the operations as needed. 诀窍在于, 可以在EMF中覆盖操作,并且可以通过OCL根据需要实现操作。

Below is a minimal example (written in OCLinEcore editor) that defines a meta-model consisting of classes A and B . 下面是一个最小示例(用OCLinEcore编辑器编写),该示例定义了一个由类AB组成的元模型。 The class A introduces an attribute label which is just redirected to the operation computeLabel() . A引入了一个属性label ,该label仅被重定向到操作computeLabel() Class 'B' provides a different implementation for the computeLabel() operation. 类“ B”为computeLabel()操作提供了不同的实现。

package workaround : workaround = 'workaround' {
    class A {
        operation computeLabel() : String {
            body: 'labelA';
        }
        attribute label : String {
            derivation: computeLabel();
        }
    }
    class B extends A {
        operation computeLabel() : String {
            body: 'labelB';
        }
    }
}

You can test it by creating a dynamic instance of class B and opening the B.xmi file in the standard Sample Reflective Ecore Model Editor . 您可以通过创建B类的动态实例并在标准Sample Reflective Ecore Model Editor打开B.xmi文件来进行测试。 You'll then see: 然后,您将看到:

在此处输入图片说明

This rather hackish alternative is to declare an operation in the subclass with the same name as the feature getter name . 这种相当骇人听闻的选择是在子类中声明一个与功能获取者名称同名的操作

This is a light-weight variation of Viliam Simko's solution. 这是Viliam Simko解决方案的轻量级版本。

With this solution you will not have a proper name feature in Class B, but when then name feature in Class A is accessed your getter in Class B will be called instead. 使用此解决方案,您将在B类中没有适当的name功能,但是当访问A类中的name功能时,将改为调用B类中的getter。

I don't even know if this is really allowed. 我什至不知道这是否真的允许。 But it seems to be working with EMF 2.13.0, Xcore 1.5.0. 但它似乎可以在EMF 2.13.0,Xcore 1.5.0中使用。

Example: 例:

class ClassA {
    String name
}

class ClassB extends ClassA {
    op String getName() {
        return "Name B"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM