-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbritish-royal-family-custom-rules.sql
498 lines (388 loc) · 14.5 KB
/
british-royal-family-custom-rules.sql
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
SPARQL DROP SPIN LIBRARY <urn:spin:nanotation:demo:royal:family:lib> ;
SPARQL CLEAR GRAPH <urn:spin:nanotation:demo:royal:family>
;
SPARQL
PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#>
PREFIX schema: <http://schema.org/>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
INSERT
{
## Custom Entity Relationship Types (Relations) ##
<#hasRelative>
a rdf:Property ;
rdfs:label "hasRelative" ;
rdfs:comment """Generic Family Relationship""" ;
rdfs:domain foaf:Person ;
rdfs:range <#MalePerson>, <#FemalePerson>, <#RoyalPerson>, foaf:Person, schema:Person ;
wdrs:describedby <#> .
<#hasUncle>
a rdf:Property ;
rdfs:subPropertyOf <#hasRelative> ;
rdfs:label "hasUncle" ;
rdfs:comment """Relationship Type that's derived from a SPARQL-based Custom Inference Rule
that associates a Person with a Male Person that's a sibling of one of their Parents.
""" ;
rdfs:domain foaf:Person ;
rdfs:range <#MalePerson> ;
wdrs:describedby <#> .
<#hasAuntie>
a rdf:Property ;
rdfs:subPropertyOf <#hasRelative> ;
rdfs:label "hasAuntie" ;
rdfs:comment """Relationship Type that's derived from a SPARQL-based Custom Inference Rule
that associates a Person with a Female Person that's a sibling of one of their Parents.
""" ;
rdfs:domain foaf:Person ;
rdfs:range <#FemalePerson> ;
wdrs:describedby <#> .
<#hasCousin>
a rdf:Property ;
rdfs:subPropertyOf <#hasRelative> ;
rdfs:label "hasCousin" ;
rdfs:comment """Relationship Type that's derived from a SPARQL-based Custom Inference Rule
that associates a Person with another Person; that's a child of the sibling of one of their Parents.
""" ;
rdfs:domain foaf:Person ;
rdfs:range <#FemalePerson>, <#MalePerson> ;
wdrs:describedby <#> .
## Custom Entity Types (Classes) ##
<#FemalePerson>
rdfs:subClassOf foaf:Person ;
rdfs:label "Female Person Class" ;
rdfs:comment """
A female person that inherits properties of the Person class identified by relative URI: foaf:Person.
The properties of this class are defined using custom inference rules using SPARQL via .
""" ;
wdrs:describedby <#> .
<#MalePerson>
rdfs:subClassOf foaf:Person ;
rdfs:label "Male Person Class" ;
rdfs:comment """
A male person that inherits properties of the Person class identified by relative URI: foaf:Person.
The properties of this class are defined using custom inference rules using SPARQL via .
""" ;
wdrs:describedby <#> .
## Entity Relationships ##
<http://dbpedia.org/resource/Prince_William_of_Wales>
a foaf:Person, <#MalePerson> ;
schema:name "Prince William" ;
rel:siblingOf <http://dbpedia.org/resource/Prince_Harry_of_Wales> .
<http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
a foaf:Person, <#FemalePerson> ;
schema:name "Queen Mother" ;
rel:parentOf <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> .
<http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>
a foaf:Person, <#FemalePerson> ;
schema:name "Queen Elizabeth II" ;
rel:parentOf <http://dbpedia.org/resource/Charles,_Prince_of_Wales>,
<http://dbpedia.org/resource/Anne,_Princess_Royal>,
<http://dbpedia.org/resource/Prince_Andrew,_Duke_of_York>,
<http://dbpedia.org/resource/Prince_Edward,_Earl_of_Wessex> ;
rel:siblingOf <http://dbpedia.org/resource/Princess_Margaret,_Countess_of_Snowdon> .
<http://dbpedia.org/resource/Charles,_Prince_of_Wales>
a foaf:Person, <#MalePerson> ;
schema:name "Prince Charles" ;
rel:parentOf <http://dbpedia.org/resource/Prince_William_of_Wales> ,
<http://dbpedia.org/resource/Prince_Harry_of_Wales> .
<http://dbpedia.org/resource/Princess_Margaret,_Countess_of_Snowdon>
a foaf:Person, <#FemalePerson> ;
schema:name "Princess Margaret" .
<http://dbpedia.org/resource/Anne,_Princess_Royal>
a foaf:Person, <#FemalePerson> ;
schema:name "Princess Anne" ;
rel:parentOf <http://dbpedia.org/resource/Peter_Phillips>, <http://dbpedia.org/resource/Zara_Phillips> .
<http://dbpedia.org/resource/Zara_Phillips>
a foaf:Person, <#FemalePerson> ;
schema:name "Princess Zara Phillips" .
<http://dbpedia.org/resource/Princess_Beatrice_of_York>
a foaf:Person, <#FemalePerson> ;
schema:name "Princess Beatrice" .
<http://dbpedia.org/resource/Princess_Eugenie_of_York>
a foaf:Person, <#FemalePerson> ;
schema:name "Princess Eugenie" .
<http://dbpedia.org/resource/Prince_Andrew,_Duke_of_York>
a foaf:Person, <#MalePerson> ;
schema:name "Prince Andrew" ;
rel:parentOf <http://dbpedia.org/resource/Princess_Eugenie_of_York>,
<http://dbpedia.org/resource/Princess_Beatrice_of_York> .
<http://dbpedia.org/resource/Prince_Edward,_Earl_of_Wessex>
a foaf:Person, <#MalePerson> ;
schema:name "Prince Edward" .
<http://dbpedia.org/resource/Prince_Harry_of_Wales>
a foaf:Person, <#MalePerson> ;
schema:name "Prince Harry" .
<http://dbpedia.org/resource/Peter_Phillips>
a foaf:Person, <#MalePerson> ;
schema:name "Prince Peter Phillips" .
} ;
SPARQL
prefix foaf: <http://xmlns.com/foaf/0.1/>
WITH <urn:spin:nanotation:demo:royal:family>
INSERT { ?s a <#RoyalPerson> } where { ?s a foaf:Person }
;
-- Class Definition that includes SPIN Rules
SPARQL CLEAR GRAPH <urn:spin:nanotation:demo:royal:family:lib> ;
TTLP ('
## Collection of Inference Rules that describe
## various British Royal Family Relationship Types
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix sp: <http://spinrdf.org/sp#> .
@prefix spl: <http://spinrdf.org/spl#> .
@prefix spin: <http://spinrdf.org/spin#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://purl.org/vocab/relationship/> .
@prefix wdrs: <http://www.w3.org/2007/05/powder-s#> .
@prefix schema: <http://schema.org/> .
<#RoyalPerson>
a rdfs:Class ;
rdfs:label "A Royal Person"^^xsd:string ;
rdfs:subClassOf foaf:Person, schema:Person ;
spin:rule
[ a sp:Construct ;
sp:text """
## A Royal Person must be related to Queen Elizabeth II as a Descendant, Ancestor, or Sibling
CONSTRUCT { ?n a ?this . }
WHERE {
{ ?n rel:descendantOf+|rel:ancestorOf+|rel:siblingOf|^rel:siblingOf <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>.
}
}
"""
] ;
spin:rule
[ a sp:Construct ;
sp:text """
## Grand Parent Relationship Type
CONSTRUCT { ?this <http://purl.org/vocab/relationship/grandParent> ?x }
WHERE {
{ ?this <http://purl.org/vocab/relationship/parentOf> ?parent .
?parent <http://purl.org/vocab/relationship/parentOf> ?x . }
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Ancestor Relationship Type
CONSTRUCT { ?this rel:ancestorOf ?n }
WHERE {
{ ?this rel:parentOf+|^rel:descendantOf ?n . }
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Descendant Relationship Type
CONSTRUCT { ?this rel:descendantOf ?n }
WHERE {
{ ?this ^rel:parentOf+ ?n . }
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Grand Sibling Relationship Type
CONSTRUCT { ?this rel:siblingOf ?n }
WHERE {
{ [] rel:parentOf ?this, ?n.
FILTER (?n != ?this) .
}
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Auntie Relationship Type
CONSTRUCT { ?this <#hasAuntie> ?n }
WHERE {
[] rel:parentOf ?n , ?prnt .
?prnt rel:parentOf ?this .
FILTER (?n != ?prnt)
?n a <#FemalePerson> .
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Uncle Relationship Type
CONSTRUCT { ?this <#hasUncle> ?n }
WHERE
{
[] a <#RoyalPerson> ;
rel:parentOf ?this ;
rel:siblingOf ?n .
?n a <#MalePerson> .
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Cousin Relationship Type
CONSTRUCT { ?this <#hasCousin> ?n }
WHERE {
[] a <#RoyalPerson> ;
rel:parentOf ?this ;
rel:siblingOf ?n .
?n a <#MalePerson> .
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Uncle Relationship Type 2 -- more specific
CONSTRUCT { ?this <#hasUncle2> ?n }
WHERE
{
[] rel:parentOf ?n , ?prnt .
?prnt rel:parentOf ?this .
FILTER (?n != ?prnt)
?n a <#MalePerson> .
}
"""
] ;
spin:rule [ a sp:Construct ;
sp:text """
## Cousin Relationship Type 2 -- more specific
CONSTRUCT { ?this <#hasCousin2> ?n }
WHERE {
[] rel:parentOf ?n_prnt, ?this_prnt .
?n_prnt rel:parentOf ?n .
?this_prnt rel:parentOf ?this .
filter (?n_prnt != ?this_prnt)
}
"""
] .
',
'', 'urn:spin:nanotation:demo:royal:family:lib', 4096)
;
-- Macro Generation
SELECT
SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:nanotation:demo:royal:family:lib');
-- STRING_TO_FILE ('urn_spin_nanotation_demo_royal_family_lib.sparql.sql', 'SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:nanotation:demo:royal:family:lib') || ';', -2);
EXEC ('SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:nanotation:demo:royal:family:lib'));
-- Test Queries
-- Test 1
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT *
WHERE { ?s a <#RoyalPerson> . };
-- Test 2
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT *
WHERE { ?s a ?t . };
-- Test 3
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT *
WHERE { ?s a <#RoyalPerson> ; rel:siblingOf ?sibling . };
-- Test 4
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT *
WHERE { ?s a <#RoyalPerson> ; rel:grandParent ?gp . };
-- Test 5
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?person rel:ancestorOf as ?relation ?descendant
WHERE { ?person a <#RoyalPerson> ;
rel:ancestorOf ?descendant . } ;
-- Test 6
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT DISTINCT ?person rel:siblingOf as ?relation ?siblingOf
WHERE { ?person a <#RoyalPerson> ;
rel:siblingOf ?siblingOf
} ;
-- Test 7
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?person <#hasAuntie> as ?relation ?hasAuntie
WHERE { ?person a <#RoyalPerson> ;
<#hasAuntie> ?hasAuntie
} ;
-- Test 8
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?person <#hasUncle> as ?relation ?hasUncle
WHERE { ?person a <#RoyalPerson> ;
<#hasUncle> ?hasUncle
} ;
-- Test 9
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?person <#hasUncle2> as ?relation ?hasUncle2
WHERE { ?person a <#RoyalPerson> ;
<#hasUncle2> ?hasUncle2
} ;
-- Test 10
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?person <#hasCousin> as ?relation ?hasCousin
WHERE { ?person a <#RoyalPerson> ;
<#hasCousin> ?hasCousin
} ;
-- Test 11
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?person <#hasCousin2> as ?relation ?hasCousin2
WHERE { ?person a <#RoyalPerson> ;
<#hasCousin2> ?hasCousin2
} ;
-- Test 12
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT ?s as ?ancestor
?descendant
WHERE { ?s a <#RoyalPerson> ;
rel:ancestorOf ?descendant
};
-- Test 13
SPARQL
DEFINE input:macro-lib <urn:spin:nanotation:demo:royal:family:lib>
PREFIX rel: <http://purl.org/vocab/relationship/>
WITH <urn:spin:nanotation:demo:royal:family>
SELECT DISTINCT *
WHERE {
{ ?parent a <#RoyalPerson> ; rel:parentOf ?parentOf .}
UNION
{ ?person a foaf:Person; <#hasAuntie> ?hasAuntie .}
UNION
{ ?person a foaf:Person; <#hasUncle> ?hasUncle .}
UNION
{ ?person a foaf:Person; <#hasCousin> ?hasCousin .}
UNION
{ ?person a foaf:Person; <#hasUncle2> ?hasUncle2 .}
UNION
{ ?person a foaf:Person; <#hasCousin2> ?hasCousin2 .}
UNION
{ ?person a foaf:Person; rel:siblingOf ?hasSibling . }
UNION
{ ?person a foaf:Person; rel:ancestorOf ?hasDescendant . }
} ;