-
Notifications
You must be signed in to change notification settings - Fork 7
/
alignments.clj
226 lines (183 loc) · 6.77 KB
/
alignments.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
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
(ns com.yetanalytics.datasim.input.model.alignments
"Models for individual Concepts, Templates, Patterns, and Object
Overrides. The term \"alignments\" is largely historical, as it was used
to refer to models, before model personae were added."
(:require [clojure.set :as cset]
[clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as sgen]
[clojure.walk :as w]
[xapi-schema.spec :as xs]
[com.yetanalytics.datasim.util.random :as random]
[com.yetanalytics.datasim.input.model.alignments.weight :as-alias weight]
[com.yetanalytics.datasim.input.model.alignments.bounds :as-alias bounds]
[com.yetanalytics.datasim.input.model.alignments.period :as-alias period]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ID
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: Use pan.axioms/iri once that has its own generator
;; nilable so that optional patterns can specify weight for `null` option
(s/def ::id (s/nilable ::xs/iri))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Weight
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; See also: ::object-overrides/weight
(s/def ::weight ::random/weight)
(s/def ::weights
(s/every (s/keys :req-un [::id ::weight]) :kind vector?))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Time Bounds
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- interval? [[start end]]
(< start end))
(defmacro bound-spec [scalar-spec]
`(s/every (s/or :scalar
~scalar-spec
:interval
(s/and (s/tuple ~scalar-spec ~scalar-spec)
interval?)
:step-interval
(s/and (s/tuple ~scalar-spec ~scalar-spec pos-int?)
interval?))
:kind vector?
:min-count 1
:gen-max 3))
(defmacro named-time-spec [index-spec name-index-map]
`(s/and
(s/or :integer ~index-spec
:string (s/and (set (keys ~name-index-map))
(s/conformer ~name-index-map
(cset/map-invert ~name-index-map))
~index-spec))
;; Remove s/or tags
(s/conformer second)))
(def day-of-week-map
{"Sunday" 0
"Monday" 1
"Tuesday" 2
"Wednesday" 3
"Thursday" 4
"Friday" 5
"Saturday" 6})
(def month-of-year-map
{"January" 1
"February" 2
"March" 3
"April" 4
"May" 5
"June" 6
"July" 7
"August" 8
"September" 9
"October" 10
"November" 11
"December" 12})
(s/def ::bounds/seconds
(bound-spec (s/int-in 0 60)))
(s/def ::bounds/minutes
(bound-spec (s/int-in 0 60)))
(s/def ::bounds/hours
(bound-spec (s/int-in 0 24)))
(s/def ::bounds/daysOfWeek
(bound-spec (named-time-spec (s/int-in 0 7) day-of-week-map)))
(s/def ::bounds/daysOfMonth
(bound-spec (s/int-in 1 32)))
(s/def ::bounds/months
(bound-spec (named-time-spec (s/int-in 1 13) month-of-year-map)))
(s/def ::bounds/years
(bound-spec pos-int?))
(s/def ::bounds
(s/every (s/keys :opt-un [::bounds/seconds
::bounds/minutes
::bounds/hours
::bounds/daysOfWeek
::bounds/daysOfMonth
::bounds/months
::bounds/years])
:kind vector?
:min-count 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Time Bound Restarts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::boundRestarts
(s/every ::xs/iri :kind vector?))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Time Period
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- has-default-period?
[periods]
(some (fn [{:keys [bounds] :as _period}] (boolean (not bounds)))
periods))
(s/def ::period/min
(s/and number? pos?))
(s/def ::period/mean
(s/and number? pos? (comp not zero?)))
(s/def ::period/fixed
(s/and number? pos? (comp not zero?)))
(s/def ::period/unit
#{"millis" "seconds" "minutes" "hours" "days" "weeks"})
(s/def ::period/bounds
::bounds)
(s/def ::periods
(s/every (s/keys :opt-un [::period/min
::period/mean
::period/fixed
::period/unit
::period/bounds])
:kind #(and (vector? %)
(has-default-period? %))
:min-count 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Max Repeat
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::repeatMax pos-int?)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Due to limitations of keywords, we cannot have IRI keys, limiting extensions
(defn- no-iri-keys?
"Returns false if there exists a key made from an IRI, e.g.
(name :https://foo.org) => \"/foo.org\""
[obj]
(cond
(map? obj)
(if-not (->> obj vals (map no-iri-keys?) (some false?))
(->> obj keys (some (partial re-matches #".*/.*")) not)
false)
(vector? obj)
(every? no-iri-keys? obj)
:else
true))
(s/def ::object
(s/with-gen (s/and (s/conformer w/stringify-keys w/keywordize-keys)
no-iri-keys?
:statement/object) ; from xapi-schema
#(->> (s/gen :statement/object)
(sgen/such-that no-iri-keys?)
(sgen/fmap w/keywordize-keys))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Alignment
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def verb-spec
(s/keys :req-un [::id
::weight]))
(def activity-spec
(s/keys :req-un [::id]
:opt-un [::weight]))
(def activity-type-spec
(s/keys :req-un [::id]
:opt-un [::weight]))
(def pattern-spec
(s/keys :req-un [::id]
:opt-un [::weights ; for alternate and optional patterns
::repeatMax ; for oneOrMore and zeroOrMore patterns
::bounds
::boundRestarts
::periods]))
(def template-spec
(s/keys :req-un [::id]
:opt-un [::bounds
::boundRestarts
::periods]))
(def object-override-spec
(s/keys :req-un [::object]
:opt-un [::weight]))