Unexpected behaviour of subList


this is my second MPS post here, but I am still new in MPS. I noticed a stranged behaviour using the method "subList".

I have a node<GoalRule> rule and I created a new node called node<GoalRule> newRule. I want to copy some information of rule and add it to newRule. rule has a list of innerElements called inner and I want to add a subList of this list to newRule. It looks like this:


I know that Java subList only returns a view of the portion of a list and so I expected the same behaviour. But in this case, the elements, which are in the subList, are deleted.
For example:

rule.inner = [input x, input y]
rule.inner.subList(0,0) = input x
newRule.inner = input x (thats correct)
rule.inner = [input y] (first element is deleted)

Should subList in MPS work like this or what is my mistake? If the bevaiour is correct, I want to understand it works like this.


I hope someone can help me.


Official comment

This is expected behavior for nodes in MPS models. A node can only have one parent. The nodes in the sublist are removed automatically during the "addAll"operation from their original location so that they could become children at the new location. You must create copies of the nodes if you want to keep them at the old location, e.g.: with nodesToCopy.select{it -> it.copy}



Wow thanks for your very fast answer!

I tried copy already and it doesn't work, but I try it again and I hope it works. If it doesn't, I write another comment. ;)







It works!!! Thank you so so much! This kept me busy far to long and I should have asked you much earlier.


Best regards



Please sign in to leave a comment.