Skip to content

Commit c475386

Browse files
committed
feat(datastructures): IModelTree
1 parent 6dffe89 commit c475386

File tree

1 file changed

+12
-8
lines changed
  • model-datastructure/src/commonMain/kotlin/org/modelix/datastructures/model

1 file changed

+12
-8
lines changed

model-datastructure/src/commonMain/kotlin/org/modelix/datastructures/model/NodeObjectData.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ data class NodeObjectData<NodeId>(
7474
} else {
7575
// persist ID only to prevent ObjectHash changes when metamodel elements are renamed
7676
@OptIn(DelicateModelixApi::class)
77-
if (index < 0) {
78-
copy(properties = properties + (role.getIdOrName() to value))
77+
val newProperties = if (index < 0) {
78+
properties + (role.getIdOrName() to value)
7979
} else {
80-
copy(properties = properties.take(index) + (role.getIdOrName() to value) + properties.drop(index + 1))
80+
properties.take(index) + (role.getIdOrName() to value) + properties.drop(index + 1)
8181
}
82+
// sorted to get a stable ObjectHash and avoid non-determinism in algorithms working with the model (e.g. sync)
83+
copy(properties = newProperties.sortedBy { it.first })
8284
}
8385
}
8486

@@ -93,11 +95,13 @@ data class NodeObjectData<NodeId>(
9395
} else {
9496
// persist ID only to prevent ObjectHash changes when metamodel elements are renamed
9597
@OptIn(DelicateModelixApi::class)
96-
if (index < 0) {
97-
copy(references = references + (role.getIdOrName() to value))
98+
val newReferences = if (index < 0) {
99+
references + (role.getIdOrName() to value)
98100
} else {
99-
copy(references = references.take(index) + (role.getIdOrName() to value) + references.drop(index + 1))
101+
references.take(index) + (role.getIdOrName() to value) + references.drop(index + 1)
100102
}
103+
// sorted to get a stable ObjectHash and avoid non-determinism in algorithms working with the model (e.g. sync)
104+
copy(references = newReferences.sortedBy { it.first })
101105
}
102106
}
103107

@@ -133,8 +137,8 @@ data class NodeObjectData<NodeId>(
133137
parent = encodeNullId(value.parentId),
134138
role = value.roleInParent.getIdOrNameOrNull(),
135139
children = value.children,
136-
properties = value.properties.sortedBy { it.first }.toMap(),
137-
references = value.references.sortedBy { it.first }.toMap(),
140+
properties = value.properties.toMap(),
141+
references = value.references.toMap(),
138142
)
139143
}
140144

0 commit comments

Comments
 (0)