eclipse-basyx / basyx-python-sdk

MIT License
60 stars 28 forks source link

Rearrange SubmodelElements within a SubmodelElementCollection #274

Closed JS-Aibel closed 5 months ago

JS-Aibel commented 5 months ago

Is it possible to rearrange SubmodelElements within a SubmodelElementCollection? In the Package Explorer one can move a property up or down in the hierarchy. Is this function possible with Basyx?

I have tried to move a property element from last to first with the following code without any luck. smc.namespace_element_sets.insert(0, smc.namespace_element_sets.pop())

jkhsjdhjs commented 5 months ago

smc.namespace_element_sets refers to the list of NamespaceSets within the current Namespace, so you're modifying the order of NamespaceSets in that list, which doesn't make any difference.

Unfortunately, the order of SubmodelElements within a SubmodelElementCollection cannot be changed, as the elements are stored in a NamespaceSet, which internally uses a python set. These are unordered by design, the order in which they are iterated or displayed is arbitrary and cannot be influenced without changing the item. This is an intentional implementation design in the basyx-python-sdk, as the order of elements in a SubmodelElementCollection is semantically meaningless.

Theoretically you could create a custom SubmodelElementCollection, that uses an OrderedNamespaceSet instead, where the order can be changed, but I don't really see the point in this. The better solution would be using a SubmodelElementList, where the order is relevant by design, and which also makes use of the OrderedNamespaceSet internally.

s-heppner commented 5 months ago

I'll close this issue for now. Feel free to reopen, if you still have questions.