-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.cljc
517 lines (459 loc) · 15 KB
/
core.cljc
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
(ns ont-app.rdf.core
{:doc "This is a backstop for shared logic between various RDF-based
implementations of IGraph.
It includes:
- support for LangStr using the #lstr custom reader
- support for ^^transit:json datatype tags
- templating utilities for the standard IGraph member access methods.
"
:author "Eric D. Scott"
}
(:require
[clojure.string :as s]
;; [clojure.java.io :as io]
[clojure.spec.alpha :as spec]
;; 3rd party
;; [selmer.parser :as selmer]
[cljstache.core :as stache]
;; [taoensso.timbre :as timbre]
[cognitect.transit :as transit]
;; ont-app
[ont-app.graph-log.core :as glog]
#?(:clj [ont-app.graph-log.levels :as levels
:refer [warn debug trace value-trace value-debug]]
:cljs [ont-app.graph-log.levels :as levels
:refer-macros [warn debug trace value-trace value-debug]])
[ont-app.igraph.core :as igraph]
[ont-app.igraph.graph :as graph]
[ont-app.vocabulary.core :as voc]
;; local
[ont-app.vocabulary.lstr :as lstr :refer [->LangStr]]
[ont-app.rdf.ont :as ont]
)
#?(:clj
(:import
[java.io ByteArrayInputStream ByteArrayOutputStream]
)))
(voc/put-ns-meta!
'ont-app.rdf.core
{
:voc/mapsTo 'ont-app.rdf.ont
}
)
;; aliases
(def prefixed
"Returns `query`, with prefixe declarations prepended
Where
- `query` is a SPARQL query"
voc/prepend-prefix-declarations)
(def ontology
"The supporting vocabulary for the RDF module"
@ont/ontology-atom)
;; FUN WITH READER MACROS
#?(:cljs
(enable-console-print!)
)
#?(:cljs
(defn on-js-reload [] )
)
;; standard clojure containers are declared by default as descendents of
;; :rdf-app/TransitData, which keys to the render-literal method for rendering
;; transit data. renders as transit by default.
;; Note that you can undo this with 'underive', in which case
;; it will probably be rendered as a string, unless you want
;; to write your own method...
(derive #?(:clj clojure.lang.PersistentVector
:cljs cljs.core.PersistentVector)
:rdf-app/TransitData)
(derive #?(:clj clojure.lang.PersistentHashSet
:cljs cljs.core.PersistentHashSet )
:rdf-app/TransitData)
(derive #?(:clj clojure.lang.PersistentArrayMap
:cljs cljs.core.PersistentArrayMap )
:rdf-app/TransitData)
(derive #?(:clj clojure.lang.PersistentList
:cljs cljs.core.PersistentList )
:rdf-app/TransitData)
(derive #?(:clj clojure.lang.Cons
:cljs cljs.core.Cons )
:rdf-app/TransitData)
(derive #?(:clj clojure.lang.LazySeq
:cljs cljs.core.LazySeq )
:rdf-app/TransitData)
(declare transit-read-handlers)
(defn read-transit-json
"Returns a value parsed from transit string `s`
Where
`s` is a "-escaped string encoded as transit
Note: custom datatypes will be informed by @transit-read-handlers
"
[^String s]
#?(:clj
(transit/read
(transit/reader
(ByteArrayInputStream. (.getBytes (clojure.string/replace
s
""" "\"")
"UTF-8"))
:json
{:handlers @transit-read-handlers}))
:cljs
(transit/read
(transit/reader
:json
{:handlers @transit-read-handlers})
(clojure.string/replace
s
""" "\""))))
(declare transit-write-handlers)
(defn render-transit-json
"Returns a string of transit for `value`
Where
`value` is any value that be handled by cognitict/transit
Note: custom datatypes will be informed by @transit-write-handlers
"
[value]
#?(:clj
(let [output-stream (ByteArrayOutputStream.)
]
(transit/write
(transit/writer output-stream :json {:handlers @transit-write-handlers})
value)
(String. (.toByteArray output-stream)))
:cljs
(transit/write
(transit/writer :json {:handlers @transit-write-handlers})
value)))
;; SPECS
(def transit-re
"Matches data tagged as transit:json"
#"^\"(.*)\"\^\^transit:json$")
(spec/def ::transit-tag (spec/and string? (fn [s] (re-matches transit-re s))))
(defn bnode-kwi?
"True when `kwi` matches output of `bnode-translator`."
[kwi]
(->> (namespace kwi)
(re-matches #"^_.*")))
(spec/def ::bnode-kwi bnode-kwi?)
;;;;;;;;;;;;;;;;;;;;
;; LITERAL SUPPORT
;;;;;;;;;;;;;;;;;;;;
(defn quote-str
"Returns `s`, in escaped quotation marks.
Where
`s` is a string, typically to be rendered in a query or RDF source.
"
[s]
(value-trace
::QuoteString
(str "\"" s "\"")
))
(def transit-write-handlers
"Atom of the form {`Class` `write-handler`, ...}
Where
`Class` is a direct reference to the class instance to be encoded
`write-handler` := fn [s] -> {`field` `value`, ...}
"
(atom
{ont_app.vocabulary.lstr.LangStr
(cognitect.transit/write-handler
"ont-app.vocabulary.lstr.LangStr"
(fn [ls]
{:lang (.lang ls)
:s (.s ls)
}))
}))
(def transit-read-handlers
"Atom of the form {`className` `read-handler`
Where
`className` is a fully qualified string naming a class to be encoded
`read-handler` := fn [from-rep] -> `instance`
`from-rep` := an Object s.t. (`field` from-rep), encoded in corresponding
write-handler in @`transit-write-handlers`.
"
(atom
{"ont-app.vocabulary.lstr.LangStr"
(cognitect.transit/read-handler
(fn [from-rep]
(->LangStr (:s from-rep) (:lang from-rep))))
}
))
(defn render-literal-as-transit-json
"Returns 'x^^transit:json'
NOTE: this will be encoded on write and decoded on read by the
cognitect/transit library."
[x]
(stache/render "\"{{x}}\"^^transit:json" {:x (render-transit-json x)}))
;; RENDER LITERAL
(def special-literal-dispatch
"A function [x] -> `dispatch-value`
Where
`x` is any value, probabaly an RDF literal
`dispatch-value` is a value to be matched to a render-literal-dispatch method.
Default is to return nil, signalling no special dispatch."
(atom (fn [_] nil)))
(defn render-literal-dispatch
"Returns a key for the render-literal method to dispatch on given `literal`
Where
`literal` is any non-keyword
NOTE: LangStr and non-nil `special-dispatch` are special cases; otherwise
(type `literal`)
"
[literal]
(value-trace
::RenderLiteralDispatch
[:log/literal literal]
(if-let [special-dispatch (@special-literal-dispatch literal)]
special-dispatch
;; else no special dispatch...
(cond
(instance? ont_app.vocabulary.lstr.LangStr literal) :rdf-app/LangStr
:default (type literal)))))
(defmulti render-literal
"Returns an RDF (Turtle) rendering of `literal`"
render-literal-dispatch)
(defmethod render-literal :rdf-app/TransitData
[v]
(render-literal-as-transit-json v))
(defmethod render-literal :default
[s]
(quote-str s))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; STANDARD TEMPLATES FOR IGRAPH MEMBER ACCESS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- query-template-map
"Returns {`k` `v`, ...} appropriate for `rdf-store`
Where
`k` and `v` are cljstache template parameters which may appear in some query,
e.g. named graph open/close clauses
`rdf-store` is an RDF store.
"
[graph-uri rdf-store]
{:graph-name-open (if graph-uri
(str "GRAPH <" (voc/iri-for graph-uri) "> {")
"")
:graph-name-close (if graph-uri
(str "}")
"")
})
(def ^:private subjects-query-template
;; note the use of 3 brackets to turn off escaping
"
Select Distinct ?s Where
{
{{{graph-name-open}}}
?s ?p ?o.
{{{graph-name-close}}}
}
")
(defn query-for-subjects
"Returns [`subject` ...] at endpoint of `rdf-store`
Where
`subject` is the uri of a subject from `rdf-store`,
rendered per the binding translator of `rdf-store`
`rdf-store` conforms to ::sparql-client spec
`query-fn` := fn [repo] -> bindings
`graph-uri` is a URI or KWI naming the graph (or nil if DEFAULT graph)
"
([query-fn rdf-store]
(query-for-subjects (fn [_] nil) query-fn rdf-store)
)
([graph-uri query-fn rdf-store]
(let [query (stache/render subjects-query-template
(query-template-map graph-uri rdf-store))
]
(map :s
(query-fn rdf-store query)))))
(def ^:private normal-form-query-template
"
Select ?s ?p ?o
Where
{
{{{graph-name-open}}}
?s ?p ?o
{{{graph-name-close}}}
}
")
(defn query-for-normal-form
"Returns IGraph normal form for `graph` named by `graph-uri` in `rdf-store`
Where
`graph` is a named graph in `rdf-store`
`graph-uri` is a URI or KWI naming the graph (default nil -> DEFAULT graph)
`rdf-store` is an RDF store
`query-fn` := fn [rdf-store sparql-query] -> #{`bmap`, ...}
`bmap` := {:?s :?p :?o}
`sparql-query` :- `normal-form-query-template`
"
([query-fn rdf-store]
(query-for-normal-form nil query-fn rdf-store))
([graph-uri query-fn rdf-store]
(letfn [
(add-o [o binding]
(conj o (:o binding)))
(add-po [po binding]
(assoc po (:p binding)
(add-o (get po (:p binding) #{})
binding)))
(collect-binding [spo binding]
(value-trace
::CollectNormalFormBinding
[:log/spo spo
:log/binding binding]
(assoc spo (:s binding)
(add-po (get spo (:s binding) {})
binding))))
]
(let [query (stache/render normal-form-query-template
(query-template-map graph-uri rdf-store))
]
(value-trace
::QueryForNormalForm
[:log/query query
:log/graph-uri graph-uri
:log/query-fn query-fn]
(reduce collect-binding
{}
(query-fn rdf-store query)))))))
(defn check-ns-metadata
"Logs a warning when `kwi` is in a namespace with no metadata."
[kwi]
(let [n (symbol (namespace kwi))]
(if-let [the-ns (find-ns n)]
(when (not (meta the-ns))
(warn ::NoMetaDataInNS
:glog/message "The namespace for {{log/kwi}} is in a namespace with no associated metadata."
:log/kwi kwi))))
kwi)
(defn check-qname
"Traps the keyword assertion error in voc and throws a more meaningful error
about blank nodes not being supported as first-class identifiers."
[uri-spec]
(if (bnode-kwi? uri-spec)
uri-spec
;;else not a blank node
(try
(voc/qname-for (check-ns-metadata uri-spec))
(catch java.lang.AssertionError e
(if (= (str e)
"java.lang.AssertionError: Assert failed: (keyword? kw)")
(throw (ex-info (str "The URI spec " uri-spec " is not a keyword.\nCould it be a blank node?\nIf so, blank nodes cannot be treated as first-class identifiers in SPARQL. Use a dedicated query that traverses the blank node instead.")
(merge (ex-data e)
{:type ::Non-Keyword-URI-spec
::uri-spec uri-spec
})))
;; else it's some other message
(throw e))))))
(def ^:private query-for-p-o-template
"
Select ?p ?o Where
{
{{{graph-name-open}}}
{{{subject}}} ?p ?o.
{{{graph-name-close}}}
}
")
(defn query-for-p-o
"Returns {`p` #{`o`...}...} for `s` at endpoint of `rdf-store`
Where
`p` is a predicate URI rendered per binding translator of `rdf-store`
`o` is an object value, rendered per the binding translator of `rdf-store`
`s` is a subject uri keyword. ~ voc/voc-re
`rdf-store` is and RDF store.
`query-fn` := fn [repo] -> bindings
`graph-uri` is a URI or KWI naming the graph (or nil if DEFAULT graph)
"
([query-fn rdf-store s]
(query-for-p-o nil query-fn rdf-store s)
)
(
[graph-uri query-fn rdf-store s]
(let [query (prefixed
(stache/render query-for-p-o-template
(merge (query-template-map graph-uri rdf-store)
{:subject (check-qname s)})))
collect-bindings (fn [acc b]
(update acc (:p b)
(fn[os] (set (conj os (:o b))))))
]
(value-debug
::query-for-po
[::query query ::subject s]
(reduce collect-bindings {}
(query-fn rdf-store query))))))
(def ^:private query-for-o-template
"
Select ?o Where
{
{{{graph-name-open}}}
{{{subject}}} {{{predicate}}} ?o.
{{{graph-name-close}}}
}
")
(defn query-for-o
"Returns #{`o`...} for `s` and `p` at endpoint of `rdf-store`
Where:
`o` is an object rendered per binding translator of `rdf-store`
`s` is a subject URI rendered per binding translator of `rdf-store`
`p` is a predicate URI rendered per binding translator of `rdf-store`
`rdf-store` is an RDF store
`query-fn` := fn [repo] -> bindings
`graph-uri` is a URI or KWI naming the graph (or nil if DEFAULT graph)
"
([query-fn rdf-store s p]
(query-for-o nil query-fn rdf-store s p))
([graph-uri query-fn rdf-store s p]
(let [query (prefixed
(stache/render
query-for-o-template
(merge (query-template-map graph-uri rdf-store)
{:subject (check-qname s)
:predicate (check-qname p)})))
collect-bindings (fn [acc b]
(conj acc (:o b)))
]
(value-debug
::query-for-o-return
[::query query
::subject s
::predicate p]
(reduce collect-bindings #{}
(query-fn rdf-store query))))))
(def ^:private ask-s-p-o-template
"ASK where
{
{{{graph-name-open}}}
{{{subject}}} {{{predicate}}} {{{object}}}.
{{graph-name-close}}
}"
)
(defn ask-s-p-o
"Returns true if `s` `p` `o` is a triple at endpoint of `rdf-store`
Where:
`s` `p` `o` are subject, predicate and object
`rdf-store` is an RDF store
`graph-uri` is a URI or KWI naming the graph (or nil if DEFAULT graph)
`ask-fn` := fn [repo] -> bindings
"
([ask-fn rdf-store s p o]
(ask-s-p-o nil ask-fn rdf-store s p o)
)
([graph-uri ask-fn rdf-store s p o]
(let [query (prefixed
(stache/render
ask-s-p-o-template
(merge (query-template-map graph-uri rdf-store)
{:subject (check-qname s)
:predicate (check-qname p)
:object (if (keyword? o)
(voc/qname-for o)
(render-literal o))})))
starting (debug ::Starting_ask-s-p-o
:log/query query
:log/subject s
:log/predicate p
:log/object o)
]
(value-debug
::ask-s-p-o-return
[:log/resultOf starting]
(ask-fn rdf-store query)))))