2

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.

Aljoscha Meyer
  • 527
  • 2
  • 9
  • 19

1 Answers1

6

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

enter image description here

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.

Vincent Aranega
  • 1,441
  • 10
  • 21
  • This looks like an impressively conplicated way to get a string into the generated output. But I guess its necessary to allow reflective access as well. Does that mean that derived properties for which the code was added by hand do not work with dynamic EMF? – Aljoscha Meyer Jun 16 '16 at 10:14
  • The solution I gave you will not inject code into your generated code but will interpret the "the string representing the implementation" when you access the attribute. This method also works if you only use dynamic EMF (without generating your `ecore` implementation). If there is no annotation in your `ecore` and a "by-hand implementation" in your generated code, EMF will use the implementation you added in your code (as long as the generated model plugins are well loaded and registered). For code injection from the metamodel into the generated code, the process is different (see edited post) – Vincent Aranega Jun 16 '16 at 11:20
  • So, to clarify whether I understood this: Your solution does not technically do what I asked (change generated code), but it does change the runtime behaviour to the same effect. Which is all I really need anyways. And to be fair, my question did not explicitely state that I wanted to manipulate the generated output code. – Aljoscha Meyer Jun 16 '16 at 11:25
  • I edited the post with the process you need to follow to inject code in the generated model implementation. – Vincent Aranega Jun 16 '16 at 11:34
  • I wish I could give another upvote, this is exactly what I was looking for. I'll edit the question to emphasize the part about changing the output as well. Thank you for helping out this much! – Aljoscha Meyer Jun 16 '16 at 11:39
  • I think you may also want to give Xcore a try. The output transforms to ecore and it generates the fields automatically for you. It works for many cases that I encounter, although the generated code is not the most beautiful. Here is some example of [deriving features](http://stackoverflow.com/questions/34903043/can-derived-attributes-be-persisted-and-derivation-disabled-in-ecore-and-ocl/) – vikin9 Jun 20 '16 at 08:30