简体   繁体   中英

Referencing Ecore Eclasses. Is it possible to reference eClasses from xmi instances?

Is it possible to reference an eClass (meta-class in the ecore) from an instance (the model itself)?

Let's suppose that I have an ecore meta-model M2.ecore containing a base class BASECLASS from which all the meta-classes (eClasses) derive. This is, BASECLASS is the superclass of all the eClasses of the M2.ecore meta-model. One of this classes called M, uses the BASECLASS, so there is an eReference between the eClass M and the BASECLASS:

M2.ecore

ClassA ----inherits_from------|> BASECLASS
ClassM ----inherits_from------|> BASECLASS
ClassM ----uses-(eReference)---> BASECLASS

Then I have an instance INSTANCE_M of the eClass M that I want to link to eClasses of M2.ecore, for example:

M2.xmi

INSTANCE_M -----eReference---> (M2.ecore).Class_A

This is, I want to link the classes of the meta-model. Apparently, using the Sample Reflective Editor I can only link instances and never meta-classes.

Why? How to solve this problem?

Thank you in advance

First of all, I would say that your need goes (in a way) against the way EMF works. With EMF, you have a model defining some concepts (let's call it "metamodel") and its instances (let's call those "models"). What you want is a model that would contain model elements and metamodel elements. The concepts that can appear in your model, need to be defined in your metamodel but the metamodel is just a regular model. In a standard EMF workflow it is just an instance of the Ecore (meta) model and Ecore is also just an instance of an EMF model, itself. So you have this situation:

M2.xmi --instanceof--> M2.ecore --instanceof--> Ecore.ecore --instanceof--> Ecore.ecore...

And you want in M2.xmi to manipulate instances of concepts defined in M2.ecore and instances of concepts defined in Ecore.ecore (one of those instances being your M2.ecore model).

You can create you M2.ecore model as containing your concepts with for example an attribute "myMetaEntity" referencing a concept from Ecore.ecore. In the ecore editor, right click "load resource", then select from "registered packages" and select "http://www.eclipse.org/emf/2002/Ecore" and "Ok". Once "imported", you can create references to elements from the Ecore.ecore model for example you can create an attribute myMetaEntity with the type "EClass". You can then use this to reference an EClass from the M2.ecore model but keep in mind that this attribute defined in M2.ecore would accept any instances of the "EClass" concepts even from M3.ecore or XYZ.ecore.

If you just want a link from an instance to its metaclass you can use the method "eClass()" but if you really want to create "complex" links between instance elements and metaclasses you can't really, but you can create a metamodel importing the concepts from the meta-metamodel (I hate that name so much...), here Ecore.ecore, to gain the opportunity to manipulate both but it's also opening some kind of "pandora's box" because you can't say "you can manipulate instances of concept defined in Ecore.ecore only if those concepts are from M2.ecore".

If you know what you are doing or if your users (or anyone not familiar with the subtlety of this problem) do not manipulate this because you will give them an API / an UI or something, you're fine but keep in mind you want to do something conceptually tricky (yet easy technically with EMF).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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