Using a capella element declared only once, in multiple occurrences

I noticed that if you made a capella element outside a transaction, then opened a transaction and added that element under the architectureS of capella (OA,SA, ARCHI, PHYS), the item would not be added to all of them, except for the last one in the code. Obviousely this item has the same variable name (lets say var1), same capella name (elem1), and same capella type of course (type1), indeed the item was created one single time.

I tried to make a similar element in a new variable (var2,elem1,type1), INSIDE the transaction, and I noticed it had been taken in account.

I tried to separate one of the architectures from the rest and put in a new transcation. So I had:
On one hand a transaction that adds the same element (var1,elem1,type1) inside SA,ARCHI,PHY, and on the other hand a second transaction that comes BELOW the first transcation, and inside which we add the element (var1,elem1,type1) inside OA.

Result → The script added the element (var1,elem1,type1) in OA and nowhere else.

I also tried to just change the name of the capella element (var1,elem2,type1), before every modifying model querry, I still got only one single occurence, the three others ignored.

Finally I tried to give the variable containing the capella element, a new python value while keeping the same properties (var1,elem1,type1), before every modifying model querry, and it worked. Basically reinjecting the same properties of the var1 (var 1 = (elem1,type1)), or in other works, declare the python variable again.

My conclusions:

  • In Python4Capella, you cannot modify the model multiple times, with a single variable created onc single time outside a transaction (or not). Meaning, If you want to add an element created “once” (one single variable), in multiple places of your model, you cannot do it more than once.
  • If you try to add that element in multiple places of your model anyway, then P4C will only take in account the very last occurence in the script, whether you call the adding query code in a single transaction multiple times, or in seperated transactions.
  • However, if you re evaluate the value of the python variable just after a “modifying model query”, with the same properties, then you wil be able to use it again. As if the variable dissapeared once used by P4C to modify the model, and the only way to prevent it from dissapearing thus making it usable again, would be to reinject its value to the python variable, or in other words redeclare the variable.

So do you think it is supposed to be working this way, or is it some bug or? Thanks.

Ps.What i call “modifying model query” are the lines of code that take this form: se.get().add(…)

That’s not related to the transaction nor Python4Capella but to the way EMF works. An EObject can only be contained by one other EObject. That means that each time you attach the EObject to the model it’s removed from the previous containing EReference. If you want to add an EObject to many containement EReference you will have to create new instances of that Object. This way EMF make sure your model is a tree and not a general DAG.

Let say you are looping over some input to create some LogicalComponent. If you create the LogicalComponent outside the loop body, this will create only one EObject in the JVM and you will experience the behavior you described since EMF only allows an EObject to be contained once.
To solve this, you can create the LogicalComponent inside the loop body, this way one EObject is created for each loop and can then be contained in a given EObject from the model.

I hope this clarify this behavior.

1 Like

Yes It clears things up quite a lot. Thank!

1 Like