Override the value of an attribute in a derived Ecore-class (EMF)
Asked Answered
Y

3

5

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. But can I somehow assign a new value to an existing, inherited attribute?

E.g. Class A defines the attribute name which is a string, the default value is set to 'defaultA'. Class B inherits from Class A and name should have the value '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'").

Isn't it even possible to change the value of an inherited attribute?

Yellowbird answered 27/6, 2013 at 15:6 Comment(1)
In my experience, these kind of things can be achieved in the generated code. I'm not aware of a way to do this directly in the model (assuming this is what you want?)Author
F
4

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...

Fulbright answered 1/7, 2013 at 15:4 Comment(0)
C
4

Attributes cannot be overridden in plain EMF but there is a workaround through 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.)

The trick is that operations can be overridden in EMF and through OCL you can implement the operations as needed.

Below is a minimal example (written in OCLinEcore editor) that defines a meta-model consisting of classes A and B. The class A introduces an attribute label which is just redirected to the operation computeLabel(). Class 'B' provides a different implementation for the computeLabel() operation.

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. You'll then see:

enter image description here

Craiova answered 26/11, 2014 at 9:43 Comment(0)
A
1

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.

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.

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.

Example:

class ClassA {
    String name
}

class ClassB extends ClassA {
    op String getName() {
        return "Name B"
    }
}
Adolpho answered 15/11, 2017 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.