/
core.cljc
502 lines (406 loc) · 13.7 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
(ns ironhide.core
(:require [com.rpl.specter :as sp]
#?(:cljs [orchestra-cljs.spec.test :as stest]
:clj [orchestra.spec.test :as stest])
[clojure.spec.alpha :as s]
[clojure.string :as cstr]))
;; =============================================================================
;; specs
(defn- ih? [x]
(and (keyword? x)
(= "ih" (namespace x))))
(defn- special? [ns key x]
(or
(and (keyword? x) (= ns (namespace x)))
(and (map? x) (contains? x key))))
(defn- micro? [x]
(special? "ihm" :ih/micro x))
(defn- sight? [x]
(special? "ihs" :ih/sight x))
(s/def ::shell
(s/keys :req [:ih/rules :ih/direction]
:opt [:ih/data :ih/sights :ih/micros :ih/values]))
(s/def :ih/rules (s/coll-of ::rule))
(s/def :ih/direction (s/cat :from ::data-source :to ::data-source))
(s/def :ih/sights (s/map-of #(= "ihs" (namespace %)) ::nested-sight))
(s/def ::map-sight
#(and (map? %) (= (count %) (count (set (vals %))))))
(s/def ::nested-sight
(s/or :shell ::shell
:map ::map-sight))
(s/def ::rule (s/coll-of
(s/or
:data-source
(s/tuple ::data-source ::path)
:ih/value
(s/tuple #(= :ih/value %) ::data-source->path))
:into []
:min-count 2
:max-count 3))
(s/def ::data-source->path
(s/map-of ::data-source ::path))
(s/def :ih/value ::data-source->path)
(s/def ::data-source keyword?)
(s/def ::pmode #{:get :set})
(s/def ::path (s/coll-of ::pelem))
(s/def ::pelem (s/or :mkey ::mkey
:sight ::sight
:vnav ::vnav))
(s/def ::mkey keyword?)
(s/def ::sight sight?)
(s/def ::vnav
(s/cat :vkey ::vkey :vfilter (s/? ::vfilter)))
(s/def ::vkey (s/or :index ::index
:wildcard ::wildcard))
(s/def ::wildcard #(= :* %))
(s/def ::index nat-int?)
(s/def ::vfilter map?)
(s/fdef execute
:args (s/cat :shell ::shell)
:ret ::shell)
(s/fdef path->sp-path
:args (s/or
:less-args (s/coll-of any? :max-count 2)
:arg3 (s/cat :ctx map? :path ::path :pmode ::pmode))
:ret sequential?)
;; =============================================================================
(defn- deep-merge [a b]
(if (and
(map? a)
(map? b))
(merge-with deep-merge a b)
b))
(defn- match-recur [errors path x pattern]
(cond
(and (map? x)
(map? pattern))
(reduce (fn [errors [k v]]
(let [path (conj path k)
ev (get x k)]
(match-recur errors path ev v)))
errors pattern)
(and (vector? pattern)
(seqable? x))
(reduce (fn [errors [k v]]
(let [path (conj path k)
ev (nth (vec x) k nil)]
(match-recur errors path ev v)))
errors
(map (fn [x i] [i x]) pattern (range)))
:else (let [err (when-not (= pattern x) {:expected pattern :but x})]
(if err
(conj errors (assoc err :path path))
errors))))
(defn- match*
[x & patterns]
(reduce (fn [acc pattern] (match-recur acc [] x pattern)) [] patterns))
(defn- m-valid? [pattern x]
(if (empty? (match* x pattern))
true
false))
;; =============================================================================
;; sights
(defmulti get-global-sight
"Returns pair of parse/unparse functions"
(fn [key args & [ctx]] key))
(defmethod get-global-sight :ihs/str<->vector [key args & [ctx]]
(let [separator (get args :separator " ")]
[#(if % (cstr/split % (re-pattern separator)) [])
#(cstr/join separator %)]))
(defmethod get-global-sight :ihs/uuid<->identity [key args & [ctx]]
[(fn [x]
#?(:clj (java.util.UUID/randomUUID)
:cljs (random-uuid)))
identity])
(defn- get-scoped-sight [{{sights :sights} :ih/internals :as ctx} key args]
(if (contains? sights key)
(key sights)
(get-global-sight key args ctx)))
(defn- inverse-shell [shell]
;; TODO: move to specter
(update shell :ih/direction reverse))
(declare get-data)
(defn- transform [shell left right & direction]
(let [[from to :as dir] (or direction
(:ih/direction shell)
[:left :right])]
(->
shell
(assoc-in [:ih/data from] left)
(assoc-in [:ih/data to] right)
(assoc-in [:ih/direction] dir)
(get-data to))))
(defn- get-transformer [shell]
(fn [data]
(transform shell data nil)))
(defn- get-inverse-transformer [shell]
(get-transformer (update shell :ih/direction reverse)))
(defn- inverse-map [m]
(into {} (map (comp vec reverse) m)))
(defn- get-nested-sight-from-map [m]
(if (contains? m :ih/rules)
[(get-transformer m) (get-inverse-transformer m)]
[m (inverse-map m)]))
(defn- add-nested-sights [shell]
(assoc-in shell [:ih/internals :sights]
(sp/transform [sp/MAP-VALS]
get-nested-sight-from-map
(get-in shell [:ih/sights]))))
;; =============================================================================
(def ^:private ALL-INDEXED [sp/INDEXED-VALS (sp/collect-one 0) 1])
(defn- wildcard? [pelem]
(m-valid? [:*] pelem))
(defn- has-wildcards? [path]
(not-empty (filter wildcard? path)))
(defn- vfilter->template [vfilter next-pelem]
(if vfilter
vfilter
(if (vector? next-pelem) [] nil)))
(defn- get-vfilter-fn [vfilter]
(if vfilter
(fn [x] (m-valid? vfilter x))
(constantly true)))
(defn- vec->specter [[navigator & [vfilter] :as pelem] next-pelem]
(let [filter-fn (get-vfilter-fn vfilter)
template (vfilter->template vfilter next-pelem)]
[sp/NIL->VECTOR
(if (wildcard? pelem)
[(sp/filterer filter-fn) ALL-INDEXED]
(if vfilter
(sp/if-path #(empty? (filter filter-fn %))
[sp/END 0 (sp/nil->val template)]
[(sp/filterer filter-fn) navigator])
[(sp/nil->val template) navigator]))]))
;; (sp/setval [(vec->specter [0] []) 0] :test [])
;; => [[:test]]
(defn- get-special-name [type pelem]
(cond
(keyword? pelem) pelem
(map? pelem) (type pelem)))
(defn- get-micro-name [pelem]
(get-special-name :ih/micro pelem))
(defn- get-sight-name [pelem]
(get-special-name :ih/sight pelem))
(defn- get-args-from-pelem [pelem]
(if (map? pelem) (into {} (remove #(ih? (first %)) pelem)) {}))
(defn- get-sight-from-pelem [ctx pelem]
(let [sight-name (get-sight-name pelem)
args (get-args-from-pelem pelem)
[parse unparse] (get-scoped-sight ctx sight-name args)]
(sp/parser parse unparse)))
(defn- sight->specter [ctx pelem]
[(get-sight-from-pelem ctx pelem)])
(defn- pelems->specter [ctx pelem next-pelem mode]
(let [a :a]
(condp s/valid? pelem
::vnav (vec->specter pelem next-pelem)
::sight (sight->specter ctx pelem)
::mkey [pelem])))
(defn path->sp-path
"Generates a specter path from ironhide path"
([path] (path->sp-path {} path))
([ctx path] (path->sp-path ctx path :set))
([ctx path pmode]
(reduce
(fn [acc [pelem next-pelem]]
(concat acc (pelems->specter ctx pelem next-pelem pmode)))
[]
(partition 2 1 [nil] path))))
(defn get-values [ctx path]
(let [sp-path (path->sp-path ctx path)]
(if (has-wildcards? path)
(sp/select sp-path ctx)
[(sp/select sp-path ctx)])))
(defn- path->value [values]
(->>
(for [value values]
[(butlast value) (last value)])
(into {})))
(defn set-values [ctx path values]
(let [values (if (vector? values)
(path->value values)
values)
sp-path (path->sp-path ctx path)]
(sp/transform
sp-path
(fn [& v]
(let [path (butlast v)
old-value (last v)
new-value (values path)
result
(if (and (map? new-value)
(map? old-value))
(deep-merge old-value new-value)
(if (some? new-value)
new-value old-value))]
result))
ctx)))
(defn- get-templates [path]
(for [[[_ vfilter] next-pelem]
(filter (comp wildcard? first) (partition 2 1 [nil] path))]
(vfilter->template vfilter next-pelem)))
;; (get-templates [[:* {:a :b}] :a [:*] [:*]])
;; => ({:a :b} [] nil)
(defn- splitcat-by-wildcard [path]
(get
(reduce
(fn [{:keys [curr-path] :as acc} pelem]
(let [new-path (conj curr-path pelem)]
(cond-> (assoc acc :curr-path new-path)
(wildcard? pelem) (update :paths conj new-path))))
{:curr-path []
:paths []}
path)
:paths))
;; (splitcat-by-wildcard [[:*] :a :b [:*] :v])
;; => [[[:*]] [[:*] :a :b [:*]]]
(defn- counting-sp-path [ctx path]
(let [[_ vfilter] (last path)]
(concat
(path->sp-path ctx (butlast path))
(if vfilter [(sp/filterer (get-vfilter-fn vfilter))] []))))
(defn- count-matched-nodes [ctx path]
(let [res (sp/select (counting-sp-path ctx path) ctx)]
(if (has-wildcards? (butlast path))
(mapv #(count (last %)) res)
[(count (first res))])))
(defn- tree-shape [ctx path]
(map #(count-matched-nodes ctx %)
(splitcat-by-wildcard path)))
(defn- count-shape-diff [source-shape sink-shape]
(map #(map - %1 (concat %2 (repeat 0)))
source-shape
sink-shape))
(defn- add-level-elems [ctx path elem ncounts]
(let [wc? (wildcard? (last path))
;; FIXME: it always has to be wc, isn't it?
first-part (butlast path)
wcs? (has-wildcards? first-part)
vfilter-fn (get-vfilter-fn (if wc? (second (last path))))
insert-path
(path->sp-path
ctx
(if wc? first-part path))
select-path
(if wc?
(concat
(path->sp-path ctx first-part)
[(sp/filterer vfilter-fn)])
path)
insert-fn
(if wcs?
(into {} (mapv (fn [[& args] ncount]
[(butlast args) (repeat ncount elem)])
(sp/select select-path ctx)
ncounts))
(fn [x] (repeat (first ncounts) elem)))]
(sp/transform
[insert-path sp/END]
(fn [& args]
(insert-fn (butlast args)))
ctx)))
(defn- add-templates [source-data sink-data [source-path sink-path]]
(let [diff (count-shape-diff
(tree-shape source-data source-path)
(tree-shape sink-data sink-path))
defaults (get-templates sink-path)
branching-paths (splitcat-by-wildcard sink-path)]
(loop [res sink-data
[path & rpaths] branching-paths
[elem & relems] defaults
[ncounts & rncounts] diff]
(if ncounts
(recur
(add-level-elems res path elem ncounts)
rpaths
relems
rncounts)
res))))
(defn- get-full-path [path & [prefix]]
(if (ih? (first path))
path
(into prefix path)))
(defn- apply-rule [ctx rule]
(let [[from to] (or (:ih/direction rule)
(:ih/direction ctx)
[:data :data])
{source-path from
sink-path to} rule
full-source-path (get-full-path source-path [:ih/data from])
full-sink-path (get-full-path sink-path [:ih/data to])
value-path (get-in rule [:ih/value to])
full-value-path (get-full-path value-path [:ih/values])
source-values (get-values ctx full-source-path)
value (get-values ctx full-value-path)
;; TODO: write a proper empty-result? fn
empty-values? (fn [x] (m-valid? [[nil]] x))
values (if (or (empty-values? source-values) (nil? source-path))
nil
source-values)]
(if sink-path
(cond
values
(set-values
(add-templates ctx ctx [full-source-path full-sink-path])
full-sink-path
values)
(and (not (empty-values? value)) (not (nil? value-path)))
(set-values
ctx
full-sink-path
(fn [k] (first (first value))))
:else
ctx)
ctx)))
(defn- microexpand [micro pelem]
(let [values (get-args-from-pelem pelem)
keys-set (set (remove ih? (keys values)))]
(sp/transform [(sp/walker #(keys-set %))] values micro)))
;; (microexpand [:telecom [:%1] :value] {:%1 :*})
;; => [:telecom [:*] :value]
(defn- pred-from-micro-name [micro-name]
(fn [x]
(or (and (keyword? x) (= micro-name x))
(and (map? x) (m-valid? {:ih/micro micro-name} x)))))
(defn- microexpand-path [{micros :ih/micros} path]
(reduce
(fn [path pelem]
(let [micro-name (get-micro-name pelem)
micro (get micros micro-name)]
(if micro
(into path (microexpand micro pelem))
(conj path pelem))))
[]
path))
;; (microexpand-path
;; {:ih/micros #:ihm {:name [:name [0]]
;; :given [:given [:ind]]}}
;; [:ihm/name {:ih/micro :ihm/given :ind 1}])
;; => [:name [0] :given [1]]
(defn microexpand-shell [{micros :ih/micros :as ctx}]
(let []
(sp/transform
[:ih/rules sp/ALL (sp/filterer #(not (ih? (first %)))) sp/ALL 1]
(fn [path]
(microexpand-path ctx path))
ctx)))
(defn execute [shell]
(let [expanded-shell (-> shell
microexpand-shell
add-nested-sights)
{rules :ih/rules} expanded-shell]
(reduce
(fn [ctx rule]
(apply-rule ctx rule))
expanded-shell
rules)))
(defn get-data [shell & [key]]
(->
(execute shell)
:ih/data
(#(get % (if key key (last (:ih/direction shell)))))))
;; TODO: Make value accept one path????
;; TODO: Default value via sight? case for value -> vector?
;; TODO: Rename value to fallback?
(when *assert*
(stest/instrument))