Skip to content

Commit af2af95

Browse files
mhuster23Oleksandr Dzhychko
andcommitted
fix(model-client): use new ConceptDependency
ChildrenDependency should not be necessary because the childlist does not change. Co-authored-by: Oleksandr Dzhychko <[email protected]>
1 parent 8b8e2f0 commit af2af95

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ class IncrementalBranch(val branch: IBranch) : IBranch, IBranchWrapper {
178178
}
179179

180180
override fun getConcept(nodeId: Long): IConcept? {
181-
accessed(UnclassifiedNodeDependency(this@IncrementalBranch, nodeId))
181+
accessed(ConceptDependency(this@IncrementalBranch, nodeId))
182182
return transaction.getConcept(nodeId)
183183
}
184184

185185
override fun getConceptReference(nodeId: Long): IConceptReference? {
186-
accessed(UnclassifiedNodeDependency(this@IncrementalBranch, nodeId))
186+
accessed(ConceptDependency(this@IncrementalBranch, nodeId))
187187
return transaction.getConceptReference(nodeId)
188188
}
189189

@@ -331,11 +331,8 @@ class IncrementalBranch(val branch: IBranch) : IBranch, IBranchWrapper {
331331
}
332332

333333
override fun setConcept(nodeId: Long, concept: IConceptReference?) {
334-
val oldParentId = transaction.getParent(nodeId)
335-
val oldRole = transaction.getRole(nodeId)
336334
transaction.setConcept(nodeId, concept)
337-
modified(ChildrenDependency(this@IncrementalBranch, oldParentId, oldRole))
338-
modified(UnclassifiedNodeDependency(this@IncrementalBranch, nodeId))
335+
modified(ConceptDependency(this@IncrementalBranch, nodeId))
339336
}
340337

341338
override fun deleteNode(nodeId: Long) {
@@ -378,12 +375,12 @@ class IncrementalBranch(val branch: IBranch) : IBranch, IBranchWrapper {
378375
}
379376

380377
override fun getConcept(nodeId: Long): IConcept? {
381-
accessed(UnclassifiedNodeDependency(this@IncrementalBranch, nodeId))
378+
accessed(ConceptDependency(this@IncrementalBranch, nodeId))
382379
return tree.getConcept(nodeId)
383380
}
384381

385382
override fun getConceptReference(nodeId: Long): IConceptReference? {
386-
accessed(UnclassifiedNodeDependency(this@IncrementalBranch, nodeId))
383+
accessed(ConceptDependency(this@IncrementalBranch, nodeId))
387384
return tree.getConceptReference(nodeId)
388385
}
389386

@@ -502,6 +499,11 @@ data class BranchDependency(val branch: IBranch) : IStateVariableReference<IBran
502499
override fun read(): IBranch = branch
503500
}
504501

502+
/**
503+
* Catch-all dependency for changes to a node.
504+
* It's the group dependency for a single node.
505+
* Also used directly when a node is added/deleted as a whole.
506+
*/
505507
data class UnclassifiedNodeDependency(val branch: IBranch, val nodeId: Long) : DependencyBase() {
506508
override fun getGroup(): IStateVariableGroup? {
507509
return try {
@@ -516,34 +518,70 @@ data class UnclassifiedNodeDependency(val branch: IBranch, val nodeId: Long) : D
516518
}
517519
}
518520

521+
/**
522+
* Dependency for a single property change.
523+
*
524+
* @see AllPropertiesDependency
525+
*/
519526
data class PropertyDependency(val branch: IBranch, val nodeId: Long, val role: String) : DependencyBase() {
520527
override fun getGroup() = AllPropertiesDependency(branch, nodeId)
521528
}
522529

530+
/**
531+
* Dependency for a single reference change.
532+
*
533+
* @see AllReferencesDependency
534+
*/
523535
data class ReferenceDependency(val branch: IBranch, val nodeId: Long, val role: String) : DependencyBase() {
524536
override fun getGroup() = AllReferencesDependency(branch, nodeId)
525537
}
526538

539+
/**
540+
* Dependency for a single child role change.
541+
*
542+
* @see AllChildrenDependency
543+
*/
527544
data class ChildrenDependency(val branch: IBranch, val nodeId: Long, val role: String?) : DependencyBase() {
528545
override fun getGroup() = AllChildrenDependency(branch, nodeId)
529546
}
530547

548+
/**
549+
* Dependency for any child role changes.
550+
*
551+
* @see ChildrenDependency
552+
*/
531553
data class AllChildrenDependency(val branch: IBranch, val nodeId: Long) : DependencyBase() {
532554
override fun getGroup() = UnclassifiedNodeDependency(branch, nodeId)
533555
}
534556

557+
/**
558+
* Dependency for any reference changes.
559+
*
560+
* @see ReferenceDependency
561+
*/
535562
data class AllReferencesDependency(val branch: IBranch, val nodeId: Long) : DependencyBase() {
536563
override fun getGroup() = UnclassifiedNodeDependency(branch, nodeId)
537564
}
538565

566+
/**
567+
* Dependency for any property changes.
568+
*
569+
* @see PropertyDependency
570+
*/
539571
data class AllPropertiesDependency(val branch: IBranch, val nodeId: Long) : DependencyBase() {
540572
override fun getGroup() = UnclassifiedNodeDependency(branch, nodeId)
541573
}
542574

575+
/**
576+
* Dependency for parent or role in parent changes.
577+
*/
543578
data class ContainmentDependency(val branch: IBranch, val nodeId: Long) : DependencyBase() {
544579
override fun getGroup() = UnclassifiedNodeDependency(branch, nodeId)
545580
}
546581

582+
/**
583+
* Dependency for concept changes.
584+
*/
547585
data class ConceptDependency(val branch: IBranch, val nodeId: Long) : DependencyBase() {
548586
override fun getGroup(): IStateVariableGroup = UnclassifiedNodeDependency(branch, nodeId)
549587
}

model-client/src/commonTest/kotlin/org/modelix/model/IncrementalBranchTest.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package org.modelix.model
1818

1919
import org.modelix.incremental.IncrementalEngine
2020
import org.modelix.incremental.incrementalFunction
21+
import org.modelix.model.api.ConceptReference
2122
import org.modelix.model.api.IConceptReference
2223
import org.modelix.model.api.IProperty
24+
import org.modelix.model.api.IReplaceableNode
2325
import org.modelix.model.api.ITree
2426
import org.modelix.model.api.PBranch
2527
import org.modelix.model.api.getRootNode
@@ -31,8 +33,7 @@ class IncrementalBranchTest {
3133

3234
@Test
3335
fun propertyChange() {
34-
val branch = PBranch(ModelFacade.newLocalTree(), IdGenerator.getInstance(17865))
35-
val incrementalBranch = IncrementalBranch(branch)
36+
val incrementalBranch = initializeIncrementalBranch()
3637
val rootNode = incrementalBranch.getRootNode()
3738
incrementalBranch.runWrite { rootNode.setPropertyValue(IProperty.fromName("name"), "abc") }
3839
val engine = IncrementalEngine()
@@ -57,6 +58,38 @@ class IncrementalBranchTest {
5758
}
5859
}
5960

61+
@Test
62+
fun conceptChange() {
63+
val incrementalBranch = initializeIncrementalBranch()
64+
val root = incrementalBranch.getRootNode() as IReplaceableNode
65+
incrementalBranch.runWrite { root.replaceNode(ConceptReference("myConcept")) }
66+
val engine = IncrementalEngine()
67+
68+
try {
69+
var callCount = 0
70+
val conceptUidWithSuffix = engine.incrementalFunction("f") { _ ->
71+
callCount++
72+
val conceptRef = incrementalBranch.getRootNode().getConceptReference()?.getUID()
73+
conceptRef + "Suffix"
74+
}
75+
assertEquals(callCount, 0)
76+
assertEquals("myConceptSuffix", incrementalBranch.computeRead { conceptUidWithSuffix() })
77+
assertEquals(callCount, 1)
78+
assertEquals("myConceptSuffix", incrementalBranch.computeRead { conceptUidWithSuffix() })
79+
assertEquals(callCount, 1)
80+
incrementalBranch.runWrite {
81+
(incrementalBranch.getRootNode() as IReplaceableNode).replaceNode(
82+
ConceptReference("myConcept2"),
83+
)
84+
}
85+
assertEquals(callCount, 1)
86+
assertEquals("myConcept2Suffix", incrementalBranch.computeRead { conceptUidWithSuffix() })
87+
assertEquals(callCount, 2)
88+
} finally {
89+
engine.dispose()
90+
}
91+
}
92+
6093
@Test
6194
fun addNewChild_modifies_containsNode() {
6295
val childId = 12345L
@@ -76,4 +109,10 @@ class IncrementalBranchTest {
76109
engine.dispose()
77110
}
78111
}
112+
113+
private fun initializeIncrementalBranch(): IncrementalBranch {
114+
val branch = PBranch(ModelFacade.newLocalTree(), IdGenerator.getInstance(17865))
115+
val incrementalBranch = IncrementalBranch(branch)
116+
return incrementalBranch
117+
}
79118
}

0 commit comments

Comments
 (0)