eclipse-emfmdamdsd

How can I create a reference to existing model objects?


I'm new to MDSD/EMF but am making great progress thanks to Eclipse's support.

I created a toy model as answered yesterday as awynne suggested, and I'm very close. I'm only using EMF and its generated tree editor.

Here's a summary of my EMF meta-model:

- EClass: Widget
  - name

- EClass: WidgetReference
  - aWidgetReference : Widget [0..1] Containment = false

- EClass: WidgetContainer
  - myWidgets : Widget [0..*] Containment = true

- EClass: ReferenceContainer
  - myReferences : WidgetReference [0..*] Containment = true

- EClass: TopLevelClass
  - myWidgetContainer    : [1] WidgetContainer    Containment = true
  - myReferenceContainer : [1] ReferenceContainer Containment = true

What's happening now is that in the generated tree editor for the model, I create everything starting from the TopLevelClass. I can successfully create multiple widgets in the WidgetContainer, and I can create a single WidgetReference in the ReferenceContainer and associate it with a specific Widget.

In case anyone runs into this, there is a bug in the generated EMF editor code:

When I try to add more than one WidgetReference to the ReferenceContainer, the tree editor only shows the first WidgetReference under the ReferenceContainer. If I click on the WidgetReferences item in the tree, it does show multiple WidgetReferences in the properties view. It just won't let me see them in the tree, and therefore won't let me select them to change their reference to associate it to an actual widget.

The workaround to this is to create as many references as you need, save and close the model, and then re-open it. Upon re-opening it, all of the references will be shown.

Thanks for any help/guidance.

Jeff


Solution

  • I think the answer to your question is related to the idea of "containment." Any parent object which has a containment reference (containment=true in the EMF metamodel) to a child object manages the lifecycle of that child. So when the child is created, the parent's reference to that object is made automatically. If the reference is not of type containment (containment=false), then you can point the reference to any existing model object of the correct type. I think you need to set your reference as containment=false.

    I have not used Acceleo so let's use the EMF tree editor as an example (the concept also applies to the GMF editior): In the tree editor, if the parent object has a containment reference to a child, the user is able to create the child by right-clicking the parent and choosing "new-->child-->ChildObject." If the reference to the child is NOT of type containment , the user will be able to choose instances of that object that already exist in the model by clicking the appropriate child property in the properties window of the parent object.

    Does that help? If this description is confusing, it may help to create a toy model with some non-containment references and generate the EMF editor for it.