-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonScenarios.kt
343 lines (299 loc) · 13.2 KB
/
CommonScenarios.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
@file:Suppress("SpellCheckingInspection")
package com.github.sszuev.graphs.scenarious
import com.github.sszuev.graphs.smallGraph
import com.github.sszuev.graphs.testutils.assertEquals
import com.github.sszuev.graphs.testutils.assertFalse
import com.github.sszuev.graphs.testutils.assertSingleOrEmpty
import com.github.sszuev.graphs.testutils.assertTrue
import com.github.sszuev.graphs.testutils.create
import com.github.sszuev.graphs.testutils.createTriple
import com.github.sszuev.graphs.testutils.statements
import com.github.sszuev.graphs.testutils.threadLocalRandom
import com.github.sszuev.graphs.testutils.toSet
import org.apache.jena.graph.Graph
import org.apache.jena.graph.Node
import org.apache.jena.graph.NodeFactory
import org.apache.jena.graph.Triple
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.vocabulary.OWL
import org.apache.jena.vocabulary.RDF
import org.junit.jupiter.api.Assertions
import java.util.concurrent.ThreadLocalRandom
import kotlin.streams.toList
private val testSubject = NodeFactory.createURI("https://ex.com#test-subject")
private val testPredicate = NodeFactory.createURI("https://ex.com#test-predicate")
private val testObject = NodeFactory.createURI("https://ex.com#test-object")
internal fun scenarioC_modifyAndRead(graph: Graph) {
smallGraph.find().forEach {
graph.add(it)
graph.find(it.subject, Node.ANY, Node.ANY).toSet()
graph.find(Node.ANY, it.predicate, Node.ANY).toSet()
graph.find(Node.ANY, it.predicate, it.`object`).toSet()
}
smallGraph.find().forEach {
graph.delete(it)
}
smallGraph.find().forEach {
graph.find(it.subject, Node.ANY, it.`object`).toSet()
graph.find(it.subject, it.predicate, Node.ANY).toSet()
graph.find(Node.ANY, Node.ANY, it.`object`).toSet()
}
}
internal fun scenarioF_modifyAndRead(graph: Graph) {
val ns = "http://ex#"
val model = ModelFactory.createModelForGraph(graph)
repeat(2) {
repeat(4) { i ->
model.statements(ns + "s$i", null, null).filter {
it.predicate.uri == ns + "p7"
}.count()
}
val triples = (1..4).map { s ->
(1..4).map { p ->
(1..4).map { o ->
model.create(ns + "s$s", ns + "y$p", ns + "f$o")
}
}.flatten()
}.flatten()
repeat(4) { i ->
model.statements(null, ns + "p$i", null).map {
it.subject
}.count()
}
model.remove(triples)
repeat(4) { s ->
repeat(4) { p ->
repeat(4) { o ->
val t = model.create(ns + "s$s", ns + "y$p", ns + "f$o")
model.statements(ns + "s$s").toList()
model.remove(t)
}
}
}
}
}
internal fun scenarioE_modifyAndRead(
graph: Graph,
getTestData: () -> ReadOperationsTestData,
minSize: Int,
maxSize: Int,
numTriplesToCreate: Int,
numTriplesToDelete: Int
) {
val newTriples = (1..numTriplesToCreate).map {
scenarioE_read(graph, getTestData(), minSize, maxSize)
when (ThreadLocalRandom.current().nextInt(4)) {
0 -> createTriple()
1 -> createTriple(subject = testSubject)
2 -> createTriple(predicate = testPredicate)
3 -> createTriple(value = testObject)
else -> throw IllegalStateException()
}.also {
graph.add(it)
}
}.toMutableList()
repeat(numTriplesToDelete) {
scenarioE_read(graph, getTestData(), minSize, maxSize)
val t1 = createTriple()
graph.remove(t1.subject, t1.predicate, t1.`object`)
scenarioE_read(graph, getTestData(), minSize, maxSize)
val t2 = newTriples.removeAt(0)
graph.delete(t2)
}
scenarioE_read(graph, getTestData(), minSize, maxSize)
}
internal fun scenarioE_modify(
graph: Graph,
numTriplesToCreate: Int,
numTriplesToDelete: Int
) {
val newTriples = (1..numTriplesToCreate).map {
when (ThreadLocalRandom.current().nextInt(4)) {
0 -> createTriple()
1 -> createTriple(subject = testSubject)
2 -> createTriple(predicate = testPredicate)
3 -> createTriple(value = testObject)
else -> throw IllegalStateException()
}.also {
graph.add(it)
}
}.toMutableList()
repeat(numTriplesToDelete) {
val t1 = createTriple()
graph.remove(t1.subject, t1.predicate, t1.`object`)
val t2 = newTriples.removeAt(0)
graph.delete(t2)
}
}
internal fun scenarioE_read(
graph: Graph,
testData: ReadOperationsTestData,
minSize: Int,
maxSize: Int,
) {
assertTrue(graph.size() in minSize..maxSize) { "actual graph size: ${graph.size()}, bounds = [$maxSize .. $maxSize]" }
val findAllToList = graph.find().toList()
assertTrue(findAllToList.size in minSize..maxSize) { "findAllCount ${findAllToList.size}, bounds = [$maxSize .. $maxSize]" }
val streamAllToSet = graph.stream().toSet()
assertTrue(streamAllToSet.size in minSize..maxSize) { "streamAllCount ${streamAllToSet.size}, bounds = [$maxSize .. $maxSize]" }
if (minSize > 0) {
assertFalse(graph.isEmpty)
}
assertEquals(testData.containsTriple1Expected, graph.contains(testData.containsTriple1Given))
assertEquals(testData.containsTriple2Expected, graph.contains(testData.containsTriple2Given))
assertEquals(testData.containsTriple3Expected, graph.contains(testData.containsTriple3Given))
Assertions.assertEquals(
testData.findTripleAsSequenceItemExpected,
graph.find(testData.findTripleAsSequenceItemGiven).asSequence()
.assertSingleOrEmpty(testData.findTripleAsSequenceItemExpected != null)
)
Assertions.assertEquals(0, graph.find(testData.findTripleAsSequenceExpectedEmpty).asSequence().count())
assertEquals(
testData.findSPOToListExpected,
graph.find(
testData.findSPOToListGiven.subject,
testData.findSPOToListGiven.predicate,
testData.findSPOToListGiven.`object`
).toList()
)
assertEquals(
testData.findSPOByPredicateToSetExpected,
graph.find(null, testData.findSPOByPredicateToSetGiven, null).toSet()
)
assertEquals(
testData.findSPOBySubjectToSetExpected,
graph.find(testData.findSPOBySubjectToSetGiven, null, null).toSet()
)
assertEquals(
testData.findSPOBySubjectAndPredicateToSetExpected,
graph.find(
testData.findSPOBySubjectAndPredicateToSetGiven.subject,
testData.findSPOBySubjectAndPredicateToSetGiven.predicate,
null
).toSet()
)
val findSPOByPredicateIteratorExpected = testData.findSPOByPredicateIteratorExpected
val findSPOByPredicateIterator = graph.find(null, testData.findSPOByPredicateIteratorGiven, null)
if (findSPOByPredicateIteratorExpected.isEmpty()) {
assertFalse(findSPOByPredicateIterator.hasNext())
} else {
val findSPOByPredicateIteratorActual = mutableSetOf<Triple>()
while (findSPOByPredicateIterator.hasNext()) {
findSPOByPredicateIteratorActual.add(findSPOByPredicateIterator.next())
}
assertEquals(findSPOByPredicateIteratorExpected, findSPOByPredicateIteratorActual)
}
assertEquals(
testData.findTripleMaskWithSubjectAndObjectToListExpected,
graph.find(testData.findTripleMaskWithSubjectAndObjectToListGiven).toList()
)
assertEquals(
testData.streamSPOToListExpected,
graph.stream(
testData.streamSPOToListGiven.subject,
testData.streamSPOToListGiven.predicate,
testData.streamSPOToListGiven.`object`
).toList()
)
assertEquals(
testData.streamSPOByPredicateToSetExpected,
graph.stream(null, testData.streamSPOByPredicateToSetGiven, null).toSet()
)
assertEquals(
testData.streamSPOByObjectToSetExpected,
graph.stream(null, null, testData.streamSPOByObjectToSetGiven).toSet()
)
assertEquals(
testData.streamSPOByPredicateAndObjectToSetExpected,
graph.stream(
null,
testData.streamSPOByPredicateAndObjectToSetGiven.predicate,
testData.streamSPOByPredicateAndObjectToSetGiven.`object`
).toSet()
)
assertEquals(
testData.countSPOAnonymousOWLClassesExpected,
graph.find(null, RDF.type.asNode(), OWL.Class.asNode()).filterKeep { it.subject.isBlank }.mapWith { 1 }
.toList().size,
)
assertEquals(
testData.countSPOAnonymousOWLClassesExpected,
graph.stream(null, RDF.type.asNode(), OWL.Class.asNode()).filter { it.subject.isBlank }.map { it.subject }
.count()
.toInt(),
)
val findTripleMaskWithTestSubjectIterator = graph.find(Triple.create(testSubject, Node.ANY, Node.ANY))
while (findTripleMaskWithTestSubjectIterator.hasNext()) {
findTripleMaskWithTestSubjectIterator.next()
}
val findTripleMaskWithTestPredicateIterator = graph.find(Triple.create(Node.ANY, testPredicate, Node.ANY))
while (findTripleMaskWithTestPredicateIterator.hasNext()) {
findTripleMaskWithTestPredicateIterator.next()
}
val findSPUByTestObjectIterator = graph.find(null, null, testObject)
while (findSPUByTestObjectIterator.hasNext()) {
findSPUByTestObjectIterator.next()
}
val findAllIterator = graph.find()
var findAllIteratorCount = 0
while (findAllIterator.hasNext()) {
findAllIterator.next()
findAllIteratorCount++
}
assertTrue(findAllIteratorCount in minSize..maxSize) { "findAllIterator ${findAllIteratorCount}, bounds = [$maxSize .. $maxSize]" }
}
internal data class ReadOperationsTestData(val triples: List<Triple>) {
constructor(graph: Graph) : this(graph.find().toList())
val containsTriple1Given: Triple = createTriple()
val containsTriple1Expected = false
val containsTriple2Given: Triple = getOrFindRandomTriple()
val containsTriple2Expected = triples.isNotEmpty()
val containsTriple3Given: Triple =
Triple.create(
NodeFactory.createBlankNode(),
getOrFindRandomTriple().predicate,
getOrFindRandomTriple().`object`
)
val containsTriple3Expected = false
val findTripleAsSequenceItemGiven: Triple = getOrFindRandomTriple()
val findTripleAsSequenceItemExpected: Triple? = if (triples.isEmpty()) null else findTripleAsSequenceItemGiven
val findTripleAsSequenceExpectedEmpty = createTriple()
val findSPOToListGiven: Triple = getOrFindRandomTriple()
val findSPOToListExpected = if (triples.isEmpty()) emptyList() else listOf(findSPOToListGiven)
val findSPOByPredicateToSetGiven: Node = getOrFindRandomTriple().predicate
val findSPOByPredicateToSetExpected =
triples.filter { findSPOByPredicateToSetGiven == it.predicate }.toSet()
val findSPOBySubjectToSetGiven: Node = getOrFindRandomTriple().subject
val findSPOBySubjectToSetExpected = triples.filter { findSPOBySubjectToSetGiven == it.subject }.toSet()
val findSPOBySubjectAndPredicateToSetGiven: Triple =
getOrFindRandomTriple().let { Triple.create(it.subject, it.predicate, Node.ANY) }
val findSPOBySubjectAndPredicateToSetExpected = triples.filter {
findSPOBySubjectAndPredicateToSetGiven.subject == it.subject && findSPOBySubjectAndPredicateToSetGiven.predicate == it.predicate
}.toSet()
val findSPOByPredicateIteratorGiven: Node = getOrFindRandomTriple().predicate
val findSPOByPredicateIteratorExpected =
triples.filter { findSPOByPredicateIteratorGiven == it.predicate }.toSet()
val findTripleMaskWithSubjectAndObjectToListGiven: Triple =
getOrFindRandomTriple().let { Triple.create(it.subject, Node.ANY, it.`object`) }
val findTripleMaskWithSubjectAndObjectToListExpected = triples.filter {
findTripleMaskWithSubjectAndObjectToListGiven.subject == it.subject && findTripleMaskWithSubjectAndObjectToListGiven.`object` == it.`object`
}.toList()
val streamSPOToListGiven: Triple = getOrFindRandomTriple()
val streamSPOToListExpected = if (triples.isEmpty()) emptyList() else listOf(streamSPOToListGiven)
val streamSPOByPredicateToSetGiven: Node = getOrFindRandomTriple().predicate
val streamSPOByPredicateToSetExpected =
triples.filter { streamSPOByPredicateToSetGiven == it.predicate }.toSet()
val streamSPOByObjectToSetGiven: Node = getOrFindRandomTriple().`object`
val streamSPOByObjectToSetExpected =
triples.filter { streamSPOByObjectToSetGiven == it.`object` }.toSet()
val streamSPOByPredicateAndObjectToSetGiven: Triple =
getOrFindRandomTriple().let { Triple.create(Node.ANY, it.predicate, it.`object`) }
val streamSPOByPredicateAndObjectToSetExpected = triples.filter {
streamSPOByPredicateAndObjectToSetGiven.predicate == it.predicate && streamSPOByPredicateAndObjectToSetGiven.`object` == it.`object`
}.toSet()
val countSPOAnonymousOWLClassesExpected =
triples.count { it.predicate == RDF.type.asNode() && it.`object` == OWL.Class.asNode() && it.subject.isBlank }
private fun getOrFindRandomTriple(): Triple {
return if (triples.isEmpty()) createTriple() else triples.threadLocalRandom()
}
}