I want to use derived
attributes and references in an ecore model, but so far I have not found any documentation on how to set the code for the methods which compute the values of derived attributes/references.
As far as I understand it, the basic workflow is to mark an attribute/reference as derived
, generate model code, and then manually add the implementation. However, I work with models dynamically generated through the Ecore API. Is there a way to take a String and specify this String as the implementation for the computation of the derived feature, without manually editing generated files?
EDIT>
To clarify: I'm looking for a way to directly change the generated Java files, by specifying method bodys (as strings) for the getters of derived EStructuralFeatures.
EMF provides a way of dealing with dedicated implementation for EOperation
and derived EAttribute
using "invocation delegate". This functionality allows you to put some implementation directly in your ecore
metamodel in a string format (as soon as the used language can be "handled" by EMF, i.e, an invocation delegate exists).
As far as I know, OCL is well supported: https://wiki.eclipse.org/OCL/OCLinEcore#Invocation_Delegate
The registration of the invocation delegate is performed either by plugin registration or by hand (for standalone usage), and the mechanism works with the EMF reflection layer (dynamic EMF): https://wiki.eclipse.org/EMF/New_and_Noteworthy/Helios#Registering_an_Invocation_Delegate
(Please note that I never experienced this mechanism. I know it exists, but I never played with it.)
EDIT>
It seems that the question was not related to dynamic code execution for derived attribute, but to code injection (I misunderstood the "Is there a way to take a String and specify this String as the implementation for the computation of the derived feature?").
EMF provides a way of injecting code placed on the ecore
metamodel directly into the generated code.
Here is the way for EAttribute
with derived
property. The EAttribute
should have the following properties set to true
: {derived volatile}
(you can also add transient
). If you only want a getter and no setter for your EAttribute
, you can also set the property changeable
to false
.
Once your EAttribute
is well "configured", you have to add a new EAnnotation
with the source set to http://www.eclipse.org/emf/2002/GenModel
and an entry with the key
set to get
and value
set to your code that will be injected (see image below).
And voilà, your code will be generated with the value
value injected in your getter.
You can add the same process for EOperation
using body
instead of get
.