Skip to content

Commit 0e73b21

Browse files
committed
feat(model-api): add and implement INode::replaceNode
1 parent e55c8e3 commit 0e73b21

File tree

17 files changed

+163
-1
lines changed

17 files changed

+163
-1
lines changed

light-model-client/src/commonMain/kotlin/org/modelix/client/light/LightModelClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ class LightModelClient internal constructor(
455455
}
456456
}
457457

458+
override fun replaceNode(concept: ConceptReference): INode {
459+
TODO("Not yet implemented")
460+
}
461+
458462
override fun removeChild(child: INode) {
459463
require(child is NodeAdapter && child.getClient() == getClient()) { "Unsupported child type: $child" }
460464
requiresWrite {

model-api/src/commonMain/kotlin/org/modelix/model/api/INode.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ interface INode {
4747

4848
fun tryGetConcept(): IConcept? = getConceptReference()?.tryResolve()
4949

50+
/**
51+
* Replaces this node with a new node of the given concept that uses the same id as this node.
52+
* Properties, references and children will be the same.
53+
*
54+
* @param concept the concept of the new node
55+
* @return replacement for this node with the new given concept
56+
*/
57+
fun replaceNode(concept: ConceptReference): INode
58+
5059
/**
5160
* Role of this node in its parent node if it exists,or null otherwise.
5261
*/

model-api/src/commonMain/kotlin/org/modelix/model/api/ITree.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ interface ITree {
254254
*/
255255
fun deleteNodes(nodeIds: LongArray): ITree
256256

257+
/**
258+
* @param nodeId id of the node
259+
* @param concept the concept ref of the new concept for the node, or null to remove the concept
260+
*/
261+
fun setConcept(nodeId: Long, concept: IConceptReference?): ITree
262+
257263
fun getAllChildrenAsFlow(parentId: Long): Flow<Long> = getAllChildren(parentId).asFlow()
258264
fun getDescendantsAsFlow(nodeId: Long, includeSelf: Boolean = false): Flow<Long> {
259265
return if (includeSelf) {

model-api/src/commonMain/kotlin/org/modelix/model/api/IWriteTransaction.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ interface IWriteTransaction : ITransaction {
105105
fun addNewChildren(parentId: Long, role: String?, index: Int, concepts: Array<IConceptReference?>): LongArray
106106
fun addNewChildren(parentId: Long, role: String?, index: Int, childIds: LongArray, concepts: Array<IConceptReference?>)
107107

108+
/**
109+
* Sets the concept of the node identified by the given id.
110+
*
111+
* @param nodeId id of the node
112+
* @param concept the new concept, or null to remove the concept
113+
*/
114+
fun setConcept(nodeId: Long, concept: IConceptReference?)
115+
108116
/**
109117
* Deletes the given node.
110118
*

model-api/src/commonMain/kotlin/org/modelix/model/api/PNodeAdapter.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ open class PNodeAdapter(val nodeId: Long, val branch: IBranch) : INode, INodeEx
8282
return PNodeAdapter(branch.writeTransaction.addNewChild(nodeId, role.key(this), index, concept), branch)
8383
}
8484

85+
override fun replaceNode(concept: ConceptReference): INode {
86+
branch.writeTransaction.setConcept(nodeId, concept)
87+
return this
88+
}
89+
8590
override val allChildren: Iterable<INode>
8691
get() {
8792
notifyAccess()

model-api/src/commonMain/kotlin/org/modelix/model/api/TreePointer.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ class TreePointer(private var tree_: ITree, val idGenerator: IIdGenerator = IdGe
138138
tree = tree.addNewChildren(parentId, role, index, childIds, concepts)
139139
}
140140

141+
override fun setConcept(nodeId: Long, concept: IConceptReference?) {
142+
tree = tree.setConcept(nodeId, concept)
143+
}
144+
141145
override fun deleteNode(nodeId: Long) {
142146
tree = tree.deleteNode(nodeId)
143147
}

model-api/src/commonMain/kotlin/org/modelix/model/api/WriteTransaction.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ class WriteTransaction(_tree: ITree, branch: IBranch, idGenerator: IIdGenerator)
9191
return newIds
9292
}
9393

94+
override fun setConcept(nodeId: Long, concept: IConceptReference?) {
95+
checkNotClosed()
96+
tree = tree.setConcept(nodeId, concept)
97+
}
98+
9499
override fun deleteNode(nodeId: Long) {
95100
checkNotClosed()
96101
tree.getAllChildren(nodeId).forEach { deleteNode(it) }

model-api/src/commonMain/kotlin/org/modelix/model/area/AreaWithMounts.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package org.modelix.model.area
1515

16+
import org.modelix.model.api.ConceptReference
1617
import org.modelix.model.api.IBranch
1718
import org.modelix.model.api.IConcept
1819
import org.modelix.model.api.IConceptReference
@@ -159,6 +160,10 @@ class AreaWithMounts(val rootArea: IArea, mounts: Map<INode, IArea>) : IArea {
159160
return node.getChildren(role).map { NodeWrapper(getMountedArea(it)?.getRoot() ?: it) }
160161
}
161162

163+
override fun replaceNode(concept: ConceptReference): INode {
164+
return NodeWrapper(node.replaceNode(concept))
165+
}
166+
162167
override fun removeChild(child: INode) {
163168
val hiddenNode = getHiddenNode(child)
164169
if (hiddenNode == null) {

model-api/src/commonMain/kotlin/org/modelix/model/area/CompositeArea.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package org.modelix.model.area
1515

16+
import org.modelix.model.api.ConceptReference
1617
import org.modelix.model.api.IBranch
1718
import org.modelix.model.api.IConcept
1819
import org.modelix.model.api.IConceptReference
@@ -163,6 +164,10 @@ class CompositeArea : IArea {
163164
throw UnsupportedOperationException("Read only. Create a new CompositeArea instance instead.")
164165
}
165166

167+
override fun replaceNode(concept: ConceptReference): INode {
168+
throw UnsupportedOperationException("Root cannot have a concept.")
169+
}
170+
166171
override fun removeChild(child: INode) {
167172
throw UnsupportedOperationException("Read only. Create a new CompositeArea instance instead.")
168173
}
@@ -219,6 +224,8 @@ class CompositeArea : IArea {
219224
return node.getChildren(role).map { NodeWrapper(it) }
220225
}
221226

227+
override fun replaceNode(concept: ConceptReference): INode = NodeWrapper(node.replaceNode(concept))
228+
222229
override fun removeChild(child: INode) {
223230
node.removeChild(unwrapNode(child))
224231
}

model-client/src/commonMain/kotlin/org/modelix/model/AutoTransactionsBranch.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class AutoWriteTransaction(branch: IBranch) : AutoTransaction(branch), IWriteTra
8080
childId: Long,
8181
concept: IConceptReference?,
8282
) = branch.computeWriteT { it.addNewChild(parentId, role, index, childId, concept) }
83+
override fun setConcept(nodeId: Long, concept: IConceptReference?) =
84+
branch.computeWriteT { it.setConcept(nodeId, concept) }
85+
8386
override fun deleteNode(nodeId: Long) = branch.computeWriteT { it.deleteNode(nodeId) }
8487

8588
override var tree: ITree

0 commit comments

Comments
 (0)