-
Notifications
You must be signed in to change notification settings - Fork 7
/
pattern.clj
110 lines (100 loc) · 4.83 KB
/
pattern.clj
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
(ns com.yetanalytics.datasim.xapi.profile.pattern
"Creation of `pattern-walk-fn` for Profile compilation."
(:require [clojure.spec.alpha :as s]
[clojure.zip :as z]
[com.yetanalytics.pan.objects.template :as template]
[com.yetanalytics.pan.objects.pattern :as pattern]
[com.yetanalytics.datasim.input.alignments :as alignment]
[com.yetanalytics.datasim.math.random :as random]
[com.yetanalytics.datasim.xapi.profile :as-alias profile]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::repeat-max
pos-int?)
(s/def ::pattern-ancestors
(s/every ::pattern/pattern))
;; `pattern-walk-fn` has the arglist `[alignment rng {:keys [repeat-max]}]`
;; and returns a template w/ `:pattern-ancestors` metadata
(s/def ::pattern-walk-fn
(s/fspec
:args (s/cat :alignment ::alignment/alignment
:rng ::random/rng
:kwargs (s/keys* :opt-un [::repeat-max]))
:ret (s/every (s/and ::template/template
(s/conformer meta)
(s/keys :req-un [::pattern-ancestors])))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pattern Walker
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- pattern-zipper
"Create a zipper over the Patterns and Statement Templates found in
`type-iri-map`. A special `::root` sentinel Pattern is created as an
alternates Pattern of all the primary Patterns in the profiles.
The zipper can then be walked; traversal will be done in a deterministic,
pseudorandom fashion, in which `rng` and `alignment` is used to choose
the children of each node in the zipper."
[type-iri-map alignment rng repeat-max]
(let [temp-iri-map (get type-iri-map "StatementTemplate")
pat-iri-map (get type-iri-map "Pattern")
primary-pat-ids (->> pat-iri-map vals (filter :primary) (mapv :id))
root-pattern {:id ::root
:type "Pattern"
:alternates primary-pat-ids}
pat-iri-map* (assoc pat-iri-map ::root root-pattern)]
(-> (z/zipper
(fn branch? [node-id] ; it is a branch if it's a pattern
(contains? pat-iri-map* node-id))
(fn children [node-id] ; choose children using rng
(let [{:keys [sequence alternates optional oneOrMore zeroOrMore]}
(get pat-iri-map* node-id)]
(cond
sequence sequence
alternates [(random/choose rng alignment alternates)]
optional (or (some->> [nil optional]
(random/choose rng alignment)
vector)
[])
oneOrMore (repeat (inc (random/rand-int rng repeat-max))
oneOrMore)
zeroOrMore (repeat (random/rand-int rng repeat-max)
zeroOrMore))))
(fn make-node [node-id _child-ids] ; this is a no-op
node-id)
::root)
(vary-meta assoc
::template-map temp-iri-map
::pattern-map pat-iri-map))))
(defn- pattern-loc->template
[{template-m ::template-map pattern-m ::pattern-map} pattern-loc]
(let [node->template #(get template-m %)
node->pattern #(get pattern-m %)]
(when-some [template (->> pattern-loc z/node node->template)]
(vary-meta template
assoc
:pattern-ancestors
(->> pattern-loc z/path rest (keep node->pattern) vec)))))
(defn- walk-pattern-zipper
"From the root of `pattern-zip`, perform a single walk of a primary Pattern,
returning a sequence of Templates. Which primary Pattern is walked will be
chosen in a pseudorandom, deterministic fashion (see how the root node is
constructed in `pattern-zipper`)."
[pattern-zip]
(->> pattern-zip
(iterate z/next)
(take-while (complement z/end?))
rest ; cut the root node off the top
(filter z/node) ; empty seq nodes will be `nil`, so filter them out
(keep (partial pattern-loc->template (meta pattern-zip)))))
(s/fdef create-pattern-walk-fn
:args (s/cat :type-iri-map ::profile/type-iri-map)
:ret ::pattern-walk-fn)
(defn create-pattern-walk-fn
"Return a function that, when called with the args `alignment rng
& {:keys [repeat-max]}`, returns a lazy sequence of Statement Templates
that have `:pattern-ancestors` metadata."
[type-iri-map]
(fn [alignment rng & {:keys [repeat-max]
:or {repeat-max 5}}]
(walk-pattern-zipper
(pattern-zipper type-iri-map alignment rng repeat-max))))