forked from jeffwheeler/hsparql
-
Notifications
You must be signed in to change notification settings - Fork 18
/
QueryGeneratorTest.hs
589 lines (465 loc) · 13.9 KB
/
QueryGeneratorTest.hs
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE PostfixOperators #-}
module Database.HSparql.QueryGeneratorTest ( testSuite ) where
import Test.Framework (testGroup, Test)
import Test.Framework.Providers.HUnit
import Test.HUnit
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.String.QQ as Str
import Database.HSparql.QueryGenerator
class CreateQuery a where
createQuery :: a -> Text
instance CreateQuery (Query SelectQuery) where
createQuery = T.pack . createSelectQuery
instance CreateQuery (Query ConstructQuery) where
createQuery = T.pack . createConstructQuery
instance CreateQuery (Query AskQuery) where
createQuery = T.pack . createAskQuery
instance CreateQuery (Query UpdateQuery) where
createQuery = T.pack . createUpdateQuery
instance CreateQuery (Query DescribeQuery) where
createQuery = T.pack . createDescribeQuery
normalizeWhitespace :: Text -> Text
normalizeWhitespace = T.strip
. T.replace " " " "
. T.replace " " " "
. T.replace " " " "
. T.replace "\n" " "
queryTexts :: [(Text, Text)]
queryTexts =
[ ( [Str.s|
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x1 ?x2
WHERE {
?x0 dbprop:genre dbpedia:Web_browser .
?x0 foaf:name ?x1 .
?x0 foaf:page ?x2 .
}
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbpprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
x <- var
name <- var
page <- var
triple_ x (dbpprop .:. "genre") (resource .:. "Web_browser")
triple_ x (foaf .:. "name") name
triple_ x (foaf .:. "page") page
selectVars [name, page]
)
, ( [Str.s|
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX example: <http://www.example.com/>
CONSTRUCT {
?x0 example:hasName ?x1 .
}
WHERE {
?x0 dbprop:genre dbpedia:Web_browser .
?x0 foaf:name ?x1 .
?x0 foaf:page ?x2 .
}
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbpprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
example <- prefix "example" (iriRef "http://www.example.com/")
x <- var
name <- var
page <- var
construct <- constructTriple x (example .:. "hasName") name
triple_ x (dbpprop .:. "genre") (resource .:. "Web_browser")
triple_ x (foaf .:. "name") name
triple_ x (foaf .:. "page") page
return ConstructQuery { queryConstructs = [construct] }
)
, ( [Str.s|
PREFIX dbpedia: <http://dbpedia.org/resource/> DESCRIBE dbpedia:Edinburgh WHERE { }
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
uri <- describeIRI (resource .:. "Edinburgh")
return DescribeQuery { queryDescribe = uri }
)
, ( [Str.s|
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
ASK {
?x0 dbprop:genre dbpedia:Web_browser .
}
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
x <- var
ask <- askTriple x (dbprop .:. "genre") (resource .:. "Web_browser")
return AskQuery { queryAsk = [ask] }
)
-- https://github.com/robstewart57/hsparql/pull/31
, ( [Str.s|
PREFIX : <http://example1.com/>
PREFIX ex: <http://example2.com/>
SELECT ?x0 WHERE {
?x0 :property1 :X .
FILTER ((?x1>(10))&&(?x1<(20))) .
{
SELECT ((AVG(?x0_0)) AS ?x1)
WHERE {
?x0 :property2 [ex:property3 ?x0_0] .
}
}
}
|]
, createQuery $ do
p1 <- prefix "" (iriRef "http://example1.com/")
s <- var
v <- var
triple_ s (p1 .:. "property1") (p1 .:. "X")
filterExpr_ $ (v .>. (10::Integer)) .&&. (v .<. (20::Integer))
subQuery_ $ do
p2 <- prefix "ex" (iriRef "http://example2.com/")
v' <- var
triple_ s (p1 .:. "property2") [mkPredicateObject (p2 .:. "property3") v']
select [avg v' `as` v]
selectVars [s]
)
-- https://www.w3.org/TR/sparql11-query/#OptionalMatching
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x1 ?x2
WHERE {
?x0 foaf:name ?x1 .
OPTIONAL { ?x0 foaf:mbox ?x2 . }
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
x <- var
name <- var
mbox <- var
triple_ x (foaf .:. "name") name
optional_ $ do
triple_ x (foaf .:. "mbox") mbox
selectVars [name, mbox]
)
-- https://www.w3.org/TR/sparql11-query/#alternatives
, ( [Str.s|
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?x1
WHERE { { ?x0 dc10:title ?x1 . } UNION { ?x0 dc11:title ?x1 . } }
|]
, createQuery $ do
dc10 <- prefix "dc10" (iriRef "http://purl.org/dc/elements/1.0/")
dc11 <- prefix "dc11" (iriRef "http://purl.org/dc/elements/1.1/")
book <- var
title <- var
let tt10 = triple_ book (dc10 .:. "title") title
tt11 = triple_ book (dc11 .:. "title") title
in union_ tt10 tt11
selectVars [title]
)
-- https://www.w3.org/TR/sparql11-query/#neg-notexists
, ( [Str.s|
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0
WHERE
{
?x0 rdf:type foaf:Person .
FILTER NOT EXISTS { ?x0 foaf:name ?x1 . }
}
|]
, createQuery $ do
rdf <- prefix "rdf" (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
person <- var
name <- var
triple_ person (rdf .:. "type") (foaf .:. "Person")
filterNotExists_ $ do
triple_ person (foaf .:. "name") name
selectVars [person]
)
, ( [Str.s|
PREFIX ex: <http://example.com/>
SELECT ?x0 ?x1
WHERE
{
?x0 ex:foo%20bar ?x1 .
}
|]
, createQuery $ do
ex <- prefix "ex" (iriRef "http://example.com/")
s <- var
o <- var
triple_ s (ex .:. "foo bar") o
selectVars [s, o]
)
-- Count distinct subjects and objects query
, ( [Str.s|
SELECT ((COUNT(?x0)) AS ?x4)
WHERE
{
{
SELECT DISTINCT ?x0 WHERE {
{ ?x0 ?x2 ?x3 . } UNION
{ ?x1 ?x2 ?x0 . }
}
}
}
|]
, createQuery $ do
r <- var
s <- var
p <- var
o <- var
subQuery_ $ do
let tt1 = triple_ r p o
tt2 = triple_ s p r
in do
distinct_
union_ tt1 tt2
selectVars [r]
countVar <- var
select [count r `as` countVar]
)
-- Count number of instances of each class in the dataset.
, ( [Str.s|
SELECT ?x1 ((COUNT(?x0)) AS ?x2) WHERE {
?x0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?x1 .
} GROUP BY ?x1
|]
, createQuery $ do
s <- var
o <- var
triple_ s (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") o
groupBy_ o
countVar <- var
select [SelectVar o, count s `as` countVar]
)
-- Count number of resources typed with a class from Wikidata.
, ( [Str.s|
SELECT ((COUNT(?x0)) AS ?x2) WHERE {
?x0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?x1 .
FILTER (CONTAINS((STR(?x1)), "http://www.wikidata.org/entity")) .
}
|]
, createQuery $ do
s <- var
o <- var
triple_ s (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") o
filterExpr_ $ contains (str o) ("http://www.wikidata.org/entity" :: Text)
countVar <- var
select [count s `as` countVar]
)
-- Create a federated query
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x1 WHERE {
<http://example.org/myfoaf/I> foaf:knows ?x0 .
SERVICE <http://people.example.org/sparql> {
?x0 foaf:name ?x1 .
}
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
person <- var
name <- var
triple_ (iriRef "http://example.org/myfoaf/I") (foaf .:. "knows") person
service_ (iriRef "http://people.example.org/sparql") $ do
triple_ person (foaf .:. "name") name
selectVars [name]
)
-- Create query containing property paths
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0 ?x1 WHERE {
?x0 ((foaf:knows/foaf:knows)/foaf:name) ?x1 .
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
s <- var
o <- var
let foafKnows = foaf .:. "knows"
let foafName = foaf .:. "name"
triple_ s (foafKnows .//. foafKnows .//. foafName) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0 ?x1 WHERE {
?x0 (^foaf:mbox) ?x1 .
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
s <- var
o <- var
let foafMbox = foaf .:. "mbox"
triple_ s (inv foafMbox) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0 ?x1 WHERE {
?x0 (foaf:knows/(^foaf:knows)) ?x1 .
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
s <- var
o <- var
let foafKnows = foaf .:. "knows"
triple_ s (foafKnows .//. inv foafKnows) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0 ?x1 WHERE {
?x0 (foaf:knows/(^foaf:knows)) ?x1 .
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
s <- var
o <- var
let foafKnows = foaf .:. "knows"
triple_ s (foafKnows .//. inv foafKnows) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0 ?x1 WHERE {
?x0 ((foaf:knows+)/foaf:name) ?x1 .
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
s <- var
o <- var
let foafKnows = foaf .:. "knows"
let foafName = foaf .:. "name"
triple_ s ((foafKnows +.) .//. foafName) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX ex: <http://example.com/>
SELECT ?x0 ?x1 WHERE {
?x0 ((ex:motherOf|ex:fatherOf)+) ?x1 .
}
|]
, createQuery $ do
ex <- prefix "ex" (iriRef "http://example.com/")
s <- var
o <- var
let exMotherOf = ex .:. "motherOf"
let exFatherOf = ex .:. "fatherOf"
triple_ s ((exMotherOf .|. exFatherOf) +.) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX ex: <http://example.com/>
SELECT ?x0 ?x1 WHERE {
?x0 ((ex:motherOf|ex:fatherOf)+) ?x1 .
}
|]
, createQuery $ do
ex <- prefix "ex" (iriRef "http://example.com/")
s <- var
o <- var
let exMotherOf = ex .:. "motherOf"
let exFatherOf = ex .:. "fatherOf"
triple_ s ((exMotherOf .|. exFatherOf) +.) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?x0 ?x1 WHERE {
?x0 (rdf:type/(rdfs:subClassOf*)) ?x1 .
}
|]
, createQuery $ do
rdf <- prefix "rdf" (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
rdfs <- prefix "rdfs" (iriRef "http://www.w3.org/2000/01/rdf-schema#")
s <- var
o <- var
let rdfType = rdf .:. "type"
let rdfsSubClassOf = rdfs .:. "subClassOf"
triple_ s (rdfType .//. (rdfsSubClassOf *.)) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?x0 ?x1 WHERE {
?x0 (a/(rdfs:subClassOf*)) ?x1 .
}
|]
, createQuery $ do
rdfs <- prefix "rdfs" (iriRef "http://www.w3.org/2000/01/rdf-schema#")
s <- var
o <- var
let rdfsSubClassOf = rdfs .:. "subClassOf"
triple_ s (a .//. (rdfsSubClassOf *.)) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?x0 ?x1 WHERE {
?x0 (a/(rdfs:subClassOf?)) ?x1 .
}
|]
, createQuery $ do
rdfs <- prefix "rdfs" (iriRef "http://www.w3.org/2000/01/rdf-schema#")
s <- var
o <- var
let rdfsSubClassOf = rdfs .:. "subClassOf"
triple_ s (a .//. (rdfsSubClassOf ?.)) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?x0 ?x1 WHERE {
?x0 !(rdf:type|rdfs:subClassOf|a) ?x1 .
}
|]
, createQuery $ do
rdf <- prefix "rdf" (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
rdfs <- prefix "rdfs" (iriRef "http://www.w3.org/2000/01/rdf-schema#")
s <- var
o <- var
let rdfType = rdf .:. "type"
let rdfsSubClassOf = rdfs .:. "subClassOf"
triple_ s (neg $ rdfType ..|.. rdfsSubClassOf ..|.. a) o
selectVars [s, o]
)
, ( [Str.s|
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?x0 ?x1 WHERE {
?x0 !(rdf:type|^rdf:type) ?x1 .
}
|]
, createQuery $ do
rdf <- prefix "rdf" (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
s <- var
o <- var
let rdfType = rdf .:. "type"
triple_ s (neg $ rdfType ..|.. inv' rdfType) o
selectVars [s, o]
)
]
testSuite :: [Test.Framework.Test]
testSuite = [
testGroup "Database.HSparql.QueryGenerator tests" $ (`map` queryTexts) $
\(expected, actual) -> testCase (T.unpack expected) $ do
assertEqual "" (normalizeWhitespace expected) (normalizeWhitespace actual)
]