eclipseeclipse-emfeclipse-emf-ecore

Adding behavior to EMF models


I'm wondering how people in general deal with adding behavior to EMF models. I have a model representing a certain type of physical file (a project file for an IDE, but the details are not interesting), and the model contains some computed attributes (such as the result of passing this file to external tools). I can see two ways to go about this: subclassing and editing the generated model classes.

I'm not so much interested in solving this particular problem (I can do that in a number of ways), as in learning how other EMF users approach this problem in general.


Solution

  • I had the same kind of problem and I talked to some modeling people and Ed Merks.

    The general approach is to statically create all possible types and use them without modification. Dynamic models (i.e. EMF models which change at runtime) aren't very common so there isn't a lot of knowledge around what's the best way to implement them.

    Some problems that you will face: Loading and saving of your runtime model classes. You better make sure that the model classes have at least the same attributes when you load a model again.

    This usually means that you have to save the EMF meta model along with the model itself.

    As for subclassing vs. editing: Think of EMF as a kind of Reflection-like API. It's really just an API to describe Java classes, to create instances, to change those instances plus persistence.

    So if you need to decide which way is better, ask yourself how you would solve it without EMF (i.e. in plain Java when using Reflection).