@@ -2,8 +2,11 @@ package org.modelix.model.test
2
2
3
3
import org.modelix.model.api.INode
4
4
import org.modelix.model.api.addNewChild
5
+ import org.modelix.model.api.async.IAsyncNode
6
+ import org.modelix.model.api.async.asAsyncNode
5
7
import org.modelix.model.api.getAncestors
6
- import org.modelix.model.api.getDescendants
8
+ import org.modelix.streams.IStream
9
+ import org.modelix.streams.plus
7
10
import kotlin.random.Random
8
11
9
12
class RandomModelChangeGenerator (val rootNode : INode , private val rand : Random ) {
@@ -91,8 +94,21 @@ class RandomModelChangeGenerator(val rootNode: INode, private val rand: Random)
91
94
}
92
95
93
96
private fun getRandomNode (condition : (INode ) -> Boolean = { true }, includeRoot : Boolean = true): INode ? {
94
- val nodes = rootNode.getDescendants(includeRoot).filter(condition).toList()
95
- if (nodes.isEmpty()) return null
96
- return nodes[rand.nextInt(nodes.size)]
97
+ return rootNode.asAsyncNode()
98
+ .getDescendantsShuffled(rand, includeRoot)
99
+ .map { it.asRegularNode() }
100
+ .filter(condition)
101
+ .firstOrNull()
102
+ .getSynchronous()
103
+ }
104
+ }
105
+
106
+ fun IAsyncNode.getDescendantsShuffled (rand : Random , includeSelf : Boolean ): IStream .Many <IAsyncNode > {
107
+ return getAllChildren().toList().flatMap { children ->
108
+ val children = children.shuffled(rand)
109
+ val splitAt = rand.nextInt(children.size + 1 )
110
+ IStream .many(children.take(splitAt)).flatMap { it.getDescendantsShuffled(rand, true ) } +
111
+ (if (includeSelf) IStream .of(this ) else IStream .empty()) +
112
+ IStream .many(children.drop(splitAt)).flatMap { it.getDescendantsShuffled(rand, true ) }
97
113
}
98
114
}
0 commit comments