matthias
matthias

Reputation: 2062

Ecore containment opposite

Image the following: (<> indicates a black diamond, which refers to a composition in UML)

+---+ 0..1   * +---+
| A |<>--------| B |
+---+          +---+

I have class A and class B. A containment reference exists from A to B with multiplicity unbound. The opposite reference has multiplicity 0..1.

My question is: Isn't this opposite multiplicity wrong / impossible ?! Because in Ecore, the containment reference specifies that B can only exist if it as an A. Therefore the multiplicity from B to A is always 1, isn't it ?!

0..1 would mean that an instance of B can have an A or not.

UPDATE: my task is to transform the following UML diagram into Ecore

+---+ 0..1   * +---+ *     1  +---+
| A |<>--------| B |--------<>| C |
+---+          +---+          +---+

I am not sure how to map the UML composition to Ecore

Am I right ?!

Upvotes: 1

Views: 2008

Answers (3)

vainolo
vainolo

Reputation: 6987

The meaning of your UML is that A can contain many Bs, C contains many Bs, and B can reference 0..1 As and exactly one B.

This UML diagram is incorrect because an instance of B cannot be shared (that is the meaning of composition). So the link B-C should also be 0..1 and in that case you can use containment from both A and C.

Upvotes: 1

E-Riz
E-Riz

Reputation: 33034

An instance of B can exist without being contained in an A, but I'm pretty sure it can not be persisted in that state (using default XML/XMI persistence). In fact, you can get a valid model into this state:

  • if you have some other object, C, with an external reference to B
  • A (and by nature of its containment, B) and C are loaded into memory and all references are resolved
  • A is edited to remove B
  • C will now have a reference to an instance of B, but that instance will not have a container and C can not be persisted (you'll get an exception about "B does not exist in a resource").

Upvotes: 0

Zolt&#225;n Ujhelyi
Zolt&#225;n Ujhelyi

Reputation: 13858

It can be zero, if B is the root of an EMF Resource. An EMF Resource root never has any container. And any EMF model can act as a resource root.

Upvotes: 0

Related Questions