-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_generator.py
543 lines (479 loc) · 25.5 KB
/
query_generator.py
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import owlready2
import pandas as pd
import random
import json
def get_configuration(dataset, query_structure, query_structure_length, weights, wanted_structure = None):
df_family = pd.read_csv("dataset/family/abox/transductive/all.txt", delimiter='\t', header=None)
df_dbpedia15k = pd.read_csv("dataset/dbpedia15k/abox/transductive/all.txt", delimiter='\t', header=None)
query_length = random.randint(1,3)
#1p
if query_length == 1:
currenct_structure = query_structure[1]
# 2p, 2i, 2u
elif query_length == 2:
idx = random.choice(query_structure_length[2])
currenct_structure = query_structure[idx]
# 3p, 3i, pi, ip, up
else:
idx = random.choice(query_structure_length[3])
currenct_structure = query_structure[idx]
#Now, we have a randomly selected structure, currenct_structure, where the probability of each length is equal.
if not wanted_structure is None:
currenct_structure = wanted_structure
#weights
concepts, w_c = list(weights[dataset]['concepts'].keys()),list(weights[dataset]['concepts'].values())
roles, w_r = list(weights[dataset]['roles'].keys()),list(weights[dataset]['roles'].values())
#1p - 3 subversions
if currenct_structure == '1p':
#Both concept and roles are compatible
is_concept = bool(random.getrandbits(1))
if is_concept:
random_concept = random.choices(concepts, weights=w_c)[0]
#get answer
return "q(?w) :- " + random_concept + "(?w)"
else:
random_role = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + random_role + "(?w,?x)"
else:
return "q(?w) :- " + random_role + "(?x,?w)"
#2p - 4 subversions
if currenct_structure == '2p':
#first atom has to be a role
first_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
#second atom
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?y,?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?w,?y)"
else:
#second atom
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?y,?w)"
else:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?w,?y)"
#3p - 8 subversions
if currenct_structure == '3p':
#first atom has to be a role
first_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
#third atom
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?y,?z)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?y,?z)^" + third_atom + "(?w,?z)"
else:
#third atom
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?z,?y)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?z,?y)^" + third_atom + "(?w,?z)"
else:
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
#third atom
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?y,?z)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?y,?z)^" + third_atom + "(?w,?z)"
else:
#third atom
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?z,?y)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?z,?y)^" + third_atom + "(?w,?z)"
#2i - 7 subversions
if currenct_structure == '2i':
is_concept = bool(random.getrandbits(1))
#if first atom is a concept
if is_concept:
first_atom = random.choices(concepts, weights=w_c)[0]
# if second atom is a concept
is_concept = bool(random.getrandbits(1))
if is_concept:
second_atom = random.choices(concepts, weights=w_c)[0]
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w)"
# if second atom is a role
else:
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?x,?w)"
#if first atom is a role
else:
first_atom = random.choices(roles, weights=w_r)[0]
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?w)^" + second_atom + "(?y,?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?w)^" + second_atom + "(?w,?y)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w,?x)^" + second_atom + "(?y,?w)"
else:
return "q(?w) :- " + first_atom + "(?w,?x)^" + second_atom + "(?w,?y)"
#3i - 15 subversions
if currenct_structure == '3i':
is_concept = bool(random.getrandbits(1))
#if first atom is a concept
if is_concept:
first_atom = random.choices(concepts, weights=w_c)[0]
# if second atom is a concept
is_concept = bool(random.getrandbits(1))
if is_concept:
second_atom = random.choices(concepts, weights=w_c)[0]
# if third atom is a concept
is_concept = bool(random.getrandbits(1))
if is_concept:
third_atom = random.choices(concepts, weights=w_c)[0]
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w)^" + third_atom + "(?w)"
else:
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w)^" + third_atom + "(?x,?w)"
# if second atom is a role
else:
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w,?y)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?w,?y)^" + third_atom + "(?x,?w)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?y,?w)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?w)^" + second_atom + "(?y,?w)^" + third_atom + "(?x,?w)"
else:
first_atom = random.choices(roles, weights=w_r)[0]
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w,?z)^" + second_atom + "(?w,?y)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?w,?z)^" + second_atom + "(?w,?y)^" + third_atom + "(?x,?w)"
else:
if is_domain:
return "q(?w) :- " + first_atom + "(?w,?z)^" + second_atom + "(?y,?w)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?w,?z)^" + second_atom + "(?y,?w)^" + third_atom + "(?x,?w)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?x) :- " + first_atom + "(?z,?w)^" + second_atom + "(?w,?y)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?z,?w)^" + second_atom + "(?w,?y)^" + third_atom + "(?x,?w)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z,?w)^" + second_atom + "(?y,?w)^" + third_atom + "(?w,?x)"
else:
return "q(?w) :- " + first_atom + "(?z,?w)^" + second_atom + "(?y,?w)^" + third_atom + "(?x,?w)"
#pi - 6 subversions
if currenct_structure == 'pi':
#has to be a role
first_atom = random.choices(roles, weights=w_r)[0]
is_domain = random.choices(roles, weights=w_r)[0]
if is_domain:
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?y,?w)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?y,?w)^" + third_atom + "(?w,?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?w,?y)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?y)^" + second_atom + "(?w,?y)^" + third_atom + "(?w,?z)"
else:
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?y,?w)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?y,?w)^" + third_atom + "(?w,?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?w,?y)^" + third_atom + "(?z,?w)"
else:
return "q(?w) :- " + first_atom + "(?y,?x)^" + second_atom + "(?w,?y)^" + third_atom + "(?w,?z)"
#ip - 14 subversions
if currenct_structure == 'ip':
is_concept = bool(random.getrandbits(1))
if is_concept:
first_atom = random.choices(concepts, weights=w_c)[0]
is_concept = bool(random.getrandbits(1))
if is_concept:
second_atom = random.choices(concepts, weights=w_c)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x)^" + second_atom + "(?x)^" + third_atom + "(?x, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x)^" + second_atom + "(?x)^" + third_atom + "(?w, ?x)"
else:
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z)^" + second_atom + "(?y,?z)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z)^" + second_atom + "(?y,?z)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z)^" + second_atom + "(?z,?y)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z)^" + second_atom + "(?z,?y)^" + third_atom + "(?w, ?z)"
else:
first_atom = random.choices(roles, weights=w_r)[0]
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?z)^" + second_atom + "(?y,?z)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?z)^" + second_atom + "(?y,?z)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?z)^" + second_atom + "(?z,?y)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?z)^" + second_atom + "(?z,?y)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z,?x)^" + second_atom + "(?y,?z)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z,?x)^" + second_atom + "(?y,?z)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z,?x)^" + second_atom + "(?z,?y)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z,?x)^" + second_atom + "(?z,?y)^" + third_atom + "(?w, ?z)"
#up - 14 subversions
if currenct_structure == 'up':
is_concept = bool(random.getrandbits(1))
if is_concept:
first_atom = random.choices(concepts, weights=w_c)[0]
is_concept = bool(random.getrandbits(1))
if is_concept:
second_atom = random.choices(concepts, weights=w_c)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x) | " + second_atom + "(?x)^" + third_atom + "(?x, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x) | " + second_atom + "(?x)^" + third_atom + "(?w, ?x)"
else:
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z) | " + second_atom + "(?y,?z)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z) | " + second_atom + "(?y,?z)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z) | " + second_atom + "(?z,?y)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z) | " + second_atom + "(?z,?y)^" + third_atom + "(?w, ?z)"
else:
first_atom = random.choices(roles, weights=w_r)[0]
second_atom = random.choices(roles, weights=w_r)[0]
third_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?z) | " + second_atom + "(?y,?z)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?z) | " + second_atom + "(?y,?z)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?z) | " + second_atom + "(?z,?y)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?z) | " + second_atom + "(?z,?y)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z,?x) | " + second_atom + "(?y,?z)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z,?x) | " + second_atom + "(?y,?z)^" + third_atom + "(?w, ?z)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?z,?x) | " + second_atom + "(?z,?y)^" + third_atom + "(?z, ?w)"
else:
return "q(?w) :- " + first_atom + "(?z,?x) | " + second_atom + "(?z,?y)^" + third_atom + "(?w, ?z)"
#2u - 7 subversions
if currenct_structure == '2u':
is_concept = bool(random.getrandbits(1))
if is_concept:
first_atom = random.choices(concepts, weights=w_c)[0]
is_concept = bool(random.getrandbits(1))
if is_concept:
second_atom = random.choices(concepts, weights=w_c)[0]
return "q(?w) :- " + first_atom + "(?w) | " + second_atom + "(?w)"
else:
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w) | " + second_atom + "(?x, ?w)"
else:
return "q(?w) :- " + first_atom + "(?w) | " + second_atom + "(?w, ?x)"
else:
first_atom = random.choices(roles, weights=w_r)[0]
second_atom = random.choices(roles, weights=w_r)[0]
is_domain = bool(random.getrandbits(1))
if is_domain:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?x,?w) | " + second_atom + "(?y, ?w)"
else:
return "q(?w) :- " + first_atom + "(?x,?w) | " + second_atom + "(?w, ?y)"
else:
is_domain = bool(random.getrandbits(1))
if is_domain:
return "q(?w) :- " + first_atom + "(?w,?x) | " + second_atom + "(?y, ?w)"
else:
return "q(?w) :- " + first_atom + "(?w,?x) | " + second_atom + "(?w, ?y)"
def get_weights():
# Get the weights from the ABox
df_family = pd.read_csv("dataset/family/abox/transductive/all.txt", delimiter='\t', header=None)
df_dbpedia15k = pd.read_csv("dataset/dbpedia15k/abox/transductive/all.txt", delimiter='\t', header=None)
#Using pandas way, Series.value_counts()
df_family_roles = df_family.iloc[:,1].value_counts()
df_dbpedia15k_roles = df_dbpedia15k.iloc[:,1].value_counts()
#Count concepts in dbpedia15k. That is, do a value count on the objects in the triples where rdf:type is the property.
df_dbpedia15k_rdftype = df_dbpedia15k.loc[df_dbpedia15k[1] == df_dbpedia15k_roles.index.tolist()[0]]
df_dbpedia15k_concepts = df_dbpedia15k_rdftype.iloc[:,2].value_counts()
tbox_family = owlready2.get_ontology("dataset/family/tbox/family_wikidata.owl").load()
tbox_dbpedia15k = owlready2.get_ontology("dataset/dbpedia15k/tbox/dbpedia15k.owl").load()
classes_family = list(tbox_family.classes())
properties_family = list(tbox_family.object_properties())
classes_dbpedia15k = list(tbox_dbpedia15k.classes())
properties_dbpedia15k = list(tbox_dbpedia15k.object_properties())
dbpedia15k_concepts = df_dbpedia15k_concepts.to_dict()
dbpedia15k_roles = df_dbpedia15k_roles.to_dict()
family_classes = dict()
family_roles = df_family_roles.to_dict()
for cl in classes_dbpedia15k:
iri = "<" + cl.iri + ">"
if not iri in dbpedia15k_concepts:
dbpedia15k_concepts[iri] = 1
for pr in properties_dbpedia15k:
iri = "<" + pr.iri + ">"
if not iri in dbpedia15k_roles:
dbpedia15k_roles[iri] = 1
for cl in classes_family:
iri = "<" + cl.iri + ">"
if not iri in family_classes:
family_classes[iri] = 1
for pr in properties_family:
iri = "<" + pr.iri + ">"
if not iri in family_roles:
family_roles[iri] = 1
#Removing RDF-type from query-structures
del dbpedia15k_roles['<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>']
del dbpedia15k_roles['<http://www.w3.org/2000/01/rdf-schema#seeAlso>']
return {'dbpedia15k':
{'concepts': dbpedia15k_concepts, 'roles': dbpedia15k_roles},
'family':
{'concepts': family_classes, 'roles': family_roles}
}
def query_gen(testcase, number_of_queries_per_structure):
"""
Generate queries for the given testcase and number of queries per structure.
Args:
testcase (str): The name of the testcase.
number_of_queries_per_structure (int): The number of queries to generate for each query structure.
Returns:
None. Writes to file.
"""
# Initialize variables
query_structure = {1: '1p', 2: '2p', 3: '3p', 4: '2i', 5: '3i', 6: 'pi', 7: 'ip', 8: 'up', 9: '2u'}
query_length = {1: [1], 2: [2, 4, 9], 3: [3, 5, 6, 7, 8]}
queries = dict()
weights = get_weights()
datasets = ['family', 'dbpedia15k']
# Generate queries for each dataset and structure
for ds in datasets:
for structure in query_structure.values():
i = 0
temp_list = list()
while (i < number_of_queries_per_structure):
# Get a query configuration and append it to the temporary list
temp_list.append(get_configuration(ds, query_structure, query_length, weights, structure))
i += 1
queries[structure] = temp_list
# Write the queries to a file
file_name = "testcases/" + testcase + "/queries/" + ds + '/queries_' + "k-" + str(number_of_queries_per_structure) + '.txt'
with open(file_name, 'w') as convert_file:
convert_file.write(json.dumps(queries, indent=2))